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
8 io io.encodings io.encodings.iana io.encodings.binary
9 io.encodings.8-bit io.crlf
11 unicode.case unicode.categories
19 : (read-header) ( -- alist )
20 [ read-crlf dup f like ] [ parse-header-line ] [ drop ] produce ;
22 : collect-headers ( assoc -- assoc' )
23 H{ } clone [ '[ _ push-at ] assoc-each ] keep ;
25 : process-header ( alist -- assoc )
26 f swap [ [ swap or dup ] dip swap ] assoc-map nip
27 collect-headers [ "; " join ] assoc-map
30 : read-header ( -- assoc )
31 (read-header) process-header ;
33 : header-value>string ( value -- string )
35 { [ dup timestamp? ] [ timestamp>http-string ] }
36 { [ dup array? ] [ [ header-value>string ] map "; " join ] }
40 : check-header-string ( str -- str )
41 #! http://en.wikipedia.org/wiki/HTTP_Header_Injection
42 dup "\r\n\"" intersects?
43 [ "Header injection attack" throw ] when ;
45 : write-header ( assoc -- )
47 [ check-header-string write ": " write ]
48 [ header-value>string check-header-string write crlf ] bi*
51 TUPLE: cookie name value version comment path domain expires max-age http-only secure ;
53 : <cookie> ( value name -- cookie )
58 : parse-set-cookie ( string -- seq )
64 { "version" [ >>version ] }
65 { "comment" [ >>comment ] }
66 { "expires" [ cookie-string>timestamp >>expires ] }
67 { "max-age" [ string>number seconds >>max-age ] }
68 { "domain" [ >>domain ] }
70 { "httponly" [ drop t >>http-only ] }
71 { "secure" [ drop t >>secure ] }
72 [ <cookie> dup , nip ]
78 : parse-cookie ( string -- seq )
84 { "$version" [ >>version ] }
85 { "$domain" [ >>domain ] }
86 { "$path" [ >>path ] }
87 [ <cookie> dup , nip ]
93 : check-cookie-string ( string -- string' )
94 dup "=;'\"\r\n" intersects?
95 [ "Bad cookie name or value" throw ] when ;
97 : unparse-cookie-value ( key value -- )
100 { t [ check-cookie-string , ] }
103 { [ dup timestamp? ] [ timestamp>cookie-string ] }
104 { [ dup duration? ] [ duration>seconds number>string ] }
105 { [ dup real? ] [ number>string ] }
108 [ check-cookie-string ] bi@ "=" glue ,
112 : check-cookie-value ( string -- string )
113 [ "Cookie value must not be f" throw ] unless* ;
115 : (unparse-cookie) ( cookie -- strings )
117 dup name>> check-cookie-string >lower
118 over value>> check-cookie-value unparse-cookie-value
119 "$path" over path>> unparse-cookie-value
120 "$domain" over domain>> unparse-cookie-value
124 : unparse-cookie ( cookies -- string )
125 [ (unparse-cookie) ] map concat "; " join ;
127 : unparse-set-cookie ( cookie -- string )
129 dup name>> check-cookie-string >lower
130 over value>> check-cookie-value unparse-cookie-value
131 "path" over path>> unparse-cookie-value
132 "domain" over domain>> unparse-cookie-value
133 "expires" over expires>> unparse-cookie-value
134 "max-age" over max-age>> unparse-cookie-value
135 "httponly" over http-only>> unparse-cookie-value
136 "secure" over secure>> unparse-cookie-value
138 ] { } make "; " join ;
148 : set-header ( request/response value key -- request/response )
149 pick header>> set-at ;
151 : <request> ( -- request )
159 "close" "connection" set-header
160 "Factor http.client" "user-agent" set-header ;
162 : header ( request/response key -- value )
175 : <response> ( -- response )
179 "close" "connection" set-header
180 now timestamp>http-string "date" set-header
181 "Factor http.server" "server" set-header
182 latin1 >>content-charset
183 V{ } clone >>cookies ;
187 [ clone ] change-header
188 [ clone ] change-cookies ;
190 : get-cookie ( request/response name -- cookie/f )
191 [ cookies>> ] dip '[ [ _ ] dip name>> = ] find nip ;
193 : delete-cookie ( request/response name -- )
194 over cookies>> [ get-cookie ] dip delete ;
196 : put-cookie ( request/response cookie -- request/response )
197 [ name>> dupd get-cookie [ dupd delete-cookie ] when* ] keep
198 over cookies>> push ;
206 : <raw-response> ( -- response )
210 TUPLE: post-data data params content-type content-encoding ;
212 : <post-data> ( content-type -- post-data )
214 swap >>content-type ;
216 : parse-content-type-attributes ( string -- attributes )
217 " " split harvest [ "=" split1 [ >lower ] dip ] { } map>assoc ;
219 : parse-content-type ( content-type -- type encoding )
220 ";" split1 parse-content-type-attributes "charset" swap at
221 name>encoding over "text/" head? latin1 binary ? or ;