@@ -38,16 +38,16 @@ pub const RequestInput = union(enum) {
3838
3939// https://developer.mozilla.org/en-US/docs/Web/API/RequestInit
4040pub const RequestInit = struct {
41- method : []const u8 = "GET" ,
42- body : []const u8 = "" ,
41+ method : ? []const u8 = null ,
42+ body : ? []const u8 = null ,
4343};
4444
4545// https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
4646const Request = @This ();
4747
4848method : Http.Method ,
4949url : [:0 ]const u8 ,
50- body : []const u8 ,
50+ body : ? []const u8 ,
5151
5252pub fn constructor (input : RequestInput , _options : ? RequestInit , page : * Page ) ! Request {
5353 const arena = page .arena ;
@@ -62,15 +62,21 @@ pub fn constructor(input: RequestInput, _options: ?RequestInit, page: *Page) !Re
6262 },
6363 };
6464
65- const method : Http.Method = blk : for (std .enums .values (Http .Method )) | method | {
66- if (std .ascii .eqlIgnoreCase (options .method , @tagName (method ))) {
67- break :blk method ;
65+ const method : Http.Method = blk : {
66+ if (options .method ) | given_method | {
67+ for (std .enums .values (Http .Method )) | method | {
68+ if (std .ascii .eqlIgnoreCase (given_method , @tagName (method ))) {
69+ break :blk method ;
70+ }
71+ } else {
72+ return error .TypeError ;
73+ }
74+ } else {
75+ break :blk Http .Method .GET ;
6876 }
69- } else {
70- return error .InvalidMethod ;
7177 };
7278
73- const body = try arena .dupe (u8 , options . body );
79+ const body = if ( options . body ) | body | try arena .dupe (u8 , body ) else null ;
7480
7581 return .{
7682 .method = method ,
@@ -87,9 +93,9 @@ pub fn get_method(self: *const Request) []const u8 {
8793 return @tagName (self .method );
8894}
8995
90- pub fn get_body (self : * const Request ) []const u8 {
91- return self .body ;
92- }
96+ // pub fn get_body(self: *const Request) ? []const u8 {
97+ // return self.body;
98+ // }
9399
94100const FetchContext = struct {
95101 arena : std.mem.Allocator ,
@@ -123,13 +129,14 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
123129 const arena = page .arena ;
124130
125131 const req = try Request .constructor (input , options , page );
132+
126133 const resolver = Env.PromiseResolver {
127134 .js_context = page .main_context ,
128135 .resolver = v8 .PromiseResolver .init (page .main_context .v8_context ),
129136 };
130137
131- const client = page . http_client ;
132- const headers = try HttpClient . Headers . init ( );
138+ var headers = try Http . Headers . init () ;
139+ try page . requestCookie (.{}). headersForRequest ( arena , req . url , & headers );
133140
134141 const fetch_ctx = try arena .create (FetchContext );
135142 fetch_ctx .* = .{
@@ -143,47 +150,51 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
143150 .url = req .url ,
144151 };
145152
146- try client .request (.{
147- .method = req . method ,
153+ try page . http_client .request (.{
154+ .ctx = @ptrCast ( fetch_ctx ) ,
148155 .url = req .url ,
156+ .method = req .method ,
149157 .headers = headers ,
150158 .body = req .body ,
151159 .cookie_jar = page .cookie_jar ,
152- .ctx = @ptrCast ( fetch_ctx ) ,
160+ .resource_type = .fetch ,
153161
154162 .start_callback = struct {
155163 fn startCallback (transfer : * HttpClient.Transfer ) ! void {
156164 const self : * FetchContext = @alignCast (@ptrCast (transfer .ctx ));
157165 log .debug (.http , "request start" , .{ .method = self .method , .url = self .url , .source = "fetch" });
166+
158167 self .transfer = transfer ;
159168 }
160169 }.startCallback ,
161170 .header_callback = struct {
162- fn headerCallback (transfer : * HttpClient.Transfer , header : []const u8 ) ! void {
163- const self : * FetchContext = @alignCast (@ptrCast (transfer .ctx ));
164- try self .headers .append (self .arena , try self .arena .dupe (u8 , header ));
165- }
166- }.headerCallback ,
167- .header_done_callback = struct {
168- fn headerDoneCallback (transfer : * HttpClient.Transfer ) ! void {
171+ fn headerCallback (transfer : * HttpClient.Transfer ) ! void {
169172 const self : * FetchContext = @alignCast (@ptrCast (transfer .ctx ));
173+
170174 const header = & transfer .response_header .? ;
171175
172176 log .debug (.http , "request header" , .{
173177 .source = "fetch" ,
178+ .method = self .method ,
174179 .url = self .url ,
175180 .status = header .status ,
176181 });
177182
178183 if (header .contentType ()) | ct | {
179184 self .mime = Mime .parse (ct ) catch {
180- return error .Todo ;
185+ return error .MimeParsing ;
181186 };
182187 }
183188
189+ var it = transfer .responseHeaderIterator ();
190+ while (it .next ()) | hdr | {
191+ const joined = try std .fmt .allocPrint (self .arena , "{s}: {s}" , .{ hdr .name , hdr .value });
192+ try self .headers .append (self .arena , joined );
193+ }
194+
184195 self .status = header .status ;
185196 }
186- }.headerDoneCallback ,
197+ }.headerCallback ,
187198 .data_callback = struct {
188199 fn dataCallback (transfer : * HttpClient.Transfer , data : []const u8 ) ! void {
189200 const self : * FetchContext = @alignCast (@ptrCast (transfer .ctx ));
@@ -196,6 +207,7 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
196207
197208 log .info (.http , "request complete" , .{
198209 .source = "fetch" ,
210+ .method = self .method ,
199211 .url = self .url ,
200212 .status = self .status ,
201213 });
@@ -212,6 +224,8 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
212224 .error_callback = struct {
213225 fn errorCallback (ctx : * anyopaque , err : anyerror ) void {
214226 const self : * FetchContext = @alignCast (@ptrCast (ctx ));
227+
228+ self .transfer = null ;
215229 const promise_resolver : Env.PromiseResolver = .{
216230 .js_context = self .js_ctx ,
217231 .resolver = self .promise_resolver .castToPromiseResolver (),
0 commit comments