1 ! Copyright (C) 2003, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel combinators math namespaces make assocs
4 sequences splitting sorting sets strings vectors hashtables
5 quotations arrays byte-arrays math.parser calendar
6 calendar.format present urls fry
7 io io.encodings io.encodings.iana io.encodings.binary
8 io.encodings.8-bit io.crlf ascii
13 CONSTANT: max-redirects 10
15 : (read-header) ( -- alist )
16 [ read-crlf dup f like ] [ parse-header-line ] produce nip ;
18 : collect-headers ( assoc -- assoc' )
19 H{ } clone [ '[ _ push-at ] assoc-each ] keep ;
21 : process-header ( alist -- assoc )
22 f swap [ [ swap or dup ] dip swap ] assoc-map nip
23 collect-headers [ "; " join ] assoc-map
26 : read-header ( -- assoc )
27 (read-header) process-header ;
29 : header-value>string ( value -- string )
31 { [ dup timestamp? ] [ timestamp>http-string ] }
32 { [ dup array? ] [ [ header-value>string ] map "; " join ] }
36 : check-header-string ( str -- str )
37 #! http://en.wikipedia.org/wiki/HTTP_Header_Injection
38 dup "\r\n" intersects?
39 [ "Header injection attack" throw ] when ;
41 : write-header ( assoc -- )
43 [ check-header-string write ": " write ]
44 [ header-value>string check-header-string write crlf ] bi*
47 TUPLE: cookie name value version comment path domain expires max-age http-only secure ;
49 : <cookie> ( value name -- cookie )
54 : parse-set-cookie ( string -- seq )
60 { "version" [ >>version ] }
61 { "comment" [ >>comment ] }
62 { "expires" [ cookie-string>timestamp >>expires ] }
63 { "max-age" [ string>number seconds >>max-age ] }
64 { "domain" [ >>domain ] }
66 { "httponly" [ drop t >>http-only ] }
67 { "secure" [ drop t >>secure ] }
68 [ <cookie> dup , nip ]
74 : parse-cookie ( string -- seq )
80 { "$version" [ >>version ] }
81 { "$domain" [ >>domain ] }
82 { "$path" [ >>path ] }
83 [ <cookie> dup , nip ]
89 : check-cookie-string ( string -- string' )
90 dup "=;'\"\r\n" intersects?
91 [ "Bad cookie name or value" throw ] when ;
93 : unparse-cookie-value ( key value -- )
96 { t [ check-cookie-string , ] }
99 { [ dup timestamp? ] [ timestamp>cookie-string ] }
100 { [ dup duration? ] [ duration>seconds number>string ] }
101 { [ dup real? ] [ number>string ] }
104 [ check-cookie-string ] bi@ "=" glue ,
108 : check-cookie-value ( string -- string )
109 [ "Cookie value must not be f" throw ] unless* ;
111 : (unparse-cookie) ( cookie -- strings )
113 dup name>> check-cookie-string >lower
114 over value>> check-cookie-value unparse-cookie-value
115 "$path" over path>> unparse-cookie-value
116 "$domain" over domain>> unparse-cookie-value
120 : unparse-cookie ( cookies -- string )
121 [ (unparse-cookie) ] map concat "; " join ;
123 : unparse-set-cookie ( cookie -- string )
125 dup name>> check-cookie-string >lower
126 over value>> check-cookie-value unparse-cookie-value
127 "path" over path>> unparse-cookie-value
128 "domain" over domain>> unparse-cookie-value
129 "expires" over expires>> unparse-cookie-value
130 "max-age" over max-age>> unparse-cookie-value
131 "httponly" over http-only>> unparse-cookie-value
132 "secure" over secure>> unparse-cookie-value
134 ] { } make "; " join ;
145 : set-header ( request/response value key -- request/response )
146 pick header>> set-at ;
148 : set-basic-auth ( request username password -- request )
149 ":" glue >base64 "Basic " prepend "Authorization" set-header ;
151 : <request> ( -- request )
159 "close" "connection" set-header
160 "Factor http.client" "user-agent" set-header
161 max-redirects >>redirects ;
163 : header ( request/response key -- value )
177 : <response> ( -- response )
181 "close" "connection" set-header
182 now timestamp>http-string "date" set-header
183 "Factor http.server" "server" set-header
184 latin1 >>content-charset
185 V{ } clone >>cookies ;
189 [ clone ] change-header
190 [ clone ] change-cookies ;
192 : get-cookie ( request/response name -- cookie/f )
193 [ cookies>> ] dip '[ [ _ ] dip name>> = ] find nip ;
195 : delete-cookie ( request/response name -- )
196 over cookies>> [ get-cookie ] dip delete ;
198 : put-cookie ( request/response cookie -- request/response )
199 [ name>> dupd get-cookie [ dupd delete-cookie ] when* ] keep
200 over cookies>> push ;
208 : <raw-response> ( -- response )
212 TUPLE: post-data data params content-type content-encoding ;
214 : <post-data> ( content-type -- post-data )
216 swap >>content-type ;
218 : parse-content-type-attributes ( string -- attributes )
221 "\"" ?head drop "\"" ?tail drop
224 : parse-content-type ( content-type -- type encoding )
226 parse-content-type-attributes "charset" swap at name>encoding
227 [ dup "text/" head? latin1 binary ? ] unless* ;