Compare commits

...

4 Commits

Author SHA1 Message Date
Jarred-Sumner
9b2f273c5a bun run prettier 2025-05-20 20:16:15 +00:00
Jarred-Sumner
2ab11f176e bun run zig-format 2025-05-20 20:15:02 +00:00
Jarred Sumner
7ae711236b Update ssl_wrapper.zig 2025-05-20 13:12:09 -07:00
Jarred Sumner
d175c8ceb4 Make the SSL wrapper use a bigger intermediate buffer Just In Case 2025-05-20 13:11:26 -07:00
3 changed files with 35 additions and 12 deletions

View File

@@ -33,6 +33,25 @@ pub fn SSLWrapper(comptime T: type) type {
flags: Flags = .{},
const StackBuffer = [BUFFER_SIZE]u8;
const TStruct = std.meta.Child(T);
inline fn getSharedBuffer() []u8 {
if (comptime @hasDecl(TStruct, "ssl_wrapper_static_buffer")) {
return &@field(TStruct, "ssl_wrapper_static_buffer");
}
@compileError("ssl_wrapper_static_buffer is not declared in " ++ @typeName(TStruct));
}
inline fn getStackBuffer() StackBuffer {
// We do not need to initialize this memory.
return undefined;
}
const getBuffer = if (@hasDecl(TStruct, "ssl_wrapper_static_buffer")) getSharedBuffer else getStackBuffer;
pub const Flags = packed struct(u8) {
handshake_state: HandshakeState = HandshakeState.HANDSHAKE_PENDING,
received_ssl_shutdown: bool = false,
@@ -377,7 +396,7 @@ pub fn SSLWrapper(comptime T: type) type {
/// Handle reading data
/// Returns true if we can call handleWriting
fn handleReading(this: *This, buffer: *[BUFFER_SIZE]u8) bool {
fn handleReading(this: *This, buffer: []u8) bool {
var read: usize = 0;
// read data from the input BIO
@@ -450,7 +469,7 @@ pub fn SSLWrapper(comptime T: type) type {
return true;
}
fn handleWriting(this: *This, buffer: *[BUFFER_SIZE]u8) void {
fn handleWriting(this: *This, buffer: []u8) void {
while (true) {
const ssl = this.ssl orelse return;
@@ -462,9 +481,9 @@ pub fn SSLWrapper(comptime T: type) type {
break;
}
// limit the read to the buffer size
const len = @min(pending, buffer.len);
const len: usize = @min(@as(usize, @intCast(pending)), buffer.len);
const pending_buffer = buffer[0..len];
const read = BoringSSL.BIO_read(output, pending_buffer.ptr, len);
const read = BoringSSL.BIO_read(output, pending_buffer.ptr, @intCast(len));
if (read > 0) {
this.triggerWannaWriteCallback(buffer[0..@intCast(read)]);
}
@@ -474,16 +493,18 @@ pub fn SSLWrapper(comptime T: type) type {
fn handleTraffic(this: *This) void {
// always handle the handshake first
if (this.updateHandshakeState()) {
// shared stack buffer for reading and writing
var buffer: [BUFFER_SIZE]u8 = undefined;
var buffer_maybe_stack = getBuffer();
const buffer: []u8 = if (@TypeOf(buffer_maybe_stack) == []u8) buffer_maybe_stack else &buffer_maybe_stack;
// drain the input BIO first
this.handleWriting(&buffer);
this.handleWriting(buffer);
// drain the output BIO
if (this.handleReading(&buffer)) {
if (this.handleReading(buffer)) {
// read data can trigger writing so we need to handle it
this.handleWriting(&buffer);
this.handleWriting(buffer);
}
}
}
};
}
const std = @import("std");

View File

@@ -70,6 +70,8 @@ var shared_request_headers_buf: [256]picohttp.Header = undefined;
// this doesn't need to be stack memory because it is immediately cloned after use
var shared_response_headers_buf: [256]picohttp.Header = undefined;
pub var ssl_wrapper_static_buffer: [1024 * 1024]u8 = undefined;
pub const end_of_chunked_http1_1_encoding_response_body = "0\r\n\r\n";
pub const Signals = struct {

View File

@@ -300,9 +300,9 @@ describe("TextDecoder", () => {
});
it("should support undefined options", () => {
expect(() => {
const decoder = new TextDecoder("utf-8", undefined);
}).not.toThrow();
expect(() => {
const decoder = new TextDecoder("utf-8", undefined);
}).not.toThrow();
});
});