Skip to content

Commit b67e1b2

Browse files
committed
remove allocators from structs that dont need them
1 parent 43d83ed commit b67e1b2

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

‎src/httpfile/parser.zig‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ pub const HttpMethod = enum {
1313
OPTIONS,
1414
TRACE,
1515
CONNECT,
16-
UNKNOWN,
1716

18-
pub fn fromString(method_str: []const u8) HttpMethod {
19-
return std.meta.stringToEnum(HttpMethod, method_str) orelse .UNKNOWN;
17+
pub fn fromString(method_str: []const u8) ?HttpMethod {
18+
return std.meta.stringToEnum(HttpMethod, method_str) orelse null;
2019
}
2120
};
2221

@@ -26,32 +25,30 @@ pub const Header = struct {
2625
};
2726

2827
pub const HttpRequest = struct {
29-
method: HttpMethod,
28+
method: ?HttpMethod,
3029
url: []const u8,
3130
headers: ArrayList(Header),
3231
body: ?[]const u8,
33-
allocator: Allocator,
3432

3533
pub fn init(allocator: Allocator) HttpRequest {
3634
return .{
37-
.method = .UNKNOWN,
35+
.method = null,
3836
.url = "",
3937
.headers = ArrayList(Header).init(allocator),
4038
.body = null,
41-
.allocator = allocator,
4239
};
4340
}
4441

45-
pub fn deinit(self: *HttpRequest) void {
46-
self.allocator.free(self.url);
42+
pub fn deinit(self: *HttpRequest, allocator: std.mem.Allocator) void {
43+
allocator.free(self.url);
4744

4845
for (self.headers.items) |header| {
49-
self.allocator.free(header.name);
50-
self.allocator.free(header.value);
46+
allocator.free(header.name);
47+
allocator.free(header.value);
5148
}
5249

5350
if (self.body) |body| {
54-
self.allocator.free(body);
51+
allocator.free(body);
5552
}
5653
self.headers.deinit();
5754
}
@@ -71,14 +68,14 @@ pub fn parseContent(allocator: std.mem.Allocator, content: []const u8) !ArrayLis
7168
var requests = ArrayList(HttpRequest).init(allocator);
7269
errdefer {
7370
for (requests.items) |*request| {
74-
request.deinit();
71+
request.deinit(allocator);
7572
}
7673

7774
requests.deinit();
7875
}
7976

8077
var current_request = HttpRequest.init(allocator);
81-
errdefer current_request.deinit();
78+
errdefer current_request.deinit(allocator);
8279

8380
var lines = std.mem.splitScalar(u8, content, '\n');
8481
var line_index: usize = 0;
@@ -100,8 +97,7 @@ pub fn parseContent(allocator: std.mem.Allocator, content: []const u8) !ArrayLis
10097
}
10198

10299
if (std.mem.startsWith(u8, trimmed_line, "###")) {
103-
if (current_request.method != .UNKNOWN) {
104-
100+
if (current_request.method != null) {
105101
// if we're in the body, dupe it and clear it
106102
if (in_body and body_buffer.items.len > 0) {
107103
current_request.body = try allocator.dupe(u8, body_buffer.items);
@@ -120,7 +116,7 @@ pub fn parseContent(allocator: std.mem.Allocator, content: []const u8) !ArrayLis
120116
continue;
121117
}
122118

123-
if (!in_headers and !in_body and current_request.method == .UNKNOWN) {
119+
if (!in_headers and !in_body and current_request.method == null) {
124120
var tokens = std.mem.tokenizeScalar(u8, trimmed_line, ' ');
125121
const method_str = tokens.next() orelse return error.InvalidRequestMissingMethod;
126122
const url = tokens.next() orelse return error.InvalidRequestMissingURL;
@@ -151,7 +147,7 @@ pub fn parseContent(allocator: std.mem.Allocator, content: []const u8) !ArrayLis
151147
}
152148
}
153149

154-
if (current_request.method != .UNKNOWN) {
150+
if (current_request.method != null) {
155151
if (in_body and body_buffer.items.len > 0) {
156152
current_request.body = try allocator.dupe(u8, body_buffer.items);
157153
}
@@ -183,7 +179,7 @@ test "HttpParser from String Contents" {
183179
var requests = try parseContent(std.testing.allocator, test_http_contents);
184180
defer {
185181
for (requests.items) |*request| {
186-
request.deinit();
182+
request.deinit(std.testing.allocator);
187183
}
188184
requests.deinit();
189185
}

0 commit comments

Comments
 (0)