Skip to content

Commit 6bad2b1

Browse files
committed
Set Response.type to basic on same-origin requests
1 parent db166b4 commit 6bad2b1

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/browser/fetch/fetch.zig

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ pub const Interfaces = .{
4343
};
4444

4545
pub const FetchContext = struct {
46+
page: *Page,
4647
arena: std.mem.Allocator,
47-
js_ctx: *Env.JsContext,
4848
promise_resolver: Env.PersistentPromiseResolver,
4949

5050
method: Http.Method,
@@ -63,9 +63,12 @@ pub const FetchContext = struct {
6363
pub fn toResponse(self: *const FetchContext) !Response {
6464
var headers: Headers = .{};
6565

66+
// seems to be the highest priority
67+
const same_origin = try isSameOriginAsPage(self.url, self.page);
68+
6669
// If the mode is "no-cors", we need to return this opaque/stripped Response.
6770
// https://developer.mozilla.org/en-US/docs/Web/API/Response/type
68-
if (self.mode == .@"no-cors") {
71+
if (!same_origin and self.mode == .@"no-cors") {
6972
return Response{
7073
.status = 0,
7174
.headers = headers,
@@ -85,7 +88,7 @@ pub const FetchContext = struct {
8588
}
8689

8790
const resp_type: Response.ResponseType = blk: {
88-
if (std.mem.startsWith(u8, self.url, "data:")) {
91+
if (same_origin or std.mem.startsWith(u8, self.url, "data:")) {
8992
break :blk .basic;
9093
}
9194

@@ -132,8 +135,8 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
132135

133136
const fetch_ctx = try arena.create(FetchContext);
134137
fetch_ctx.* = .{
138+
.page = page,
135139
.arena = arena,
136-
.js_ctx = page.main_context,
137140
.promise_resolver = resolver,
138141
.method = req.method,
139142
.url = req.url,
@@ -234,6 +237,11 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
234237
return resolver.promise();
235238
}
236239

240+
fn isSameOriginAsPage(url: []const u8, page: *const Page) !bool {
241+
const origin = try page.origin(page.call_arena);
242+
return std.mem.startsWith(u8, url, origin);
243+
}
244+
237245
const testing = @import("../../testing.zig");
238246
test "fetch: fetch" {
239247
try testing.htmlRunner("fetch/fetch.html");

src/tests/fetch/fetch.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@
33
const promise1 = new Promise((resolve) => {
44
fetch('http://127.0.0.1:9582/xhr/json')
55
.then((res) => {
6+
testing.expectEqual('cors', res.type);
7+
return res.json()
8+
})
9+
.then((json) => {
10+
resolve(json);
11+
});
12+
});
13+
14+
testing.async(promise1, (json) => {
15+
testing.expectEqual({over: '9000!!!'}, json);
16+
});
17+
</script>
18+
19+
<script id=same-origin type=module>
20+
const promise1 = new Promise((resolve) => {
21+
fetch('http://localhost:9582/xhr/json')
22+
.then((res) => {
23+
testing.expectEqual('basic', res.type);
624
return res.json()
725
})
826
.then((json) => {

src/tests/testing.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@
9595
async function async(promise, cb) {
9696
const script_id = document.currentScript.id;
9797
const stack = new Error().stack;
98+
this._captured = {script_id: script_id, stack: stack};
9899
const value = await promise;
100+
// reset it, because await promise could change it.
99101
this._captured = {script_id: script_id, stack: stack};
100102
cb(value);
101103
this._captured = null;

0 commit comments

Comments
 (0)