diff options
author | 2022-11-09 15:11:14 -0800 | |
---|---|---|
committer | 2022-11-09 15:11:14 -0800 | |
commit | da257336b0b70df8c31da647496899cf70670000 (patch) | |
tree | 6baca9ead683633e8feac503529d94e8a565445c /src | |
parent | 565996a087df6d06b2b5109b6825c720d4c8b168 (diff) | |
download | bun-da257336b0b70df8c31da647496899cf70670000.tar.gz bun-da257336b0b70df8c31da647496899cf70670000.tar.zst bun-da257336b0b70df8c31da647496899cf70670000.zip |
Fix #1354
Diffstat (limited to 'src')
-rw-r--r-- | src/bun.js/api/server.zig | 6 | ||||
-rw-r--r-- | src/bun.js/webcore/response.zig | 47 | ||||
-rw-r--r-- | src/deps/uws.zig | 2 | ||||
-rw-r--r-- | src/http_client_async.zig | 2 | ||||
-rw-r--r-- | src/url.zig | 2 |
5 files changed, 44 insertions, 15 deletions
diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 8a9ae8c54..c95949aa2 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -4079,6 +4079,7 @@ pub fn NewServer(comptime ssl_enabled_: bool, comptime debug_mode_: bool) type { .vm = JSC.VirtualMachine.vm, .allocator = Arena.getThreadlocalDefault(), }; + if (RequestContext.pool == null) { RequestContext.pool = server.allocator.create(RequestContext.RequestContextStackAllocator) catch @panic("Out of memory!"); RequestContext.pool.?.* = .{ @@ -4256,11 +4257,12 @@ pub fn NewServer(comptime ssl_enabled_: bool, comptime debug_mode_: bool) type { var ctx = this.request_pool_allocator.create(RequestContext) catch @panic("ran out of memory"); ctx.create(this, req, resp); var request_object = this.allocator.create(JSC.WebCore.Request) catch unreachable; + request_object.* = .{ .url = "", .method = ctx.method, .uws_request = req, - .base_url_string_for_joining = this.base_url_string_for_joining, + .https = ssl_enabled, .body = .{ .Empty = .{}, }, @@ -4339,8 +4341,8 @@ pub fn NewServer(comptime ssl_enabled_: bool, comptime debug_mode_: bool) type { .url = "", .method = ctx.method, .uws_request = req, - .base_url_string_for_joining = this.base_url_string_for_joining, .upgrader = ctx, + .https = ssl_enabled, .body = .{ .Empty = .{}, }, diff --git a/src/bun.js/webcore/response.zig b/src/bun.js/webcore/response.zig index 3e6d954ae..36ed271fd 100644 --- a/src/bun.js/webcore/response.zig +++ b/src/bun.js/webcore/response.zig @@ -5166,17 +5166,17 @@ pub const Body = struct { // https://developer.mozilla.org/en-US/docs/Web/API/Request pub const Request = struct { url: []const u8 = "", + url_was_allocated: bool = false, + headers: ?*FetchHeaders = null, body: Body.Value = Body.Value{ .Empty = .{} }, method: Method = Method.GET, uws_request: ?*uws.Request = null, + https: bool = false, upgrader: ?*anyopaque = null, // We must report a consistent value for this reported_estimated_size: ?u63 = null, - base_url_string_for_joining: []const u8 = "", - - url_was_allocated: bool = false, pub usingnamespace JSC.Codegen.JSRequest; @@ -5368,29 +5368,54 @@ pub const Request = struct { return ZigString.init(this.url).withEncoding().toValueGC(globalObject); } - pub fn sizeOfURL(this: *Request) usize { + pub fn sizeOfURL(this: *const Request) usize { if (this.url.len > 0) return this.url.len; if (this.uws_request) |req| { - return this.base_url_string_for_joining.len + req.url().len; + const fmt = ZigURL.HostFormatter{ + .is_https = this.https, + .host = req.header("host") orelse "", + }; + + return this.getProtocol().len + req.url().len + std.fmt.count("{any}", .{fmt}); } return 0; } + pub fn getProtocol(this: *const Request) []const u8 { + if (this.https) + return "https://"; + + return "http://"; + } + pub fn ensureURL(this: *Request) !void { if (this.url.len > 0) return; if (this.uws_request) |req| { - if (this.base_url_string_for_joining.len > 0) { - this.url = try strings.append(bun.default_allocator, this.base_url_string_for_joining, req.url()); + const req_url = req.url(); + if (req.header("host")) |host| { + const fmt = ZigURL.HostFormatter{ + .is_https = this.https, + .host = host, + }; + const url = try std.fmt.allocPrint(bun.default_allocator, "{s}{any}{s}", .{ + this.getProtocol(), + fmt, + req_url, + }); + if (comptime Environment.allow_assert) { + std.debug.assert(this.sizeOfURL() == url.len); + } + this.url = url; this.url_was_allocated = true; - - // don't keep this around when we don't need it - this.base_url_string_for_joining = ""; } else { - this.url = try bun.default_allocator.dupe(u8, req.url()); + if (comptime Environment.allow_assert) { + std.debug.assert(this.sizeOfURL() == req_url.len); + } + this.url = try bun.default_allocator.dupe(u8, req_url); this.url_was_allocated = true; } } diff --git a/src/deps/uws.zig b/src/deps/uws.zig index d4a84752c..1551efba7 100644 --- a/src/deps/uws.zig +++ b/src/deps/uws.zig @@ -915,6 +915,8 @@ pub const Request = opaque { return ptr[0..req.uws_req_get_method(&ptr)]; } pub fn header(req: *Request, name: []const u8) ?[]const u8 { + std.debug.assert(std.ascii.isLower(name[0])); + var ptr: [*]const u8 = undefined; const len = req.uws_req_get_header(name.ptr, name.len, &ptr); if (len == 0) return null; diff --git a/src/http_client_async.zig b/src/http_client_async.zig index 9f2b9a163..114181c60 100644 --- a/src/http_client_async.zig +++ b/src/http_client_async.zig @@ -1148,7 +1148,7 @@ pub fn buildRequest(this: *HTTPClient, body_len: usize) picohttp.Request { if (!override_host_header) { request_headers_buf[header_count] = picohttp.Header{ .name = host_header_name, - .value = this.url.hostname, + .value = this.url.host, }; header_count += 1; } diff --git a/src/url.zig b/src/url.zig index 1e8bcae32..7e4772079 100644 --- a/src/url.zig +++ b/src/url.zig @@ -83,7 +83,7 @@ pub const URL = struct { pub const HostFormatter = struct { host: string, - port: string, + port: string = "80", is_https: bool = false, pub fn format(formatter: HostFormatter, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { |