Skip to content

Commit 43d83ed

Browse files
committed
minor refactorings
1 parent 00df5d8 commit 43d83ed

1 file changed

Lines changed: 83 additions & 107 deletions

File tree

‎src/httpfile/parser.zig‎

Lines changed: 83 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,7 @@ pub const HttpMethod = enum {
1616
UNKNOWN,
1717

1818
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;
19+
return std.meta.stringToEnum(HttpMethod, method_str) orelse .UNKNOWN;
3020
}
3121
};
3222

@@ -53,9 +43,7 @@ pub const HttpRequest = struct {
5343
}
5444

5545
pub fn deinit(self: *HttpRequest) void {
56-
if (self.url.len > 0) {
57-
self.allocator.free(self.url);
58-
}
46+
self.allocator.free(self.url);
5947

6048
for (self.headers.items) |header| {
6149
self.allocator.free(header.name);
@@ -69,124 +57,112 @@ pub const HttpRequest = struct {
6957
}
7058
};
7159

72-
pub const HttpParser = struct {
73-
allocator: Allocator,
60+
pub fn parseFile(allocator: std.mem.Allocator, file_path: []const u8) !ArrayList(HttpRequest) {
61+
const file = try std.fs.cwd().openFile(file_path, .{});
62+
defer file.close();
7463

75-
pub fn init(allocator: Allocator) HttpParser {
76-
return .{
77-
.allocator = allocator,
78-
};
79-
}
64+
const file_content = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
65+
defer allocator.free(file_content);
8066

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();
67+
return try parseContent(file_content);
68+
}
8469

85-
const file_content = try file.readToEndAlloc(self.allocator, std.math.maxInt(usize));
86-
defer self.allocator.free(file_content);
70+
pub fn parseContent(allocator: std.mem.Allocator, content: []const u8) !ArrayList(HttpRequest) {
71+
var requests = ArrayList(HttpRequest).init(allocator);
72+
errdefer {
73+
for (requests.items) |*request| {
74+
request.deinit();
75+
}
8776

88-
return try self.parseContent(file_content);
77+
requests.deinit();
8978
}
9079

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();
80+
var current_request = HttpRequest.init(allocator);
81+
errdefer current_request.deinit();
10382

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();
83+
var lines = std.mem.splitScalar(u8, content, '\n');
84+
var line_index: usize = 0;
85+
var in_headers = false;
86+
var in_body = false;
87+
var body_buffer = ArrayList(u8).init(allocator);
88+
defer body_buffer.deinit();
11089

111-
while (lines.next()) |line| {
112-
line_index += 1;
113-
const trimmed_line = std.mem.trim(u8, line, &std.ascii.whitespace);
90+
while (lines.next()) |line| {
91+
line_index += 1;
92+
const trimmed_line = std.mem.trim(u8, line, &std.ascii.whitespace);
11493

115-
if (trimmed_line.len == 0) {
116-
if (in_headers) {
117-
in_headers = false;
118-
in_body = true;
119-
}
120-
continue;
94+
if (trimmed_line.len == 0) {
95+
if (in_headers) {
96+
in_headers = false;
97+
in_body = true;
12198
}
99+
continue;
100+
}
122101

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-
}
102+
if (std.mem.startsWith(u8, trimmed_line, "###")) {
103+
if (current_request.method != .UNKNOWN) {
131104

132-
try requests.append(current_request);
133-
current_request = HttpRequest.init(self.allocator);
134-
in_body = false;
135-
in_headers = false;
105+
// if we're in the body, dupe it and clear it
106+
if (in_body and body_buffer.items.len > 0) {
107+
current_request.body = try allocator.dupe(u8, body_buffer.items);
108+
body_buffer.clearRetainingCapacity();
136109
}
137-
continue;
138-
}
139110

140-
if (std.mem.startsWith(u8, trimmed_line, "#") or std.mem.startsWith(u8, trimmed_line, "//")) {
141-
continue;
111+
try requests.append(current_request);
112+
current_request = HttpRequest.init(allocator);
113+
in_body = false;
114+
in_headers = false;
142115
}
116+
continue;
117+
}
143118

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;
119+
if (std.mem.startsWith(u8, trimmed_line, "#") or std.mem.startsWith(u8, trimmed_line, "//")) {
120+
continue;
121+
}
148122

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-
}
123+
if (!in_headers and !in_body and current_request.method == .UNKNOWN) {
124+
var tokens = std.mem.tokenizeScalar(u8, trimmed_line, ' ');
125+
const method_str = tokens.next() orelse return error.InvalidRequestMissingMethod;
126+
const url = tokens.next() orelse return error.InvalidRequestMissingURL;
154127

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-
}
128+
current_request.method = HttpMethod.fromString(method_str);
129+
current_request.url = try allocator.dupe(u8, url);
130+
in_headers = true;
131+
continue;
132+
}
168133

169-
if (in_body) {
170-
try body_buffer.appendSlice(trimmed_line);
171-
try body_buffer.append('\n');
134+
if (in_headers) {
135+
if (std.mem.indexOf(u8, trimmed_line, ":")) |colon_pos| {
136+
const header_name = std.mem.trim(u8, trimmed_line[0..colon_pos], &std.ascii.whitespace);
137+
const header_value = std.mem.trim(u8, trimmed_line[colon_pos + 1 ..], &std.ascii.whitespace);
138+
139+
try current_request.headers.append(Header{
140+
.name = try allocator.dupe(u8, header_name),
141+
.value = try allocator.dupe(u8, header_value),
142+
});
143+
} else {
144+
return error.InvalidHeaderFormat;
172145
}
173146
}
174147

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-
}
148+
if (in_body) {
149+
try body_buffer.appendSlice(trimmed_line);
150+
try body_buffer.append('\n');
151+
}
152+
}
179153

180-
try requests.append(current_request);
154+
if (current_request.method != .UNKNOWN) {
155+
if (in_body and body_buffer.items.len > 0) {
156+
current_request.body = try allocator.dupe(u8, body_buffer.items);
181157
}
182158

183-
return requests;
159+
try requests.append(current_request);
184160
}
185-
};
186161

187-
test "HttpParser from String Contents" {
188-
var parser = HttpParser.init(std.testing.allocator);
162+
return requests;
163+
}
189164

165+
test "HttpParser from String Contents" {
190166
const test_http_contents =
191167
\\GET https://api.example.com
192168
\\Accept: */*
@@ -204,16 +180,16 @@ test "HttpParser from String Contents" {
204180
\\}
205181
;
206182

207-
var requests = try parser.parseContent(test_http_contents);
183+
var requests = try parseContent(std.testing.allocator, test_http_contents);
208184
defer {
209185
for (requests.items) |*request| {
210186
request.deinit();
211187
}
212188
requests.deinit();
213189
}
214190

215-
try std.testing.expectEqual("GET", @tagName(requests.items[0].method));
216-
try std.testing.expectEqual("POST", @tagName(requests.items[1].method));
191+
try std.testing.expectEqual(HttpMethod.GET, requests.items[0].method);
192+
try std.testing.expectEqual(HttpMethod.POST, requests.items[1].method);
217193
try std.testing.expectEqualStrings("https://api.example.com", requests.items[0].url);
218194
try std.testing.expectEqualStrings("https://api.example.com/users", requests.items[1].url);
219195
try std.testing.expectEqualStrings("Authorization", requests.items[0].headers.items[1].name);

0 commit comments

Comments
 (0)