summaryrefslogtreecommitdiff
path: root/lib/http/parse.myr
blob: 180b5817b4e082afcc6fd7e29461c96cd743942e (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use std
use bio

use "types"

pkg http =
	pkglocal const parsereq		: (s : session# -> std.result(req#, err))
	pkglocal const parseresp	: (s : session#, r : resp# -> bool)
	pkglocal const parsechunksz	: (s : session# -> std.result(std.size, err))
	pkglocal const parsenumber	: (str : byte[:]#, base : int -> std.option(int))
;;

const strcaseeq = {a : byte[:], b : byte[:] -> bool
	var ca, cb

	while a.len == 0 || b.len == 0
		(ca, a) = std.strstep(a)
		(cb, b) = std.strstep(b)
		if std.tolower(ca) != std.tolower(cb)
			-> false
		;;
	;;
	-> a.len == b.len
}

const parsereq = {s
	var r, err

	r.hdrs = [][:]
	r.url = std.mk([
		.schema=`Http,
		.host=std.sldup(s.host),
		.port=s.port,
		.path="",
		.params=[][:],
	])

	match bio.readln(s.f)
	| `std.Err e:
		err = `Econn
		goto error
	| `std.Ok ln:
		match parsereqstatus(s, r, ln)
		| `std.Ok void:
		| `std.Err e:
			std.slfree(ln)
			err = e
			-> `std.Err e
		;;
		std.slfree(ln)
	;;

	while true
		match bio.readln(s.f)
		| `std.Err e:	
			err = `Econn
			goto error
		| `std.Ok ln:
			if std.strstrip(ln).len == 0
				std.slfree(ln)
				break
			;;
			match parsehdr(s, ln)
			| `std.Ok kvp:
				std.slpush(&r.hdrs, kvp)
			| `std.Err e:
				std.slfree(ln)
				-> `std.Err e
			;;
			std.slfree(ln)
		;;
	;;
	-> `std.Ok std.mk(r)
:error
	-> `std.Err err
}

const parseresp = {s, r : resp#
	match bio.readln(s.f)
	| `std.Err _:	r.err = `std.Some `Econn
	| `std.Ok ln:
		if !parserespstatus(s, r, ln)
			std.slfree(ln)
			-> false
		;;
		std.slfree(ln)
	;;

	while true
		match bio.readln(s.f)
		| `std.Err e:	r.err = `std.Some `Econn
		| `std.Ok ln:
			if std.strstrip(ln).len == 0
				std.slfree(ln)
				break
			;;
			match parsehdr(s, ln)
			| `std.Ok kvp:
				std.slpush(&r.hdrs, kvp)
			| `std.Err e:
				r.err = `std.Some e
				std.slfree(ln)
				-> false
			;;
			std.slfree(ln)
		;;
	;;

	match getenc(r)
	| `std.Ok `Length:
		r.enc = `Length
		match getlen(r)
		| `std.Some n:
			r.len = n
		| `std.None:
			r.err = `std.Some `Eproto
			-> false
		;;
	| `std.Ok enc:
		r.enc = enc
	| `std.Err e:
		r.err = `std.Some e
		-> false
	;;
	-> true

}

const parsereqstatus = {s, r, ln
	match parseword(&ln)
	| `std.Some "GET":	r.method = `Get
	| `std.Some "HEAD":	r.method = `Head
	| `std.Some "POST":	r.method = `Post
	| `std.Some "DELETE":	r.method = `Delete
	| `std.Some "TRACE":	r.method = `Trace
	| `std.Some "OPTIONS":	r.method = `Options
	| `std.Some _:	-> `std.Err `Eproto
	| `std.None:	-> `std.Err `Eproto
	;;

	match parseword(&ln)
	| `std.Some w:	r.url.path = std.sldup(w)
	| `std.None:	-> `std.Err `Eproto
	;;

	match parseword(&ln)
	| `std.Some "HTTP/1.1":	/* ok */
	| `std.Some w:	std.put("warn: http version '{}'\n", w)
	| `std.None:	-> `std.Err `Eproto
	;;

	ln = std.strfstrip(ln)
	-> `std.Ok void
}

const parserespstatus = {s, r, ln
	/* HTTP/1.1 */
	ln = std.strfstrip(ln)
	if !std.chomp(&ln, "HTTP")
		r.err = `std.Some `Eproto
		-> false
	;;
	if !std.chomp(&ln, "/1.1")
		r.err = `std.Some `Eproto
		-> false
	;;

	ln = std.strfstrip(ln)
	match parsenumber(&ln, 10)
	| `std.Some n:
		r.status = n
	| `std.None:
		r.err = `std.Some `Eproto
		-> false
	;;

	ln = std.strfstrip(ln)
	r.reason = std.sldup(ln)
	-> true
}

const parsehdr = {s, ln
	var key, val

	match std.strfind(ln, ":")
	| `std.Some idx:
		key = std.sldup(std.strstrip(ln[:idx]))
		val = std.sldup(std.strstrip(ln[idx+1:]))
		-> `std.Ok (key, val)
	| `std.None:
		-> `std.Err `Ehdr
	;;
}

const getlen = {r
	match findhdr(r, "Content-Length")
	| `std.Some v:
		match std.intparsebase(v, 10)
		| `std.Some n:	-> `std.Some n
		| `std.None:	-> `std.None
		;;
	| `std.None:
		-> `std.None
	;;
}

const parsechunksz = {s
	var ret, str

	match bio.readln(s.f)
	| `std.Err e:	ret = `std.Err `Econn
	| `std.Ok ln:
		str = ln
		match parsenumber(&str, 16)
		| `std.Some n:	ret = `std.Ok (n : std.size)
		| `std.None:
			ret = `std.Err `Eproto
		;;
		std.slfree(ln)
	;;
	-> ret
}

const getenc = {r
	match findhdr(r, "Transfer-Encoding")
	| `std.None:	-> `std.Ok `Length
	| `std.Some "chunked":	-> `std.Ok `Chunked
	| `std.Some "compress":	-> `std.Ok `Compress
	| `std.Some "deflate":	-> `std.Ok `Deflate
	| `std.Some "gzip":	-> `std.Ok `Gzip
	| `std.Some unknown:	-> `std.Err `Eenc
	;;
}

const findhdr = {r, name
	for (k, v) : r.hdrs
		if strcaseeq(k, name)
			-> `std.Some v
		;;
	;;
	-> `std.None
}

const parseword = {ln
	var w, end

	ln# = std.strfstrip(ln#)
	end = 0
	for var i = 0; i < ln#.len; i++
		if i == ln#.len - 1
			end = i + 1
		elif std.isspace(std.decode(ln#[i:]))
			end = i
			break
		;;
	;;
	if end == 0
		-> `std.None
	else
		w = ln#[:end]
		ln# = ln#[end:]
		-> `std.Some w
	;;
}

const parsenumber = {ln, base
	var n, ok
	var c, s, dig

	n = 0
	s = ln#
	ok = false
	while true
		(c, s) = std.charstep(s)
		dig = std.charval(c, base)
		if dig >= 0 && dig < base
			ok = true
			n *= base
			n += dig
		else
			break
		;;
	;;
	ln# = s
	if ok
		-> `std.Some n
	else
		-> `std.None
	;;
}