main.bend
emerging-ezhttp@0.21.0.0 · 417 lines · 65 definitions
1
2
3
4
5import Base
6import bend-kit-bytes@0.3.2.0/bytes.bend as Bytes
7import emerging-ezhttp@0.21.0.0/src/url.bend as Url
8import emerging-ezhttp@0.21.0.0/src/pct.bend as Pct
9import emerging-ezhttp@0.21.0.0/src/http.bend as Http
10import emerging-ezhttp@0.21.0.0/src/body.bend as Body
11import emerging-ezhttp@0.21.0.0/src/form.bend as Form
12import emerging-ezhttp@0.21.0.0/src/client.bend as Client
13import emerging-ezhttp@0.21.0.0/src/server.bend as Server
14import emerging-ezhttp@0.21.0.0/src/cookie.bend as Cookie
15import emerging-ezhttp@0.21.0.0/src/cache.bend as Cache
16import emerging-ezhttp@0.21.0.0/src/cors.bend as Cors
17import emerging-ezhttp@0.21.0.0/src/html.bend as Html
18import emerging-ezhttp@0.21.0.0/src/qs.bend as Qs
19import emerging-ezhttp@0.21.0.0/src/unescape.bend as Unescape
20import emerging-ezhttp@0.21.0.0/src/echo.bend as Echo
21
22
23def parse_url(url: String) -> Url.Loc:
24 Url.parse(url)
25
26
27
28
29def quote_path(text: String) -> String:
30 Pct.quote(True{}, text)
31
32
33
34def quote_component(text: String) -> String:
35 Pct.quote(False{}, text)
36
37
38
39
40
41def unquote(text: String) -> String:
42 Qs.unquote(text)
43
44
45def unquote_plus(text: String) -> String:
46 Qs.unquote.plus(text)
47
48
49
50def quote_plus(text: String) -> String:
51 Qs.quote.plus(text)
52
53
54def Kv() -> Data:
55 Qs.Kv()
56
57
58
59def urlencode(pairs: List<&2, Qs.Kv()>) -> String:
60 Qs.urlencode(pairs)
61
62
63
64
65
66
67def parse_query(text: String) -> List<&2, Qs.Kv()>:
68 Qs.parse(text)
69
70
71
72def query_get(pairs: List<&2, Qs.Kv()>, name: String) -> Maybe<&2, String>:
73 Qs.get(pairs, name)
74
75
76
77def parse_response(raw: String) -> Http.Reply:
78 Http.parse(raw)
79
80
81
82
83def parse_response_to(method: String, raw: String) -> Http.Reply:
84 Http.parse.to(method, raw)
85
86
87
88
89def reason_phrase(raw: String) -> String:
90 Http.phrase.to(raw)
91
92
93def parse_request(raw: String) -> Http.Request:
94 Http.ask(raw)
95
96
97def format_response(status: U32, headers: List<&2, Http.Header>, body: String)
98 -> String:
99 Http.respond(status, headers, body)
100
101
102def empty_body() -> Body.Body:
103 Body.body.empty()
104
105
106def text_body(text: String) -> Body.Body:
107 Body.body.text(text)
108
109
110def octets_body(bytes: Bytes.Bytes) -> Body.Body:
111 Body.body.octets(bytes)
112
113
114def octets_list_body(bs: List<&2, U32>) -> Body.Body:
115 Body.body.list(bs)
116
117
118def form_field(name: String, value: String) -> Form.Upload:
119 Form.form.field(name, value)
120
121
122
123def form_file(name: String, filename: String, ctype: String,
124 bytes: Bytes.Bytes) -> Form.Upload:
125 Form.form.file(name, filename, ctype, bytes)
126
127
128
129
130def form_body(boundary: String, parts: List<&1, Form.Upload>) -> Form.Form:
131 Form.form.body(boundary, parts)
132
133
134def encode(~A: Data, ~to_text: A -> String, value: A) -> Body.Body:
135 Body.body.encode(A, to_text, value)
136
137
138def decode(
139 ~A: Data,
140 ~from_text: String -> Maybe<&2, A>,
141 text: String
142) -> Maybe<&2, A>:
143 Body.body.decode(A, from_text, text)
144
145
146def bearer(token: String) -> Http.Header:
147 Client.client.bearer(token)
148
149
150def basic(user: String, password: String) -> Http.Header:
151 Client.client.basic(user, password)
152
153
154def set_cookie(cookie: Cookie.Cookie) -> Http.Header:
155 Cookie.cookie.set(cookie)
156
157
158def parse_cookie(line: String) -> Maybe<&2, Cookie.Cookie>:
159 Cookie.cookie.parse(line)
160
161
162def cookie_header(cs: List<&2, Cookie.Cookie>) -> Http.Header:
163 Cookie.cookie.request(cs)
164
165
166def cache_control(value: String) -> Cache.Cc:
167 Cache.cache.parse(value)
168
169
170def fresh(value: String, age: Nat) -> Bool:
171 Cache.cache.fresh(Cache.cache.parse(value), age)
172
173
174def cors_reply(
175 cfg: Cors.Cfg,
176 req: Http.Request,
177 reply: Http.Reply
178) -> Http.Reply:
179 Cors.cors.on(cfg, req, reply)
180
181
182
183def html_escape(text: String) -> String:
184 Html.escape(text)
185
186
187
188
189
190def html_unescape(text: String) -> String:
191 Unescape.unescape(text)
192
193
194
195
196
197def cookie_from_set_cookies(values: List<&2, String>) -> String:
198 Echo.echo(values)
199
200
201def cookie_from_headers(headers: List<&2, Http.Header>) -> String:
202 Echo.echo(Echo.values(headers))
203
204
205def http.get(url: String) -> IO(Client.Response):
206 Client.client.get(url, [])
207
208
209def http.get_with(url: String, headers: List<&2, Http.Header>)
210 -> IO(Client.Response):
211 Client.client.get(url, headers)
212
213
214
215
216def http.get_opts(
217 url: String,
218 headers: List<&2, Http.Header>,
219 timeout_ms: U32,
220 follow: Bool
221) -> IO(Client.Response):
222 Client.client.get.opts(url, headers, timeout_ms, follow)
223
224
225
226
227
228def http.request_opts(
229 method: String,
230 url: String,
231 headers: List<&2, Http.Header>,
232 body: Body.Body,
233 timeout_ms: U32,
234 follow: Bool
235) -> IO(Client.Response):
236 Client.client.request.opts(method, url, headers, body, timeout_ms, follow)
237
238
239
240def http.post_opts(
241 url: String,
242 headers: List<&2, Http.Header>,
243 body: Body.Body,
244 timeout_ms: U32
245) -> IO(Client.Response):
246 http.request_opts("POST", url, headers, body, timeout_ms, False{})
247
248
249
250def http.put_opts(
251 url: String,
252 headers: List<&2, Http.Header>,
253 body: Body.Body,
254 timeout_ms: U32
255) -> IO(Client.Response):
256 http.request_opts("PUT", url, headers, body, timeout_ms, False{})
257
258
259
260def http.delete_opts(
261 url: String,
262 headers: List<&2, Http.Header>,
263 body: Body.Body,
264 timeout_ms: U32
265) -> IO(Client.Response):
266 http.request_opts("DELETE", url, headers, body, timeout_ms, False{})
267
268
269def http.timed_out(r: Client.Response) -> Bool:
270 Client.client.timed_out(r)
271
272
273
274def body_text(bytes: Bytes.Bytes) -> String:
275 Client.client.text(bytes)
276
277
278def http.head(url: String) -> IO(Client.Response):
279 Client.client.head(url, [])
280
281
282def http.head_with(url: String, headers: List<&2, Http.Header>)
283 -> IO(Client.Response):
284 Client.client.head(url, headers)
285
286
287def http.post(url: String, body: Body.Body) -> IO(Client.Response):
288 Client.client.post(url, [], body)
289
290
291def http.post_with(
292 url: String,
293 headers: List<&2, Http.Header>,
294 body: Body.Body
295) -> IO(Client.Response):
296 Client.client.post(url, headers, body)
297
298
299def http.put(url: String, body: Body.Body) -> IO(Client.Response):
300 Client.client.put(url, [], body)
301
302
303def http.put_with(url: String, headers: List<&2, Http.Header>, body: Body.Body)
304 -> IO(Client.Response):
305 Client.client.put(url, headers, body)
306
307
308def http.delete(url: String, body: Body.Body) -> IO(Client.Response):
309 Client.client.delete(url, [], body)
310
311
312def http.delete_with(
313 url: String,
314 headers: List<&2, Http.Header>,
315 body: Body.Body
316) -> IO(Client.Response):
317 Client.client.delete(url, headers, body)
318
319
320def http.options(url: String) -> IO(Client.Response):
321 Client.client.options(url, [])
322
323
324def http.options_with(url: String, headers: List<&2, Http.Header>)
325 -> IO(Client.Response):
326 Client.client.options(url, headers)
327
328
329def http.serve(
330 ~handle: Http.Request -> Http.Reply,
331 host: String,
332 port: U32,
333 limit: U32
334) -> IO(Unit):
335 Server.server.serve(handle, host, port, limit)
336
337
338
339def http.serve_io(
340 ~handle: Http.Request -> IO(Http.Reply),
341 host: String,
342 port: U32,
343 limit: U32
344) -> IO(Unit):
345 Server.server.serve_io(handle, host, port, limit)
346
347
348@unsafe def http.serve_forever(
349 ~handle: Http.Request -> IO(Http.Reply),
350 host: String,
351 port: U32
352) -> IO(Unit):
353 Server.server.serve_forever(handle, host, port)
354
355
356
357
358
359def http.serve_with(
360 ~C: Data,
361 ~handle: C -> Server.Req -> Server.Answer,
362 ctx: C,
363 host: String,
364 port: U32,
365 limit: U32,
366 max: U32
367) -> IO(Unit):
368 Server.server.serve_with(~C, handle, ctx, host, port, limit, max)
369
370
371def http.serve_io_with(
372 ~C: Data,
373 ~handle: C -> Server.Req -> IO(Server.Answer),
374 ctx: C,
375 host: String,
376 port: U32,
377 limit: U32,
378 max: U32
379) -> IO(Unit):
380 Server.server.serve_io_with(~C, handle, ctx, host, port, limit, max)
381
382
383@unsafe def http.serve_forever_with(
384 ~C: Data,
385 ~handle: C -> Server.Req -> IO(Server.Answer),
386 ctx: C,
387 host: String,
388 port: U32,
389 max: U32
390) -> IO(Unit):
391 Server.server.serve_forever_with(~C, handle, ctx, host, port, max)
392
393
394def text_reply(
395 status: U32, headers: List<&2, Http.Header>, body: String) -> Server.Answer:
396 Server.TextReply{status, headers, body}
397
398
399def octets_reply(
400 status: U32, headers: List<&2, Http.Header>, bytes: Bytes.Bytes)
401 -> Server.Answer:
402 Server.OctetsReply{status, headers, bytes}
403
404
405
406def stream_reply(
407 status: U32, headers: List<&2, Http.Header>, chunks: Chan(Bytes.Bytes))
408 -> Server.Answer:
409 Server.StreamReply{status, headers, chunks}
410
411
412def http.serve_once(
413 ~handle: Http.Request -> Http.Reply,
414 host: String,
415 port: U32
416) -> IO(Unit):
417 Server.server.once(handle, host, port)