diff --git a/basis/http/client/client.factor b/basis/http/client/client.factor index fc6e296a4f..f2c0a862eb 100644 --- a/basis/http/client/client.factor +++ b/basis/http/client/client.factor @@ -25,7 +25,7 @@ IN: http.client dup header>> >hashtable over url>> host>> [ over url>> url-host "host" pick set-at ] when over post-data>> [ - [ raw>> length "content-length" pick set-at ] + [ data>> length "content-length" pick set-at ] [ content-type>> "content-type" pick set-at ] bi ] when* @@ -36,19 +36,35 @@ GENERIC: >post-data ( object -- post-data ) M: post-data >post-data ; -M: string >post-data utf8 encode "application/octet-stream" ; +M: string >post-data + utf8 encode + "application/octet-stream" + swap >>data ; -M: byte-array >post-data "application/octet-stream" ; +M: byte-array >post-data + "application/octet-stream" + swap >>data ; -M: assoc >post-data assoc>query ascii encode "application/x-www-form-urlencoded" ; +M: assoc >post-data + "application/x-www-form-urlencoded" + swap >>params ; M: f >post-data ; +: normalize-post-data ( request -- request ) + dup post-data>> [ + dup params>> [ + assoc>query ascii encode >>data + ] when* drop + ] when* ; + : unparse-post-data ( request -- request ) - [ >post-data ] change-post-data ; + [ >post-data ] change-post-data + normalize-post-data ; : write-post-data ( request -- request ) - dup method>> [ "POST" = ] [ "PUT" = ] bi or [ dup post-data>> raw>> write ] when ; + dup method>> [ "POST" = ] [ "PUT" = ] bi or + [ dup post-data>> data>> write ] when ; : write-request ( request -- ) unparse-post-data diff --git a/basis/http/http-tests.factor b/basis/http/http-tests.factor index 92a296c2d3..6fa23b4b1f 100644 --- a/basis/http/http-tests.factor +++ b/basis/http/http-tests.factor @@ -35,7 +35,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 { content "blah" } { raw "blah" } { content-type "application/octet-stream" } } } + { post-data T{ post-data { data "blah" } { content-type "application/octet-stream" } } } { cookies V{ } } } ] [ diff --git a/basis/http/http.factor b/basis/http/http.factor index b29f5222db..c85cfc9c41 100755 --- a/basis/http/http.factor +++ b/basis/http/http.factor @@ -213,14 +213,11 @@ body ; raw-response new "1.1" >>version ; -TUPLE: post-data raw content content-type form-variables uploaded-files ; +TUPLE: post-data data params content-type content-encoding ; -: ( form-variables uploaded-files raw content-type -- post-data ) +: ( content-type -- post-data ) post-data new - swap >>content-type - swap >>raw - swap >>uploaded-files - swap >>form-variables ; + swap >>content-type ; : parse-content-type-attributes ( string -- attributes ) " " split harvest [ "=" split1 [ >lower ] dip ] { } map>assoc ; diff --git a/basis/http/server/cgi/cgi.factor b/basis/http/server/cgi/cgi.factor index 0c2f639cba..959642b706 100644 --- a/basis/http/server/cgi/cgi.factor +++ b/basis/http/server/cgi/cgi.factor @@ -34,7 +34,7 @@ IN: http.server.cgi request get "accept" header "HTTP_ACCEPT" set post-request? [ - request get post-data>> raw>> + request get post-data>> data>> [ "CONTENT_TYPE" set ] [ length number>string "CONTENT_LENGTH" set ] bi @@ -54,7 +54,7 @@ IN: http.server.cgi swap '[ binary encode-output _ output-stream get swap binary [ - post-request? [ request get post-data>> raw>> write flush ] when + post-request? [ request get post-data>> data>> write flush ] when input-stream get swap (stream-copy) ] with-stream ] >>body ; diff --git a/basis/http/server/server.factor b/basis/http/server/server.factor index 1c516e9051..c328e1d6a3 100755 --- a/basis/http/server/server.factor +++ b/basis/http/server/server.factor @@ -55,18 +55,17 @@ ERROR: no-boundary ; : read-content ( request -- bytes ) "content-length" header string>number read ; -: parse-content ( request content-type -- form-variables uploaded-files raw ) - { - { "multipart/form-data" [ read-multipart-data f ] } - { "application/x-www-form-urlencoded" [ read-content [ f f ] dip ] } - [ drop read-content [ f f ] dip ] +: parse-content ( request content-type -- post-data ) + [ swap ] keep { + { "multipart/form-data" [ read-multipart-data assoc-union >>params ] } + { "application/x-www-form-urlencoded" [ read-content query>assoc >>params ] } + [ drop read-content >>data ] } case ; : read-post-data ( request -- request ) dup method>> "POST" = [ dup dup "content-type" header - [ ";" split1 drop parse-content ] keep - >>post-data + ";" split1 drop parse-content >>post-data ] when ; : extract-host ( request -- request )