Compare commits

...

12 Commits

3 changed files with 204 additions and 17 deletions

View File

@ -18,6 +18,10 @@ pub fn build(b: *std.build.Builder) void {
deps.addAllTo(exe);
exe.install();
const exe_test = b.addTest("src/main.zig");
exe_test.setTarget(target);
deps.addAllTo(exe_test);
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
@ -26,4 +30,7 @@ pub fn build(b: *std.build.Builder) void {
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const test_step = b.step("test", "Test stuff");
test_step.dependOn(&exe_test.step);
}

View File

@ -3,9 +3,12 @@ const http = @import("apple_pie");
const hzzp = @import("hzzp");
const mimetypes = @import("mimetypes");
const fmt = std.fmt;
const images_dir_path = "./images";
var registry: mimetypes.Registry = undefined;
var registry: ?mimetypes.Registry = null;
var global_allocator: ?*std.mem.Allocator = null;
pub fn main() anyerror!void {
std.log.info("welcome to webscale", .{});
@ -14,8 +17,8 @@ pub fn main() anyerror!void {
defer _ = gpa.deinit();
registry = mimetypes.Registry.init(std.heap.page_allocator);
defer registry.deinit();
try registry.load();
defer registry.?.deinit();
try registry.?.load();
// TODO: configurable addr via env var
const bind_addr = try std.net.Address.parseIp("0.0.0.0", 8080);
@ -24,6 +27,8 @@ pub fn main() anyerror!void {
// TODO: configurable path via env var
try std.fs.cwd().makePath(images_dir_path);
global_allocator = &gpa.allocator;
try http.listenAndServe(
&gpa.allocator,
bind_addr,
@ -58,14 +63,54 @@ fn generateImageId(buffer: []u8) []const u8 {
const StreamT = std.io.FixedBufferStream([]const u8);
const ContentDisposition = struct {
allocator: *std.mem.Allocator,
name: []const u8,
filename: []const u8,
const Self = @This();
pub fn deinit(self: *const Self) void {
self.allocator.free(self.name);
self.allocator.free(self.filename);
}
pub fn format(self: Self, comptime f: []const u8, options: fmt.FormatOptions, writer: anytype) !void {
if (f.len != 0) {
@compileError("Unknown format character: '" ++ f ++ "'");
}
return fmt.format(
writer,
"Disposition{{.name='{s}', .filename='{s}'}}",
.{ self.name, self.filename },
);
}
};
const Part = struct {
allocator: *std.mem.Allocator,
disposition: ContentDisposition,
content_type: []const u8,
body: []const u8,
const Self = @This();
pub fn deinit(self: *const Self) void {
self.disposition.deinit();
self.allocator.free(self.content_type);
}
pub fn format(self: Self, comptime f: []const u8, options: fmt.FormatOptions, writer: anytype) !void {
if (f.len != 0) {
@compileError("Unknown format character: '" ++ f ++ "'");
}
return fmt.format(
writer,
"Part{{.content_type='{s}', .disposition={}, .body='{s}'}}",
.{ self.content_type, self.disposition, self.body },
);
}
};
const Multipart = struct {
@ -103,11 +148,11 @@ const Multipart = struct {
};
}
pub fn next(self: *Self, hzzp_buffer: []u8) !?Part {
pub fn next(self: *Self, hzzp_buffer: []u8, allocator: *std.mem.Allocator) !?Part {
var reader = self.stream.reader();
// first self.boundary.len+2 bytes MUST be boundary + \r + \n
var boundary_buffer: [512]u8 = undefined;
const maybe_boundary_raw = (try reader.readUntilDelimiterOrEof(&boundary_buffer, '\n')).?;
const maybe_boundary_raw = (try reader.readUntilDelimiterOrEof(&boundary_buffer, '\n')) orelse return null;
const maybe_boundary_strip1 = std.mem.trimRight(u8, maybe_boundary_raw, "\n");
const maybe_boundary_strip2 = std.mem.trimRight(u8, maybe_boundary_strip1, "\r");
@ -126,7 +171,7 @@ const Multipart = struct {
var content_disposition: ?ContentDisposition = null;
var content_type: ?[]const u8 = null;
std.log.debug("next bytes: {any}", .{self.stream.buffer[self.stream.pos..(self.stream.pos + 50)]});
std.log.debug("next bytes: {s}", .{self.stream.buffer[self.stream.pos..(self.stream.pos + 50)]});
while (try parser.next()) |event| {
std.log.debug("got event: {}", .{event});
@ -162,12 +207,13 @@ const Multipart = struct {
}
content_disposition = ContentDisposition{
.name = dispo_name,
.filename = dispo_filename,
.allocator = allocator,
.name = try std.mem.dupe(allocator, u8, dispo_name),
.filename = try std.mem.dupe(allocator, u8, dispo_filename),
};
std.log.debug("got content disposition for part! {}", .{content_disposition});
} else if (std.mem.eql(u8, header.name, "Content-Type")) {
content_type = header.value;
content_type = try std.mem.dupe(allocator, u8, header.value);
std.log.debug("got content type for part! {s}", .{content_type});
}
},
@ -185,18 +231,49 @@ const Multipart = struct {
// we can use the fact that we know the reader is FixedBufferStream
// to extract the remaining body, then trim the boundary!
//
// THIS ASSUMES ONLY ONE FILE IS IN THE WIRE.
//
// when we find a marker, we also need to know if its an ending marker,
// because the multipart files are prefixed with the boundary, not suffixed.
//
// bc of that we need to set the reader so that its directly on top of
// the next boundary marker for the next file to work
const remaining_body = self.stream.buffer[self.stream.pos..self.stream.buffer.len];
var end_boundary_buf: [512]u8 = undefined;
const boundary_end_marker = try std.fmt.bufPrint(&end_boundary_buf, "{s}--\r\n", .{self.boundary});
const actual_body = std.mem.trim(u8, remaining_body, boundary_end_marker);
// read body until we find the boundary end marker (--{s} OR --{s}--)
var it = std.mem.split(remaining_body, self.boundary);
const almost_actual_body = it.next() orelse return error.MissingPartBody;
var body_it = std.mem.split(almost_actual_body, "\r\n");
const body = body_it.next().?;
const next_boundary_pos = self.stream.pos + body.len;
const next_boundary_body = self.stream.buffer[next_boundary_pos..self.stream.buffer.len];
// check out on the next 2 chars
const possible_end = it.next() orelse return error.MissingNextPrefixOrEndSuffix;
if (std.mem.startsWith(u8, possible_end, "--")) {
// we just got the ending boundary marker. the reader should be disabled
// for future reads.
self.stream.pos = self.stream.buffer.len;
return Part{
.allocator = allocator,
.disposition = content_disposition.?,
.content_type = content_type.?,
.body = body,
};
}
// there is a next file, the reader should be shifted forward to the
// boundary marker
self.stream.pos = self.stream.pos + body.len;
return Part{
.allocator = allocator,
.disposition = content_disposition.?,
.content_type = content_type.?,
.body = actual_body,
.body = body,
};
}
};
@ -220,13 +297,14 @@ fn uploadFile(response: *http.Response, request: http.Request) !void {
var multipart = try Multipart.init(request.body, content_type.?, &boundary_buffer);
var hzzp_buffer: [1024]u8 = undefined;
while (try multipart.next(&hzzp_buffer)) |part| {
while (try multipart.next(&hzzp_buffer, global_allocator.?)) |part| {
defer part.deinit();
std.log.info(
"got part from multipart request! name='{s}' filename='{s}' content_type='{s}' length={d}",
.{ part.disposition.name, part.disposition.filename, part.content_type, part.body.len },
);
var extensions = registry.getExtensionsByType(part.content_type);
var extensions = registry.?.getExtensionsByType(part.content_type);
if (extensions == null) return error.InvalidContentMimeType;
const extension = extensions.?.items[0];
@ -268,3 +346,105 @@ fn fetchFile(response: *http.Response, request: http.Request, filename: []const
try response.writer().writeAll(&file_write_buffer);
}
}
pub const log_level: std.log.Level = .debug;
test "multipart" {
const PART1_REAL_BODY =
"Hello!\n";
const PART2_REAL_BODY =
"{\"status\": \"OK\"}";
const body =
"--1234\r\n" ++
"Content-Type: text/plain\r\n" ++
"Content-Disposition: form-data; name=file1; filename=ab.txt\r\n" ++
"\r\n" ++
PART1_REAL_BODY ++
"--1234\r\n" ++
"Content-Type: application/json\r\n" ++
// TODO: add 'content-type' support to content-disposition as well
"Content-Disposition: form-data; name=file2; filename=data.json\r\n" ++
"\r\n" ++
PART2_REAL_BODY ++
"--1234--\r\n";
var buf: [512]u8 = undefined;
var multipart = try Multipart.init(
body,
"multipart/form-data; boundary=1234",
&buf,
);
var hzzp_buffer: [1024]u8 = undefined;
var part1 = (try multipart.next(&hzzp_buffer, std.testing.allocator)).?;
defer part1.deinit();
std.debug.warn(
"\npart={}\n",
.{part1},
);
std.testing.expectEqualSlices(u8, "text/plain", part1.content_type);
std.testing.expectEqualSlices(u8, "file1", part1.disposition.name);
std.testing.expectEqualSlices(u8, "ab.txt", part1.disposition.filename);
std.testing.expectEqualSlices(u8, PART1_REAL_BODY, part1.body);
var part2 = (try multipart.next(&hzzp_buffer, std.testing.allocator)).?;
defer part2.deinit();
std.debug.warn(
"\npart2={}\n",
.{part2},
);
std.testing.expectEqualSlices(u8, "application/json", part2.content_type);
std.testing.expectEqualSlices(u8, "file2", part2.disposition.name);
std.testing.expectEqualSlices(u8, "data.json", part2.disposition.filename);
std.testing.expectEqualSlices(u8, PART2_REAL_BODY, part2.body);
// stop the loop (if there were any) afterwards
std.testing.expectEqual(
@as(?Part, null),
try multipart.next(&hzzp_buffer, std.testing.allocator),
);
}
test "multipart single file" {
const PART1_REAL_BODY =
"Hello!";
const body =
"--1234\r\n" ++
"Content-Type: text/plain\r\n" ++
"Content-Disposition: form-data; name=file1; filename=ab.txt\r\n" ++
"\r\n" ++
PART1_REAL_BODY ++
"--1234--\r\n";
var buf: [512]u8 = undefined;
var multipart = try Multipart.init(
body,
"multipart/form-data; boundary=1234",
&buf,
);
var hzzp_buffer: [1024]u8 = undefined;
var part1 = (try multipart.next(&hzzp_buffer, std.testing.allocator)).?;
defer part1.deinit();
std.debug.warn(
"\npart={}\n",
.{part1},
);
std.testing.expectEqualSlices(u8, "text/plain", part1.content_type);
std.testing.expectEqualSlices(u8, "file1", part1.disposition.name);
std.testing.expectEqualSlices(u8, "ab.txt", part1.disposition.filename);
std.testing.expectEqualSlices(u8, PART1_REAL_BODY, part1.body);
std.testing.expectEqual(
@as(?Part, null),
try multipart.next(&hzzp_buffer, std.testing.allocator),
);
}

View File

@ -3,7 +3,7 @@ name: yet-another-pomf-clone
main: src/main.zig
dev_dependencies:
- src: git https://github.com/Luukdegram/apple_pie
- src: git https://github.com/lun-4/apple_pie
name: apple_pie
main: src/apple_pie.zig