@@ -43,8 +43,8 @@ pub const Interfaces = .{
4343};
4444
4545pub 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+
237245const testing = @import ("../../testing.zig" );
238246test "fetch: fetch" {
239247 try testing .htmlRunner ("fetch/fetch.html" );
0 commit comments