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 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 ] produce ;
30 : collect-headers ( assoc -- assoc' )
31 H{ } clone [ '[ , push-at ] assoc-each ] keep ;
33 : process-header ( alist -- assoc )
34 f swap [ [ swap or dup ] dip swap ] assoc-map nip
35 collect-headers [ "; " join ] assoc-map
38 : read-header ( -- assoc )
39 (read-header) process-header ;
41 : header-value>string ( value -- string )
43 { [ dup timestamp? ] [ timestamp>http-string ] }
44 { [ dup array? ] [ [ header-value>string ] map "; " join ] }
48 : check-header-string ( str -- str )
49 #! http://en.wikipedia.org/wiki/HTTP_Header_Injection
50 dup "\r\n\"" intersect empty?
51 [ "Header injection attack" throw ] unless ;
53 : write-header ( assoc -- )
55 [ check-header-string write ": " write ]
56 [ header-value>string check-header-string write crlf ] bi*
59 TUPLE: cookie name value version comment path domain expires max-age http-only secure ;
61 : <cookie> ( value name -- cookie )
66 : parse-set-cookie ( string -- seq )
72 { "version" [ >>version ] }
73 { "comment" [ >>comment ] }
74 { "expires" [ cookie-string>timestamp >>expires ] }
75 { "max-age" [ string>number seconds >>max-age ] }
76 { "domain" [ >>domain ] }
78 { "httponly" [ drop t >>http-only ] }
79 { "secure" [ drop t >>secure ] }
80 [ <cookie> dup , nip ]
86 : parse-cookie ( string -- seq )
92 { "$version" [ >>version ] }
93 { "$domain" [ >>domain ] }
94 { "$path" [ >>path ] }
95 [ <cookie> dup , nip ]
101 : check-cookie-string ( string -- string' )
102 dup "=;'\"\r\n" intersect empty?
103 [ "Bad cookie name or value" throw ] unless ;
105 : unparse-cookie-value ( key value -- )
108 { t [ check-cookie-string , ] }
111 { [ dup timestamp? ] [ timestamp>cookie-string ] }
112 { [ dup duration? ] [ duration>seconds number>string ] }
113 { [ dup real? ] [ number>string ] }
116 [ check-cookie-string ] bi@ "=" swap 3append ,
120 : check-cookie-value ( string -- string )
121 [ "Cookie value must not be f" throw ] unless* ;
123 : (unparse-cookie) ( cookie -- strings )
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
132 : unparse-cookie ( cookies -- string )
133 [ (unparse-cookie) ] map concat "; " join ;
135 : unparse-set-cookie ( cookie -- string )
137 dup name>> check-cookie-string >lower
138 over value>> check-cookie-value unparse-cookie-value
139 "path" over path>> unparse-cookie-value
140 "domain" over domain>> unparse-cookie-value
141 "expires" over expires>> unparse-cookie-value
142 "max-age" over max-age>> unparse-cookie-value
143 "httponly" over http-only>> unparse-cookie-value
144 "secure" over secure>> unparse-cookie-value
146 ] { } make "; " join ;
156 : set-header ( request/response value key -- request/response )
157 pick header>> set-at ;
159 : <request> ( -- request )
167 "close" "connection" set-header
168 "Factor http.client" "user-agent" set-header ;
170 : header ( request/response key -- value )
183 : <response> ( -- response )
187 "close" "connection" set-header
188 now timestamp>http-string "date" set-header
189 "Factor http.server" "server" set-header
190 latin1 >>content-charset
191 V{ } clone >>cookies ;
195 [ clone ] change-header
196 [ clone ] change-cookies ;
198 : get-cookie ( request/response name -- cookie/f )
199 [ cookies>> ] dip '[ [ , ] dip name>> = ] find nip ;
201 : delete-cookie ( request/response name -- )
202 over cookies>> [ get-cookie ] dip delete ;
204 : put-cookie ( request/response cookie -- request/response )
205 [ name>> dupd get-cookie [ dupd delete-cookie ] when* ] keep
206 over cookies>> push ;
214 : <raw-response> ( -- response )
218 TUPLE: post-data raw content content-type ;
220 : <post-data> ( raw content-type -- post-data )
225 : parse-content-type-attributes ( string -- attributes )
226 " " split harvest [ "=" split1 [ >lower ] dip ] { } map>assoc ;
228 : parse-content-type ( content-type -- type encoding )
229 ";" split1 parse-content-type-attributes "charset" swap at
230 name>encoding over "text/" head? latin1 binary ? or ;