Compare commits
No commits in common. "a44af53a8785c57d7aa390cff74bda26788b7d18" and "4ea0e086d930c7cbf20db56d053722ae34b0c0c1" have entirely different histories.
a44af53a87
...
4ea0e086d9
1 changed files with 23 additions and 75 deletions
72
src/main.zig
72
src/main.zig
|
@ -58,7 +58,6 @@ const ContentDisposition = struct {
|
||||||
const Part = struct {
|
const Part = struct {
|
||||||
disposition: ContentDisposition,
|
disposition: ContentDisposition,
|
||||||
content_type: []const u8,
|
content_type: []const u8,
|
||||||
body: []const u8,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const Multipart = struct {
|
const Multipart = struct {
|
||||||
|
@ -68,31 +67,24 @@ const Multipart = struct {
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
// TODO: move boundary_buffer to allocator
|
pub fn init(body: []const u8, content_type: []const u8) !Multipart {
|
||||||
pub fn init(body: []const u8, content_type: []const u8, boundary_buffer: []u8) !Multipart {
|
|
||||||
// parse content_type into what we want (the boundary)
|
// parse content_type into what we want (the boundary)
|
||||||
var it = std.mem.split(content_type, ";");
|
var it = std.mem.split(content_type, ";");
|
||||||
const should_be_multipart = it.next() orelse return error.MissingContentType;
|
const should_be_multipart = it.next() orelse return error.MissingContentType;
|
||||||
std.log.debug("should be multipart: {s}", .{should_be_multipart});
|
|
||||||
if (!std.mem.eql(u8, should_be_multipart, "multipart/form-data"))
|
if (!std.mem.eql(u8, should_be_multipart, "multipart/form-data"))
|
||||||
return error.InvalidContentType;
|
return error.InvalidContentType;
|
||||||
|
|
||||||
const should_be_boundary = it.next() orelse return error.MissingBoundary;
|
const should_be_boundary = it.next() orelse return error.MissingBoundary;
|
||||||
std.log.debug("should be boundary: {s} {d}", .{ should_be_boundary, should_be_boundary.len });
|
|
||||||
if (!std.mem.startsWith(u8, should_be_boundary, " boundary="))
|
if (!std.mem.startsWith(u8, should_be_boundary, " boundary="))
|
||||||
return error.InvalidBoundary;
|
return error.InvalidBoundary;
|
||||||
|
|
||||||
var boundary_it = std.mem.split(should_be_boundary, "=");
|
var boundary_it = std.mem.split(should_be_boundary, "=");
|
||||||
_ = boundary_it.next();
|
_ = boundary_it.next();
|
||||||
const boundary_value = boundary_it.next() orelse return error.InvalidBoundary;
|
const boundary_value = boundary_it.next() orelse return error.InvalidBoundary;
|
||||||
std.log.debug("boundary value: {s} {d}", .{ boundary_value, boundary_value.len });
|
|
||||||
|
|
||||||
const actual_boundary_value = try std.fmt.bufPrint(boundary_buffer, "--{s}", .{boundary_value});
|
|
||||||
std.log.debug("actual boundary value: {s} {d}", .{ actual_boundary_value, actual_boundary_value.len });
|
|
||||||
|
|
||||||
return Self{
|
return Self{
|
||||||
.stream = StreamT{ .buffer = body, .pos = 0 },
|
.stream = StreamT{ .buffer = body, .pos = 0 },
|
||||||
.boundary = actual_boundary_value,
|
.boundary = boundary_value,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,15 +92,14 @@ const Multipart = struct {
|
||||||
var reader = self.stream.reader();
|
var reader = self.stream.reader();
|
||||||
// first self.boundary.len+2 bytes MUST be boundary + \r + \n
|
// first self.boundary.len+2 bytes MUST be boundary + \r + \n
|
||||||
var boundary_buffer: [512]u8 = undefined;
|
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, '\r')).?;
|
||||||
|
|
||||||
const maybe_boundary_strip1 = std.mem.trimRight(u8, maybe_boundary_raw, "\n");
|
const maybe_boundary_strip1 = std.mem.trimRight(u8, maybe_boundary_raw, "\n");
|
||||||
const maybe_boundary_strip2 = std.mem.trimRight(u8, maybe_boundary_strip1, "\r");
|
const maybe_boundary_strip2 = std.mem.trimRight(u8, maybe_boundary_strip1, "\r");
|
||||||
if (!std.mem.eql(u8, maybe_boundary_strip2, self.boundary)) {
|
if (!std.mem.eql(u8, maybe_boundary_strip2, self.boundary)) {
|
||||||
std.log.err("expected '{s}' {}, got '{s}' {}", .{ self.boundary, self.boundary.len, maybe_boundary_strip2, maybe_boundary_strip2.len });
|
std.log.err("expected '{s}', got '{s}'", .{ self.boundary, maybe_boundary_strip2 });
|
||||||
return error.InvalidBoundaryBody;
|
return error.InvalidBoundaryBody;
|
||||||
}
|
}
|
||||||
std.log.debug("got successful boundary {s}", .{maybe_boundary_strip2});
|
|
||||||
|
|
||||||
// from there ownwards, its just http!
|
// from there ownwards, its just http!
|
||||||
var parser = hzzp.parser.request.create(hzzp_buffer, reader);
|
var parser = hzzp.parser.request.create(hzzp_buffer, reader);
|
||||||
|
@ -119,14 +110,9 @@ 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)]});
|
|
||||||
|
|
||||||
while (try parser.next()) |event| {
|
while (try parser.next()) |event| {
|
||||||
std.log.debug("got event: {}", .{event});
|
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.status => unreachable,
|
.status => unreachable,
|
||||||
.end => break,
|
|
||||||
.head_done => {},
|
|
||||||
.header => |header| {
|
.header => |header| {
|
||||||
// TODO lowercase header name
|
// TODO lowercase header name
|
||||||
if (std.mem.eql(u8, header.name, "Content-Disposition")) {
|
if (std.mem.eql(u8, header.name, "Content-Disposition")) {
|
||||||
|
@ -139,57 +125,24 @@ const Multipart = struct {
|
||||||
|
|
||||||
while (disposition_it.next()) |disposition_part_raw| {
|
while (disposition_it.next()) |disposition_part_raw| {
|
||||||
const disposition_part = std.mem.trim(u8, disposition_part_raw, " ");
|
const disposition_part = std.mem.trim(u8, disposition_part_raw, " ");
|
||||||
|
|
||||||
if (std.mem.eql(u8, disposition_part, "form-data")) continue;
|
|
||||||
|
|
||||||
// we have an A=B thing
|
|
||||||
var single_part_it = std.mem.split(disposition_part, "=");
|
|
||||||
|
|
||||||
const inner_part_name = single_part_it.next().?;
|
|
||||||
const inner_part_value_quoted = single_part_it.next().?;
|
|
||||||
|
|
||||||
const inner_part_value = std.mem.trim(u8, inner_part_value_quoted, "\"");
|
|
||||||
|
|
||||||
if (std.mem.eql(u8, inner_part_name, "name")) dispo_name = inner_part_value;
|
|
||||||
if (std.mem.eql(u8, inner_part_name, "filename")) dispo_filename = inner_part_value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
content_disposition = ContentDisposition{
|
content_disposition = ContentDisposition{
|
||||||
.name = dispo_name,
|
.name = dispo_name,
|
||||||
.filename = dispo_filename,
|
.filename = dispo_filename,
|
||||||
};
|
};
|
||||||
std.log.debug("got content disposition for part! {}", .{content_disposition});
|
|
||||||
} else if (std.mem.eql(u8, header.name, "Content-Type")) {
|
} else if (std.mem.eql(u8, header.name, "Content-Type")) {
|
||||||
content_type = header.value;
|
content_type = header.value;
|
||||||
std.log.debug("got content type for part! {s}", .{content_type});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
else => {
|
.end => break,
|
||||||
std.log.err("unexpected event: {}", .{event});
|
else => @panic("shit"),
|
||||||
@panic("shit");
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// the rest of the reader until we find a matching boundary is the part body.
|
|
||||||
// hzzp does not do it for us because it cant find a body encoding
|
|
||||||
// (content-length, content-encoding)
|
|
||||||
//
|
|
||||||
// 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.
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
return Part{
|
return Part{
|
||||||
.disposition = content_disposition.?,
|
.disposition = content_disposition.?,
|
||||||
.content_type = content_type.?,
|
.content_type = content_type.?,
|
||||||
.body = actual_body,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -209,15 +162,12 @@ fn uploadFile(response: *http.Response, request: http.Request) !void {
|
||||||
if (content_type == null) return error.InvalidContentType;
|
if (content_type == null) return error.InvalidContentType;
|
||||||
|
|
||||||
// parse multipart data
|
// parse multipart data
|
||||||
var boundary_buffer: [512]u8 = undefined;
|
var multipart = try Multipart.init(request.body, content_type.?);
|
||||||
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)) |part| {
|
||||||
std.log.info(
|
std.log.info("part: {}", .{part});
|
||||||
"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 image_id_buffer: [256]u8 = undefined;
|
var image_id_buffer: [256]u8 = undefined;
|
||||||
const image_id = generateImageId(&image_id_buffer);
|
const image_id = generateImageId(&image_id_buffer);
|
||||||
|
@ -230,11 +180,9 @@ fn uploadFile(response: *http.Response, request: http.Request) !void {
|
||||||
);
|
);
|
||||||
|
|
||||||
const image_file = try std.fs.cwd().createFile(image_path, .{});
|
const image_file = try std.fs.cwd().createFile(image_path, .{});
|
||||||
try image_file.writer().writeAll(part.body);
|
try image_file.writer().writeAll(request.body);
|
||||||
|
|
||||||
try response.writer().writeAll(image_path);
|
try response.writer().writeAll(image_path);
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fetchFile(response: *http.Response, request: http.Request, filename: []const u8) !void {
|
fn fetchFile(response: *http.Response, request: http.Request, filename: []const u8) !void {
|
||||||
|
|
Loading…
Reference in a new issue