]> gitweb.factorcode.org Git - factor.git/blob - basis/http/client/client.factor
9260f15a7b3df0aab9f3d6730a983ea64b640076
[factor.git] / basis / http / client / client.factor
1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs kernel math math.parser namespaces make
4 sequences io io.sockets io.streams.string io.files io.timeouts
5 strings splitting calendar continuations accessors vectors
6 math.order hashtables byte-arrays prettyprint destructors
7 io.encodings
8 io.encodings.string
9 io.encodings.ascii
10 io.encodings.8-bit
11 io.encodings.binary
12 io.streams.duplex
13 fry debugger summary ascii urls urls.encoding present
14 http http.parsers ;
15 IN: http.client
16
17 : write-request-line ( request -- request )
18     dup
19     [ method>> write bl ]
20     [ url>> relative-url present write bl ]
21     [ "HTTP/" write version>> write crlf ]
22     tri ;
23
24 : url-host ( url -- string )
25     [ host>> ] [ port>> ] bi dup "http" protocol-port =
26     [ drop ] [ ":" swap number>string 3append ] if ;
27
28 : write-request-header ( request -- request )
29     dup header>> >hashtable
30     over url>> host>> [ over url>> url-host "host" pick set-at ] when
31     over post-data>> [
32         [ raw>> length "content-length" pick set-at ]
33         [ content-type>> "content-type" pick set-at ]
34         bi
35     ] when*
36     over cookies>> [ unparse-cookie "cookie" pick set-at ] unless-empty
37     write-header ;
38
39 GENERIC: >post-data ( object -- post-data )
40
41 M: post-data >post-data ;
42
43 M: string >post-data "application/octet-stream" <post-data> ;
44
45 M: byte-array >post-data "application/octet-stream" <post-data> ;
46
47 M: assoc >post-data assoc>query "application/x-www-form-urlencoded" <post-data> ;
48
49 M: f >post-data ;
50
51 : unparse-post-data ( request -- request )
52     [ >post-data ] change-post-data ;
53
54 : write-post-data ( request -- request )
55     dup method>> "POST" = [ dup post-data>> raw>> write ] when ; 
56
57 : write-request ( request -- )
58     unparse-post-data
59     write-request-line
60     write-request-header
61     write-post-data
62     flush
63     drop ;
64
65 : read-response-line ( response -- response )
66     read-crlf parse-response-line first3
67     [ >>version ] [ >>code ] [ >>message ] tri* ;
68
69 : read-response-header ( response -- response )
70     read-header >>header
71     dup "set-cookie" header parse-set-cookie >>cookies
72     dup "content-type" header [
73         parse-content-type
74         [ >>content-type ]
75         [ >>content-charset ] bi*
76     ] when* ;
77
78 : read-response ( -- response )
79     <response>
80     read-response-line
81     read-response-header ;
82
83 : max-redirects 10 ;
84
85 ERROR: too-many-redirects ;
86
87 M: too-many-redirects summary
88     drop
89     [ "Redirection limit of " % max-redirects # " exceeded" % ] "" make ;
90
91 <PRIVATE
92
93 DEFER: (with-http-request)
94
95 SYMBOL: redirects
96
97 : redirect-url ( request url -- request )
98     '[ _ >url derive-url ensure-port ] change-url ;
99
100 : redirect? ( response -- ? )
101     code>> 300 399 between? ;
102
103 : do-redirect ( quot: ( chunk -- ) response -- response )
104     redirects inc
105     redirects get max-redirects < [
106         request get clone
107         swap "location" header redirect-url
108         "GET" >>method swap (with-http-request)
109     ] [ too-many-redirects ] if ; inline recursive
110
111 : read-chunk-size ( -- n )
112     read-crlf ";" split1 drop [ blank? ] trim-right
113     hex> [ "Bad chunk size" throw ] unless* ;
114
115 : read-chunked ( quot: ( chunk -- ) -- )
116     read-chunk-size dup zero?
117     [ 2drop ] [
118         read [ swap call ] [ drop ] 2bi
119         read-crlf B{ } assert= read-chunked
120     ] if ; inline recursive
121
122 : read-unchunked ( quot: ( chunk -- ) -- )
123     8192 read-partial dup [
124         [ swap call ] [ drop read-unchunked ] 2bi
125     ] [ 2drop ] if ; inline recursive
126
127 : read-response-body ( quot response -- )
128     binary decode-input
129     "transfer-encoding" header "chunked" =
130     [ read-chunked ] [ read-unchunked ] if ; inline
131
132 : <request-socket> ( -- stream )
133     request get url>> url-addr ascii <client> drop
134     1 minutes over set-timeout ;
135
136 : (with-http-request) ( request quot: ( chunk -- ) -- response )
137     swap
138     request [
139         <request-socket> [
140             [
141                 out>>
142                 [ request get write-request ]
143                 with-output-stream*
144             ] [
145                 in>> [
146                     read-response dup redirect? [ t ] [
147                         [ nip response set ]
148                         [ read-response-body ]
149                         [ ]
150                         2tri f
151                     ] if
152                 ] with-input-stream*
153             ] bi
154         ] with-disposal
155         [ do-redirect ] [ nip ] if
156     ] with-variable ; inline recursive
157
158 PRIVATE>
159
160 : success? ( code -- ? ) 200 = ;
161
162 ERROR: download-failed response ;
163
164 M: download-failed error.
165     "HTTP request failed:" print nl
166     response>> . ;
167
168 : check-response ( response -- response )
169     dup code>> success? [ download-failed ] unless ;
170
171 : with-http-request ( request quot -- response )
172     (with-http-request) check-response ; inline
173
174 : http-request ( request -- response data )
175     [ [ % ] with-http-request ] B{ } make
176     over content-charset>> decode ;
177
178 : <get-request> ( url -- request )
179     <request>
180         "GET" >>method
181         swap >url ensure-port >>url ;
182
183 : http-get ( url -- response data )
184     <get-request> http-request ;
185
186 : with-http-get ( url quot -- response )
187     [ <get-request> ] dip with-http-request ; inline
188
189 : download-name ( url -- name )
190     present file-name "?" split1 drop "/" ?tail drop ;
191
192 : download-to ( url file -- )
193     binary [ [ write ] with-http-get drop ] with-file-writer ;
194
195 : download ( url -- )
196     dup download-name download-to ;
197
198 : <post-request> ( post-data url -- request )
199     <request>
200         "POST" >>method
201         swap >url ensure-port >>url
202         swap >>post-data ;
203
204 : http-post ( post-data url -- response data )
205     <post-request> http-request ;