1 ! Copyright (C) 2003, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel combinators math namespaces
5 assocs sequences splitting sorting sets debugger
6 strings vectors hashtables quotations arrays byte-arrays
7 math.parser calendar calendar.format present
9 io io.server io.sockets.secure
11 unicode.case unicode.categories qualified
13 urls html.templates xml xml.data xml.writer ;
19 : crlf ( -- ) "\r\n" write ;
21 : add-header ( value key assoc -- )
22 [ at dup [ "; " rot 3append ] [ drop ] if ] 2keep set-at ;
24 : header-line ( line -- )
32 swap >lower dup "last-header" set
33 "header" get add-header
39 : read-lf ( -- string )
40 "\n" read-until CHAR: \n assert= ;
42 : read-crlf ( -- string )
44 [ CHAR: \r assert= read1 CHAR: \n assert= ] when* ;
46 : read-header-line ( -- )
48 empty? [ drop ] [ header-line read-header-line ] if ;
50 : read-header ( -- assoc )
52 "header" [ read-header-line ] with-variable
55 : header-value>string ( value -- string )
57 { [ dup timestamp? ] [ timestamp>http-string ] }
58 { [ dup array? ] [ [ header-value>string ] map "; " join ] }
62 : check-header-string ( str -- str )
63 #! http://en.wikipedia.org/wiki/HTTP_Header_Injection
64 dup "\r\n" intersect empty?
65 [ "Header injection attack" throw ] unless ;
67 : write-header ( assoc -- )
69 swap url-encode write ": " write
70 header-value>string check-header-string write crlf
73 TUPLE: cookie name value path domain expires max-age http-only ;
75 : <cookie> ( value name -- cookie )
80 : parse-cookies ( string -- seq )
85 [ blank? ] trim "=" split1 swap >lower {
86 { "expires" [ cookie-string>timestamp >>expires ] }
87 { "max-age" [ string>number seconds >>max-age ] }
88 { "domain" [ >>domain ] }
90 { "httponly" [ drop t >>http-only ] }
92 [ <cookie> dup , nip ]
99 : (unparse-cookie) ( key value -- )
105 { [ dup timestamp? ] [ timestamp>cookie-string ] }
106 { [ dup duration? ] [ dt>seconds number>string ] }
113 : unparse-cookie ( cookie -- strings )
115 dup name>> >lower over value>> (unparse-cookie)
116 "path" over path>> (unparse-cookie)
117 "domain" over domain>> (unparse-cookie)
118 "expires" over expires>> (unparse-cookie)
119 "max-age" over max-age>> (unparse-cookie)
120 "httponly" over http-only>> (unparse-cookie)
124 : unparse-cookies ( cookies -- string )
125 [ unparse-cookie ] map concat "; " join ;
135 : set-header ( request/response value key -- request/response )
136 pick header>> set-at ;
138 : <request> ( -- request )
147 "close" "connection" set-header
148 "Factor http.client vocabulary" "user-agent" set-header ;
150 : read-method ( request -- request )
151 " " read-until [ "Bad request: method" throw ] unless
154 : check-absolute ( url -- url )
155 dup path>> "/" head? [ "Bad request: URL" throw ] unless ; inline
157 : read-url ( request -- request )
159 dup empty? [ drop read-url ] [ >url check-absolute >>url ] if
160 ] [ "Bad request: URL" throw ] if ;
162 : parse-version ( string -- version )
163 "HTTP/" ?head [ "Bad request: version" throw ] unless
164 dup { "1.0" "1.1" } member? [ "Bad request: version" throw ] unless ;
166 : read-request-version ( request -- request )
167 read-crlf [ CHAR: \s = ] left-trim
171 : read-request-header ( request -- request )
172 read-header >>header ;
174 : header ( request/response key -- value )
177 TUPLE: post-data raw content content-type ;
179 : <post-data> ( raw content-type -- post-data )
184 : parse-post-data ( post-data -- post-data )
185 [ ] [ raw>> ] [ content-type>> ] tri {
186 { "application/x-www-form-urlencoded" [ query>assoc ] }
187 { "text/xml" [ string>xml ] }
191 : read-post-data ( request -- request )
192 dup method>> "POST" = [
194 [ "content-length" header string>number read ]
195 [ "content-type" header ] tri
196 <post-data> parse-post-data >>post-data
199 : extract-host ( request -- request )
200 [ ] [ url>> ] [ "host" header parse-host ] tri
201 [ >>host ] [ >>port ] bi*
205 : extract-cookies ( request -- request )
206 dup "cookie" header [ parse-cookies >>cookies ] when* ;
208 : parse-content-type-attributes ( string -- attributes )
209 " " split harvest [ "=" split1 [ >lower ] dip ] { } map>assoc ;
211 : parse-content-type ( content-type -- type encoding )
212 ";" split1 parse-content-type-attributes "charset" swap at ;
214 : detect-protocol ( request -- request )
215 dup url>> remote-address get secure? "https" "http" ? >>protocol drop ;
217 : read-request ( -- request )
228 : write-method ( request -- request )
229 dup method>> write bl ;
231 : write-request-url ( request -- request )
232 dup url>> relative-url present write bl ;
234 : write-version ( request -- request )
235 "HTTP/" write dup request-version write crlf ;
237 : url-host ( url -- string )
238 [ host>> ] [ port>> ] bi dup "http" protocol-port =
239 [ drop ] [ ":" swap number>string 3append ] if ;
241 : write-request-header ( request -- request )
242 dup header>> >hashtable
243 over url>> host>> [ over url>> url-host "host" pick set-at ] when
245 [ raw>> length "content-length" pick set-at ]
246 [ content-type>> "content-type" pick set-at ]
249 over cookies>> f like [ unparse-cookies "cookie" pick set-at ] when*
252 GENERIC: >post-data ( object -- post-data )
254 M: post-data >post-data ;
256 M: string >post-data "application/octet-stream" <post-data> ;
258 M: byte-array >post-data "application/octet-stream" <post-data> ;
260 M: xml >post-data xml>string "text/xml" <post-data> ;
262 M: assoc >post-data assoc>query "application/x-www-form-urlencoded" <post-data> ;
266 : unparse-post-data ( request -- request )
267 [ >post-data ] change-post-data ;
269 : write-post-data ( request -- request )
270 dup method>> "POST" = [ dup post-data>> raw>> write ] when ;
272 : write-request ( request -- )
282 GENERIC: write-response ( response -- )
284 GENERIC: write-full-response ( request response -- )
296 : <response> ( -- response )
300 "close" "connection" set-header
301 now timestamp>http-string "date" set-header
302 V{ } clone >>cookies ;
304 : read-response-version ( response -- response )
306 [ "Bad response: version" throw ] unless
310 : read-response-code ( response -- response )
311 " \t" read-until [ "Bad response: code" throw ] unless
312 string>number [ "Bad response: code" throw ] unless*
315 : read-response-message ( response -- response )
316 read-crlf >>message ;
318 : read-response-header ( response -- response )
320 dup "set-cookie" header parse-cookies >>cookies
321 dup "content-type" header [
322 parse-content-type [ >>content-type ] [ >>content-charset ] bi*
325 : read-response ( -- response )
327 read-response-version
329 read-response-message
330 read-response-header ;
332 : write-response-version ( response -- response )
334 dup version>> write bl ;
336 : write-response-code ( response -- response )
337 dup code>> number>string write bl ;
339 : write-response-message ( response -- response )
340 dup message>> write crlf ;
342 : unparse-content-type ( request -- content-type )
343 [ content-type>> "application/octet-stream" or ]
344 [ content-charset>> ] bi
345 [ "; charset=" swap 3append ] when* ;
347 : write-response-header ( response -- response )
349 over cookies>> f like [ unparse-cookies "set-cookie" pick set-at ] when*
350 over unparse-content-type "content-type" pick set-at
353 : write-response-body ( response -- response )
354 dup body>> call-template ;
356 M: response write-response ( respose -- )
357 write-response-version
359 write-response-message
360 write-response-header
364 M: response write-full-response ( request response -- )
366 swap method>> "HEAD" = [ write-response-body ] unless ;
368 : get-cookie ( request/response name -- cookie/f )
369 [ cookies>> ] dip '[ , _ name>> = ] find nip ;
371 : delete-cookie ( request/response name -- )
372 over cookies>> [ get-cookie ] dip delete ;
374 : put-cookie ( request/response cookie -- request/response )
375 [ name>> dupd get-cookie [ dupd delete-cookie ] when* ] keep
376 over cookies>> push ;
384 : <raw-response> ( -- response )
388 M: raw-response write-response ( respose -- )
389 write-response-version
391 write-response-message
395 M: raw-response write-full-response ( response -- )