]> gitweb.factorcode.org Git - factor.git/blob - basis/http/http.factor
Merge branch 'master' of git://factorcode.org/git/factor into clean-linux-x86-32
[factor.git] / basis / http / http.factor
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
9 http.parsers
10 base64 ;
11 IN: http
12
13 : (read-header) ( -- alist )
14     [ read-crlf dup f like ] [ parse-header-line ] produce nip ;
15
16 : collect-headers ( assoc -- assoc' )
17     H{ } clone [ '[ _ push-at ] assoc-each ] keep ;
18
19 : process-header ( alist -- assoc )
20     f swap [ [ swap or dup ] dip swap ] assoc-map nip
21     collect-headers [ "; " join ] assoc-map
22     >hashtable ;
23
24 : read-header ( -- assoc )
25     (read-header) process-header ;
26
27 : header-value>string ( value -- string )
28     {
29         { [ dup timestamp? ] [ timestamp>http-string ] }
30         { [ dup array? ] [ [ header-value>string ] map "; " join ] }
31         [ present ]
32     } cond ;
33
34 : check-header-string ( str -- str )
35     #! http://en.wikipedia.org/wiki/HTTP_Header_Injection
36     dup "\r\n" intersects?
37     [ "Header injection attack" throw ] when ;
38
39 : write-header ( assoc -- )
40     >alist sort-keys [
41         [ check-header-string write ": " write ]
42         [ header-value>string check-header-string write crlf ] bi*
43     ] assoc-each crlf ;
44
45 TUPLE: cookie name value version comment path domain expires max-age http-only secure ;
46
47 : <cookie> ( value name -- cookie )
48     cookie new
49         swap >>name
50         swap >>value ;
51
52 : parse-set-cookie ( string -- seq )
53     [
54         f swap
55         (parse-set-cookie)
56         [
57             swap {
58                 { "version" [ >>version ] }
59                 { "comment" [ >>comment ] }
60                 { "expires" [ cookie-string>timestamp >>expires ] }
61                 { "max-age" [ string>number seconds >>max-age ] }
62                 { "domain" [ >>domain ] }
63                 { "path" [ >>path ] }
64                 { "httponly" [ drop t >>http-only ] }
65                 { "secure" [ drop t >>secure ] }
66                 [ <cookie> dup , nip ]
67             } case
68         ] assoc-each
69         drop
70     ] { } make ;
71
72 : parse-cookie ( string -- seq )
73     [
74         f swap
75         (parse-cookie)
76         [
77             swap {
78                 { "$version" [ >>version ] }
79                 { "$domain" [ >>domain ] }
80                 { "$path" [ >>path ] }
81                 [ <cookie> dup , nip ]
82             } case
83         ] assoc-each
84         drop
85     ] { } make ;
86
87 : check-cookie-string ( string -- string' )
88     dup "=;'\"\r\n" intersects?
89     [ "Bad cookie name or value" throw ] when ;
90
91 : unparse-cookie-value ( key value -- )
92     {
93         { f [ drop ] }
94         { t [ check-cookie-string , ] }
95         [
96             {
97                 { [ dup timestamp? ] [ timestamp>cookie-string ] }
98                 { [ dup duration? ] [ duration>seconds number>string ] }
99                 { [ dup real? ] [ number>string ] }
100                 [ ]
101             } cond
102             [ check-cookie-string ] bi@ "=" glue ,
103         ]
104     } case ;
105
106 : check-cookie-value ( string -- string )
107     [ "Cookie value must not be f" throw ] unless* ;
108
109 : (unparse-cookie) ( cookie -- strings )
110     [
111         dup name>> check-cookie-string >lower
112         over value>> check-cookie-value unparse-cookie-value
113         "$path" over path>> unparse-cookie-value
114         "$domain" over domain>> unparse-cookie-value
115         drop
116     ] { } make ;
117
118 : unparse-cookie ( cookies -- string )
119     [ (unparse-cookie) ] map concat "; " join ;
120
121 : unparse-set-cookie ( cookie -- string )
122     [
123         dup name>> check-cookie-string >lower
124         over value>> check-cookie-value unparse-cookie-value
125         "path" over path>> unparse-cookie-value
126         "domain" over domain>> unparse-cookie-value
127         "expires" over expires>> unparse-cookie-value
128         "max-age" over max-age>> unparse-cookie-value
129         "httponly" over http-only>> unparse-cookie-value
130         "secure" over secure>> unparse-cookie-value
131         drop
132     ] { } make "; " join ;
133
134 TUPLE: request
135 method
136 url
137 version
138 header
139 post-data
140 cookies ;
141
142 : set-header ( request/response value key -- request/response )
143     pick header>> set-at ;
144
145 : set-basic-auth ( request username password -- request )
146     ":" glue >base64 "Basic " prepend "Authorization" set-header ;
147     
148 : <request> ( -- request )
149     request new
150         "1.1" >>version
151         <url>
152             H{ } clone >>query
153         >>url
154         H{ } clone >>header
155         V{ } clone >>cookies
156         "close" "connection" set-header
157         "Factor http.client" "user-agent" set-header ;
158
159 : header ( request/response key -- value )
160     swap header>> at ;
161
162
163 TUPLE: response
164 version
165 code
166 message
167 header
168 cookies
169 content-type
170 content-charset
171 body ;
172
173 : <response> ( -- response )
174     response new
175         "1.1" >>version
176         H{ } clone >>header
177         "close" "connection" set-header
178         now timestamp>http-string "date" set-header
179         "Factor http.server" "server" set-header
180         latin1 >>content-charset
181         V{ } clone >>cookies ;
182
183 M: response clone
184     call-next-method
185         [ clone ] change-header
186         [ clone ] change-cookies ;
187
188 : get-cookie ( request/response name -- cookie/f )
189     [ cookies>> ] dip '[ [ _ ] dip name>> = ] find nip ;
190
191 : delete-cookie ( request/response name -- )
192     over cookies>> [ get-cookie ] dip delete ;
193
194 : put-cookie ( request/response cookie -- request/response )
195     [ name>> dupd get-cookie [ dupd delete-cookie ] when* ] keep
196     over cookies>> push ;
197
198 TUPLE: raw-response
199 version
200 code
201 message
202 body ;
203
204 : <raw-response> ( -- response )
205     raw-response new
206         "1.1" >>version ;
207
208 TUPLE: post-data data params content-type content-encoding ;
209
210 : <post-data> ( content-type -- post-data )
211     post-data new
212         swap >>content-type ;
213
214 : parse-content-type-attributes ( string -- attributes )
215     " " split harvest [
216         "=" split1
217         "\"" ?head drop "\"" ?tail drop
218     ] { } map>assoc ;
219
220 : parse-content-type ( content-type -- type encoding )
221     ";" split1
222     parse-content-type-attributes "charset" swap at name>encoding
223     [ dup "text/" head? latin1 binary ? ] unless* ;