forked from rebolek/gritter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-tools.red
159 lines (152 loc) · 3.26 KB
/
http-tools.red
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
Red [
Title: "HTTP Tools"
File: %http-tools.red
Author: "Boleslav Březovský"
Description: "Collection of tools to make using HTTP easier"
Date: "10-4-2017"
]
do %json.red
make-url: function [
"Make URL from simple dialect"
data
] [
value: none
args: clear []
link: make url! 80
args-rule: [
ahead block! into [
some [
set value set-word! (append args rejoin [form value #"="])
set value [word! | string! | integer!] (
if word? value [value: get :value]
append args rejoin [value #"&"]
)
]
]
]
parse append clear [] data [
some [
args-rule
| set value [set-word! | file! | url! ] (append link dirize form value)
| set value word! (append link dirize form get :value)
]
]
unless empty? args [
change back tail link #"?"
append link args
]
head remove back tail link
]
send-request: function [
link
method
/data "Use with POST and other methods"
content
/with
args
/auth
auth-type [word!]
auth-data
] [
header: clear #()
if with [extend header args]
if auth [
switch auth-type [
Basic [
extend header compose [
Authorization: (rejoin [auth-type space enbase rejoin [first auth-data #":" second auth-data]])
]
]
OAuth [
; TODO: Add OAuth (see Twitter API)
]
Bearer [
; token passing for Gitter
extend header compose [
Authorization: (rejoin [auth-type space auth-data])
]
]
]
]
data: reduce [method body-of header]
if content [append data content]
reply: write/info link data
type: first split reply/2/Content-Type #";"
make map! reduce [
quote code: reply/1
quote headers: reply/2
quote raw: reply/3
; TODO: decode data based on reply/2/Content-Type
; data: (www-form/decode reply/3 type)
quote data: json/decode reply/3
]
]
www-form: object [
encode: function [
data
/only "Ignore NONE values"
/with
pattern
] [
if any [map? data object? data] [data: body-of data]
print mold data
unless with [pattern: [key {="} value {", }]]
output: collect/into [
foreach [key value] data [
if any [not only all [only value]] [
keep rejoin bind pattern 'key
]
]
] make string! 1000
cut-tail/part output either with [length? form last pattern] [2]
]
decode: function [
text
type
] [
; TODO: just www-form decoder should be here
; there should be another function on top of this (MIME-DECODER)
switch type [
"application/json" [text]
"application/x-www-form-urlencoded" [
text: make map! split text charset "=&"
]
"text/html" [
text: make map! split text charset "=&"
]
]
text
]
]
make-nonce: function [] [
nonce: enbase/base checksum form random/secure 2147483647 'SHA512 64
remove-each char nonce [find "+/=" char]
copy/part nonce 32
]
get-unix-timestamp: function [
"Read UNIX timestamp from Internet"
] [
date: none
page: read http://www.unixtimestamp.com/
parse page [
thru "The Current Unix Timestamp"
thru <h3 class="text-danger">
copy date to <small>
]
to integer! date
]
url-encode: function [
text [any-string!]
] [
value: none
chars: charset ["!'*,-.~_" #"0" - #"9" #"A" - #"Z" #"a" - #"z"]
rejoin head insert parse text [
collect [
some [
keep some chars
| space keep #"+"
| set value skip keep (head insert enbase/base form value 16 %"%")
]
]
] ""
]