better http.server that handles multipart data in the same way as regular form data. fix http client for changes

db4
Doug Coleman 2009-01-21 19:55:25 -06:00
parent 29968f3082
commit 3150722c7f
5 changed files with 34 additions and 22 deletions

View File

@ -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" <post-data> ;
M: string >post-data
utf8 encode
"application/octet-stream" <post-data>
swap >>data ;
M: byte-array >post-data "application/octet-stream" <post-data> ;
M: byte-array >post-data
"application/octet-stream" <post-data>
swap >>data ;
M: assoc >post-data assoc>query ascii encode "application/x-www-form-urlencoded" <post-data> ;
M: assoc >post-data
"application/x-www-form-urlencoded" <post-data>
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

View File

@ -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{ } }
}
] [

View File

@ -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 ;
: <post-data> ( form-variables uploaded-files raw content-type -- post-data )
: <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 ;

View File

@ -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 <cgi-process> binary <process-stream> [
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 ;

View File

@ -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 )
[ <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> >>post-data
";" split1 drop parse-content >>post-data
] when ;
: extract-host ( request -- request )