factor/extra/http/http.factor

420 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 sequences splitting sorting sets debugger
strings vectors hashtables quotations arrays byte-arrays
math.parser calendar calendar.format
io io.streams.string io.encodings.utf8 io.encodings.string
io.sockets io.sockets.secure io.server
2008-04-22 21:23:49 -04:00
2008-05-26 01:47:27 -04:00
unicode.case unicode.categories qualified
urls html.templates ;
2008-04-22 21:23:49 -04:00
EXCLUDE: fry => , ;
2007-09-20 18:09:08 -04:00
IN: http
: secure-protocol? ( protocol -- ? )
"https" = ;
2008-02-25 15:53:18 -05:00
: url-addr ( url -- addr )
[ [ host>> ] [ port>> ] bi <inet> ] [ protocol>> ] bi
secure-protocol? [ <secure> ] when ;
2008-05-14 00:36:55 -04:00
: protocol-port ( protocol -- port )
2008-05-14 00:36:55 -04:00
{
{ "http" [ 80 ] }
{ "https" [ 443 ] }
2008-05-14 00:36:55 -04:00
} case ;
: ensure-port ( url -- url' )
dup protocol>> '[ , protocol-port or ] change-port ;
2007-09-20 18:09:08 -04:00
2008-02-29 01:57:38 -05:00
: crlf "\r\n" write ;
: add-header ( value key assoc -- )
[ at dup [ "; " rot 3append ] [ drop ] if ] 2keep set-at ;
: header-line ( line -- )
dup first blank? [
[ blank? ] left-trim
"last-header" get
"header" get
add-header
] [
": " split1 dup [
swap >lower dup "last-header" set
"header" get add-header
] [
2drop
] if
] if ;
2008-04-22 21:23:49 -04:00
: read-lf ( -- string )
"\n" read-until CHAR: \n assert= ;
: read-crlf ( -- string )
"\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
2008-02-29 01:57:38 -05:00
: read-header-line ( -- )
2008-04-22 21:23:49 -04:00
read-crlf dup
2008-02-29 01:57:38 -05:00
empty? [ drop ] [ header-line read-header-line ] if ;
: read-header ( -- assoc )
H{ } clone [
"header" [ read-header-line ] with-variable
] keep ;
: header-value>string ( value -- string )
{
{ [ dup number? ] [ number>string ] }
{ [ dup timestamp? ] [ timestamp>http-string ] }
{ [ dup url? ] [ url>string ] }
2008-02-29 01:57:38 -05:00
{ [ dup string? ] [ ] }
{ [ dup sequence? ] [ [ header-value>string ] map "; " join ] }
} 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 [
swap url-encode write ": " write
header-value>string check-header-string write crlf
] assoc-each crlf ;
TUPLE: cookie name value path domain expires max-age http-only ;
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-cookies ( string -- seq )
[
f swap
";" split [
[ blank? ] trim "=" split1 swap >lower {
{ "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 ] }
{ "" [ drop ] }
[ <cookie> dup , nip ]
} case
] each
drop
] { } make ;
: (unparse-cookie) ( key value -- )
{
2008-04-11 13:55:57 -04:00
{ f [ drop ] }
{ t [ , ] }
[
{
{ [ dup timestamp? ] [ timestamp>cookie-string ] }
{ [ dup duration? ] [ dt>seconds number>string ] }
[ ]
} cond
"=" swap 3append ,
]
2008-04-11 13:55:57 -04:00
} case ;
2008-02-29 01:57:38 -05:00
: unparse-cookie ( cookie -- strings )
[
dup name>> >lower over value>> (unparse-cookie)
"path" over path>> (unparse-cookie)
"domain" over domain>> (unparse-cookie)
"expires" over expires>> (unparse-cookie)
"max-age" over max-age>> (unparse-cookie)
2008-02-29 01:57:38 -05:00
"httponly" over http-only>> (unparse-cookie)
drop
] { } make ;
: unparse-cookies ( cookies -- string )
[ unparse-cookie ] map concat "; " join ;
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
post-data-type
cookies ;
2008-02-25 15:53:18 -05:00
: set-header ( request/response value key -- request/response )
pick header>> set-at ;
2008-02-25 15:53:18 -05:00
: <request>
request new
"1.1" >>version
<url>
"http" >>protocol
H{ } clone >>query
>>url
2008-03-15 07:22:47 -04:00
H{ } clone >>header
V{ } clone >>cookies
"close" "connection" set-header
"Factor http.client vocabulary" "user-agent" set-header ;
2008-02-29 01:57:38 -05:00
2008-03-03 03:19:36 -05:00
: chop-hostname ( str -- str' )
2008-05-14 07:08:57 -04:00
":" split1 "//" ?head drop nip
2008-03-03 03:19:36 -05:00
CHAR: / over index over length or tail
dup empty? [ drop "/" ] when ;
2008-02-25 15:53:18 -05:00
: url>path ( url -- path )
2008-03-03 03:19:36 -05:00
#! Technically, only proxies are meant to support hostnames
#! in HTTP requests, but IE sends these sometimes so we
#! just chop the hostname part.
2008-05-14 00:36:55 -04:00
url-decode
dup { "http://" "https://" } [ head? ] with contains?
[ chop-hostname ] when ;
2008-02-25 15:53:18 -05:00
: read-method ( request -- request )
" " read-until [ "Bad request: method" throw ] unless
>>method ;
: check-absolute ( url -- url )
dup path>> "/" head? [ "Bad request: URL" throw ] unless ; inline
2008-02-25 15:53:18 -05:00
: read-url ( request -- request )
" " read-until [
dup empty? [ drop read-url ] [ >url check-absolute >>url ] if
] [ "Bad request: URL" throw ] if ;
2008-02-25 15:53:18 -05:00
: parse-version ( string -- version )
"HTTP/" ?head [ "Bad request: version" throw ] unless
dup { "1.0" "1.1" } member? [ "Bad request: version" throw ] unless ;
2008-02-25 15:53:18 -05:00
: read-request-version ( request -- request )
2008-04-22 21:23:49 -04:00
read-crlf [ CHAR: \s = ] left-trim
2008-02-25 15:53:18 -05:00
parse-version
>>version ;
: read-request-header ( request -- request )
read-header >>header ;
2008-02-29 01:57:38 -05:00
: header ( request/response key -- value )
swap header>> at ;
2008-02-25 15:53:18 -05:00
SYMBOL: max-post-request
1024 256 * max-post-request set-global
: content-length ( header -- n )
2008-02-29 01:57:38 -05:00
"content-length" swap at string>number dup [
2008-02-25 15:53:18 -05:00
dup max-post-request get > [
"content-length > max-post-request" throw
] when
] when ;
: read-post-data ( request -- request )
dup header>> content-length [ read >>post-data ] when* ;
: extract-host ( request -- request )
[ ] [ url>> ] [ "host" header parse-host ] tri
[ >>host ] [ >>port ] bi*
ensure-port
drop ;
2008-02-25 15:53:18 -05:00
: extract-post-data-type ( request -- request )
2008-02-29 01:57:38 -05:00
dup "content-type" header >>post-data-type ;
: parse-post-data ( request -- request )
dup post-data-type>> "application/x-www-form-urlencoded" =
[ dup post-data>> query>assoc >>post-data ] when ;
2008-02-29 01:57:38 -05:00
: extract-cookies ( request -- request )
dup "cookie" header [ parse-cookies >>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 )
";" split1 parse-content-type-attributes "charset" swap at ;
: detect-protocol ( request -- request )
dup url>> remote-address get secure? "https" "http" ? >>protocol drop ;
2008-02-25 15:53:18 -05:00
: read-request ( -- request )
<request>
read-method
read-url
read-request-version
read-request-header
read-post-data
detect-protocol
2008-02-25 15:53:18 -05:00
extract-host
2008-02-29 01:57:38 -05:00
extract-post-data-type
parse-post-data
2008-02-29 01:57:38 -05:00
extract-cookies ;
2008-02-25 15:53:18 -05:00
: write-method ( request -- request )
dup method>> write bl ;
: write-request-url ( request -- request )
dup url>> relative-url url>string write bl ;
2008-02-25 15:53:18 -05:00
: write-version ( request -- request )
"HTTP/" write dup request-version write crlf ;
: unparse-post-data ( request -- request )
dup post-data>> dup sequence? [ drop ] [
assoc>query >>post-data
"application/x-www-form-urlencoded" >>post-data-type
] if ;
: 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
2008-02-29 01:57:38 -05:00
over post-data>> [ length "content-length" pick set-at ] when*
over post-data-type>> [ "content-type" pick set-at ] when*
over cookies>> f like [ unparse-cookies "cookie" pick set-at ] when*
2008-02-25 15:53:18 -05:00
write-header ;
: write-post-data ( request -- request )
dup post-data>> [ write ] when* ;
: write-request ( request -- )
unparse-post-data
2008-02-25 15:53:18 -05:00
write-method
2008-02-29 01:57:38 -05:00
write-request-url
2008-02-25 15:53:18 -05:00
write-version
write-request-header
write-post-data
flush
drop ;
2008-05-14 00:36:55 -04:00
: request-with-url ( request url -- request )
'[ , >url derive-url ensure-port ] change-url ;
2008-02-25 15:53:18 -05:00
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 new
"1.1" >>version
H{ } clone >>header
"close" "connection" set-header
now timestamp>http-string "date" set-header
V{ } clone >>cookies ;
2008-02-25 15:53:18 -05:00
: read-response-version
2008-02-29 01:57:38 -05:00
" \t" read-until
2008-02-25 15:53:18 -05:00
[ "Bad response: version" throw ] unless
parse-version
>>version ;
: read-response-code
2008-02-29 01:57:38 -05:00
" \t" read-until [ "Bad response: code" throw ] unless
2008-02-25 15:53:18 -05:00
string>number [ "Bad response: code" throw ] unless*
>>code ;
: read-response-message
2008-04-22 21:23:49 -04:00
read-crlf >>message ;
2008-02-25 15:53:18 -05:00
: read-response-header
2008-02-29 01:57:38 -05:00
read-header >>header
2008-05-01 17:24:50 -04:00
extract-cookies
dup "content-type" header [
parse-content-type [ >>content-type ] [ >>content-charset ] bi*
] when* ;
2008-02-25 15:53:18 -05:00
: read-response ( -- response )
<response>
read-response-version
read-response-code
read-response-message
read-response-header ;
: write-response-version ( response -- response )
"HTTP/" write
dup version>> write bl ;
: write-response-code ( response -- response )
dup code>> number>string write bl ;
: write-response-message ( response -- response )
dup message>> write crlf ;
2008-05-01 17:24:50 -04:00
: unparse-content-type ( request -- content-type )
[ content-type>> "application/octet-stream" or ]
[ content-charset>> ] bi
[ "; charset=" swap 3append ] when* ;
2008-02-25 15:53:18 -05:00
: write-response-header ( response -- response )
2008-02-29 01:57:38 -05:00
dup header>> clone
2008-05-01 17:24:50 -04:00
over cookies>> f like [ unparse-cookies "set-cookie" pick set-at ] when*
over unparse-content-type "content-type" pick set-at
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 -- )
2008-02-25 15:53:18 -05:00
write-response-version
write-response-code
write-response-message
write-response-header
flush
drop ;
2008-02-29 01:57:38 -05:00
M: response write-full-response ( request response -- )
dup write-response
swap method>> "HEAD" = [ write-response-body ] unless ;
: 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-02-29 01:57:38 -05:00
"1.1" >>version ;
M: raw-response write-response ( respose -- )
write-response-version
write-response-code
write-response-message
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 ;