1 ! Copyright (C) 2003, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel combinators math namespaces
4 assocs assocs.lib sequences splitting sorting sets debugger
5 strings vectors hashtables quotations arrays byte-arrays
6 math.parser calendar calendar.format present
8 io io.encodings io.encodings.iana io.encodings.binary
11 unicode.case unicode.categories qualified
21 : crlf ( -- ) "\r\n" write ;
23 : read-crlf ( -- bytes )
25 [ CHAR: \r assert= read1 CHAR: \n assert= ] when* ;
27 : (read-header) ( -- alist )
28 [ read-crlf dup f like ] [ parse-header-line ] [ drop ] unfold ;
30 : process-header ( alist -- assoc )
31 f swap [ [ swap or dup ] dip swap ] assoc-map nip
32 [ ?push ] histogram [ "; " join ] assoc-map
35 : read-header ( -- assoc )
36 (read-header) process-header ;
38 : header-value>string ( value -- string )
40 { [ dup timestamp? ] [ timestamp>http-string ] }
41 { [ dup array? ] [ [ header-value>string ] map "; " join ] }
45 : check-header-string ( str -- str )
46 #! http://en.wikipedia.org/wiki/HTTP_Header_Injection
47 dup "\r\n\"" intersect empty?
48 [ "Header injection attack" throw ] unless ;
50 : write-header ( assoc -- )
52 [ check-header-string write ": " write ]
53 [ header-value>string check-header-string write crlf ] bi*
56 TUPLE: cookie name value version comment path domain expires max-age http-only secure ;
58 : <cookie> ( value name -- cookie )
63 : parse-set-cookie ( string -- seq )
69 { "version" [ >>version ] }
70 { "comment" [ >>comment ] }
71 { "expires" [ cookie-string>timestamp >>expires ] }
72 { "max-age" [ string>number seconds >>max-age ] }
73 { "domain" [ >>domain ] }
75 { "httponly" [ drop t >>http-only ] }
76 { "secure" [ drop t >>secure ] }
77 [ <cookie> dup , nip ]
83 : parse-cookie ( string -- seq )
89 { "$version" [ >>version ] }
90 { "$domain" [ >>domain ] }
91 { "$path" [ >>path ] }
92 [ <cookie> dup , nip ]
98 : check-cookie-string ( string -- string' )
99 dup "=;'\"\r\n" intersect empty?
100 [ "Bad cookie name or value" throw ] unless ;
102 : unparse-cookie-value ( key value -- )
105 { t [ check-cookie-string , ] }
108 { [ dup timestamp? ] [ timestamp>cookie-string ] }
109 { [ dup duration? ] [ dt>seconds number>string ] }
110 { [ dup real? ] [ number>string ] }
113 check-cookie-string "=" swap check-cookie-string 3append ,
117 : (unparse-cookie) ( cookie -- strings )
119 dup name>> check-cookie-string >lower
120 over value>> unparse-cookie-value
121 "$path" over path>> unparse-cookie-value
122 "$domain" over domain>> unparse-cookie-value
126 : unparse-cookie ( cookies -- string )
127 [ (unparse-cookie) ] map concat "; " join ;
129 : unparse-set-cookie ( cookie -- string )
131 dup name>> check-cookie-string >lower
132 over value>> unparse-cookie-value
133 "path" over path>> unparse-cookie-value
134 "domain" over domain>> unparse-cookie-value
135 "expires" over expires>> unparse-cookie-value
136 "max-age" over max-age>> unparse-cookie-value
137 "httponly" over http-only>> unparse-cookie-value
138 "secure" over secure>> unparse-cookie-value
140 ] { } make "; " join ;
150 : set-header ( request/response value key -- request/response )
151 pick header>> set-at ;
153 : <request> ( -- request )
161 "close" "connection" set-header
162 "Factor http.client" "user-agent" set-header ;
164 : 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 '[ , _ 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 raw content content-type ;
214 : <post-data> ( raw content-type -- post-data )
219 : parse-content-type-attributes ( string -- attributes )
220 " " split harvest [ "=" split1 [ >lower ] dip ] { } map>assoc ;
222 : parse-content-type ( content-type -- type encoding )
223 ";" split1 parse-content-type-attributes "charset" swap at
224 name>encoding over "text/" head? latin1 binary ? or ;