Compare commits

..

4 Commits

Author SHA1 Message Date
Luna c3c019f19f add test for single body 2021-04-13 21:32:36 -03:00
Luna 4287ae9c2a add allocator to handler, fix registry call 2021-04-13 21:32:24 -03:00
Luna fa4590171d fix bodies having CRLF 2021-04-13 21:32:12 -03:00
Luna 6a1f9a7a34 add global_allocator 2021-04-13 21:31:58 -03:00
1 changed files with 54 additions and 10 deletions

View File

@ -7,7 +7,8 @@ const fmt = std.fmt;
const images_dir_path = "./images"; 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 { pub fn main() anyerror!void {
std.log.info("welcome to webscale", .{}); std.log.info("welcome to webscale", .{});
@ -16,8 +17,8 @@ pub fn main() anyerror!void {
defer _ = gpa.deinit(); defer _ = gpa.deinit();
registry = mimetypes.Registry.init(std.heap.page_allocator); registry = mimetypes.Registry.init(std.heap.page_allocator);
defer registry.deinit(); defer registry.?.deinit();
try registry.load(); try registry.?.load();
// TODO: configurable addr via env var // TODO: configurable addr via env var
const bind_addr = try std.net.Address.parseIp("0.0.0.0", 8080); const bind_addr = try std.net.Address.parseIp("0.0.0.0", 8080);
@ -26,6 +27,8 @@ pub fn main() anyerror!void {
// TODO: configurable path via env var // TODO: configurable path via env var
try std.fs.cwd().makePath(images_dir_path); try std.fs.cwd().makePath(images_dir_path);
global_allocator = &gpa.allocator;
try http.listenAndServe( try http.listenAndServe(
&gpa.allocator, &gpa.allocator,
bind_addr, bind_addr,
@ -66,7 +69,7 @@ const ContentDisposition = struct {
const Self = @This(); const Self = @This();
pub fn deinit(self: *Self) void { pub fn deinit(self: *const Self) void {
self.allocator.free(self.name); self.allocator.free(self.name);
self.allocator.free(self.filename); self.allocator.free(self.filename);
} }
@ -92,7 +95,7 @@ const Part = struct {
const Self = @This(); const Self = @This();
pub fn deinit(self: *Self) void { pub fn deinit(self: *const Self) void {
self.disposition.deinit(); self.disposition.deinit();
self.allocator.free(self.content_type); self.allocator.free(self.content_type);
} }
@ -168,7 +171,7 @@ const Multipart = struct {
var content_disposition: ?ContentDisposition = null; var content_disposition: ?ContentDisposition = null;
var content_type: ?[]const u8 = 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| { while (try parser.next()) |event| {
std.log.debug("got event: {}", .{event}); std.log.debug("got event: {}", .{event});
@ -239,7 +242,8 @@ const Multipart = struct {
// read body until we find the boundary end marker (--{s} OR --{s}--) // read body until we find the boundary end marker (--{s} OR --{s}--)
var it = std.mem.split(remaining_body, self.boundary); var it = std.mem.split(remaining_body, self.boundary);
const body = it.next() orelse return error.MissingPartBody; const almost_actual_body = it.next() orelse return error.MissingPartBody;
const body = std.mem.trimRight(u8, almost_actual_body, "\r\n");
const next_boundary_pos = self.stream.pos + body.len; const next_boundary_pos = self.stream.pos + body.len;
const next_boundary_body = self.stream.buffer[next_boundary_pos..self.stream.buffer.len]; const next_boundary_body = self.stream.buffer[next_boundary_pos..self.stream.buffer.len];
@ -291,13 +295,14 @@ fn uploadFile(response: *http.Response, request: http.Request) !void {
var multipart = try Multipart.init(request.body, content_type.?, &boundary_buffer); var multipart = try Multipart.init(request.body, content_type.?, &boundary_buffer);
var hzzp_buffer: [1024]u8 = undefined; 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( std.log.info(
"got part from multipart request! name='{s}' filename='{s}' content_type='{s}' length={d}", "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 }, .{ 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; if (extensions == null) return error.InvalidContentMimeType;
const extension = extensions.?.items[0]; const extension = extensions.?.items[0];
@ -346,7 +351,7 @@ test "multipart" {
"Hello!\n"; "Hello!\n";
const PART2_REAL_BODY = const PART2_REAL_BODY =
"{\"status\": \"OK\"}\n"; "{\"status\": \"OK\"}";
const body = const body =
"--1234\r\n" ++ "--1234\r\n" ++
@ -402,3 +407,42 @@ test "multipart" {
try multipart.next(&hzzp_buffer, std.testing.allocator), 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),
);
}