multipart: create full boundary value

This commit is contained in:
Luna 2021-04-10 15:12:17 -03:00
parent 4ea0e086d9
commit 5a0736cf2a
1 changed files with 9 additions and 2 deletions

View File

@ -67,24 +67,31 @@ const Multipart = struct {
const Self = @This();
pub fn init(body: []const u8, content_type: []const u8) !Multipart {
// TODO: move boundary_buffer to allocator
pub fn init(body: []const u8, content_type: []const u8, boundary_buffer: []u8) !Multipart {
// parse content_type into what we want (the boundary)
var it = std.mem.split(content_type, ";");
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"))
return error.InvalidContentType;
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="))
return error.InvalidBoundary;
var boundary_it = std.mem.split(should_be_boundary, "=");
_ = boundary_it.next();
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{
.stream = StreamT{ .buffer = body, .pos = 0 },
.boundary = boundary_value,
.boundary = actual_boundary_value,
};
}