factor/extra/http/http.factor

397 lines
11 KiB
Factor
Raw Normal View History

2008-02-25 15:53:18 -05:00
! Copyright (C) 2003, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2008-04-22 21:23:49 -04:00
USING: accessors kernel combinators math namespaces
assocs assocs.lib sequences splitting sorting sets debugger
2008-04-22 21:23:49 -04:00
strings vectors hashtables quotations arrays byte-arrays
math.parser calendar calendar.format present
2008-04-22 21:23:49 -04:00
2008-06-16 02:35:06 -04:00
io io.encodings io.encodings.iana io.encodings.binary
io.encodings.8-bit
2008-04-22 21:23:49 -04:00
2008-05-26 01:47:27 -04:00
unicode.case unicode.categories qualified
urls html.templates xml xml.data xml.writer
http.parsers ;
2008-04-22 21:23:49 -04:00
EXCLUDE: fry => , ;
2007-09-20 18:09:08 -04:00
IN: http
: crlf ( -- ) "\r\n" write ;
2008-02-29 01:57:38 -05:00
: read-crlf ( -- bytes )
2008-04-22 21:23:49 -04:00
"\r" read-until
2008-04-23 01:53:42 -04:00
[ CHAR: \r assert= read1 CHAR: \n assert= ] when* ;
2008-04-22 21:23:49 -04:00
: (read-header) ( -- alist )
[ read-crlf dup f like ] [ parse-header-line ] [ drop ] unfold ;
: process-header ( alist -- assoc )
f swap [ [ swap or dup ] dip swap ] assoc-map nip
[ ?push ] histogram [ "; " join ] assoc-map
>hashtable ;
2008-02-29 01:57:38 -05:00
: read-header ( -- assoc )
(read-header) process-header ;
2008-02-29 01:57:38 -05:00
: header-value>string ( value -- string )
{
{ [ dup timestamp? ] [ timestamp>http-string ] }
{ [ dup array? ] [ [ header-value>string ] map "; " join ] }
[ present ]
2008-02-29 01:57:38 -05:00
} cond ;
: check-header-string ( str -- str )
#! http://en.wikipedia.org/wiki/HTTP_Header_Injection
dup "\r\n\"" intersect empty?
2008-03-11 04:39:09 -04:00
[ "Header injection attack" throw ] unless ;
2008-02-29 01:57:38 -05:00
: write-header ( assoc -- )
>alist sort-keys [
[ check-header-string write ": " write ]
[ header-value>string check-header-string write crlf ] bi*
2008-02-29 01:57:38 -05:00
] assoc-each crlf ;
TUPLE: cookie name value version comment path domain expires max-age http-only secure ;
2008-02-29 01:57:38 -05:00
: <cookie> ( value name -- cookie )
cookie new
swap >>name
swap >>value ;
2008-02-29 01:57:38 -05:00
: parse-set-cookie ( string -- seq )
2008-02-29 01:57:38 -05:00
[
f swap
(parse-set-cookie)
[
swap {
{ "version" [ >>version ] }
{ "comment" [ >>comment ] }
{ "expires" [ cookie-string>timestamp >>expires ] }
2008-04-27 05:27:04 -04:00
{ "max-age" [ string>number seconds >>max-age ] }
2008-02-29 01:57:38 -05:00
{ "domain" [ >>domain ] }
{ "path" [ >>path ] }
{ "httponly" [ drop t >>http-only ] }
{ "secure" [ drop t >>secure ] }
2008-02-29 01:57:38 -05:00
[ <cookie> dup , nip ]
} case
] assoc-each
drop
] { } make ;
2008-02-29 01:57:38 -05:00
: parse-cookie ( string -- seq )
[
f swap
(parse-cookie)
[
swap {
{ "$version" [ >>version ] }
{ "$domain" [ >>domain ] }
{ "$path" [ >>path ] }
[ <cookie> dup , nip ]
} case
] assoc-each
2008-02-29 01:57:38 -05:00
drop
] { } make ;
: check-cookie-string ( string -- string' )
dup "=;'\"\r\n" intersect empty?
[ "Bad cookie name or value" throw ] unless ;
: unparse-cookie-value ( key value -- )
2008-02-29 01:57:38 -05:00
{
2008-04-11 13:55:57 -04:00
{ f [ drop ] }
{ t [ check-cookie-string , ] }
[
{
{ [ dup timestamp? ] [ timestamp>cookie-string ] }
{ [ dup duration? ] [ dt>seconds number>string ] }
{ [ dup real? ] [ number>string ] }
[ ]
} cond
check-cookie-string "=" swap check-cookie-string 3append ,
]
2008-04-11 13:55:57 -04:00
} case ;
2008-02-29 01:57:38 -05:00
: (unparse-cookie) ( cookie -- strings )
2008-02-29 01:57:38 -05:00
[
dup name>> check-cookie-string >lower
over value>> unparse-cookie-value
"$path" over path>> unparse-cookie-value
"$domain" over domain>> unparse-cookie-value
2008-02-29 01:57:38 -05:00
drop
] { } make ;
: unparse-cookie ( cookies -- string )
[ (unparse-cookie) ] map concat "; " join ;
: unparse-set-cookie ( cookie -- string )
[
dup name>> check-cookie-string >lower
over value>> unparse-cookie-value
"path" over path>> unparse-cookie-value
"domain" over domain>> unparse-cookie-value
"expires" over expires>> unparse-cookie-value
"max-age" over max-age>> unparse-cookie-value
"httponly" over http-only>> unparse-cookie-value
"secure" over secure>> unparse-cookie-value
drop
] { } make "; " join ;
2008-02-29 01:57:38 -05:00
2008-02-25 15:53:18 -05:00
TUPLE: request
method
url
2008-02-25 15:53:18 -05:00
version
header
post-data
2008-02-29 01:57:38 -05:00
cookies ;
2008-02-25 15:53:18 -05:00
: check-url ( string -- url )
>url dup path>> "/" head? [ "Bad request: URL" throw ] unless ; inline
: read-request-line ( request -- request )
read-crlf parse-request-line first3
[ >>method ] [ check-url >>url ] [ >>version ] tri* ;
: set-header ( request/response value key -- request/response )
pick header>> set-at ;
: <request> ( -- request )
request new
"1.1" >>version
<url>
H{ } clone >>query
>>url
2008-03-15 07:22:47 -04:00
H{ } clone >>header
V{ } clone >>cookies
"close" "connection" set-header
2008-06-16 04:34:17 -04:00
"Factor http.client" "user-agent" set-header ;
2008-02-29 01:57:38 -05:00
: check-absolute ( url -- url )
dup path>> "/" head? [ "Bad request: URL" throw ] unless ; inline
2008-02-25 15:53:18 -05:00
: read-request-header ( request -- request )
read-header >>header ;
2008-02-29 01:57:38 -05:00
: header ( request/response key -- value )
swap header>> at ;
TUPLE: post-data raw content content-type ;
2008-02-25 15:53:18 -05:00
: <post-data> ( raw content-type -- post-data )
post-data new
swap >>content-type
swap >>raw ;
2008-02-25 15:53:18 -05:00
: parse-post-data ( post-data -- post-data )
[ ] [ raw>> ] [ content-type>> ] tri {
{ "application/x-www-form-urlencoded" [ query>assoc ] }
{ "text/xml" [ string>xml ] }
[ drop ]
} case >>content ;
2008-02-25 15:53:18 -05:00
: read-post-data ( request -- request )
dup method>> "POST" = [
[ ]
[ "content-length" header string>number read ]
[ "content-type" header ] tri
<post-data> parse-post-data >>post-data
] when ;
2008-02-25 15:53:18 -05:00
: extract-host ( request -- request )
[ ] [ url>> ] [ "host" header parse-host ] tri
[ >>host ] [ >>port ] bi*
drop ;
2008-02-25 15:53:18 -05:00
2008-02-29 01:57:38 -05:00
: extract-cookies ( request -- request )
dup "cookie" header [ parse-cookie >>cookies ] when* ;
2008-02-25 15:53:18 -05:00
2008-05-01 17:24:50 -04:00
: parse-content-type-attributes ( string -- attributes )
2008-05-26 01:47:27 -04:00
" " split harvest [ "=" split1 [ >lower ] dip ] { } map>assoc ;
2008-05-01 17:24:50 -04:00
: parse-content-type ( content-type -- type encoding )
2008-06-25 04:25:08 -04:00
";" split1 parse-content-type-attributes "charset" swap at
name>encoding over "text/" head? latin1 binary ? or ;
2008-05-01 17:24:50 -04:00
2008-02-25 15:53:18 -05:00
: read-request ( -- request )
<request>
read-request-line
2008-02-25 15:53:18 -05:00
read-request-header
read-post-data
extract-host
2008-02-29 01:57:38 -05:00
extract-cookies ;
2008-02-25 15:53:18 -05:00
: write-request-line ( request -- request )
dup
[ method>> write bl ]
[ url>> relative-url present write bl ]
[ "HTTP/" write version>> write crlf ]
tri ;
2008-02-25 15:53:18 -05:00
: url-host ( url -- string )
[ host>> ] [ port>> ] bi dup "http" protocol-port =
2008-05-14 00:36:55 -04:00
[ drop ] [ ":" swap number>string 3append ] if ;
2008-02-25 15:53:18 -05:00
: write-request-header ( request -- request )
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 ]
[ content-type>> "content-type" pick set-at ]
bi
] when*
over cookies>> f like [ unparse-cookie "cookie" pick set-at ] when*
2008-02-25 15:53:18 -05:00
write-header ;
GENERIC: >post-data ( object -- post-data )
M: post-data >post-data ;
M: string >post-data "application/octet-stream" <post-data> ;
M: byte-array >post-data "application/octet-stream" <post-data> ;
M: xml >post-data xml>string "text/xml" <post-data> ;
M: assoc >post-data assoc>query "application/x-www-form-urlencoded" <post-data> ;
M: f >post-data ;
: unparse-post-data ( request -- request )
[ >post-data ] change-post-data ;
2008-02-25 15:53:18 -05:00
: write-post-data ( request -- request )
dup method>> "POST" = [ dup post-data>> raw>> write ] when ;
2008-02-25 15:53:18 -05:00
: write-request ( request -- )
unparse-post-data
write-request-line
2008-02-25 15:53:18 -05:00
write-request-header
write-post-data
flush
drop ;
2008-02-29 01:57:38 -05:00
GENERIC: write-response ( response -- )
GENERIC: write-full-response ( request response -- )
2008-02-25 15:53:18 -05:00
TUPLE: response
version
code
message
2008-02-29 01:57:38 -05:00
header
cookies
2008-05-01 17:24:50 -04:00
content-type
content-charset
2008-02-29 01:57:38 -05:00
body ;
2008-02-25 15:53:18 -05:00
: <response> ( -- response )
response new
"1.1" >>version
H{ } clone >>header
"close" "connection" set-header
now timestamp>http-string "date" set-header
2008-06-16 04:34:17 -04:00
"Factor http.server" "server" set-header
latin1 >>content-charset
V{ } clone >>cookies ;
2008-02-25 15:53:18 -05:00
2008-06-16 02:35:06 -04:00
M: response clone
call-next-method
[ clone ] change-header
[ clone ] change-cookies ;
: read-response-line ( response -- response )
read-crlf parse-response-line first3
[ >>version ] [ >>code ] [ >>message ] tri* ;
2008-02-25 15:53:18 -05:00
: read-response-header ( response -- response )
2008-02-29 01:57:38 -05:00
read-header >>header
dup "set-cookie" header parse-set-cookie >>cookies
2008-05-01 17:24:50 -04:00
dup "content-type" header [
parse-content-type
[ >>content-type ]
2008-06-25 04:25:08 -04:00
[ >>content-charset ] bi*
2008-05-01 17:24:50 -04:00
] when* ;
2008-02-25 15:53:18 -05:00
: read-response ( -- response )
<response>
read-response-line
2008-02-25 15:53:18 -05:00
read-response-header ;
: write-response-line ( response -- response )
dup
[ "HTTP/" write version>> write bl ]
[ code>> present write bl ]
[ message>> write crlf ]
tri ;
2008-02-25 15:53:18 -05:00
2008-05-01 17:24:50 -04:00
: unparse-content-type ( request -- content-type )
[ content-type>> "application/octet-stream" or ]
[ content-charset>> encoding>name ]
bi
2008-05-01 17:24:50 -04:00
[ "; charset=" swap 3append ] when* ;
: ensure-domain ( cookie -- cookie )
[
request get url>>
host>> dup "localhost" =
[ drop ] [ or ] if
] change-domain ;
2008-02-25 15:53:18 -05:00
: write-response-header ( response -- response )
#! We send one set-cookie header per cookie, because that's
#! what Firefox expects.
dup header>> >alist >vector
2008-05-01 17:24:50 -04:00
over unparse-content-type "content-type" pick set-at
over cookies>> [
ensure-domain unparse-set-cookie
"set-cookie" swap 2array over push
] each
2008-02-29 01:57:38 -05:00
write-header ;
2008-02-25 15:53:18 -05:00
2008-04-15 07:10:08 -04:00
: write-response-body ( response -- response )
2008-05-26 01:47:27 -04:00
dup body>> call-template ;
2008-04-15 07:10:08 -04:00
2008-02-29 01:57:38 -05:00
M: response write-response ( respose -- )
write-response-line
2008-02-25 15:53:18 -05:00
write-response-header
flush
drop ;
2008-02-29 01:57:38 -05:00
M: response write-full-response ( request response -- )
dup write-response
2008-06-16 02:35:06 -04:00
swap method>> "HEAD" = [
[ content-charset>> encode-output ]
[ write-response-body ]
bi
] unless ;
2008-02-29 01:57:38 -05:00
: get-cookie ( request/response name -- cookie/f )
2008-05-26 01:47:27 -04:00
[ cookies>> ] dip '[ , _ name>> = ] find nip ;
2008-02-29 01:57:38 -05:00
: delete-cookie ( request/response name -- )
2008-05-26 01:47:27 -04:00
over cookies>> [ get-cookie ] dip delete ;
2008-02-29 01:57:38 -05:00
: put-cookie ( request/response cookie -- request/response )
2008-03-11 04:39:09 -04:00
[ name>> dupd get-cookie [ dupd delete-cookie ] when* ] keep
2008-02-29 01:57:38 -05:00
over cookies>> push ;
2008-05-01 17:24:50 -04:00
TUPLE: raw-response
2008-02-29 01:57:38 -05:00
version
code
message
body ;
: <raw-response> ( -- response )
raw-response new
2008-06-02 16:00:03 -04:00
"1.1" >>version ;
2008-02-29 01:57:38 -05:00
M: raw-response write-response ( respose -- )
write-response-line
2008-02-29 01:57:38 -05:00
write-response-body
drop ;
2008-02-25 15:53:18 -05:00
2008-02-29 01:57:38 -05:00
M: raw-response write-full-response ( response -- )
write-response nip ;