http server can upload files. WOW!

db4
Doug Coleman 2009-01-20 16:35:52 -06:00
parent 4306656556
commit 050dbf8a60
2 changed files with 35 additions and 13 deletions

8
basis/http/http.factor Normal file → Executable file
View File

@ -213,12 +213,14 @@ body ;
raw-response new raw-response new
"1.1" >>version ; "1.1" >>version ;
TUPLE: post-data raw content content-type ; TUPLE: post-data raw content content-type form-variables uploaded-files ;
: <post-data> ( raw content-type -- post-data ) : <post-data> ( form-variables uploaded-files raw content-type -- post-data )
post-data new post-data new
swap >>content-type swap >>content-type
swap >>raw ; swap >>raw
swap >>uploaded-files
swap >>form-variables ;
: parse-content-type-attributes ( string -- attributes ) : parse-content-type-attributes ( string -- attributes )
" " split harvest [ "=" split1 [ >lower ] dip ] { } map>assoc ; " " split harvest [ "=" split1 [ >lower ] dip ] { } map>assoc ;

40
basis/http/server/server.factor Normal file → Executable file
View File

@ -15,6 +15,8 @@ io.streams.limited
io.servers.connection io.servers.connection
io.timeouts io.timeouts
fry logging logging.insomniac calendar urls urls.encoding fry logging logging.insomniac calendar urls urls.encoding
mime.multipart
unicode.categories
http http
http.parsers http.parsers
http.server.responses http.server.responses
@ -36,17 +38,35 @@ IN: http.server
: read-request-header ( request -- request ) : read-request-header ( request -- request )
read-header >>header ; read-header >>header ;
: parse-post-data ( post-data -- post-data ) ERROR: no-boundary ;
[ ] [ raw>> ] [ content-type>> ] tri
"application/x-www-form-urlencoded" = [ query>assoc ] when : parse-multipart-form-data ( string -- separator )
>>content ; ";" split1 nip
"=" split1 nip [ no-boundary ] unless* ;
: read-multipart-data ( request -- form-variables uploaded-files )
[ "content-type" header ]
[ "content-length" header string>number ] bi
unlimit-input
stream-eofs limit-input
binary decode-input
parse-multipart-form-data parse-multipart ;
: 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 ]
} case ;
: read-post-data ( request -- request ) : read-post-data ( request -- request )
dup method>> "POST" = [ dup method>> "POST" = [
[ ] dup dup "content-type" header
[ "content-length" header string>number read ] [ ";" split1 drop parse-content ] keep
[ "content-type" header ] tri <post-data> >>post-data
<post-data> parse-post-data >>post-data
] when ; ] when ;
: extract-host ( request -- request ) : extract-host ( request -- request )
@ -80,7 +100,7 @@ GENERIC: write-full-response ( request response -- )
[ content-type>> "application/octet-stream" or ] [ content-type>> "application/octet-stream" or ]
[ content-charset>> encoding>name ] [ content-charset>> encoding>name ]
bi bi
[ "; charset=" swap 3append ] when* ; [ "; charset=" glue ] when* ;
: ensure-domain ( cookie -- cookie ) : ensure-domain ( cookie -- cookie )
[ [
@ -236,7 +256,7 @@ TUPLE: http-server < threaded-server ;
M: http-server handle-client* M: http-server handle-client*
drop drop
[ [
64 1024 * limit-input 64 1024 * stream-throws limit-input
?refresh-all ?refresh-all
[ read-request ] ?benchmark [ read-request ] ?benchmark
[ do-request ] ?benchmark [ do-request ] ?benchmark