summaryrefslogtreecommitdiff
path: root/lib/http/client.myr
blob: 21ce46928d8bd56969bf223c757a5c8b7e1a1293 (plain)
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
use std
use bio

use "types"
use "session"
use "parse"

pkg http =
	/* simple versions */
	const get	: (s : session#, r : url# -> std.result(resp#, err))
	const head	: (s : session#, r : url# -> std.result(resp#, err))
	const put	: (s : session#, r : url#, data : byte[:] -> std.result(resp#, err))
	const post	: (s : session#, r : url#, data : byte[:] -> std.result(resp#, err))
	const delete	: (s : session#, r : url# -> std.result(resp#, err))
	const options	: (s : session#, r : url# -> std.result(resp#, err))
	const trace	: (s : session#, r : url# -> std.result(resp#, err))

	/* request based versions */
	const getreq	: (s : session#, r : req# -> std.result(resp#, err))
	const headreq	: (s : session#, r : req# -> std.result(resp#, err))
	const putreq	: (s : session#, r : req#, data : byte[:] -> std.result(resp#, err))
	const postreq	: (s : session#, r : req#, data : byte[:] -> std.result(resp#, err))
	const deletereq	: (s : session#, r : req# -> std.result(resp#, err))
	const optionsreq	: (s : session#, r : req# -> std.result(resp#, err))
	const tracereq	: (s : session#, r : req# -> std.result(resp#, err))

	const freeresp	: (r : resp# -> void)
;;

const get = {s, path; -> getreq(s, &[.url=path])}
const head = {s, path; -> headreq(s, &[.url=path])}
const put = {s, path, data; -> putreq(s, &[.url=path], data)}
const post = {s, path, data; -> postreq(s, &[.url=path], data)}
const delete = {s, path; -> deletereq(s, &[.url=path])}
const options = {s, path; -> optionsreq(s, &[.url=path])}
const trace = {s, path; -> tracereq(s, &[.url=path])}


const getreq = {s, r
	match request(s, `Get, r, `std.None)
	| `std.Ok _:	/* nothing */
	| `std.Err e:	-> `std.Err e
	;;

	-> response(s, true)
}

const headreq = {s, r
	match request(s, `Head, r, `std.None)
	| `std.Ok _:	/* nothing */
	| `std.Err e:	-> `std.Err e
	;;

	-> response(s, false)
}

const putreq = {s, r, data
	match request(s, `Put, r, `std.Some data)
	| `std.Ok _:	/* nothing */
	| `std.Err e:	-> `std.Err e
	;;

	-> response(s, true)
}

const postreq = {s, r, data
	match request(s, `Post, r, `std.Some data)
	| `std.Ok _:	/* nothing */
	| `std.Err e:	-> `std.Err e
	;;

	-> response(s, true)
}

const deletereq = {s, r
	match request(s, `Delete, r, `std.None)
	| `std.Ok _:	/* nothing */
	| `std.Err e:	-> `std.Err e
	;;

	-> response(s, true)
}

const optionsreq = {s, r
	match request(s, `Options, r, `std.None)
	| `std.Ok _:	/* nothing */
	| `std.Err e:	-> `std.Err e
	;;

	-> response(s, true)
}

const tracereq = {s, r
	match request(s, `Trace, r, `std.None)
	| `std.Ok _:	/* nothing */
	| `std.Err e:	-> `std.Err e
	;;

	-> response(s, true)
}

const response = {s, body
	var resp

	resp = std.mk([
		.hdrs = [][:],
		.len = 0,
		.err = `std.None,
		.reason = "",
		.status = 0,
		.enc = `Length,
	])

	if parseresp(s, resp)
		if !body
			-> `std.Ok resp
		else
			match readbody(s, resp)
			| `std.Ok buf:	resp.body = buf
			| `std.Err e:	-> `std.Err e
			;;
		;;
	else
		match resp.err
		| `std.Some e:	-> `std.Err e
		| `std.None:	-> `std.Err `Ewat
		;;
	;;
	-> `std.Ok resp
}

const request = {s, method, r, data
	/* status */
	ioput(s, "{} {p} HTTP/1.1\r\n", method, r.url)

	/* headers */
	ioput(s, "Host: {}\r\n", s.host)
	ioput(s, "User-Agent: {}\r\n", s.ua)
	match data
	| `std.Some d:	ioput(s, "Content-Length: {}\r\n", d.len)
	| `std.None:	/* nothing to do */
	;;
	for (k, v) : r.hdrs
		ioput(s, "{}: {}\r\n", k, v)
	;;
	ioput(s, "\r\n")

	/* body */
	match data
	| `std.None:	/* nothing to do */
	| `std.Some d:
		iowrite(s, d)
		ioput(s, "\r\n")
	;;
	ioflush(s)

	if s.err
		-> `std.Err `Econn
	else
		-> `std.Ok void
	;;

}

const readbody = {s, r -> std.result(byte[:], err)
	match r.enc
	| `Length:	-> readlenbody(s, r)
	| `Chunked:	-> readchunkedbody(s, r)
	| badenc:	std.fatal("unsupported encoding {}\n", badenc)
	;;
}

const readlenbody = {s, r
	var buf

	buf = ""
	if r.len == 0
		-> `std.Ok buf
	;;

	buf = std.slalloc(r.len)
	match bio.read(s.f, buf)
	| `std.Err e:	goto shortread
	| `std.Ok rd:
		if rd.len != r.len
			goto shortread
		;;
	;;
	-> `std.Ok buf
:shortread
	std.slfree(buf)
	-> `std.Err `Eshort
}

const __init__ = {
	var m

	m = `Get
	std.fmtinstall(std.typeof(m), fmtmethod)
}

const readchunkedbody = {s, r
	var buf, len

	buf = ""
	len = 0
	while true
		match parsechunksz(s)
		| `std.Err e:
			std.slfree(buf)
			-> `std.Err e
		| `std.Ok 0:
			break
		| `std.Ok sz:
			std.slgrow(&buf, buf.len + sz)
			match bio.read(s.f, buf[len:len + sz])
			| `std.Err e:
				std.slfree(buf)
				-> `std.Err `Econn
			| `std.Ok str:
				if str.len != sz
					std.slfree(buf)
					-> `std.Err `Eshort
				;;
				len += sz
				match checkendln(s)
				| `std.Ok _:	/* nothing */
				| `std.Err e:	
					std.slfree(buf)
					-> `std.Err e
				;;
			;;
		;;
	;;
	-> `std.Ok buf
}

const checkendln = {s
	var r

	match bio.readln(s.f)
	| `std.Err e:	r = `std.Err `Econn
	| `std.Ok crlf:
		if std.strstrip(crlf).len == 0
			r = `std.Ok void
		else
			r = `std.Err `Eproto
		;;
		std.slfree(crlf)
	;;
	-> r
}

const fmtmethod = {sb, ap, opt
	var r

	r = std.vanext(ap)
	match r
	| `Get: std.sbputs(sb, "GET")
	| `Head: std.sbputs(sb, "HEAD")
	| `Put: std.sbputs(sb, "PUT")
	| `Post: std.sbputs(sb, "POST")
	| `Delete: std.sbputs(sb, "DELETE")
	| `Trace: std.sbputs(sb, "TRACE")
	| `Options: std.sbputs(sb, "OPTIONS")
	;;
}

const freeresp = {r
	for (k, v) : r.hdrs
		std.slfree(k)
		std.slfree(v)
	;;
	std.slfree(r.reason)
	std.free(r)
}