-
Notifications
You must be signed in to change notification settings - Fork 16
/
response.odin
424 lines (341 loc) · 11.5 KB
/
response.odin
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package http
import "core:bytes"
import "core:io"
import "core:log"
import "core:net"
import "core:slice"
import "core:strconv"
import "nbio"
Response :: struct {
// Add your headers and cookies here directly.
headers: Headers,
cookies: [dynamic]Cookie,
// If the response has been sent.
sent: bool,
// NOTE: use `http.response_status` if the response body might have been set already.
status: Status,
// Only for internal usage.
_conn: ^Connection,
// TODO/PERF: with some internal refactoring, we should be able to write directly to the
// connection (maybe a small buffer in this struct).
_buf: bytes.Buffer,
_heading_written: bool,
}
response_init :: proc(r: ^Response, allocator := context.allocator) {
r.status = .Not_Found
r.cookies.allocator = allocator
r._buf.buf.allocator = allocator
headers_init(&r.headers, allocator)
}
/*
Prefer the procedure group `body_set`.
*/
body_set_bytes :: proc(r: ^Response, byts: []byte, loc := #caller_location) {
assert(bytes.buffer_length(&r._buf) == 0, "the response body has already been written", loc)
_response_write_heading(r, len(byts))
bytes.buffer_write(&r._buf, byts)
}
/*
Prefer the procedure group `body_set`.
*/
body_set_str :: proc(r: ^Response, str: string, loc := #caller_location) {
// This is safe because we don't write to the bytes.
body_set_bytes(r, transmute([]byte)str, loc)
}
/*
Sets the response body. After calling this you can no longer add headers to the response.
If, after calling, you want to change the status code, use the `response_status` procedure.
For bodies where you do not know the size or want an `io.Writer`, use the `response_writer_init`
procedure to create a writer.
*/
body_set :: proc{
body_set_str,
body_set_bytes,
}
/*
Sets the status code with the safety of being able to do this after writing (part of) the body.
*/
response_status :: proc(r: ^Response, status: Status) {
if r.status == status do return
r.status = status
// If we have already written the heading, we can address the bytes directly to overwrite,
// this is because of the fact that every status code is of length 3, and because we omit
// the "optional" reason phrase out of the response.
if bytes.buffer_length(&r._buf) > 0 {
OFFSET :: len("HTTP/1.1 ")
status_int_str := status_string(r.status)
if len(status_int_str) < 4 {
status_int_str = "500 "
} else {
status_int_str = status_int_str[0:4]
}
copy(r._buf.buf[OFFSET:OFFSET + 4], status_int_str)
}
}
Response_Writer :: struct {
r: ^Response,
// The writer you can write to.
w: io.Writer,
// A dynamic wrapper over the `buffer` given in `response_writer_init`, doesn't allocate.
buf: [dynamic]byte,
// If destroy or close has been called.
ended: bool,
}
/*
Initialize a writer you can use to write responses. Use the `body_set` procedure group if you have
a string or byte slice.
The buffer can be used to avoid very small writes, like the ones when you use the json package
(each write in the json package is only a few bytes). You are allowed to pass nil which will disable
buffering.
NOTE: You need to call io.destroy to signal the end of the body, OR io.close to send the response.
*/
response_writer_init :: proc(rw: ^Response_Writer, r: ^Response, buffer: []byte) -> io.Writer {
headers_set_unsafe(&r.headers, "transfer-encoding", "chunked")
_response_write_heading(r, -1)
rw.buf = slice.into_dynamic(buffer)
rw.r = r
rw.w = io.Stream{
procedure = proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
ws :: bytes.buffer_write_string
write_chunk :: proc(b: ^bytes.Buffer, chunk: []byte) {
plen := i64(len(chunk))
if plen == 0 do return
log.debugf("response_writer chunk of size: %i", plen)
bytes.buffer_grow(b, 16)
size_buf := _dynamic_unwritten(b.buf)
size := strconv.append_int(size_buf, plen, 16)
_dynamic_add_len(&b.buf, len(size))
ws(b, "\r\n")
bytes.buffer_write(b, chunk)
ws(b, "\r\n")
}
rw := (^Response_Writer)(stream_data)
b := &rw.r._buf
#partial switch mode {
case .Flush:
assert(!rw.ended)
write_chunk(b, rw.buf[:])
clear(&rw.buf)
return 0, nil
case .Destroy:
assert(!rw.ended)
// Write what is left.
write_chunk(b, rw.buf[:])
// Signals the end of the body.
ws(b, "0\r\n\r\n")
rw.ended = true
return 0, nil
case .Close:
// Write what is left.
write_chunk(b, rw.buf[:])
if !rw.ended {
// Signals the end of the body.
ws(b, "0\r\n\r\n")
rw.ended = true
}
// Send the response.
respond(rw.r)
return 0, nil
case .Write:
assert(!rw.ended)
// No space, first write rw.buf, then check again for space, if still no space,
// fully write the given p.
if len(rw.buf) + len(p) > cap(rw.buf) {
write_chunk(b, rw.buf[:])
clear(&rw.buf)
if len(p) > cap(rw.buf) {
write_chunk(b, p)
} else {
append(&rw.buf, ..p)
}
} else {
// Space, append bytes to the buffer.
append(&rw.buf, ..p)
}
return i64(len(p)), .None
case .Query:
return io.query_utility({.Write, .Flush, .Destroy, .Close})
}
return 0, .Empty
},
data = rw,
}
return rw.w
}
/*
Writes the response status and headers to the buffer.
This is automatically called before writing anything to the Response.body or before calling a procedure
that sends the response.
You can pass `content_length < 0` to omit the content-length header, note that this header is
required on most responses, but there are things like transfer-encodings that could leave it out.
*/
_response_write_heading :: proc(r: ^Response, content_length: int) {
if r._heading_written do return
r._heading_written = true
ws :: bytes.buffer_write_string
conn := r._conn
b := &r._buf
MIN :: len("HTTP/1.1 200 \r\ndate: \r\ncontent-length: 1000\r\n") + DATE_LENGTH
AVG_HEADER_SIZE :: 20
reserve_size := MIN + content_length + (AVG_HEADER_SIZE * headers_count(r.headers))
bytes.buffer_grow(&r._buf, reserve_size)
// According to RFC 7230 3.1.2 the reason phrase is insignificant,
// because not doing so (and the fact that a status code is always length 3), we can change
// the status code when we are already writing a body by just addressing the 3 bytes directly.
status_int_str := status_string(r.status)
if len(status_int_str) < 4 {
status_int_str = "500 "
} else {
status_int_str = status_int_str[0:4]
}
ws(b, "HTTP/1.1 ")
ws(b, status_int_str)
ws(b, "\r\n")
// Per RFC 9910 6.6.1 a Date header must be added in 2xx, 3xx, 4xx responses.
if r.status >= .OK && r.status <= .Internal_Server_Error && !headers_has_unsafe(r.headers, "date") {
ws(b, "date: ")
ws(b, server_date(conn.server))
ws(b, "\r\n")
}
if (
content_length > -1 &&
!headers_has_unsafe(r.headers, "content-length") &&
response_needs_content_length(r, conn) \
) {
if content_length == 0 {
ws(b, "content-length: 0\r\n")
} else {
ws(b, "content-length: ")
assert(content_length < 1000000000000000000 && content_length > -1000000000000000000)
buf: [20]byte
ws(b, strconv.itoa(buf[:], content_length))
ws(b, "\r\n")
}
}
bstream := bytes.buffer_to_stream(b)
for header, value in r.headers._kv {
ws(b, header) // already has newlines escaped.
ws(b, ": ")
write_escaped_newlines(bstream, value)
ws(b, "\r\n")
}
for cookie in r.cookies {
cookie_write(bstream, cookie)
ws(b, "\r\n")
}
// Empty line denotes end of headers and start of body.
ws(b, "\r\n")
}
// Sends the response over the connection.
// Frees the allocator (should be a request scoped allocator).
// Closes the connection or starts the handling of the next request.
@(private)
response_send :: proc(r: ^Response, conn: ^Connection, loc := #caller_location) {
assert(!r.sent, "response has already been sent", loc)
r.sent = true
check_body :: proc(res: rawptr, body: Body, err: Body_Error) {
res := cast(^Response)res
will_close: bool
if err != nil {
// Any read error should close the connection.
response_status(res, body_error_status(err))
headers_set_close(&res.headers)
will_close = true
}
response_send_got_body(res, will_close)
}
// RFC 7230 6.3: A server MUST read
// the entire request message body or close the connection after sending
// its response, since otherwise the remaining data on a persistent
// connection would be misinterpreted as the next request.
if !response_must_close(&conn.loop.req, r) {
// Body has been drained during handling.
if _, got_body := conn.loop.req._body_ok.?; got_body {
response_send_got_body(r, false)
} else {
body(&conn.loop.req, Max_Post_Handler_Discard_Bytes, r, check_body)
}
} else {
response_send_got_body(r, true)
}
}
@(private)
response_send_got_body :: proc(r: ^Response, will_close: bool) {
conn := r._conn
if will_close {
if !connection_set_state(r._conn, .Will_Close) do return
}
if bytes.buffer_length(&r._buf) == 0 {
_response_write_heading(r, 0)
}
buf := bytes.buffer_to_bytes(&r._buf)
nbio.send_all(&td.io, conn.socket, buf, conn, on_response_sent)
}
@(private)
on_response_sent :: proc(conn_: rawptr, sent: int, err: net.Network_Error) {
conn := cast(^Connection)conn_
if err != nil {
log.errorf("could not send response: %v", err)
if !connection_set_state(conn, .Will_Close) do return
}
clean_request_loop(conn)
}
// Response has been sent, clean up and close/handle next.
@(private)
clean_request_loop :: proc(conn: ^Connection, close: Maybe(bool) = nil) {
// blocks, size, used := allocator_free_all(&conn.temp_allocator)
// log.debugf("temp_allocator had %d blocks of a total size of %m of which %m was used", blocks, size, used)
free_all(context.temp_allocator)
scanner_reset(&conn.scanner)
conn.loop.req = {}
conn.loop.res = {}
if c, ok := close.?; (ok && c) || conn.state == .Will_Close {
connection_close(conn)
} else {
if !connection_set_state(conn, .Idle) do return
conn_handle_req(conn, context.temp_allocator)
}
}
// A server MUST NOT send a Content-Length header field in any response
// with a status code of 1xx (Informational) or 204 (No Content). A
// server MUST NOT send a Content-Length header field in any 2xx
// (Successful) response to a CONNECT request.
@(private)
response_needs_content_length :: proc(r: ^Response, conn: ^Connection) -> bool {
if status_is_informational(r.status) || r.status == .No_Content {
return false
}
line := conn.loop.req.line.?
if status_is_success(r.status) && line.method == .Connect {
return false
}
return true
}
// Determines if the connection needs to be closed after sending the response.
@(private)
response_must_close :: proc(req: ^Request, res: ^Response) -> bool {
// If the request we are responding to indicates it is closing the connection, close our side too.
if req, req_has := headers_get_unsafe(req.headers, "connection"); req_has && req == "close" {
return true
}
// If we are responding with a close connection header, make sure we close.
if res, res_has := headers_get_unsafe(res.headers, "connection"); res_has && res == "close" {
return true
}
// If the body was tried to be received, but failed, close.
if body_ok, got_body := req._body_ok.?; got_body && !body_ok {
headers_set_close(&res.headers)
return true
}
// If the connection's state indicates closing, close.
if res._conn.state >= .Will_Close {
headers_set_close(&res.headers)
return true
}
// HTTP 1.0 does not have persistent connections.
line := req.line.?
if line.version == {1, 0} {
return true
}
return false
}