@@ -43,7 +43,7 @@ const Location = @import("../html/location.zig").Location;
4343
4444const storage = @import ("../storage/storage.zig" );
4545
46- const HttpClient = @import ("../http/client.zig" ). Client ;
46+ const http = @import ("../http/client.zig" );
4747const UserContext = @import ("../user_context.zig" ).UserContext ;
4848
4949const polyfill = @import ("../polyfill/polyfill.zig" );
@@ -60,7 +60,7 @@ pub const Browser = struct {
6060 app : * App ,
6161 session : ? * Session ,
6262 allocator : Allocator ,
63- http_client : * HttpClient ,
63+ http_client : * http.Client ,
6464 session_pool : SessionPool ,
6565 page_arena : std.heap.ArenaAllocator ,
6666
@@ -130,10 +130,12 @@ pub const Session = struct {
130130
131131 window : Window ,
132132
133- // TODO move the shed to the browser?
133+ // TODO move the shed/jar to the browser?
134134 storage_shed : storage.Shed ,
135+ cookie_jar : storage.CookieJar ,
136+
135137 page : ? Page = null ,
136- http_client : * HttpClient ,
138+ http_client : * http.Client ,
137139
138140 jstypes : [Types .len ]usize = undefined ,
139141
@@ -148,6 +150,7 @@ pub const Session = struct {
148150 .http_client = browser .http_client ,
149151 .storage_shed = storage .Shed .init (allocator ),
150152 .arena = std .heap .ArenaAllocator .init (allocator ),
153+ .cookie_jar = storage .CookieJar .init (allocator ),
151154 .window = Window .create (null , .{ .agent = user_agent }),
152155 };
153156
@@ -183,6 +186,7 @@ pub const Session = struct {
183186 }
184187 self .env .deinit ();
185188 self .arena .deinit ();
189+ self .cookie_jar .deinit ();
186190 self .storage_shed .deinit ();
187191 }
188192
@@ -371,14 +375,16 @@ pub const Page = struct {
371375 } });
372376
373377 // load the data
374- var request = try self .session . http_client . request (.GET , self .uri );
378+ var request = try self .newHTTPRequest (.GET , self .uri , .{ . navigation = true } );
375379 defer request .deinit ();
376- var response = try request .sendSync (.{});
377380
381+ var response = try request .sendSync (.{});
378382 const header = response .header ;
383+ try self .session .cookie_jar .populateFromResponse (self .uri , & header );
384+
379385 log .info ("GET {any} {d}" , .{ self .uri , header .status });
380386
381- const ct = response . header .get ("content-type" ) orelse {
387+ const ct = header .get ("content-type" ) orelse {
382388 // no content type in HTTP headers.
383389 // TODO try to sniff mime type from the body.
384390 log .info ("no content-type HTTP header" , .{});
@@ -439,7 +445,9 @@ pub const Page = struct {
439445
440446 // replace the user context document with the new one.
441447 try session .env .setUserContext (.{
448+ .uri = self .uri ,
442449 .document = html_doc ,
450+ .cookie_jar = @ptrCast (& self .session .cookie_jar ),
443451 .http_client = @ptrCast (self .session .http_client ),
444452 });
445453
@@ -614,13 +622,19 @@ pub const Page = struct {
614622 }
615623 const u = try std .Uri .resolve_inplace (self .uri , res_src , & b );
616624
617- var request = try self .session .http_client .request (.GET , u );
625+ var request = try self .newHTTPRequest (.GET , u , .{
626+ .origin_uri = self .uri ,
627+ .navigation = false ,
628+ });
618629 defer request .deinit ();
630+
619631 var response = try request .sendSync (.{});
632+ var header = response .header ;
633+ try self .session .cookie_jar .populateFromResponse (u , & header );
620634
621- log .info ("fetch {any}: {d}" , .{ u , response . header .status });
635+ log .info ("fetch {any}: {d}" , .{ u , header .status });
622636
623- if (response . header .status != 200 ) {
637+ if (header .status != 200 ) {
624638 return FetchError .BadStatusCode ;
625639 }
626640
@@ -645,6 +659,21 @@ pub const Page = struct {
645659 try s .eval (arena , & self .session .env , body );
646660 }
647661
662+ fn newHTTPRequest (self : * const Page , method : http.Request.Method , uri : std.Uri , opts : storage.cookie.LookupOpts ) ! http.Request {
663+ const session = self .session ;
664+ var request = try session .http_client .request (method , uri );
665+ errdefer request .deinit ();
666+
667+ var arr : std .ArrayListUnmanaged (u8 ) = .{};
668+ try session .cookie_jar .forRequest (uri , arr .writer (self .arena ), opts );
669+
670+ if (arr .items .len > 0 ) {
671+ try request .addHeader ("Cookie" , arr .items , .{});
672+ }
673+
674+ return request ;
675+ }
676+
648677 const Script = struct {
649678 element : * parser.Element ,
650679 kind : Kind ,
0 commit comments