diff options
author | 2021-05-23 10:05:21 -0700 | |
---|---|---|
committer | 2021-05-23 10:05:21 -0700 | |
commit | ac4ac8f5a85bcfe723d9319920b0d675da4ecf5d (patch) | |
tree | a8094ddd8005ddaf5a0c078c25d763646f1d948b | |
parent | 45b55a897022b6f8dc052cd6d43183ce56951bd6 (diff) | |
download | bun-ac4ac8f5a85bcfe723d9319920b0d675da4ecf5d.tar.gz bun-ac4ac8f5a85bcfe723d9319920b0d675da4ecf5d.tar.zst bun-ac4ac8f5a85bcfe723d9319920b0d675da4ecf5d.zip |
muck
-rw-r--r-- | src/http.zig | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/src/http.zig b/src/http.zig index 4f2a4ba2a..790bdbd2b 100644 --- a/src/http.zig +++ b/src/http.zig @@ -121,26 +121,31 @@ pub const Server = struct { _ = writer.write("\r\n") catch 0; - _ = try ctx.conn.client.write(writer.getWritten(), SOCKET_FLAGS); + _ = try ctx.writeSocket(writer.getWritten(), SOCKET_FLAGS); + } + + pub fn writeSocket(ctx: *RequestContext, buf: anytype, flags: anytype) !usize { + ctx.conn.client.setWriteBufferSize(@intCast(u32, buf.len)) catch {}; + return ctx.conn.client.write(buf, SOCKET_FLAGS); } pub fn writeBodyBuf(ctx: *RequestContext, body: []const u8) void { - _ = ctx.conn.client.write(body, SOCKET_FLAGS) catch 0; + _ = ctx.writeSocket(body, SOCKET_FLAGS) catch 0; } pub fn writeStatus(ctx: *RequestContext, comptime code: HTTPStatusCode) !void { - _ = try ctx.conn.client.write(comptime printStatusLine(code), SOCKET_FLAGS); + _ = try ctx.writeSocket(comptime printStatusLine(code), SOCKET_FLAGS); ctx.status = code; } pub fn init(req: Request, allocator: *std.mem.Allocator, conn: *tcp.Connection, bundler_: *Bundler) !RequestContext { return RequestContext{ .request = req, - .conn = conn, .allocator = allocator, .bundler = bundler_, .url = URLPath.parse(req.path), .log = logger.Log.init(allocator), + .conn = conn, .method = Method.which(req.method) orelse return error.InvalidMethod, }; } @@ -285,7 +290,7 @@ pub const Server = struct { ctx.writeStatus(200) catch {}; try ctx.prepareToSendBody(file_read, false); if (!send_body) return; - _ = try ctx.conn.client.write(file_slice, SOCKET_FLAGS); + _ = try ctx.writeSocket(file_slice, SOCKET_FLAGS); }, else => { var chunk_written: usize = 0; @@ -300,7 +305,7 @@ pub const Server = struct { // Read from the file until we reach either end of file or the max chunk size chunk_written = handle.read(file_chunk_slice) catch |err| { if (pushed_chunk_count > 0) { - _ = try ctx.conn.client.write("0\r\n\r\n", SOCKET_FLAGS); + _ = try ctx.writeSocket("0\r\n\r\n", SOCKET_FLAGS); } return ctx.sendInternalError(err); }; @@ -315,7 +320,7 @@ pub const Server = struct { return; } - _ = try ctx.conn.client.write("0\r\n\r\n", SOCKET_FLAGS); + _ = try ctx.writeSocket("0\r\n\r\n", SOCKET_FLAGS); break; // final chunk } else if (chunk_written < file_chunk_size - 1) { @@ -329,9 +334,9 @@ pub const Server = struct { ctx.prepareToSendBody(0, true) catch {}; if (!send_body) return; } - _ = try ctx.conn.client.write(size_slice, SOCKET_FLAGS); - _ = try ctx.conn.client.write(file_chunk_slice[0..chunk_written], SOCKET_FLAGS); - _ = try ctx.conn.client.write(trailing_newline_slice, SOCKET_FLAGS); + _ = try ctx.writeSocket(size_slice, SOCKET_FLAGS); + _ = try ctx.writeSocket(file_chunk_slice[0..chunk_written], SOCKET_FLAGS); + _ = try ctx.writeSocket(trailing_newline_slice, SOCKET_FLAGS); break; // full chunk } else { @@ -347,7 +352,7 @@ pub const Server = struct { remainder_slice[0] = '\r'; remainder_slice[1] = '\n'; - _ = try ctx.conn.client.write(&file_chunk_buf, SOCKET_FLAGS); + _ = try ctx.writeSocket(&file_chunk_buf, SOCKET_FLAGS); } } }, @@ -383,7 +388,7 @@ pub const Server = struct { ctx.writeStatus(200) catch {}; try ctx.prepareToSendBody(output.contents.len, false); if (!send_body) return; - _ = try ctx.conn.client.write(output.contents, SOCKET_FLAGS); + _ = try ctx.writeSocket(output.contents, SOCKET_FLAGS); }, } |