diff --git a/src/main.zig b/src/main.zig index 6d313d2..132b65e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -46,9 +46,59 @@ fn generateImageId(buffer: []u8) []const u8 { return buffer[0..i]; } +const Part = struct {}; + +const Multipart = struct { + body: []const u8, + boundary: []const u8, + cursor: usize = 0, + + const Self = @This(); + + pub fn init(body: []const u8, content_type: []const 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; + if (!std.mem.eql(u8, should_be_multipart, "multipart/form-data")) + return error.InvalidContentType; + + const should_be_boundary = it.next() orelse return error.MissingBoundary; + 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; + + return Self{ .body = body, .boundary = boundary_value }; + } + + pub fn next(self: *Self) !?Part { + return null; + } +}; + fn uploadFile(response: *http.Response, request: http.Request) !void { std.log.info("upload! got {d} bytes", .{request.body.len}); + // find content-type header + var it = request.iterator(); + var content_type: ?[]const u8 = null; + while (it.next()) |header| { + if (std.mem.eql(u8, header.key, "Content-Type")) { + content_type = header.value; + } + } + + if (content_type == null) return error.InvalidContentType; + + // parse multipart data + var multipart = try Multipart.init(request.body, content_type.?); + + while (try multipart.next()) |part| { + std.log.info("part: {}", .{part}); + } + var image_id_buffer: [256]u8 = undefined; const image_id = generateImageId(&image_id_buffer);