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