-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.zig
More file actions
232 lines (214 loc) · 9.11 KB
/
parser.zig
File metadata and controls
232 lines (214 loc) · 9.11 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
const std = @import("std");
const http = std.http;
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const ParserState = enum { headers, body };
const AssertionType = enum {
equal,
not_equal,
contains,
not_contains,
starts_with,
ends_with,
matches_regex,
not_matches_regex,
pub fn fromString(s: []const u8) ?AssertionType {
if (std.ascii.eqlIgnoreCase(s, "==")) return .equal;
if (std.ascii.eqlIgnoreCase(s, "equal")) return .equal;
if (std.ascii.eqlIgnoreCase(s, "!=")) return .not_equal;
if (std.ascii.eqlIgnoreCase(s, "contains")) return .contains;
if (std.ascii.eqlIgnoreCase(s, "not_contains")) return .not_contains;
if (std.ascii.eqlIgnoreCase(s, "starts_with")) return .starts_with;
if (std.ascii.eqlIgnoreCase(s, "ends_with")) return .ends_with;
if (std.ascii.eqlIgnoreCase(s, "matches_regex")) return .matches_regex;
if (std.ascii.eqlIgnoreCase(s, "not_matches_regex")) return .not_matches_regex;
return null;
}
};
pub const Assertion = struct {
key: []const u8,
value: []const u8,
assertion_type: AssertionType,
};
pub const HttpRequest = struct {
method: ?http.Method,
url: []const u8,
headers: ArrayList(http.Header),
body: ?[]const u8,
assertions: ArrayList(Assertion),
// TODO: Add a name for the request if needed.
pub fn init(allocator: Allocator) HttpRequest {
return .{
.method = null,
.url = "",
.headers = ArrayList(http.Header).init(allocator),
.body = null,
.assertions = ArrayList(Assertion).init(allocator),
};
}
pub fn deinit(self: *HttpRequest, allocator: Allocator) void {
if (self.url.len > 0) allocator.free(self.url);
for (self.assertions.items) |assertion| {
if (assertion.key.len > 0) allocator.free(assertion.key);
if (assertion.value.len > 0) allocator.free(assertion.value);
}
self.assertions.deinit();
for (self.headers.items) |header| {
if (header.name.len > 0) allocator.free(header.name);
if (header.value.len > 0) allocator.free(header.value);
}
self.headers.deinit();
if (self.body) |body| {
if (body.len > 0) allocator.free(body);
}
}
};
pub fn parseFile(allocator: Allocator, file_path: []const u8) !ArrayList(HttpRequest) {
const file = try std.fs.cwd().openFile(file_path, .{});
defer file.close();
const file_content = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(file_content);
return try parseContent(allocator, file_content);
}
pub fn parseContent(allocator: Allocator, content: []const u8) !ArrayList(HttpRequest) {
var requests = ArrayList(HttpRequest).init(allocator);
errdefer {
for (requests.items) |*request| request.deinit(allocator);
requests.deinit();
}
var current_request = HttpRequest.init(allocator);
errdefer current_request.deinit(allocator);
var state: ?ParserState = null;
var lines = std.mem.splitScalar(u8, content, '\n');
var body_buffer = ArrayList(u8).init(allocator);
defer body_buffer.deinit();
while (lines.next()) |line| {
const trimmed_line = std.mem.trim(u8, line, &std.ascii.whitespace);
if (trimmed_line.len == 0) {
if (state == .headers) state = .body;
continue;
}
if (std.mem.startsWith(u8, trimmed_line, "###")) {
if (current_request.method != null) {
if (state == .body and body_buffer.items.len > 0) {
current_request.body = try allocator.dupe(u8, body_buffer.items);
body_buffer.clearRetainingCapacity();
}
try requests.append(current_request);
current_request = HttpRequest.init(allocator);
state = null;
}
continue;
}
if (std.mem.startsWith(u8, trimmed_line, "//#")) {
// Assertion line
var assertion_tokens = std.mem.tokenizeScalar(u8, std.mem.trim(u8, trimmed_line[3..], " "), ' ');
const key = assertion_tokens.next() orelse return error.InvalidAssertionFormat;
const type_str = assertion_tokens.next() orelse return error.InvalidAssertionFormat;
const value = assertion_tokens.next() orelse return error.InvalidAssertionFormat;
const assertion_type = AssertionType.fromString(type_str) orelse return error.InvalidAssertionFormat;
const assertion = Assertion{
.key = try allocator.dupe(u8, key),
.value = try allocator.dupe(u8, value),
.assertion_type = assertion_type,
};
if (assertion.key.len == 0 or assertion.value.len == 0) return error.InvalidAssertionFormat;
try current_request.assertions.append(assertion);
continue;
}
if (std.mem.startsWith(u8, trimmed_line, "#") or std.mem.startsWith(u8, trimmed_line, "//")) {
continue;
}
if (state == null and current_request.method == null) {
var tokens = std.mem.tokenizeScalar(u8, trimmed_line, ' ');
const method_str = tokens.next() orelse return error.InvalidRequestMissingMethod;
const url = tokens.next() orelse return error.InvalidRequestMissingURL;
current_request.method = std.meta.stringToEnum(http.Method, method_str) orelse null;
current_request.url = try allocator.dupe(u8, url);
state = .headers;
continue;
}
if (state == .headers) {
if (std.mem.indexOf(u8, trimmed_line, ":")) |colon_pos| {
const header_name = std.mem.trim(u8, trimmed_line[0..colon_pos], &std.ascii.whitespace);
const header_value = std.mem.trim(u8, trimmed_line[colon_pos + 1 ..], &std.ascii.whitespace);
try current_request.headers.append(http.Header{
.name = try allocator.dupe(u8, header_name),
.value = try allocator.dupe(u8, header_value),
});
} else {
return error.InvalidHeaderFormat;
}
continue;
}
if (state == .body) {
try body_buffer.appendSlice(trimmed_line);
try body_buffer.append('\n');
continue;
}
}
if (current_request.method != null) {
if (state == .body and body_buffer.items.len > 0) {
current_request.body = try allocator.dupe(u8, body_buffer.items);
}
try requests.append(current_request);
}
return requests;
}
test "HttpParser from String Contents" {
const test_http_contents =
\\GET https://api.example.com
\\Accept: */*
\\Authorization: Bearer ABC123
\\
\\###
\\
\\POST https://api.example.com/users
\\Accept: */*
\\Authorization: Bearer ABC123
\\
\\{
\\ "name": "John Doe",
\\ "email": "John@Doe.com",
\\}
;
var requests = try parseContent(std.testing.allocator, test_http_contents);
defer {
for (requests.items) |*request| {
request.deinit(std.testing.allocator);
}
requests.deinit();
}
try std.testing.expectEqual(http.Method.GET, requests.items[0].method);
try std.testing.expectEqual(http.Method.POST, requests.items[1].method);
try std.testing.expectEqualStrings("https://api.example.com", requests.items[0].url);
try std.testing.expectEqualStrings("https://api.example.com/users", requests.items[1].url);
try std.testing.expectEqualStrings("Authorization", requests.items[0].headers.items[1].name);
try std.testing.expectEqualStrings("Bearer ABC123", requests.items[0].headers.items[1].value);
try std.testing.expectEqualStrings("Authorization", requests.items[1].headers.items[1].name);
try std.testing.expectEqualStrings("Bearer ABC123", requests.items[1].headers.items[1].value);
try std.testing.expectEqual(0, (requests.items[0].body orelse "").len);
try std.testing.expect(0 != (requests.items[1].body orelse "").len);
}
test "HttpParser parses assertions" {
const test_http_contents =
\\GET https://api.example.com
\\Accept: */*
\\Authorization: Bearer ABC123
\\
\\//# status equal 200
;
var requests = try parseContent(std.testing.allocator, test_http_contents);
defer {
for (requests.items) |*request| {
request.deinit(std.testing.allocator);
}
requests.deinit();
}
try std.testing.expectEqual(http.Method.GET, requests.items[0].method);
try std.testing.expectEqualStrings("https://api.example.com", requests.items[0].url);
try std.testing.expectEqualStrings("status", requests.items[0].assertions.items[0].key);
try std.testing.expectEqual(AssertionType.equal, requests.items[0].assertions.items[0].assertion_type);
try std.testing.expectEqualStrings("200", requests.items[0].assertions.items[0].value);
try std.testing.expectEqual(0, (requests.items[0].body orelse "").len);
}