aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-10-25 04:46:01 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-10-25 04:46:01 -0700
commit2ed6605cc35adb8ee04d53d96e38617aa4597510 (patch)
tree687a7267b09404cad8a883c2b21091fecc17922e
parent42c264bf7b45bdf7944d10260beeaf7c8b50a21a (diff)
downloadbun-2ed6605cc35adb8ee04d53d96e38617aa4597510.tar.gz
bun-2ed6605cc35adb8ee04d53d96e38617aa4597510.tar.zst
bun-2ed6605cc35adb8ee04d53d96e38617aa4597510.zip
[Bun.js] When `Body` is UTF-16 encoded, convert it to UTF-8.
-rw-r--r--src/javascript/jsc/webcore/response.zig36
1 files changed, 30 insertions, 6 deletions
diff --git a/src/javascript/jsc/webcore/response.zig b/src/javascript/jsc/webcore/response.zig
index 76be74d84..16f3318b9 100644
--- a/src/javascript/jsc/webcore/response.zig
+++ b/src/javascript/jsc/webcore/response.zig
@@ -430,10 +430,6 @@ pub const Fetch = struct {
}
var url_zig_str = ZigString.init("");
- JSValue.fromRef(arguments[0]).toZigString(
- &url_zig_str,
- VirtualMachine.vm.global,
- );
var url_str = url_zig_str.slice();
if (url_str.len == 0) {
const fetch_error = fetch_error_blank_url;
@@ -1232,8 +1228,27 @@ pub const Body = struct {
return body;
}
+ if (!wtf_string.is8Bit()) {
+ var js_string = js.JSValueToStringCopy(ctx, body_ref, exception);
+ defer js.JSStringRelease(js_string);
+ body.ptr_allocator = default_allocator;
+ const len = js.JSStringGetLength(js_string);
+ var body_string = default_allocator.alloc(u8, len + 1) catch unreachable;
+ body.ptr = body_string.ptr;
+ body.len = body_string.len;
+ body.value = .{ .String = body_string.ptr[0..js.JSStringGetUTF8CString(js_string, body_string.ptr, body_string.len)] };
+ return body;
+ }
+
+ var slice = wtf_string.characters8()[0..wtf_string.length()];
+
+ if (slice.len == 0) {
+ body.value = .{ .String = "" };
+ return body;
+ }
+
body.value = Value{
- .String = wtf_string.characters8()[0..wtf_string.length()],
+ .String = slice,
};
// body.ptr = body.
// body.len = body.value.String.len;str.characters8()[0..len] };
@@ -1599,7 +1614,7 @@ pub const FetchEvent = struct {
Output.printElapsed(@intToFloat(f64, (this.request_context.timer.lap())) / std.time.ns_per_ms);
Output.prettyError(
- " <b>/{s}<r><d> - <b>{d}<r> <d>transpiled, <d><b>{d}<r> <d>imports<r>\n",
+ " <b>{s}<r><d> - <b>{d}<r> <d>transpiled, <d><b>{d}<r> <d>imports<r>\n",
.{
this.request_context.matched_route.?.name,
VirtualMachine.vm.transpiled_count,
@@ -1662,6 +1677,15 @@ pub const FetchEvent = struct {
}
defer this.request_context.done();
+ defer {
+ if (response.body.ptr_allocator) |alloc| {
+ if (response.body.ptr) |ptr| {
+ alloc.free(ptr[0..response.body.len]);
+ }
+
+ response.body.ptr_allocator = null;
+ }
+ }
this.request_context.writeStatusSlow(response.body.init.status_code) catch return js.JSValueMakeUndefined(ctx);
this.request_context.prepareToSendBody(content_length_, false) catch return js.JSValueMakeUndefined(ctx);