2021-04-09 20:57:18 +00:00
|
|
|
const std = @import("std");
|
2021-04-09 21:11:44 +00:00
|
|
|
const http = @import("apple_pie");
|
2021-04-10 05:03:43 +00:00
|
|
|
const hzzp = @import("hzzp");
|
2021-04-09 20:57:18 +00:00
|
|
|
|
2021-04-09 21:30:32 +00:00
|
|
|
const images_dir_path = "./images";
|
|
|
|
|
2021-04-09 20:57:18 +00:00
|
|
|
pub fn main() anyerror!void {
|
2021-04-10 05:03:43 +00:00
|
|
|
std.log.info("welcome to webscale", .{});
|
2021-04-09 21:11:44 +00:00
|
|
|
|
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
|
|
defer _ = gpa.deinit();
|
|
|
|
|
2021-04-10 05:03:43 +00:00
|
|
|
// TODO: configurable addr via env var
|
2021-04-09 21:30:32 +00:00
|
|
|
const bind_addr = try std.net.Address.parseIp("0.0.0.0", 8080);
|
|
|
|
std.log.info("serving on {}", .{bind_addr});
|
|
|
|
|
|
|
|
// TODO: configurable path via env var
|
|
|
|
try std.fs.cwd().makePath(images_dir_path);
|
|
|
|
|
2021-04-09 21:11:44 +00:00
|
|
|
try http.listenAndServe(
|
|
|
|
&gpa.allocator,
|
2021-04-09 21:30:32 +00:00
|
|
|
bind_addr,
|
|
|
|
comptime http.router.router(&[_]http.router.Route{
|
|
|
|
http.router.get("/", index),
|
|
|
|
http.router.post("/api/upload", uploadFile),
|
2021-04-09 21:33:21 +00:00
|
|
|
http.router.get("/i/:filename", fetchFile),
|
2021-04-09 21:30:32 +00:00
|
|
|
}),
|
2021-04-09 21:11:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index(response: *http.Response, request: http.Request) !void {
|
|
|
|
try response.writer().writeAll("Hello Zig!");
|
2021-04-09 20:57:18 +00:00
|
|
|
}
|
2021-04-09 21:30:32 +00:00
|
|
|
|
|
|
|
fn generateImageId(buffer: []u8) []const u8 {
|
|
|
|
var i: usize = 0;
|
|
|
|
|
|
|
|
const seed = @truncate(u64, @bitCast(u128, std.time.nanoTimestamp()));
|
|
|
|
var r = std.rand.DefaultPrng.init(seed);
|
|
|
|
|
|
|
|
while (i < 16) : (i += 1) {
|
|
|
|
// random ascii lowercase char
|
|
|
|
var idx = @intCast(u8, r.random.uintLessThan(u5, 24));
|
|
|
|
var letter = @as(u8, 97) + idx;
|
|
|
|
buffer[i] = letter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer[0..i];
|
|
|
|
}
|
|
|
|
|
2021-04-10 05:03:43 +00:00
|
|
|
const StreamT = std.io.FixedBufferStream([]const u8);
|
|
|
|
|
|
|
|
const ContentDisposition = struct {
|
|
|
|
name: []const u8,
|
|
|
|
filename: []const u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
const Part = struct {
|
|
|
|
disposition: ContentDisposition,
|
|
|
|
content_type: []const u8,
|
|
|
|
};
|
2021-04-10 02:32:59 +00:00
|
|
|
|
|
|
|
const Multipart = struct {
|
2021-04-10 05:03:43 +00:00
|
|
|
stream: StreamT,
|
2021-04-10 02:32:59 +00:00
|
|
|
boundary: []const u8,
|
|
|
|
cursor: usize = 0,
|
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
2021-04-10 18:12:17 +00:00
|
|
|
// TODO: move boundary_buffer to allocator
|
|
|
|
pub fn init(body: []const u8, content_type: []const u8, boundary_buffer: []u8) !Multipart {
|
2021-04-10 02:32:59 +00:00
|
|
|
// 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;
|
2021-04-10 18:12:17 +00:00
|
|
|
std.log.debug("should be multipart: {s}", .{should_be_multipart});
|
2021-04-10 02:32:59 +00:00
|
|
|
if (!std.mem.eql(u8, should_be_multipart, "multipart/form-data"))
|
|
|
|
return error.InvalidContentType;
|
|
|
|
|
|
|
|
const should_be_boundary = it.next() orelse return error.MissingBoundary;
|
2021-04-10 18:12:17 +00:00
|
|
|
std.log.debug("should be boundary: {s} {d}", .{ should_be_boundary, should_be_boundary.len });
|
2021-04-10 02:32:59 +00:00
|
|
|
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;
|
2021-04-10 18:12:17 +00:00
|
|
|
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 });
|
2021-04-10 02:32:59 +00:00
|
|
|
|
2021-04-10 05:03:43 +00:00
|
|
|
return Self{
|
|
|
|
.stream = StreamT{ .buffer = body, .pos = 0 },
|
2021-04-10 18:12:17 +00:00
|
|
|
.boundary = actual_boundary_value,
|
2021-04-10 05:03:43 +00:00
|
|
|
};
|
2021-04-10 02:32:59 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 05:03:43 +00:00
|
|
|
pub fn next(self: *Self, hzzp_buffer: []u8) !?Part {
|
|
|
|
var reader = self.stream.reader();
|
|
|
|
// first self.boundary.len+2 bytes MUST be boundary + \r + \n
|
|
|
|
var boundary_buffer: [512]u8 = undefined;
|
2021-04-10 18:12:38 +00:00
|
|
|
const maybe_boundary_raw = (try reader.readUntilDelimiterOrEof(&boundary_buffer, '\n')).?;
|
2021-04-10 05:03:43 +00:00
|
|
|
|
|
|
|
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)) {
|
2021-04-10 18:12:38 +00:00
|
|
|
std.log.err("expected '{s}' {}, got '{s}' {}", .{ self.boundary, self.boundary.len, maybe_boundary_strip2, maybe_boundary_strip2.len });
|
2021-04-10 05:03:43 +00:00
|
|
|
return error.InvalidBoundaryBody;
|
|
|
|
}
|
2021-04-10 18:12:38 +00:00
|
|
|
std.log.debug("got successful boundary {s}", .{maybe_boundary_strip2});
|
2021-04-10 05:03:43 +00:00
|
|
|
|
|
|
|
// from there ownwards, its just http!
|
|
|
|
var parser = hzzp.parser.request.create(hzzp_buffer, reader);
|
|
|
|
|
|
|
|
// This is a hack so that it doesnt try to parse an http header.
|
|
|
|
parser.state = .header;
|
|
|
|
|
|
|
|
var content_disposition: ?ContentDisposition = null;
|
|
|
|
var content_type: ?[]const u8 = null;
|
|
|
|
|
2021-04-10 18:12:38 +00:00
|
|
|
std.log.debug("next bytes: {any}", .{self.stream.buffer[self.stream.pos..(self.stream.pos + 50)]});
|
|
|
|
|
2021-04-10 05:03:43 +00:00
|
|
|
while (try parser.next()) |event| {
|
2021-04-10 18:12:38 +00:00
|
|
|
std.log.debug("got event: {}", .{event});
|
2021-04-10 05:03:43 +00:00
|
|
|
switch (event) {
|
|
|
|
.status => unreachable,
|
2021-04-10 18:12:38 +00:00
|
|
|
.end => break,
|
|
|
|
.head_done => {},
|
2021-04-10 05:03:43 +00:00
|
|
|
.header => |header| {
|
|
|
|
// TODO lowercase header name
|
|
|
|
if (std.mem.eql(u8, header.name, "Content-Disposition")) {
|
|
|
|
// parse disposition
|
|
|
|
var disposition_it = std.mem.split(header.value, ";");
|
|
|
|
_ = disposition_it.next();
|
|
|
|
|
|
|
|
var dispo_name: []const u8 = undefined;
|
|
|
|
var dispo_filename: []const u8 = undefined;
|
|
|
|
|
|
|
|
while (disposition_it.next()) |disposition_part_raw| {
|
|
|
|
const disposition_part = std.mem.trim(u8, disposition_part_raw, " ");
|
2021-04-10 18:12:38 +00:00
|
|
|
|
|
|
|
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().?;
|
2021-04-10 18:29:15 +00:00
|
|
|
const inner_part_value_quoted = single_part_it.next().?;
|
|
|
|
|
|
|
|
const inner_part_value = std.mem.trim(u8, inner_part_value_quoted, "\"");
|
2021-04-10 18:12:38 +00:00
|
|
|
|
|
|
|
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;
|
2021-04-10 05:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
content_disposition = ContentDisposition{
|
|
|
|
.name = dispo_name,
|
|
|
|
.filename = dispo_filename,
|
|
|
|
};
|
2021-04-10 18:12:38 +00:00
|
|
|
std.log.debug("got content disposition for part! {}", .{content_disposition});
|
2021-04-10 05:03:43 +00:00
|
|
|
} else if (std.mem.eql(u8, header.name, "Content-Type")) {
|
|
|
|
content_type = header.value;
|
2021-04-10 18:12:38 +00:00
|
|
|
std.log.debug("got content type for part! {s}", .{content_type});
|
2021-04-10 05:03:43 +00:00
|
|
|
}
|
|
|
|
},
|
2021-04-10 18:12:38 +00:00
|
|
|
else => {
|
|
|
|
std.log.err("unexpected event: {}", .{event});
|
|
|
|
@panic("shit");
|
|
|
|
},
|
2021-04-10 05:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Part{
|
|
|
|
.disposition = content_disposition.?,
|
|
|
|
.content_type = content_type.?,
|
|
|
|
};
|
2021-04-10 02:32:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-09 21:30:32 +00:00
|
|
|
fn uploadFile(response: *http.Response, request: http.Request) !void {
|
|
|
|
std.log.info("upload! got {d} bytes", .{request.body.len});
|
|
|
|
|
2021-04-10 02:32:59 +00:00
|
|
|
// 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
|
2021-04-10 18:12:38 +00:00
|
|
|
var boundary_buffer: [512]u8 = undefined;
|
|
|
|
var multipart = try Multipart.init(request.body, content_type.?, &boundary_buffer);
|
2021-04-10 05:03:43 +00:00
|
|
|
var hzzp_buffer: [1024]u8 = undefined;
|
2021-04-10 02:32:59 +00:00
|
|
|
|
2021-04-10 05:03:43 +00:00
|
|
|
while (try multipart.next(&hzzp_buffer)) |part| {
|
2021-04-10 02:32:59 +00:00
|
|
|
std.log.info("part: {}", .{part});
|
|
|
|
}
|
|
|
|
|
2021-04-09 21:30:32 +00:00
|
|
|
var image_id_buffer: [256]u8 = undefined;
|
|
|
|
const image_id = generateImageId(&image_id_buffer);
|
|
|
|
|
|
|
|
var image_path_buffer: [512]u8 = undefined;
|
|
|
|
const image_path = try std.fmt.bufPrint(
|
|
|
|
&image_path_buffer,
|
|
|
|
"{s}/{s}.jpg",
|
|
|
|
.{ images_dir_path, image_id },
|
|
|
|
);
|
|
|
|
|
|
|
|
const image_file = try std.fs.cwd().createFile(image_path, .{});
|
|
|
|
try image_file.writer().writeAll(request.body);
|
|
|
|
|
|
|
|
try response.writer().writeAll(image_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fetchFile(response: *http.Response, request: http.Request, filename: []const u8) !void {
|
|
|
|
std.log.info("got name: {s}", .{filename});
|
2021-04-09 21:33:21 +00:00
|
|
|
var image_path_buffer: [512]u8 = undefined;
|
|
|
|
|
2021-04-09 21:30:32 +00:00
|
|
|
const images_dir = try std.fs.cwd().openDir(images_dir_path, .{});
|
2021-04-09 21:33:21 +00:00
|
|
|
const image_path = try std.fmt.bufPrint(
|
|
|
|
&image_path_buffer,
|
|
|
|
"{s}/{s}",
|
|
|
|
.{ images_dir_path, filename },
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO return 404 on error
|
|
|
|
const image_file = try std.fs.cwd().openFile(image_path, .{ .read = false });
|
|
|
|
while (true) {
|
|
|
|
var file_write_buffer: [1024]u8 = undefined;
|
|
|
|
const bytes_read = try image_file.read(&file_write_buffer);
|
|
|
|
if (bytes_read == 0) return;
|
|
|
|
try response.writer().writeAll(&file_write_buffer);
|
|
|
|
}
|
2021-04-09 21:30:32 +00:00
|
|
|
}
|