multipart: parse content disposition
This commit is contained in:
parent
5a0736cf2a
commit
aed35035f5
1 changed files with 27 additions and 5 deletions
32
src/main.zig
32
src/main.zig
|
@ -99,14 +99,15 @@ const Multipart = struct {
|
|||
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, '\r')).?;
|
||||
const maybe_boundary_raw = (try reader.readUntilDelimiterOrEof(&boundary_buffer, '\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");
|
||||
if (!std.mem.eql(u8, maybe_boundary_strip2, self.boundary)) {
|
||||
std.log.err("expected '{s}', got '{s}'", .{ self.boundary, maybe_boundary_strip2 });
|
||||
std.log.err("expected '{s}' {}, got '{s}' {}", .{ self.boundary, self.boundary.len, maybe_boundary_strip2, maybe_boundary_strip2.len });
|
||||
return error.InvalidBoundaryBody;
|
||||
}
|
||||
std.log.debug("got successful boundary {s}", .{maybe_boundary_strip2});
|
||||
|
||||
// from there ownwards, its just http!
|
||||
var parser = hzzp.parser.request.create(hzzp_buffer, reader);
|
||||
|
@ -117,9 +118,14 @@ 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)]});
|
||||
|
||||
while (try parser.next()) |event| {
|
||||
std.log.debug("got event: {}", .{event});
|
||||
switch (event) {
|
||||
.status => unreachable,
|
||||
.end => break,
|
||||
.head_done => {},
|
||||
.header => |header| {
|
||||
// TODO lowercase header name
|
||||
if (std.mem.eql(u8, header.name, "Content-Disposition")) {
|
||||
|
@ -132,18 +138,33 @@ const Multipart = struct {
|
|||
|
||||
while (disposition_it.next()) |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 = single_part_it.next().?;
|
||||
|
||||
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{
|
||||
.name = dispo_name,
|
||||
.filename = 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;
|
||||
std.log.debug("got content type for part! {s}", .{content_type});
|
||||
}
|
||||
},
|
||||
.end => break,
|
||||
else => @panic("shit"),
|
||||
else => {
|
||||
std.log.err("unexpected event: {}", .{event});
|
||||
@panic("shit");
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -169,7 +190,8 @@ fn uploadFile(response: *http.Response, request: http.Request) !void {
|
|||
if (content_type == null) return error.InvalidContentType;
|
||||
|
||||
// parse multipart data
|
||||
var multipart = try Multipart.init(request.body, content_type.?);
|
||||
var boundary_buffer: [512]u8 = undefined;
|
||||
var multipart = try Multipart.init(request.body, content_type.?, &boundary_buffer);
|
||||
var hzzp_buffer: [1024]u8 = undefined;
|
||||
|
||||
while (try multipart.next(&hzzp_buffer)) |part| {
|
||||
|
|
Loading…
Reference in a new issue