Use send() syscall instead of write()

This commit is contained in:
jaina heartles 2022-11-16 21:11:51 -08:00
parent 432a11dbc9
commit 58a81d74ec

View file

@ -9,13 +9,15 @@ pub const Response = struct {
alloc: std.mem.Allocator,
stream: std.net.Stream,
should_close: bool = false,
pub const Stream = response.ResponseStream(std.net.Stream.Writer);
pub const Stream = response.ResponseStream(SendStream.Writer);
pub fn open(self: *Response, status: http.Status, headers: *const http.Fields) !Stream {
if (headers.get("Connection")) |hdr| {
if (std.ascii.indexOfIgnoreCase(hdr, "close")) |_| self.should_close = true;
}
return response.open(self.alloc, self.stream.writer(), headers, status);
const stream = SendStream{ .sockfd = self.stream.handle };
return response.open(self.alloc, stream.writer(), headers, status);
}
pub fn upgrade(self: *Response, status: http.Status, headers: *const http.Fields) !std.net.Stream {
@ -24,6 +26,24 @@ pub const Response = struct {
}
};
// Replacement for std.net.Stream that uses os.send instead of os.write
// see https://github.com/ziglang/zig/issues/5614
const SendStream = struct {
sockfd: std.os.socket_t,
const WriteError = std.os.SendError;
const Writer = std.io.Writer(SendStream, WriteError, write);
fn write(self: SendStream, bytes: []const u8) WriteError!usize {
if (std.io.is_async) @compileError("TODO: Async not supported yet");
return std.os.send(self.sockfd, bytes, std.os.MSG.NOSIGNAL);
}
fn writer(self: SendStream) Writer {
return .{ .context = self };
}
};
const Request = http.Request;
const request_buf_size = 1 << 16;