-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.odin
More file actions
371 lines (286 loc) · 7.55 KB
/
http.odin
File metadata and controls
371 lines (286 loc) · 7.55 KB
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
package websocket
import "core:bufio"
import "core:bytes"
import "core:fmt"
import "core:io"
import "core:log"
import "core:net"
import "core:strconv"
import "core:strings"
HTTPS :: "https"
HTTP :: "http"
Headers :: map[string]string
Method :: enum {
Get,
Post,
}
Status_Code :: enum (int) {
Continue = 100,
Switching_Protocols = 101,
Success = 200,
Bad_Request = 400,
Unauthorized = 401,
Forbidden = 403,
Not_Found = 404,
}
URL :: struct {
protocol: string,
host: string,
port: string,
path: string,
query: string,
}
Request :: struct {
method: Method,
headers: Headers,
url: URL,
}
Response :: struct {
status: Status_Code,
headers: Headers,
body: string, //TODO(devon): Determine if thias project actually needs to use the body
}
method_to_string :: proc(method: Method) -> string {
switch method {
case .Get:
{
return "GET"
}
case .Post:
{
return "POST"
}
}
return "N/A"
}
request_init :: proc(allocator := context.allocator) -> ^Request {
req := new(Request, allocator)
req.headers = make(Headers, allocator)
return req
}
request_destory :: proc(req: ^Request, allocator := context.allocator) {
delete(req.headers)
free(req, allocator)
}
request_method :: proc(req: ^Request, method: Method) {
req.method = method
}
request_url :: proc(req: ^Request, full_url: string, allocator := context.allocator) {
url, err := parse_url(full_url, allocator)
if err {
log.errorf("ERROR - Failed to parse URL")
}
req.url = url
}
request_add_header :: proc(req: ^Request, key: string, value: string, overwrite := false) {
if key in req.headers && !overwrite {
log.warnf(
"Attempted to add header(%v) of value(%v) to HTTP request, but the header already exists and overwrite flag not set..",
)
return
}
log.debug("Header(%v) of value(%v) added to HTTP request..")
req.headers[key] = value
}
response_destroy :: proc(resp: ^Response, allocator := context.allocator) {
delete(resp.headers)
free(resp, allocator)
}
/*
NOTE(devon) This is crap, replace in the future. Just a quick and dirty to get to the real problem at hand
__Does not handle malformed urls__! The caller of this function will either need to create their own parser,
or will need to be responsible validating the urls prior to passing it into the function
*/
parse_url :: proc(raw_url: string, allocator := context.allocator) -> (url: URL, err: bool) {
log.debug("Parsing url: %v\n", raw_url)
start_index: int = 7 // http://
if strings.starts_with(raw_url, "https://") {
url.protocol = HTTPS
start_index = 8
} else if strings.starts_with(raw_url, "http://") {
url.protocol = HTTP
} else {
// some.unsecure.com/ will be resolved to http
// localhost:3000 will be resolved to http
// 127.0.0.1:3000 will be resolved to http
url.protocol = HTTP
start_index = 0
}
reader: strings.Reader
strings.reader_init(&reader, raw_url)
strings.reader_seek(&reader, i64(start_index), .Start)
has_port := false
// Extract host
for {
r, _, r_err := strings.reader_read_rune(&reader)
if r_err == .EOF {
url.host = strings.cut(raw_url, start_index, int(reader.i) - start_index)
// log.errorf("ERROR - Invalid URL: %v\n", raw_url)
// err = true
return
}
// Start of the port
if r == ':' {
url.host = strings.cut(raw_url, start_index, int(reader.i) - 1 - start_index)
start_index = int(reader.i)
has_port = true
break
}
// No port
if r == '/' {
url.host = strings.cut(raw_url, start_index, int(reader.i) - 1 - start_index)
start_index = int(reader.i)
break
}
}
// Extract port, if it exists
if has_port {
for {
r, _, r_err := strings.reader_read_rune(&reader)
// Start of the path
if r == '/' {
url.port = strings.cut(raw_url, start_index, int(reader.i) - 1 - start_index)
start_index = int(reader.i)
break
}
if r_err == .EOF {
url.port = strings.cut(raw_url, start_index, int(reader.i) - start_index)
return
}
}
}
// Extract path
for {
r, _, r_err := strings.reader_read_rune(&reader)
// Start of the path
if r == '?' {
url.path = strings.cut(raw_url, start_index, int(reader.i) - 1 - start_index)
start_index = int(reader.i)
break
}
if r_err == .EOF {
url.path = strings.cut(raw_url, start_index, int(reader.i) - start_index)
return
}
}
// TODO(devon): Query support
return
}
send :: proc(socket: ^net.TCP_Socket, req: ^Request) -> (n: int, ok: bool) {
buffer: bytes.Buffer
defer bytes.buffer_destroy(&buffer)
// Request Start Line
method: string = method_to_string(req.method)
path: string = fmt.tprintf("%v /%v HTTP/1.1\r\n", method, req.url.path)
bytes.buffer_write_string(&buffer, path)
for k, v in req.headers {
bytes.buffer_write_string(&buffer, fmt.tprintf("%v: %v\r\n", k, v))
}
// New line to separate the headers and the body (even if the body is empty)
bytes.buffer_write_string(&buffer, "\r\n")
data := bytes.buffer_to_bytes(&buffer)
// Log Request
request_log(data)
// No SSL for now
bytes_read, send_err := net.send_tcp(socket^, data)
if send_err != nil {
log.errorf("FAILED - Failed to send packet: %v\n", send_err)
ok = false
return
} else {
log.info("Handshake sent, awaiting reponse..\n")
}
n = bytes_read
ok = true
return
}
recv :: proc(
socket: net.TCP_Socket,
allocator := context.allocator,
) -> (
resp: ^Response,
ok: bool,
) {
log.info("\n[RESPONSE]:\n")
stream: io.Stream
stream.data = rawptr(uintptr(socket))
stream.procedure = socket_stream
stream_reader := io.to_reader(stream)
scanner: bufio.Scanner
bufio.scanner_init(&scanner, stream_reader, allocator)
defer bufio.scanner_destroy(&scanner)
get_line :: proc(scanner: ^bufio.Scanner) -> (line: string, status: WebSocket_Error) {
if !bufio.scanner_scan(scanner) {
scan_err := bufio.scanner_error(scanner)
log.errorf("ERROR - Scanner encountered an error: %v\n", scan_err)
status = .Handshake_Failure
return
}
line = bufio.scanner_text(scanner)
log.debug(line)
if len(transmute([]u8)line) <= 0 {
status = .Nil
return
}
return
}
// Get request start line
start_line_raw, sl_err := get_line(&scanner)
if sl_err != .Nil {
ok = false
return
}
sl_chunks, _ := strings.split(start_line_raw, " ", context.temp_allocator)
resp = new(Response, allocator)
// Get Headers
response_headers: map[string]string
for {
next_line, err := get_line(&scanner)
if err != .Nil {
ok = false
return
}
if next_line == "" {
break
}
line_chunks, _ := strings.split(next_line, ":", context.temp_allocator)
response_headers[line_chunks[0]] = line_chunks[1]
}
// Log the HTTP response
builder: strings.Builder
strings.builder_init(&builder, context.temp_allocator)
strings.write_string(&builder, fmt.tprintf("\n\tStatus: %v\n", sl_chunks[1]))
for k, v in response_headers {
strings.write_string(&builder, fmt.tprintf("\t%v: %v\n", k, v))
}
log.info(strings.to_string(builder))
status, _ := strconv.parse_i64(sl_chunks[1])
resp.status = Status_Code(status)
resp.headers = response_headers
// Get Body
strings.builder_reset(&builder)
// response_body: [dynamic]string
for {
if scanner.done || scanner.start >= scanner.end {
break
}
next_line, err := get_line(&scanner)
if err != .Nil {
ok = false
return
}
if next_line == "" {
break
}
log.info(next_line)
strings.write_string(&builder, fmt.tprintf("\t%v\n", next_line))
}
log.info("\n[BODY]:\n")
log.info(strings.to_string(builder))
ok = true
return
}
request_log :: proc(data: []byte) {
log.debug("\n[REQUEST]:\n")
log.debug(string(data))
}