Skip to content

Commit

Permalink
http: rename post-data slot to data
Browse files Browse the repository at this point in the history
Added backwards compatibility generic slot words so hopefully
this isn't a breaking change
  • Loading branch information
mrjbq7 committed Dec 3, 2024
1 parent b421db6 commit 5d557ae
Show file tree
Hide file tree
Showing 22 changed files with 78 additions and 84 deletions.
6 changes: 3 additions & 3 deletions basis/couchdb/couchdb.factor
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ PREDICATE: file-exists-error < couchdb-error
: couch-get ( url -- assoc )
<get-request> couch-request ;

: <json-post-data> ( assoc -- post-data )
: <json-data> ( assoc -- post-data )
>json utf8 encode "application/json" <post-data> swap >>data ;

: couch-put ( assoc url -- assoc' )
[ <json-post-data> ] dip <put-request> couch-request ;
[ <json-data> ] dip <put-request> couch-request ;

: couch-post ( assoc url -- assoc' )
[ <json-post-data> ] dip <post-request> couch-request ;
[ <json-data> ] dip <post-request> couch-request ;

: couch-delete ( url -- assoc )
<delete-request> couch-request ;
Expand Down
9 changes: 4 additions & 5 deletions basis/furnace/asides/asides.factor
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ furnace.utilities hashtables html.templates.chloe.syntax http
http.server kernel logging math.parser namespaces urls ;
IN: furnace.asides

TUPLE: aside < server-state
session method url post-data ;
TUPLE: aside < server-state session method url post-data ;

: <aside> ( id -- aside )
aside new-server-state ;
Expand Down Expand Up @@ -56,16 +55,16 @@ M: asides call-responder*
swap >>url
session get id>> >>session
request get method>> >>method
request get post-data>> >>post-data
request get data>> >>post-data
[ touch-aside ] [ insert-tuple ] [ set-aside ] tri ;

: end-aside-post ( aside -- response )
request [
clone
over post-data>> >>post-data
over post-data>> >>data
over url>> >>url
] change
[ [ post-data>> params>> params set ] [ url>> url set ] bi ]
[ [ data>> params>> params set ] [ url>> url set ] bi ]
[ url>> path>> split-path asides get responder>> call-responder ] bi ;

\ end-aside-post DEBUG add-input-logging
Expand Down
16 changes: 8 additions & 8 deletions basis/http/client/client-docs.factor
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ HELP: <get-request>
{ $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;

HELP: <post-request>
{ $values { "post-data" object } { "url" { $or url string } } { "request" request } }
{ $values { "data" object } { "url" { $or url string } } { "request" request } }
{ $description "Constructs an HTTP POST request for submitting post data to the URL." }
{ $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;

Expand Down Expand Up @@ -55,23 +55,23 @@ HELP: http-get*
{ http-get http-get* } related-words

HELP: http-post
{ $values { "post-data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
{ $values { "data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
{ $description "Submits an HTTP POST request." }
{ $errors "Throws an error if the HTTP request fails." } ;

HELP: http-post*
{ $values { "post-data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
{ $values { "data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
{ $description "Submits an HTTP POST request, but does not check the HTTP response code for success." } ;

{ http-post http-post* } related-words

HELP: http-put
{ $values { "put-data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
{ $values { "data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
{ $description "Submits an HTTP PUT request." }
{ $errors "Throws an error if the HTTP request fails." } ;

HELP: http-put*
{ $values { "put-data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
{ $values { "data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
{ $description "Submits an HTTP PUT request, but does not check the HTTP response code for success." } ;

{ http-put http-put* } related-words
Expand Down Expand Up @@ -110,12 +110,12 @@ HELP: http-options*
{ http-options http-options* } related-words

HELP: http-patch
{ $values { "patch-data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
{ $values { "data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
{ $description "Submits an HTTP PATCH request." }
{ $errors "Throws an error if the HTTP request fails." } ;

HELP: http-patch*
{ $values { "patch-data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
{ $values { "data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
{ $description "Submits an HTTP PATCH request, but does not check the HTTP response code for success." } ;

{ http-patch http-patch* } related-words
Expand Down Expand Up @@ -170,7 +170,7 @@ ARTICLE: "http.client.get" "GET requests with the HTTP client"
} ;

ARTICLE: "http.client.post-data" "HTTP client post data"
"HTTP POST and PUT request words take a post data parameter, which can be one of the following:"
"HTTP POST, PUT, and PATCH request words take a " { $snippet "data" } " parameter, which can be one of the following:"
{ $list
{ "a " { $link byte-array } ": the data is sent the server without further encoding" }
{ "a " { $link string } ": the data is encoded and then sent as a series of bytes" }
Expand Down
29 changes: 13 additions & 16 deletions basis/http/client/client.factor
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ ERROR: download-failed response ;
over url>> host>> [ set-host-header ] when
over url>> "Authorization" ?set-basic-auth
over proxy-url>> "Proxy-Authorization" ?set-basic-auth
over post-data>> [ set-post-data-headers ] when*
over data>> [ set-post-data-headers ] when*
over cookies>> [ set-cookie-header ] unless-empty
write-header ;

Expand Down Expand Up @@ -123,7 +123,7 @@ SYMBOL: redirects
redirects get request get redirects>> < [
request get clone
response "location" header redirect-url
response code>> 307 = [ "GET" >>method f >>post-data ] unless
response code>> 307 = [ "GET" >>method f >>data ] unless
] [ too-many-redirects ] if ; inline recursive

: read-chunk-size ( -- n )
Expand Down Expand Up @@ -288,7 +288,7 @@ SYMBOL: request-socket
<request>
swap >>method
swap request-url >>url
swap >>post-data ;
swap >>data ;

: <rest-request> ( url method -- request )
[ f ] 2dip <rest-request-with-body> ;
Expand All @@ -308,24 +308,22 @@ SYMBOL: request-socket
: http-get* ( url -- response data )
<get-request> http-request* ;

: <post-request> ( post-data url -- request )
"POST" <client-request>
swap >>post-data ;
: <post-request> ( data url -- request )
"POST" <client-request> swap >>data ;

: http-post ( post-data url -- response data )
: http-post ( data url -- response data )
<post-request> http-request ;

: http-post* ( post-data url -- response data )
: http-post* ( data url -- response data )
<post-request> http-request* ;

: <put-request> ( post-data url -- request )
"PUT" <client-request>
swap >>post-data ;
: <put-request> ( data url -- request )
"PUT" <client-request> swap >>data ;

: http-put ( put-data url -- response data )
: http-put ( data url -- response data )
<put-request> http-request ;

: http-put* ( put-data url -- response data )
: http-put* ( data url -- response data )
<put-request> http-request* ;

: <delete-request> ( url -- request )
Expand Down Expand Up @@ -355,9 +353,8 @@ SYMBOL: request-socket
: http-options* ( url -- response data )
<options-request> http-request* ;

: <patch-request> ( patch-data url -- request )
"PATCH" <client-request>
swap >>post-data ;
: <patch-request> ( data url -- request )
"PATCH" <client-request> swap >>data ;

: http-patch ( patch-data url -- response data )
<patch-request> http-request ;
Expand Down
7 changes: 3 additions & 4 deletions basis/http/client/post-data/post-data.factor
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ M: object (write-post-data)
<measured-stream> ;

: normalize-post-data ( request -- request )
dup post-data>> [
dup data>> [
dup params>> [
assoc>query ascii encode >>data
] when*
Expand Down Expand Up @@ -85,8 +85,7 @@ M: object >post-data
swap >>data ;

: unparse-post-data ( request -- request )
[ >post-data ] change-post-data
normalize-post-data ;
[ >post-data ] change-data normalize-post-data ;

: write-post-data ( request -- request )
dup post-data>> [ data>> (write-post-data) ] when* ;
dup data>> [ data>> (write-post-data) ] when* ;
4 changes: 2 additions & 2 deletions basis/http/http-tests.factor
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ blah
{ method "POST" }
{ version "1.1" }
{ header H{ { "some-header" "1; 2" } { "content-length" "4" } { "content-type" "application/octet-stream" } } }
{ post-data T{ post-data { data "blah" } { content-type "application/octet-stream" } } }
{ data T{ post-data { data "blah" } { content-type "application/octet-stream" } } }
{ cookies V{ } }
{ redirects 10 }
}
Expand Down Expand Up @@ -465,7 +465,7 @@ test-db <db-persistence> [
"a" add-responder
<action>
[
request get post-data>> data>> "data" =
request get data>> data>> "data" =
[ "OK" "text/plain" <content> ] [ "OOPS" throw ] if
] >>submit
"b" add-responder [
Expand Down
6 changes: 5 additions & 1 deletion basis/http/http.factor
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,14 @@ TUPLE: request
proxy-url
version
header
post-data
data
cookies
redirects ;

! These are for backwards compatibility
M: request post-data>> data>> ;
M: request post-data<< data<< ;

: set-header ( request/response value key -- request/response )
pick header>> set-at ;

Expand Down
18 changes: 7 additions & 11 deletions basis/http/json/json.factor
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ IN: http.json
: <json-request> ( url method -- request )
<client-request> accept-json ;

: <json-post-data> ( assoc/json-string -- post-data )
: <json-data> ( assoc/json-string -- post-data )
dup string? [ >json ] unless utf8 encode
"application/json" <post-data> swap >>data ;

Expand All @@ -22,13 +22,11 @@ IN: http.json
: http-get-json ( url -- response json )
"GET" <json-request> http-request-json ;

: http-put-json ( post-data url -- response json )
[ <json-post-data> ] dip "PUT" <json-request> swap
>>post-data http-request-json ;
: http-put-json ( assoc/json-string url -- response json )
[ <json-data> ] dip "PUT" <json-request> swap >>data http-request-json ;

: http-post-json ( post-data url -- response json )
[ <json-post-data> ] dip "POST" <json-request> swap
>>post-data http-request-json ;
: http-post-json ( assoc/json-string url -- response json )
[ <json-data> ] dip "POST" <json-request> swap >>data http-request-json ;

: http-head-json ( url -- response json )
"HEAD" <json-request> http-request-json ;
Expand All @@ -43,12 +41,10 @@ IN: http.json
"TRACE" <json-request> http-request-json ;

: http-patch-json ( assoc/json-string url -- response json )
[ <json-post-data> ] dip "PATCH" <json-request>
swap >>post-data http-request-json ;
[ <json-data> ] dip "PATCH" <json-request> swap >>data http-request-json ;

: rest-request-json ( url method -- response json )
<json-request> http-request-json ;

: rest-request-json-with-body ( body url method -- response json )
[ <json-post-data> ] 2dip
<json-request> swap >>post-data http-request-json ;
[ <json-data> ] 2dip <json-request> swap >>data http-request-json ;
4 changes: 2 additions & 2 deletions basis/http/server/cgi/cgi.factor
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ IN: http.server.cgi
request get "accept" header "HTTP_ACCEPT" ,,

post-request? [
request get post-data>> data>>
request get data>> data>>
[ "CONTENT_TYPE" ,, ]
[ length number>string "CONTENT_LENGTH" ,, ]
bi
Expand All @@ -54,7 +54,7 @@ IN: http.server.cgi
swap '[
binary encode-output
output-stream get _ normalize-path <cgi-process> binary <process-stream> [
post-request? [ request get post-data>> data>> write flush ] when
post-request? [ request get data>> data>> write flush ] when
'[ _ stream-write ] each-block
] with-stream
] >>body ;
Expand Down
10 changes: 5 additions & 5 deletions basis/http/server/requests/requests-tests.factor
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ IN: http.server.requests.tests
! POST requests
{ "foo=bar" "7" } [
"foo=bar" "localhost" <post-request> request>string string>request
[ post-data>> data>> ] [ header>> "content-length" of ] bi
[ data>> data>> ] [ header>> "content-length" of ] bi
] unit-test

{ f "0" } [
"" "localhost" <post-request> request>string string>request
[ post-data>> data>> ] [ header>> "content-length" of ] bi
[ data>> data>> ] [ header>> "content-length" of ] bi
] unit-test

! Incorrect content-length works fine
{ LH{ { "foo" "bar" } } } [
{ { "foo" "bar" } } "localhost" <post-request> request>string
"7" "190" replace string>request post-data>> params>>
"7" "190" replace string>request data>> params>>
] unit-test

{ LH{ { "name" "John Smith" } } } [
{ { "name" "John Smith" } } "localhost" <post-request> request>string
string>request post-data>> params>>
string>request data>> params>>
] unit-test

! multipart/form-data
Expand Down Expand Up @@ -59,7 +59,7 @@ hello
}
} [
test-multipart/form-data lf>crlf string>request
post-data>> params>> "text" of [ filename>> ] [ headers>> ] bi
data>> params>> "text" of [ filename>> ] [ headers>> ] bi
] unit-test

! Error handling
Expand Down
8 changes: 4 additions & 4 deletions basis/http/server/requests/requests.factor
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ upload-limit [ 200,000,000 ] initialize
[ drop nip read >>data ]
} case ;

: read-post-data ( request -- request )
dup method>> "POST" = [
: read-request-data ( request -- request )
dup method>> { "POST" "PUT" "PATCH" } member? [
dup dup "content-type" header
";" split1 drop parse-content >>post-data
";" split1 drop parse-content >>data
] when ;

: extract-host ( request -- request )
Expand All @@ -84,6 +84,6 @@ upload-limit [ 200,000,000 ] initialize
<request>
read-request-line
read-request-header
read-post-data
read-request-data
extract-host
extract-cookies ;
6 changes: 3 additions & 3 deletions basis/http/server/server.factor
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ LOG: httpd-header NOTICE
{ "HEAD" [ url>> query>> ] }
{ "OPTIONS" [ url>> query>> ] }
{ "DELETE" [ url>> query>> ] }
{ "POST" [ post-data>> params>> ] }
{ "PATCH" [ post-data>> params>> ] }
{ "PUT" [ post-data>> params>> ] }
{ "POST" [ data>> params>> ] }
{ "PATCH" [ data>> params>> ] }
{ "PUT" [ data>> params>> ] }
[ 2drop H{ } clone ]
} case ;

Expand Down
2 changes: 1 addition & 1 deletion basis/oauth1/oauth1-tests.factor
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ strings namespaces urls ;
12345 >>timestamp
54321 >>nonce
<request-token-request>
post-data>>
data>>
"oauth_signature" of
>string
] unit-test
2 changes: 1 addition & 1 deletion basis/oauth1/oauth1.factor
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ TUPLE: oauth-request-params < token-params access-token ;
params
[
access-token>> key>> "oauth_token" ,,
request post-data>> %%
request data>> %%
] make-token-params
sign-params ;

Expand Down
3 changes: 1 addition & 2 deletions basis/xml-rpc/xml-rpc.factor
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ TAG: array xml>item
xml>string utf8 encode "text/xml" <post-data> swap >>data ;

: rpc-post-request ( xml url -- request )
[ send-rpc xml-post-data ] [ "POST" <client-request> ] bi*
swap >>post-data ;
[ send-rpc xml-post-data ] [ <post-request> ] bi* ;

PRIVATE>

Expand Down
Loading

0 comments on commit 5d557ae

Please sign in to comment.