Skip to content

Commit a997963

Browse files
committed
Initial commit for Http file parsing
0 parents  commit a997963

6 files changed

Lines changed: 394 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.zig-cache/

‎.tool-versions‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
zig 0.14.0

‎build.zig‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const std = @import("std");
2+
3+
// Although this function looks imperative, note that its job is to
4+
// declaratively construct a build graph that will be executed by an external
5+
// runner.
6+
pub fn build(b: *std.Build) void {
7+
// Standard target options allows the person running `zig build` to choose
8+
// what target to build for. Here we do not override the defaults, which
9+
// means any target is allowed, and the default is native. Other options
10+
// for restricting supported target set are available.
11+
const target = b.standardTargetOptions(.{});
12+
13+
// Standard optimization options allow the person running `zig build` to select
14+
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
15+
// set a preferred release mode, allowing the user to decide how to optimize.
16+
const optimize = b.standardOptimizeOption(.{});
17+
18+
// We will also create a module for our other entry point, 'main.zig'.
19+
const exe_mod = b.createModule(.{
20+
// `root_source_file` is the Zig "entry point" of the module. If a module
21+
// only contains e.g. external object files, you can make this `null`.
22+
// In this case the main source file is merely a path, however, in more
23+
// complicated build scripts, this could be a generated file.
24+
.root_source_file = b.path("src/main.zig"),
25+
.target = target,
26+
.optimize = optimize,
27+
});
28+
29+
// This creates another `std.Build.Step.Compile`, but this one builds an executable
30+
// rather than a static library.
31+
const exe = b.addExecutable(.{
32+
.name = "httpspec",
33+
.root_module = exe_mod,
34+
});
35+
36+
// This declares intent for the executable to be installed into the
37+
// standard location when the user invokes the "install" step (the default
38+
// step when running `zig build`).
39+
b.installArtifact(exe);
40+
41+
// This *creates* a Run step in the build graph, to be executed when another
42+
// step is evaluated that depends on it. The next line below will establish
43+
// such a dependency.
44+
const run_cmd = b.addRunArtifact(exe);
45+
46+
// By making the run step depend on the install step, it will be run from the
47+
// installation directory rather than directly from within the cache directory.
48+
// This is not necessary, however, if the application depends on other installed
49+
// files, this ensures they will be present and in the expected location.
50+
run_cmd.step.dependOn(b.getInstallStep());
51+
52+
// This allows the user to pass arguments to the application in the build
53+
// command itself, like this: `zig build run -- arg1 arg2 etc`
54+
if (b.args) |args| {
55+
run_cmd.addArgs(args);
56+
}
57+
58+
// This creates a build step. It will be visible in the `zig build --help` menu,
59+
// and can be selected like this: `zig build run`
60+
// This will evaluate the `run` step rather than the default, which is "install".
61+
const run_step = b.step("run", "Run the app");
62+
run_step.dependOn(&run_cmd.step);
63+
64+
const exe_unit_tests = b.addTest(.{
65+
.root_module = exe_mod,
66+
});
67+
68+
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
69+
70+
// Similar to creating the run step earlier, this exposes a `test` step to
71+
// the `zig build --help` menu, providing a way for the user to request
72+
// running the unit tests.
73+
const test_step = b.step("test", "Run unit tests");
74+
test_step.dependOn(&run_exe_unit_tests.step);
75+
}

‎build.zig.zon‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
.{
2+
// This is the default name used by packages depending on this one. For
3+
// example, when a user runs `zig fetch --save <url>`, this field is used
4+
// as the key in the `dependencies` table. Although the user can choose a
5+
// different name, most users will stick with this provided value.
6+
//
7+
// It is redundant to include "zig" in this name because it is already
8+
// within the Zig package namespace.
9+
.name = .httpspec,
10+
11+
// This is a [Semantic Version](https://semver.org/).
12+
// In a future version of Zig it will be used for package deduplication.
13+
.version = "0.0.0",
14+
15+
// Together with name, this represents a globally unique package
16+
// identifier. This field is generated by the Zig toolchain when the
17+
// package is first created, and then *never changes*. This allows
18+
// unambiguous detection of one package being an updated version of
19+
// another.
20+
//
21+
// When forking a Zig project, this id should be regenerated (delete the
22+
// field and run `zig build`) if the upstream project is still maintained.
23+
// Otherwise, the fork is *hostile*, attempting to take control over the
24+
// original project's identity. Thus it is recommended to leave the comment
25+
// on the following line intact, so that it shows up in code reviews that
26+
// modify the field.
27+
.fingerprint = 0xf620f76579ad1903, // Changing this has security and trust implications.
28+
29+
// Tracks the earliest Zig version that the package considers to be a
30+
// supported use case.
31+
.minimum_zig_version = "0.14.0",
32+
33+
// This field is optional.
34+
// Each dependency must either provide a `url` and `hash`, or a `path`.
35+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
36+
// Once all dependencies are fetched, `zig build` no longer requires
37+
// internet connectivity.
38+
.dependencies = .{
39+
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
40+
//.example = .{
41+
// // When updating this field to a new URL, be sure to delete the corresponding
42+
// // `hash`, otherwise you are communicating that you expect to find the old hash at
43+
// // the new URL. If the contents of a URL change this will result in a hash mismatch
44+
// // which will prevent zig from using it.
45+
// .url = "https://example.com/foo.tar.gz",
46+
//
47+
// // This is computed from the file contents of the directory of files that is
48+
// // obtained after fetching `url` and applying the inclusion rules given by
49+
// // `paths`.
50+
// //
51+
// // This field is the source of truth; packages do not come from a `url`; they
52+
// // come from a `hash`. `url` is just one of many possible mirrors for how to
53+
// // obtain a package matching this `hash`.
54+
// //
55+
// // Uses the [multihash](https://multiformats.io/multihash/) format.
56+
// .hash = "...",
57+
//
58+
// // When this is provided, the package is found in a directory relative to the
59+
// // build root. In this case the package's hash is irrelevant and therefore not
60+
// // computed. This field and `url` are mutually exclusive.
61+
// .path = "foo",
62+
//
63+
// // When this is set to `true`, a package is declared to be lazily
64+
// // fetched. This makes the dependency only get fetched if it is
65+
// // actually used.
66+
// .lazy = false,
67+
//},
68+
},
69+
70+
// Specifies the set of files and directories that are included in this package.
71+
// Only files and directories listed here are included in the `hash` that
72+
// is computed for this package. Only files listed here will remain on disk
73+
// when using the zig package manager. As a rule of thumb, one should list
74+
// files required for compilation plus any license(s).
75+
// Paths are relative to the build root. Use the empty string (`""`) to refer to
76+
// the build root itself.
77+
// A directory listed here means that all files within, recursively, are included.
78+
.paths = .{
79+
"build.zig",
80+
"build.zig.zon",
81+
"src",
82+
// For example...
83+
//"LICENSE",
84+
//"README.md",
85+
},
86+
}

‎src/httpfile/parser.zig‎

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
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+
}

‎src/main.zig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//! By convention, main.zig is where your main function lives in the case that
2+
//! you are building an executable. If you are making a library, the convention
3+
//! is to delete this file and start with root.zig instead.
4+
const HttpParser = @import("./httpfile/parser.zig");
5+
6+
pub fn main() !void {}

0 commit comments

Comments
 (0)