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
|
const std = @import("std");
const Allocator = std.mem.Allocator;
const split = std.mem.split;
const net = std.net;
const expect = std.testing.expect;
const expectError = std.testing.expectError;
const StringHashMap = std.StringHashMap;
pub const HttpError = error {
ParsingError
};
/// HttpStatus
pub const HttpStatus = struct{
protocol: []const u8,
status: usize,
status_msg: []const u8,
};
pub const HttpStatusParser = struct{
status: HttpStatus,
rawline: []u8,
allocator: Allocator,
const Self = @This();
pub fn init(allocator: Allocator) Self{
return Self{
.status = undefined, // Can I do this?
.rawline = "",
.allocator = allocator,
};
}
/// Takes a reader and consumes one status line of HTTP response allocating
/// it's own copy of the line. Must call `deinit()` to clean the
/// allocations.
/// Fills it's `status` field with the results of that.
pub fn parseReader(self: *Self, reader: anytype) !void{
self.rawline = try reader.readUntilDelimiterAlloc(self.allocator, '\r', 1000);
// Drop \n
_ = try reader.readByte();
var components = split(u8, self.rawline, " ");
const p = if(components.next()) |p| p else return error.ParsingError;
const s = if(components.next()) |s| s else return error.ParsingError;
const sm = components.rest();
if (sm.len == 0) return error.ParsingError;
self.status.protocol = p;
self.status.status = try std.fmt.parseUnsigned(usize, s, 10);
self.status.status_msg = sm;
}
pub fn deinit(self: *Self) void{
self.allocator.free(self.rawline);
}
};
test "200 OK is parsed correctly in an HttpStatus" {
const status_line = "HTTP/1.1 200 OK\r\n";
const allocator = std.testing.allocator;
var fis = std.io.fixedBufferStream(status_line);
const reader = fis.reader();
var status = HttpStatusParser.init(allocator);
defer status.deinit();
try status.parseReader(reader);
try expect( std.mem.eql(u8, status.status.protocol, "HTTP/1.1") );
try expect( status.status.status == 200 );
try expect( std.mem.eql(u8, status.status.status_msg, "OK") );
}
test "306 Switch Proxy is parsed correctly in an HttpStatus" {
const status_line = "HTTP/1.1 306 Switch Proxy\r\n";
const allocator = std.testing.allocator;
var fis = std.io.fixedBufferStream(status_line);
const reader = fis.reader();
var status = HttpStatusParser.init(allocator);
defer status.deinit();
try status.parseReader(reader);
try expect( std.mem.eql(u8, status.status.protocol, "HTTP/1.1") );
try expect( status.status.status == 306 );
try expect( std.mem.eql(u8, status.status.status_msg, "Switch Proxy") );
}
test "Broken statusline is detected correctly in an HttpStatus" {
const status_line = "HTTP/1.1 306 \r\n";
const allocator = std.testing.allocator;
var fis = std.io.fixedBufferStream(status_line);
const reader = fis.reader();
var status = HttpStatusParser.init(allocator);
defer status.deinit();
try expectError( error.ParsingError, status.parseReader(reader) );
}
/// HttpHeaders
pub const HttpHeaders = struct{
values: StringHashMap([]const u8),
owned_data: std.ArrayList([]u8),
allocator: Allocator,
const Self = @This();
pub fn init(allocator: Allocator) Self{
return Self{
.values = StringHashMap([]const u8).init(allocator),
.allocator = allocator,
.owned_data = std.ArrayList([]u8).init(allocator),
};
}
pub fn get(self: *Self, k: []const u8) !?[]const u8{
var key:[]u8 = try self.allocator.alloc(u8, k.len);
defer self.allocator.free(key);
for (key) |*char, index|{
char.* = std.ascii.toLower(k[index]);
}
return self.values.get(key);
}
pub fn put(self: *Self, k: []const u8, v: []const u8) !void{
// Own key
var key:[]u8 = try self.allocator.alloc(u8, k.len);
for (key) |*char,index|{
char.* = std.ascii.toLower(char.*);
char.* = std.ascii.toLower(k[index]);
}
try self.owned_data.append(key);
// Own value
var value:[]u8 = try self.allocator.alloc(u8, v.len);
std.mem.copy(u8, value, v);
try self.owned_data.append(value);
// Store header
try self.values.put(key, value);
}
pub fn deinit(self: *Self) void{
self.values.deinit();
for (self.owned_data.items) |item|{
self.allocator.free(item);
}
self.owned_data.deinit();
}
};
pub const HttpHeaderParser = struct{
headers: HttpHeaders,
allocator: Allocator,
const Self = @This();
pub fn init(allocator: Allocator) Self{
return Self{
.headers = HttpHeaders.init(allocator),
.allocator = allocator,
};
}
pub fn parseReader(self: *Self, reader: anytype) !void{
while(true) {
var line:[100000]u8 = undefined;
var slice = try reader.readUntilDelimiter(&line, '\r');
// Drop \n
_ = try reader.readByte();
if(slice.len == 0){
return; // Finished header block
}
var hit = std.mem.split(u8, slice, ":");
const key = if (hit.next()) |k| k else return error.ParsingError;
const val = hit.rest();
if (val.len == 0){
return error.ParsingError;
}
var spaces:usize = 0;
for(val) |ch|{
if(ch == ' '){
spaces = spaces + 1;
} else {
break;
}
} else {
return error.ParsingError;
}
try self.headers.put(key, val[spaces..]);
}
}
pub fn deinit(self: *Self) void{
self.headers.deinit();
}
};
test "Header block is parsed correctly, is case-insensitive and ignores spaces after :" {
const allocator = std.testing.allocator;
const block =
"Host: api.open-meteo.com\r\n" ++
"Transfer-Encoding: chunked\r\n" ++
"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0\r\n" ++
"\r\n";
var fis = std.io.fixedBufferStream(block);
const reader = fis.reader();
var headers = HttpHeaderParser.init(allocator);
defer headers.deinit();
try headers.parseReader(reader);
if (try headers.headers.get("HOST")) |host| {
try expect( std.mem.eql(u8, host, "api.open-meteo.com") );
}
if (try headers.headers.get("host")) |host| {
try expect( std.mem.eql(u8, host, "api.open-meteo.com") );
}
if (try headers.headers.get("Transfer-Encoding")) |tencoding| {
try expect( std.mem.eql(u8, tencoding, "chunked") );
}
if (try headers.headers.get("User-Agent")) |useragent| {
try expect( std.mem.eql(u8, useragent,
"Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0") );
}
}
test "Header line with no semicolon is a parsing error" {
const allocator = std.testing.allocator;
const block =
"Host api.open-meteo.com\r\n" ++
"Transfer-Encoding: chunked\r\n" ++
"\r\n";
var fis = std.io.fixedBufferStream(block);
const reader = fis.reader();
var headers = HttpHeaderParser.init(allocator);
defer headers.deinit();
try expectError(error.ParsingError, headers.parseReader(reader));
}
/// HttpBody
pub const HttpBodyParser = struct {
data: []u8,
allocator: Allocator,
const Self = @This();
pub fn init(allocator: Allocator) !Self{
return Self{
.data = try allocator.create([0]u8),
.allocator = allocator,
};
}
pub fn deinit(self: *Self) void{
self.allocator.free(self.data);
}
pub fn parseChunkedReader(self: *Self, body_reader: anytype) !void{
var cursor:usize = 0;
while (true){
var buffer:[100]u8 = undefined;
// Read chunk size line
var size = try body_reader.readUntilDelimiter(&buffer, '\r');
_ = try body_reader.readByte(); // Drop \n
// Parse the number
const chunk_size = try std.fmt.parseUnsigned(usize, size, 16);
// If chunk is empty finish
if( chunk_size == 0 ){ return; }
// Reallocate the data to fit the incoming chunk
self.data = try self.allocator.realloc(self.data, cursor + chunk_size);
// Read all the bytes of the chunk
for( self.data[cursor..cursor+chunk_size] ) |*byte|{
byte.* = try body_reader.readByte();
}
// Drop \r\n
_ = try body_reader.readByte();
_ = try body_reader.readByte();
cursor = cursor + chunk_size;
}
}
pub fn parseFixedSizeReader(self: *Self, body_reader: anytype, size: usize) !void{
self.allocator.free(self.data);
self.data = try body_reader.readAllAlloc(self.allocator, size);
}
};
test "Parse chunked body from Reader"{
const chunked_body =
"7\r\n" ++
"Mozilla\r\n" ++
"12\r\n" ++
" Developer Network\r\n" ++
"0\r\n" ++
"\r\n".*;
var fis = std.io.fixedBufferStream(chunked_body);
const reader = fis.reader();
const allocator = std.testing.allocator;
var body = try HttpBodyParser.init(allocator);
defer body.deinit();
try body.parseChunkedReader(reader);
try expect( std.mem.eql(u8, "Mozilla Developer Network", body.data) );
}
/// HttpResponse
pub const HttpResponseParser = struct{
statusparser: HttpStatusParser,
headerparser: HttpHeaderParser,
bodyparser: HttpBodyParser,
const Self = @This();
pub fn init(allocator: Allocator) !Self{
return Self{
.statusparser = HttpStatusParser.init(allocator),
.headerparser = HttpHeaderParser.init(allocator),
.bodyparser = try HttpBodyParser.init(allocator),
};
}
pub fn parseReader(self: *Self, reader: anytype) !void{
try self.statusparser.parseReader(reader);
try self.headerparser.parseReader(reader);
if(try self.headerparser.headers.get("Transfer-Encoding")) |te|{
if( std.mem.containsAtLeast(u8, te, 1, "chunked") ) {
try self.bodyparser.parseChunkedReader(reader);
}
} else {
if(try self.headerparser.headers.get("Content-Length")) |cl|{
const content_length = try std.fmt.parseUnsigned(usize, cl, 10);
try self.bodyparser.parseFixedSizeReader(reader, content_length);
}
}
}
pub fn deinit(self: *Self) void{
self.statusparser.deinit();
self.headerparser.deinit();
self.bodyparser.deinit();
}
pub fn body(self: Self) []u8{
return self.bodyparser.data;
}
pub fn headers(self: Self) HttpHeaders{
return self.headerparser.headers;
}
pub fn status(self: Self) usize{
return self.statusparser.status.status;
}
pub fn statusMsg(self: Self) []const u8{
return self.statusparser.status.status_msg;
}
pub fn protocol(self: Self) []const u8{
return self.statusparser.status.protocol;
}
};
// Full answer parser
test "Parse full response correctly" {
const responsedata =
"HTTP/1.1 200 OK\r\n" ++
"Content-Type: text/plain\r\n" ++
"Transfer-Encoding: chunked\r\n" ++
"\r\n" ++
"7\r\n" ++
"Mozilla\r\n" ++
"12\r\n" ++
" Developer Network\r\n" ++
"0\r\n" ++
"\r\n";
var fis = std.io.fixedBufferStream(responsedata);
const reader = fis.reader();
const allocator = std.testing.allocator;
var response = try HttpResponseParser.init(allocator);
defer response.deinit();
try response.parseReader(reader);
try expect( std.mem.eql(u8, response.body(), "Mozilla Developer Network") );
try expect( response.status() == 200 );
try expect( std.mem.eql(u8, response.statusMsg(), "OK") );
}
|