-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrest.r
292 lines (251 loc) · 7.44 KB
/
rest.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
Rebol [
Title: "REST-Friendly HTTP Protocol"
Author: "Christopher Ross-Gill"
Date: 5-Feb-2017
Home: http://www.ross-gill.com/page/REST_Protocol
File: %rest.r
Version: 0.1.5
Purpose: {
An elementary HTTP protocol allowing more versatility when developing Web
Services clients.
}
Rights: http://opensource.org/licenses/Apache-2.0
Type: 'module
Name: 'rgchris.rest
History: [
05-Feb-2017 0.1.5 "Correct usage of CURL's /INTO refinement"
12-Jan-2017 0.1.4 "Tidy up of OAuth portion"
30-Oct-2012 0.1.3 "Use CURL in place of native TCP; Added OAuth"
21-Aug-2010 0.1.2 "Submitted to Rebol.org"
09-Nov-2008 0.1.1 "Minor changes"
15-Aug-2006 0.1.0 "Original REST Version"
]
Notes: {To Do: Multipart and Multipart OAuth; Better Header Support}
]
do %altwebform.r
do %curl.r
unless in system/schemes 'rest [
system/schemes: make system/schemes [REST: none]
]
system/schemes/rest: make system/standard/port [
scheme: 'rest
port-id: 80
passive: none
cache-size: 5
proxy: make object! [host: port-id: user: pass: type: bypass: none]
]
system/schemes/rest/handler: use [prepare transcribe execute][
prepare: use [
request-prototype header-prototype
oauth-credentials oauth-prototype
sign
][
request-prototype: context [
version: 1.1
action: "GET"
headers: none
query: none
oauth: target: content: length: timeout: none
type: 'application/x-www-form-urlencoded
]
header-prototype: context [
Accept: "*/*"
Connection: "close"
User-Agent: rejoin ["Rebol/" system/product " " system/version]
Content-Length: Content-Type: Authorization: Range: none
]
oauth-credentials: context [
consumer-key: consumer-secret:
oauth-token: oauth-token-secret:
oauth-callback: none
]
oauth-prototype: context [
oauth_callback: none
oauth_consumer_key: none
oauth_token: oauth_nonce: none
oauth_signature_method: "HMAC-SHA1"
oauth_timestamp: none
oauth_version: 1.0
oauth_verifier: oauth_signature: none
]
sign: func [request [object!] /local header params timestamp out][
out: copy ""
timestamp: now/precise
header: make oauth-prototype [
oauth_consumer_key: request/oauth/consumer-key
oauth_token: request/oauth/oauth-token
oauth_callback: request/oauth/oauth-callback
oauth_nonce: enbase/base checksum/secure join timestamp oauth_consumer_key 64
oauth_timestamp: form any [
attempt [to integer! difference timestamp 1-Jan-1970/0:0:0]
timestamp - 1-Jan-1970/0:0:0 * 86400.0
]
clear find/last oauth_timestamp "."
]
params: make header any [request/content []]
params: sort/skip body-of params 2
header/oauth_signature: enbase/base checksum/secure/key rejoin [
uppercase form request/action "&" url-encode form request/url "&"
url-encode replace/all to-webform params "+" "%20"
] rejoin [
request/oauth/consumer-secret "&" any [request/oauth/oauth-token-secret ""]
] 64
foreach [name value] body-of header [
if value [
repend out [", " form name {="} url-encode form value {"}]
]
]
switch request/action [
"GET" [
if request/content [
request/url: join request/url to-webform/prefix request/content
request/content: none
]
]
"POST" "PUT" [
request/content: to-webform any [request/content []]
request/headers/Content-Type: "application/x-www-form-urlencoded"
]
]
request/headers/Authorization: join "OAuth" next out
]
prepare: func [port [port!] /local request][
port/locals/request: request: make request-prototype port/locals/request
request/action: uppercase form request/action
request/headers: make header-prototype any [request/headers []]
request/content: any [port/state/custom request/content]
either request/oauth [
request/oauth: make oauth-credentials request/oauth
sign request
][
request/headers/Authorization: any [
request/headers/authorization
if all [port/user port/pass][
join "Basic " enbase join port/user [#":" port/pass]
]
]
]
if port/state/index > 0 [
request/version: 1.1
request/headers/Range: rejoin ["bytes=" port/state/index "-"]
]
case/all [
block? request/content [
request/content: to-webform request/content
request/headers/Content-Type: "application/x-www-form-urlencoded"
]
string? request/content [
request/length: length? request/content
request/headers/Content-Length: length? request/content
request/headers/Content-Type: request/type
]
]
port
]
]
execute: func [port [port!]][
curl/full/method/header/with/timeout/into ; url action headers content response
port/locals/request/url
port/locals/request/action
port/locals/request/headers
port/locals/request/content
port/locals/request/timeout
port/locals/response
]
transcribe: use [
response-code header-feed header-name header-part
response-prototype header-prototype
][
response-code: use [digit][
digit: charset "0123456789"
[3 digit]
]
header-feed: [newline | crlf]
header-part: use [chars][
chars: complement charset [#"^(00)" - #"^(1F)"]
[some chars any [header-feed some " " some chars]]
]
header-name: use [chars][
chars: charset ["_-0123456789" #"a" - #"z" #"A" - #"Z"]
[some chars]
]
space: use [space][
space: charset " ^-"
[some space]
]
response-prototype: context [
status: message: http-headers: headers: content: binary: type: length: none
]
header-prototype: context [
Date: Server: Last-Modified: Accept-Ranges: Content-Encoding: Content-Type:
Content-Length: Location: Expires: Referer: Connection: Authorization: none
]
transcribe: func [port [port!] /local response name value pos][
port/locals/response: response: make response-prototype [
unless parse/all port/locals/response [
"HTTP/1." ["0" | "1"] space
copy status response-code (message: "")
opt [space copy message header-part] header-feed
(net-utils/net-log reform ["HTTP Response:" status message])
(
status: load status
headers: make block! []
)
some [
copy name header-name ":" any " "
copy value header-part header-feed
(repend headers [to set-word! name value])
]
header-feed content: to end (
content: as-string binary: as-binary copy content
)
][
net-utils/net-log pos
make error! "Could Not Parse Response"
]
headers: make header-prototype http-headers: new-line/all/skip headers true 2
type: all [
path? type: attempt [load headers/Content-Type]
type
]
length: any [attempt [headers/Content-Length: to integer! headers/Content-Length] 0]
]
]
]
context [
port-flags: system/standard/port-flags/pass-thru
init: func [port [port!] spec [url! block!] /local url][
port/locals: context [
request: case/all [
url? spec [
spec: compose [url: (to url! replace form spec rest:// http://)]
]
block? spec [
if verify [
url: case/all [
get-word? url: pick find compose spec quote url: 2 [
url: get :url
]
url? url [url]
][
make error! "REST spec needs a URL"
]
parse/all url ["http" opt "s" "://" to end] [
make error! "REST Spec only works with HTTP(S) urls"
]
][
spec
]
]
]
response: make string! ""
]
]
open: func [port [port!]][
port/state/flags: port/state/flags or port-flags
execute prepare port
]
copy: :transcribe
close: does []
]
]