From 37ffe4501c2618ca34d652d1db167794a1806592 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 27 Sep 2021 21:03:00 -0700 Subject: [PATCH] Ignore leading invalid unicode characters in response bodies in Response.text() --- src/http_client.zig | 4 ++-- src/javascript/jsc/webcore/response.zig | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/http_client.zig b/src/http_client.zig index 34525e8461..fd7088ff43 100644 --- a/src/http_client.zig +++ b/src/http_client.zig @@ -35,7 +35,7 @@ header_entries: Headers.Entries, header_buf: string, url: URL, allocator: *std.mem.Allocator, -verbose: bool = true, +verbose: bool = false, pub fn init(allocator: *std.mem.Allocator, method: Method, url: URL, header_entries: Headers.Entries, header_buf: string) HTTPClient { return HTTPClient{ @@ -408,7 +408,7 @@ pub fn sendHTTPS(this: *HTTPClient, body_str: []const u8, body_out_str: *Mutable remaining_content_length -= size; } - body_out_str.list.items.len = body_size; + body_out_str.list.shrinkRetainingCapacity(body_size); } return response; diff --git a/src/javascript/jsc/webcore/response.zig b/src/javascript/jsc/webcore/response.zig index 8073921b17..df283060d7 100644 --- a/src/javascript/jsc/webcore/response.zig +++ b/src/javascript/jsc/webcore/response.zig @@ -75,8 +75,14 @@ pub const Response = struct { (brk: { switch (this.body.value) { .Unconsumed => { - if (this.body.ptr) |_ptr| { - break :brk ZigString.init(_ptr[0..this.body.len]).toValue(VirtualMachine.vm.global); + if (this.body.len > 0) { + if (this.body.ptr) |_ptr| { + var offset: usize = 0; + while (offset < this.body.len and _ptr[offset] > 127 or strings.utf8ByteSequenceLength(_ptr[offset]) == 0) : (offset += 1) {} + if (offset < this.body.len) { + break :brk ZigString.init(_ptr[offset..this.body.len]).toValue(VirtualMachine.vm.global); + } + } } break :brk ZigString.init("").toValue(VirtualMachine.vm.global);