Skip to content

Commit 2171bb0

Browse files
committed
wip
1 parent ff8a847 commit 2171bb0

File tree

4 files changed

+122
-6
lines changed

4 files changed

+122
-6
lines changed

src/cdp/cdp.zig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,16 @@ pub fn BrowserContext(comptime CDP_T: type) type {
463463
self.cdp.browser.notification.unregister(.http_headers_done, self);
464464
}
465465

466-
pub fn fetchEnable(self: *Self) !void {
466+
pub fn fetchEnable(self: *Self, auth: bool) !void {
467467
try self.cdp.browser.notification.register(.http_request_intercept, self, onHttpRequestIntercept);
468+
if (auth) {
469+
try self.cdp.browser.notification.register(.http_request_auth_required, self, onHttpRequestAuthRequired);
470+
}
468471
}
469472

470473
pub fn fetchDisable(self: *Self) void {
471474
self.cdp.browser.notification.unregister(.http_request_intercept, self);
475+
self.cdp.browser.notification.unregister(.http_request_auth_required, self);
472476
}
473477

474478
pub fn onPageRemove(ctx: *anyopaque, _: Notification.PageRemove) !void {
@@ -504,6 +508,12 @@ pub fn BrowserContext(comptime CDP_T: type) type {
504508
try @import("domains/fetch.zig").requestIntercept(self.notification_arena, self, data);
505509
}
506510

511+
pub fn onHttpRequestAuthRequired(ctx: *anyopaque, data: *const Notification.RequestAuthRequired) !void {
512+
const self: *Self = @alignCast(@ptrCast(ctx));
513+
defer self.resetNotificationArena();
514+
try @import("domains/fetch.zig").requestAuthRequired(self.notification_arena, self, data);
515+
}
516+
507517
pub fn onHttpRequestFail(ctx: *anyopaque, data: *const Notification.RequestFail) !void {
508518
const self: *Self = @alignCast(@ptrCast(ctx));
509519
defer self.resetNotificationArena();

src/cdp/domains/fetch.zig

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,8 @@ fn enable(cmd: anytype) !void {
144144
return cmd.sendResult(null, .{});
145145
}
146146

147-
if (params.handleAuthRequests) {
148-
log.warn(.cdp, "not implemented", .{ .feature = "Fetch.enable handleAuthRequests is not supported yet" });
149-
}
150-
151147
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
152-
try bc.fetchEnable();
148+
try bc.fetchEnable(params.handleAuthRequests);
153149

154150
return cmd.sendResult(null, .{});
155151
}
@@ -346,6 +342,92 @@ fn failRequest(cmd: anytype) !void {
346342
return cmd.sendResult(null, .{});
347343
}
348344

345+
const AuthChallenge = struct {
346+
scheme: enum { basic, digest },
347+
realm: []const u8,
348+
349+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/WWW-Authenticate
350+
// Supports only basic and digest schemes.
351+
pub fn parse(header: []const u8) !AuthChallenge {
352+
var ac: AuthChallenge = .{
353+
.scheme = undefined,
354+
.realm = "",
355+
};
356+
357+
const pos = std.mem.indexOfPos(u8, std.mem.trim(u8, header, std.ascii.whitespace[0..]), 0, " ") orelse header.len;
358+
const _scheme = header[0..pos];
359+
if (std.ascii.eqlIgnoreCase(_scheme, "basic")) {
360+
ac.scheme = .basic;
361+
} else if (std.ascii.eqlIgnoreCase(_scheme, "digest")) {
362+
ac.scheme = .digest;
363+
} else {
364+
return error.UnknownAuthChallengeScheme;
365+
}
366+
367+
// TODO get the realm
368+
369+
return ac;
370+
}
371+
};
372+
373+
pub fn requestAuthRequired(arena: Allocator, bc: anytype, intercept: *const Notification.RequestAuthRequired) !void {
374+
// unreachable because we _have_ to have a page.
375+
const session_id = bc.session_id orelse unreachable;
376+
const target_id = bc.target_id orelse unreachable;
377+
const page = bc.session.currentPage() orelse unreachable;
378+
379+
// We keep it around to wait for modifications to the request.
380+
// NOTE: we assume whomever created the request created it with a lifetime of the Page.
381+
// TODO: What to do when receiving replies for a previous page's requests?
382+
383+
const transfer = intercept.transfer;
384+
try bc.intercept_state.put(transfer);
385+
386+
var challenge: AuthChallenge = undefined;
387+
var source: enum { server, proxy } = undefined;
388+
var it = transfer.responseHeaderIterator();
389+
while (it.next()) |hdr| {
390+
if (std.ascii.eqlIgnoreCase("WWW-Authenticate", hdr.name)) {
391+
source = .server;
392+
challenge = try AuthChallenge.parse(hdr.value);
393+
break;
394+
}
395+
if (std.ascii.eqlIgnoreCase("Proxy-Authenticate", hdr.name)) {
396+
source = .proxy;
397+
challenge = try AuthChallenge.parse(hdr.value);
398+
break;
399+
}
400+
}
401+
402+
try bc.cdp.sendEvent("Fetch.authRequired", .{
403+
.requestId = try std.fmt.allocPrint(arena, "INTERCEPT-{d}", .{transfer.id}),
404+
.request = network.TransferAsRequestWriter.init(transfer),
405+
.frameId = target_id,
406+
.resourceType = switch (transfer.req.resource_type) {
407+
.script => "Script",
408+
.xhr => "XHR",
409+
.document => "Document",
410+
},
411+
.authChallenge = .{
412+
.source = if (source == .server) "Server" else "Proxy",
413+
.origin = "", // TODO get origin, could be the proxy address for example.
414+
.scheme = if (challenge.scheme == .digest) "digest" else "basic",
415+
.realm = challenge.realm,
416+
},
417+
.networkId = try std.fmt.allocPrint(arena, "REQ-{d}", .{transfer.id}),
418+
}, .{ .session_id = session_id });
419+
420+
log.debug(.cdp, "request auth required", .{
421+
.state = "paused",
422+
.id = transfer.id,
423+
.url = transfer.uri,
424+
});
425+
// Await continueWithAuth
426+
427+
intercept.wait_for_interception.* = true;
428+
page.request_intercepted = true;
429+
}
430+
349431
// Get u64 from requestId which is formatted as: "INTERCEPT-{d}"
350432
fn idFromRequestId(request_id: []const u8) !u64 {
351433
if (!std.mem.startsWith(u8, request_id, "INTERCEPT-")) {

src/http/Client.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ pub const Transfer = struct {
559559
_handle: ?*Handle = null,
560560

561561
_redirecting: bool = false,
562+
_forbidden: bool = false,
562563

563564
fn deinit(self: *Transfer) void {
564565
self.req.headers.deinit();
@@ -738,6 +739,22 @@ pub const Transfer = struct {
738739
if (i >= ct.?.amount) break;
739740
}
740741

742+
// If the response requires an auth challenge and the auth
743+
// interception is enable, we must create a new request and wait
744+
// for the interception's response.
745+
// We won't call the req's callbacks now, but for the following request only.
746+
const status = transfer.response_header.?.status;
747+
if (status == 401 or status == 407) {
748+
if (transfer.client.notification) |notification| {
749+
var wait_for_interception = false;
750+
notification.dispatch(.http_request_auth_required, &.{ .transfer = transfer, .wait_for_interception = &wait_for_interception });
751+
if (wait_for_interception) {
752+
// The user is send an invitation to intercept this request.
753+
return buf_len;
754+
}
755+
}
756+
}
757+
741758
transfer.req.header_callback(transfer) catch |err| {
742759
log.err(.http, "header_callback", .{ .err = err, .req = transfer });
743760
// returning < buf_len terminates the request

src/notification.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub const Notification = struct {
6363
http_request_fail: List = .{},
6464
http_request_start: List = .{},
6565
http_request_intercept: List = .{},
66+
http_request_auth_required: List = .{},
6667
http_headers_done: List = .{},
6768
notification_created: List = .{},
6869
};
@@ -75,6 +76,7 @@ pub const Notification = struct {
7576
http_request_fail: *const RequestFail,
7677
http_request_start: *const RequestStart,
7778
http_request_intercept: *const RequestIntercept,
79+
http_request_auth_required: *const RequestAuthRequired,
7880
http_headers_done: *const ResponseHeadersDone,
7981
notification_created: *Notification,
8082
};
@@ -102,6 +104,11 @@ pub const Notification = struct {
102104
wait_for_interception: *bool,
103105
};
104106

107+
pub const RequestAuthRequired = struct {
108+
transfer: *Transfer,
109+
wait_for_interception: *bool,
110+
};
111+
105112
pub const ResponseHeadersDone = struct {
106113
transfer: *Transfer,
107114
};

0 commit comments

Comments
 (0)