/
http-patched.r
360 lines (335 loc) · 13.3 KB
/
http-patched.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
REBOL [
Title: "REBOL Protocols: HTTP"
Version: 2.7.6
Rights: "Copyright REBOL Technologies 2008. All rights reserved."
Home: http://www.rebol.com
Date: 14-Mar-2008
; You are free to use, modify, and distribute this file as long as the
; above header, copyright, and this entire comment remains intact.
; This software is provided "as is" without warranties of any kind.
; In no event shall REBOL Technologies or source contributors be liable
; for any damages of any kind, even if advised of the possibility of such
; damage. See license for more information.
; Please help us to improve this software by contributing changes and
; fixes. See http://www.rebol.com/support.html for details.
]
; I've made some changes to the create-request function to allow http PUT and GET with cookie support.
;
; Eg.
;
; read/custom URL [ put %file [ Cookie: "authtoken=foo" ]] will upload a file to URL and also send the cookie header.
;
; read/custom URL [ put "some text" ] will PUT the "some text" to the URL
;
; read/custom URL [ get "" [ Cookie: "authtoken=anotherfoo" ]] will read the page at URL while sending the cookie header.
;
; NB: Rebol2's existing prot-http currently supports this undocumented method using a 'header keyword
;
; read URL [ header [ Cookie: "authtoken=anotherfoo" ]]
;
; read/custom URL reduce [ 'soap payload [soapaction: "" ]] will send the SOAP payload to the URL
;
; Graham
;
; 7-Aug-2009
;
; added HEAD support
;
; read/custom URL [ HEAD "" ]
make Root-Protocol [
"The HTTP protocol."
open: func [
port "the port to open"
/local http-packet http-command response-actions success error response-line
target headers http-version post-data result generic-proxy? sub-protocol
build-port send-and-check create-request line continue-post
tunnel-actions tunnel-success response-code forward proxyauth
][
; RAMBO #4039: moved QUERYING to locals
; also now QUERY will initialize port/locals
unless port/locals [port/locals: make object! [list: copy [] headers: none querying: no]]
generic-proxy?: all [port/proxy/type = 'generic not none? port/proxy/host]
build-port: func [] [
sub-protocol: either port/scheme = 'https ['ssl] ['tcp]
open-proto/sub-protocol/generic port sub-protocol
port/url: rejoin [lowercase to-string port/scheme "://" port/host either port/port-id <> 80 [join #":" port/port-id] [copy ""] slash]
if found? port/path [append port/url port/path]
if found? port/target [append port/url port/target]
if sub-protocol = 'ssl [
if generic-proxy? [
HTTP-Get-Header: make object! [
Host: join port/host any [all [port/port-id (port/port-id <> 80) join #":" port/port-id] #]
]
user: get in port/proxy 'user
pass: get in port/proxy 'pass
if string? :user [
HTTP-Get-Header: make HTTP-Get-Header [
Proxy-Authorization: join "Basic " enbase join user [#":" pass]
]
]
http-packet: reform ["CONNECT" HTTP-Get-Header/Host "HTTP/1.1^/"]
append http-packet net-utils/export HTTP-Get-Header
append http-packet "^/"
net-utils/net-log http-packet
insert port/sub-port http-packet
continue-post/tunnel
]
system/words/set-modes port/sub-port [secure: true]
]
]
; smarter query
http-command: either port/locals/querying ["HEAD"] ["GET"]
create-request: func [/local target user pass u data ] [
HTTP-Get-Header: make object! [
Accept: "*/*"
Connection: "close"
User-Agent: get in get in system/schemes port/scheme 'user-agent
Host: join port/host any [all [port/port-id (port/port-id <> 80) join #":" port/port-id] #]
]
if all [block? port/state/custom post-data: select port/state/custom 'header block? post-data] [
HTTP-Get-Header: make HTTP-Get-Header post-data
]
HTTP-Header: make object! [
Date: Server: Last-Modified: Accept-Ranges: Content-Encoding: Content-Type:
Content-Length: Location: Expires: Referer: Connection: Authorization: none
]
http-version: "HTTP/1.0^/"
all [port/user port/pass HTTP-Get-Header: make HTTP-Get-Header [Authorization: join "Basic " enbase join port/user [#":" port/pass]]]
user: get in port/proxy 'user
pass: get in port/proxy 'pass
if all [generic-proxy? string? :user] [
HTTP-Get-Header: make HTTP-Get-Header [
Proxy-Authorization: join "Basic " enbase join user [#":" pass]
]
]
; range request
if port/state/index > 0 [
http-version: "HTTP/1.1^/"
HTTP-Get-Header: make HTTP-Get-Header [
Range: rejoin ["bytes=" port/state/index "-"]
]
]
target: next mold to-file join (join "/" either found? port/path [port/path] [""]) either found? port/target [port/target] [""]
post-data: none
comment { ; original code
if all [block? port/state/custom post-data: find port/state/custom 'post post-data/2] [
http-command: "POST"
HTTP-Get-Header: make HTTP-Get-Header append [
Referer: either find port/url #"?" [head clear find copy port/url #"?"] [port/url]
Content-Type: "application/x-www-form-urlencoded"
Content-Length: length? post-data/2
] either block? post-data/3 [post-data/3] [[]]
post-data: post-data/2
]
}
; start Graham's changes
either all [block? port/state/custom post-data: find port/state/custom 'post post-data/2] [
http-command: "POST"
HTTP-Get-Header: make HTTP-Get-Header append [
Referer: either find port/url #"?" [head clear find copy port/url #"?"] [port/url]
Content-Type: "application/x-www-form-urlencoded"
Content-Length: length? post-data/2
] either block? post-data/3 [post-data/3] [[]]
post-data: post-data/2
][
either all [
block? port/state/custom
any [
post-data: find port/state/custom to-word http-command: "GET"
post-data: find port/state/custom to-word http-command: "HEAD"
]
post-data/2
] [
HTTP-Get-Header: make HTTP-Get-Header append [
Referer: either find port/url #"?" [head clear find copy port/url #"?"] [port/url]
] either block? post-data/3 [post-data/3] [[]]
post-data: none
][
either all [block? port/state/custom post-data: find port/state/custom 'put post-data/2] [
http-command: "PUT"
data: either file? post-data/2 [
system/words/read/binary post-data/2
][
post-data/2
]
HTTP-Get-Header: make HTTP-Get-Header append [
Content-Type: "application/octet-stream"
Content-Length: length? data
] either block? post-data/3 [post-data/3] [[]]
post-data: data
][
either all [block? port/state/custom post-data: find port/state/custom 'soap post-data/2] [
http-command: "POST"
data: either file? post-data/2 [
system/words/read/binary post-data/2
][
post-data/2
]
HTTP-Get-Header: make HTTP-Get-Header append [
Content-Type: {text/xml; charset="utf-8"}
Content-Length: length? data
] either block? post-data/3 [post-data/3] [[]]
post-data: data
][
if all [block? port/state/custom post-data: find port/state/custom 'delete post-data/2] [
http-command: "DELETE"
HTTP-Get-Header: make HTTP-Get-Header append [
Referer: either find port/url #"?" [head clear find copy port/url #"?"] [port/url]
] either block? post-data/3 [post-data/3] [[]]
post-data: none
]
]
]
]
]
; end changes from Graham
http-packet: reform [http-command either generic-proxy? [port/url] [target] http-version]
append http-packet net-utils/export HTTP-Get-Header
; append http-packet "^/"
; if post-data [append http-packet post-data]
]
send-and-check: func [/local ptr data] [
net-utils/net-log http-packet
; Sterling, why was this changed from insert to write-io ? It causes HTTP to be sent
; without cr and breaks things.
; write-io port/sub-port http-packet length? http-packet
insert port/sub-port http-packet
; Begin Max's patch
; This lets more than 16K be sent as post data when using https.
;-- old
; if post-data [
; write-io port/sub-port post-data length? post-data
; ]
;-- new
if post-data [
ptr: post-data
until [
data: copy/part ptr 8192
ptr: skip ptr 8192
write-io port/sub-port data length? data
tail? ptr
]
]
; End Max's patch
continue-post
]
continue-post: func [/tunnel /local digit space] [
response-line: system/words/pick port/sub-port 1
net-utils/net-log response-line
either none? response-line [do error][
; fixes #3494: should accept an HTTP/0.9 simple response.
digit: charset "1234567890"
space: charset " ^-"
either parse/all response-line [
; relaxing rule a bit
;"HTTP/" digit "." digit some space copy response-code 3 digit some space to end
"HTTP/" digit "." digit some space copy response-code 3 digit to end
] [
; valid status line
response-code: to integer! response-code
result: select either tunnel [tunnel-actions] [response-actions] response-code
either none? result [do error] [do get result]
] [
; could not parse status line, assuming HTTP/0.9
port/status: 'file
]
]
]
tunnel-actions: [
200 tunnel-success ; Tunnel established
]
response-actions: [
100 continue-post ; HTTP/1.1 continue with posting data
200 success ; standard valid response
201 success ; post command successful - new url included
204 success ; no new content (maybe use :true here?)
206 success ; read partial content
300 forward ; multiple choices of locations in the body - maybe preferred in Location:
301 forward ; moved permanently - Location: hold new loc
302 forward ; moved temporarily - Location: hold new loc
304 success ; not modified since the If-Modified-Since header date
307 forward ; temporary redirect
407 proxyauth ; requires proxy authorization
]
tunnel-success: [
while [ ( line: pick port/sub-port 1 ) <> "" ] [net-utils/net-log line]
]
success: [
headers: make string! 500
while [ ( line: pick port/sub-port 1 ) <> "" ] [append headers join line "^/"] ; remove the headers
port/locals/headers: headers: Parse-Header HTTP-Header headers
port/size: 0
if port/locals/querying [if headers/Content-Length [port/size: load headers/Content-Length]]
if error? try [port/date: parse-header-date headers/Last-Modified] [port/date: none]
port/status: 'file
]
error: [
system/words/close port/sub-port
net-error reform ["Error. Target url:" port/url "could not be retrieved. Server response:" response-line]
]
forward: [
page: copy ""
while [ ( str: pick port/sub-port 1 ) <> "" ][ append page reduce [str newline] ]
headers: Parse-Header HTTP-Header page
insert port/locals/list port/url
either found? headers/Location [
either any [find/match headers/Location "http://" find/match headers/Location "https://"] [ ; new whole url to go to
port/path: port/target: port/port-id: none
net-utils/URL-Parser/parse-url/set-scheme port to-url port/url: headers/Location
if not port/port-id: any [port/port-id all [in system/schemes port/scheme get in get in system/schemes port/scheme 'port-id]] [
net-error reform ["HTTP forwarding error: Scheme" port/scheme "for URL" port/url "not supported in this REBOL."]
]
] [
either (first headers/Location) = slash [port/path: none remove headers/Location] [either port/path [insert port/path "/"] [port/path: copy "/"]]
port/target: headers/Location
port/url: rejoin [lowercase to-string port/scheme "://" port/host either port/path [port/path] [""] either port/target [port/target] [""]]
]
if find/case port/locals/list port/url [net-error reform ["Error. Target url:" port/url "could not be retrieved. Circular forwarding detected"]]
system/words/close port/sub-port
build-port
create-request
send-and-check
] [
do error
]
]
proxyauth: [
system/words/close port/sub-port
either all [ generic-proxy? (not string? get in port/proxy 'user) ] [
port/proxy/user: system/schemes/http/proxy/user: port/proxy/user
port/proxy/pass: system/schemes/http/proxy/pass: port/proxy/pass
if not error? try [result: get in system/schemes 'https] [
result/proxy/user: port/proxy/user
result/proxy/pass: port/proxy/pass
]
] [
net-error reform ["Error. Target url:" port/url "could not be retrieved: Proxy authentication denied"]
]
build-port
create-request
send-and-check
]
build-port
create-request
send-and-check
]
query: func [port] [
if not port/locals [
; RAMBO #4039: query mode is local to port now
port/locals: make object! [list: copy [] headers: none querying: yes]
open port
; port was kept open after query
; attempt for extra safety
; also note, local close on purpose
attempt [close port]
; RAMBO #3718 - superceded by fix for #4039
;querying: false
]
none
]
close: func [port] [system/words/close port/sub-port]
net-utils/net-install HTTP self 80
system/schemes/http: make system/schemes/http [user-agent: reform ["REBOL" system/product system/version]]
net-utils/net-install HTTPS self 443
system/schemes/https: make system/schemes/https [user-agent: reform ["REBOL" system/product system/version]]
]