|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +const Allocator = std.mem.Allocator; |
| 4 | +const ArrayList = std.ArrayList; |
| 5 | + |
| 6 | +pub const HttpMethod = enum { |
| 7 | + GET, |
| 8 | + POST, |
| 9 | + PUT, |
| 10 | + DELETE, |
| 11 | + PATCH, |
| 12 | + HEAD, |
| 13 | + OPTIONS, |
| 14 | + TRACE, |
| 15 | + CONNECT, |
| 16 | + UNKNOWN, |
| 17 | + |
| 18 | + pub fn fromString(method_str: []const u8) HttpMethod { |
| 19 | + if (std.ascii.eqlIgnoreCase(method_str, "GET")) return .GET; |
| 20 | + if (std.ascii.eqlIgnoreCase(method_str, "POST")) return .POST; |
| 21 | + if (std.ascii.eqlIgnoreCase(method_str, "PUT")) return .PUT; |
| 22 | + if (std.ascii.eqlIgnoreCase(method_str, "DELETE")) return .DELETE; |
| 23 | + if (std.ascii.eqlIgnoreCase(method_str, "PATCH")) return .PATCH; |
| 24 | + if (std.ascii.eqlIgnoreCase(method_str, "HEAD")) return .HEAD; |
| 25 | + if (std.ascii.eqlIgnoreCase(method_str, "OPTIONS")) return .OPTIONS; |
| 26 | + if (std.ascii.eqlIgnoreCase(method_str, "TRACE")) return .TRACE; |
| 27 | + if (std.ascii.eqlIgnoreCase(method_str, "CONNECT")) return .CONNECT; |
| 28 | + |
| 29 | + return .UNKNOWN; |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +pub const Header = struct { |
| 34 | + name: []const u8, |
| 35 | + value: []const u8, |
| 36 | +}; |
| 37 | + |
| 38 | +pub const HttpRequest = struct { |
| 39 | + method: HttpMethod, |
| 40 | + url: []const u8, |
| 41 | + headers: ArrayList(Header), |
| 42 | + body: ?[]const u8, |
| 43 | + allocator: Allocator, |
| 44 | + |
| 45 | + pub fn init(allocator: Allocator) HttpRequest { |
| 46 | + return .{ |
| 47 | + .method = .UNKNOWN, |
| 48 | + .url = "", |
| 49 | + .headers = ArrayList(Header).init(allocator), |
| 50 | + .body = null, |
| 51 | + .allocator = allocator, |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + pub fn deinit(self: *HttpRequest) void { |
| 56 | + if (self.url.len > 0) { |
| 57 | + self.allocator.free(self.url); |
| 58 | + } |
| 59 | + |
| 60 | + for (self.headers.items) |header| { |
| 61 | + self.allocator.free(header.name); |
| 62 | + self.allocator.free(header.value); |
| 63 | + } |
| 64 | + |
| 65 | + if (self.body) |body| { |
| 66 | + self.allocator.free(body); |
| 67 | + } |
| 68 | + self.headers.deinit(); |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +pub const HttpParser = struct { |
| 73 | + allocator: Allocator, |
| 74 | + |
| 75 | + pub fn init(allocator: Allocator) HttpParser { |
| 76 | + return .{ |
| 77 | + .allocator = allocator, |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + pub fn parseFile(self: *HttpParser, file_path: []const u8) !ArrayList(HttpRequest) { |
| 82 | + const file = try std.fs.cwd().openFile(file_path, .{}); |
| 83 | + defer file.close(); |
| 84 | + |
| 85 | + const file_content = try file.readToEndAlloc(self.allocator, std.math.maxInt(usize)); |
| 86 | + defer self.allocator.free(file_content); |
| 87 | + |
| 88 | + return try self.parseContent(file_content); |
| 89 | + } |
| 90 | + |
| 91 | + pub fn parseContent(self: *HttpParser, content: []const u8) !ArrayList(HttpRequest) { |
| 92 | + var requests = ArrayList(HttpRequest).init(self.allocator); |
| 93 | + errdefer { |
| 94 | + for (requests.items) |*request| { |
| 95 | + request.deinit(); |
| 96 | + } |
| 97 | + |
| 98 | + requests.deinit(); |
| 99 | + } |
| 100 | + |
| 101 | + var current_request = HttpRequest.init(self.allocator); |
| 102 | + errdefer current_request.deinit(); |
| 103 | + |
| 104 | + var lines = std.mem.splitSequence(u8, content, "\n"); |
| 105 | + var line_index: usize = 0; |
| 106 | + var in_headers = false; |
| 107 | + var in_body = false; |
| 108 | + var body_buffer = ArrayList(u8).init(self.allocator); |
| 109 | + defer body_buffer.deinit(); |
| 110 | + |
| 111 | + while (lines.next()) |line| { |
| 112 | + line_index += 1; |
| 113 | + const trimmed_line = std.mem.trim(u8, line, &std.ascii.whitespace); |
| 114 | + |
| 115 | + if (trimmed_line.len == 0) { |
| 116 | + if (in_headers) { |
| 117 | + in_headers = false; |
| 118 | + in_body = true; |
| 119 | + } |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + if (std.mem.startsWith(u8, trimmed_line, "###")) { |
| 124 | + if (current_request.method != .UNKNOWN) { |
| 125 | + |
| 126 | + // if we're in the body, dupe it and clear it |
| 127 | + if (in_body and body_buffer.items.len > 0) { |
| 128 | + current_request.body = try self.allocator.dupe(u8, body_buffer.items); |
| 129 | + body_buffer.clearRetainingCapacity(); |
| 130 | + } |
| 131 | + |
| 132 | + try requests.append(current_request); |
| 133 | + current_request = HttpRequest.init(self.allocator); |
| 134 | + in_body = false; |
| 135 | + in_headers = false; |
| 136 | + } |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + if (std.mem.startsWith(u8, trimmed_line, "#") or std.mem.startsWith(u8, trimmed_line, "//")) { |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + if (!in_headers and !in_body and current_request.method == .UNKNOWN) { |
| 145 | + var tokens = std.mem.tokenizeScalar(u8, trimmed_line, ' '); |
| 146 | + const method_str = tokens.next() orelse return error.InvalidRequestMissingMethod; |
| 147 | + const url = tokens.next() orelse return error.InvalidRequestMissingURL; |
| 148 | + |
| 149 | + current_request.method = HttpMethod.fromString(method_str); |
| 150 | + current_request.url = try self.allocator.dupe(u8, url); |
| 151 | + in_headers = true; |
| 152 | + continue; |
| 153 | + } |
| 154 | + |
| 155 | + if (in_headers) { |
| 156 | + if (std.mem.indexOf(u8, trimmed_line, ":")) |colon_pos| { |
| 157 | + const header_name = std.mem.trim(u8, trimmed_line[0..colon_pos], &std.ascii.whitespace); |
| 158 | + const header_value = std.mem.trim(u8, trimmed_line[colon_pos + 1 ..], &std.ascii.whitespace); |
| 159 | + |
| 160 | + try current_request.headers.append(Header{ |
| 161 | + .name = try self.allocator.dupe(u8, header_name), |
| 162 | + .value = try self.allocator.dupe(u8, header_value), |
| 163 | + }); |
| 164 | + } else { |
| 165 | + return error.InvalidHeaderFormat; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if (in_body) { |
| 170 | + try body_buffer.appendSlice(trimmed_line); |
| 171 | + try body_buffer.append('\n'); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + if (current_request.method != .UNKNOWN) { |
| 176 | + if (in_body and body_buffer.items.len > 0) { |
| 177 | + current_request.body = try self.allocator.dupe(u8, body_buffer.items); |
| 178 | + } |
| 179 | + |
| 180 | + try requests.append(current_request); |
| 181 | + } |
| 182 | + |
| 183 | + return requests; |
| 184 | + } |
| 185 | +}; |
| 186 | + |
| 187 | +test "HttpParser from String Contents" { |
| 188 | + var parser = HttpParser.init(std.testing.allocator); |
| 189 | + |
| 190 | + const test_http_contents = |
| 191 | + \\GET https://api.example.com |
| 192 | + \\Accept: */* |
| 193 | + \\Authorization: Bearer ABC123 |
| 194 | + \\ |
| 195 | + \\### |
| 196 | + \\ |
| 197 | + \\POST https://api.example.com/users |
| 198 | + \\Accept: */* |
| 199 | + \\Authorization: Bearer ABC123 |
| 200 | + \\ |
| 201 | + \\{ |
| 202 | + \\ "name": "John Doe", |
| 203 | + \\ "email": "John@Doe.com", |
| 204 | + \\} |
| 205 | + ; |
| 206 | + |
| 207 | + var requests = try parser.parseContent(test_http_contents); |
| 208 | + defer { |
| 209 | + for (requests.items) |*request| { |
| 210 | + request.deinit(); |
| 211 | + } |
| 212 | + requests.deinit(); |
| 213 | + } |
| 214 | + |
| 215 | + try std.testing.expectEqual("GET", @tagName(requests.items[0].method)); |
| 216 | + try std.testing.expectEqual("POST", @tagName(requests.items[1].method)); |
| 217 | + try std.testing.expectEqualStrings("https://api.example.com", requests.items[0].url); |
| 218 | + try std.testing.expectEqualStrings("https://api.example.com/users", requests.items[1].url); |
| 219 | + try std.testing.expectEqualStrings("Authorization", requests.items[0].headers.items[1].name); |
| 220 | + try std.testing.expectEqualStrings("Bearer ABC123", requests.items[0].headers.items[1].value); |
| 221 | + try std.testing.expectEqualStrings("Authorization", requests.items[1].headers.items[1].name); |
| 222 | + try std.testing.expectEqualStrings("Bearer ABC123", requests.items[1].headers.items[1].value); |
| 223 | + try std.testing.expectEqual(0, (requests.items[0].body orelse "").len); |
| 224 | + try std.testing.expect(0 != (requests.items[1].body orelse "").len); |
| 225 | +} |
0 commit comments