|
1 |
| -// Copyright (C) 2023-2024 Lightpanda (Selecy SAS) |
| 1 | +// Copyright (C) 2023-2025 Lightpanda (Selecy SAS) |
2 | 2 | //
|
3 | 3 | // Francis Bouvier <francis@lightpanda.io>
|
4 | 4 | // Pierre Tachoire <pierre@lightpanda.io>
|
|
17 | 17 | // along with this program. If not, see <https://www.gnu.org/licenses/>.
|
18 | 18 |
|
19 | 19 | const std = @import("std");
|
| 20 | +const Allocator = std.mem.Allocator; |
| 21 | +const Notification = @import("../../notification.zig").Notification; |
| 22 | +const log = @import("../../log.zig"); |
| 23 | +const Request = @import("../../http/Client.zig").Request; |
| 24 | +const Method = @import("../../http/Client.zig").Method; |
20 | 25 |
|
21 | 26 | pub fn processMessage(cmd: anytype) !void {
|
22 | 27 | const action = std.meta.stringToEnum(enum {
|
23 | 28 | disable,
|
| 29 | + enable, |
| 30 | + continueRequest, |
| 31 | + failRequest, |
24 | 32 | }, cmd.input.action) orelse return error.UnknownMethod;
|
25 | 33 |
|
26 | 34 | switch (action) {
|
27 |
| - .disable => return cmd.sendResult(null, .{}), |
| 35 | + .disable => return disable(cmd), |
| 36 | + .enable => return enable(cmd), |
| 37 | + .continueRequest => return continueRequest(cmd), |
| 38 | + .failRequest => return failRequest(cmd), |
28 | 39 | }
|
29 | 40 | }
|
| 41 | + |
| 42 | +// Stored in CDP |
| 43 | +pub const InterceptState = struct { |
| 44 | + const Self = @This(); |
| 45 | + waiting: std.AutoArrayHashMap(u64, Request), |
| 46 | + |
| 47 | + pub fn init(allocator: Allocator) !InterceptState { |
| 48 | + return .{ |
| 49 | + .waiting = std.AutoArrayHashMap(u64, Request).init(allocator), |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + pub fn deinit(self: *Self) void { |
| 54 | + self.waiting.deinit(); |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +const RequestPattern = struct { |
| 59 | + urlPattern: []const u8 = "*", // Wildcards ('*' -> zero or more, '?' -> exactly one) are allowed. Escape character is backslash. Omitting is equivalent to "*". |
| 60 | + resourceType: ?ResourceType = null, |
| 61 | + requestStage: RequestStage = .Request, |
| 62 | +}; |
| 63 | +const ResourceType = enum { |
| 64 | + Document, |
| 65 | + Stylesheet, |
| 66 | + Image, |
| 67 | + Media, |
| 68 | + Font, |
| 69 | + Script, |
| 70 | + TextTrack, |
| 71 | + XHR, |
| 72 | + Fetch, |
| 73 | + Prefetch, |
| 74 | + EventSource, |
| 75 | + WebSocket, |
| 76 | + Manifest, |
| 77 | + SignedExchange, |
| 78 | + Ping, |
| 79 | + CSPViolationReport, |
| 80 | + Preflight, |
| 81 | + FedCM, |
| 82 | + Other, |
| 83 | +}; |
| 84 | +const RequestStage = enum { |
| 85 | + Request, |
| 86 | + Response, |
| 87 | +}; |
| 88 | + |
| 89 | +const EnableParam = struct { |
| 90 | + patterns: []RequestPattern = &.{}, |
| 91 | + handleAuthRequests: bool = false, |
| 92 | +}; |
| 93 | +const ErrorReason = enum { |
| 94 | + Failed, |
| 95 | + Aborted, |
| 96 | + TimedOut, |
| 97 | + AccessDenied, |
| 98 | + ConnectionClosed, |
| 99 | + ConnectionReset, |
| 100 | + ConnectionRefused, |
| 101 | + ConnectionAborted, |
| 102 | + ConnectionFailed, |
| 103 | + NameNotResolved, |
| 104 | + InternetDisconnected, |
| 105 | + AddressUnreachable, |
| 106 | + BlockedByClient, |
| 107 | + BlockedByResponse, |
| 108 | +}; |
| 109 | + |
| 110 | +fn disable(cmd: anytype) !void { |
| 111 | + const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded; |
| 112 | + bc.fetchDisable(); |
| 113 | + return cmd.sendResult(null, .{}); |
| 114 | +} |
| 115 | + |
| 116 | +fn enable(cmd: anytype) !void { |
| 117 | + const params = (try cmd.params(EnableParam)) orelse EnableParam{}; |
| 118 | + if (params.patterns.len != 0) log.warn(.cdp, "Fetch.enable No patterns yet", .{}); |
| 119 | + if (params.handleAuthRequests) log.warn(.cdp, "Fetch.enable No auth yet", .{}); |
| 120 | + |
| 121 | + const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded; |
| 122 | + try bc.fetchEnable(); |
| 123 | + |
| 124 | + return cmd.sendResult(null, .{}); |
| 125 | +} |
| 126 | + |
| 127 | +pub fn requestPaused(arena: Allocator, bc: anytype, intercept: *const Notification.RequestIntercept) !void { |
| 128 | + var cdp = bc.cdp; |
| 129 | + |
| 130 | + // unreachable because we _have_ to have a page. |
| 131 | + const session_id = bc.session_id orelse unreachable; |
| 132 | + const target_id = bc.target_id orelse unreachable; |
| 133 | + |
| 134 | + // We keep it around to wait for modifications to the request. |
| 135 | + // NOTE: we assume whomever created the request created it with a lifetime of the Page. |
| 136 | + // TODO: What to do when receiving replies for a previous page's requests? |
| 137 | + |
| 138 | + try cdp.intercept_state.waiting.put(intercept.request.id.?, intercept.request.*); |
| 139 | + |
| 140 | + // NOTE: .request data preparation is duped from network.zig |
| 141 | + const full_request_url = try std.Uri.parse(intercept.request.url); |
| 142 | + const request_url = try @import("network.zig").urlToString(arena, &full_request_url, .{ |
| 143 | + .scheme = true, |
| 144 | + .authentication = true, |
| 145 | + .authority = true, |
| 146 | + .path = true, |
| 147 | + .query = true, |
| 148 | + }); |
| 149 | + const request_fragment = try @import("network.zig").urlToString(arena, &full_request_url, .{ |
| 150 | + .fragment = true, |
| 151 | + }); |
| 152 | + const headers = try intercept.request.headers.asHashMap(arena); |
| 153 | + // End of duped code |
| 154 | + |
| 155 | + try cdp.sendEvent("Fetch.requestPaused", .{ |
| 156 | + .requestId = try std.fmt.allocPrint(arena, "INTERCEPT-{d}", .{intercept.request.id.?}), |
| 157 | + .request = .{ |
| 158 | + .url = request_url, |
| 159 | + .urlFragment = request_fragment, |
| 160 | + .method = @tagName(intercept.request.method), |
| 161 | + .hasPostData = intercept.request.body != null, |
| 162 | + .headers = std.json.ArrayHashMap([]const u8){ .map = headers }, |
| 163 | + }, |
| 164 | + .frameId = target_id, |
| 165 | + .resourceType = ResourceType.Document, // TODO! |
| 166 | + .networkId = try std.fmt.allocPrint(arena, "REQ-{d}", .{intercept.request.id.?}), |
| 167 | + }, .{ .session_id = session_id }); |
| 168 | + |
| 169 | + // Await either continueRequest, failRequest or fulfillRequest |
| 170 | + intercept.wait_for_interception.* = true; |
| 171 | +} |
| 172 | + |
| 173 | +const HeaderEntry = struct { |
| 174 | + name: []const u8, |
| 175 | + value: []const u8, |
| 176 | +}; |
| 177 | + |
| 178 | +fn continueRequest(cmd: anytype) !void { |
| 179 | + const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded; |
| 180 | + const params = (try cmd.params(struct { |
| 181 | + requestId: []const u8, // "INTERCEPT-{d}" |
| 182 | + url: ?[]const u8 = null, |
| 183 | + method: ?[]const u8 = null, |
| 184 | + postData: ?[]const u8 = null, |
| 185 | + headers: ?[]const HeaderEntry = null, |
| 186 | + interceptResponse: bool = false, |
| 187 | + })) orelse return error.InvalidParams; |
| 188 | + if (params.postData != null or params.headers != null or params.interceptResponse) return error.NotYetImplementedParams; |
| 189 | + |
| 190 | + const request_id = try idFromRequestId(params.requestId); |
| 191 | + var waiting_request = (bc.cdp.intercept_state.waiting.fetchSwapRemove(request_id) orelse return error.RequestNotFound).value; |
| 192 | + |
| 193 | + // Update the request with the new parameters |
| 194 | + if (params.url) |url| { |
| 195 | + // The request url must be modified in a way that's not observable by page. So page.url is not updated. |
| 196 | + waiting_request.url = try bc.cdp.browser.page_arena.allocator().dupeZ(u8, url); |
| 197 | + } |
| 198 | + if (params.method) |method| { |
| 199 | + waiting_request.method = std.meta.stringToEnum(Method, method) orelse return error.InvalidParams; |
| 200 | + } |
| 201 | + |
| 202 | + log.info(.cdp, "Request continued by intercept", .{ .id = params.requestId }); |
| 203 | + try bc.cdp.browser.http_client.request(waiting_request); |
| 204 | + |
| 205 | + return cmd.sendResult(null, .{}); |
| 206 | +} |
| 207 | + |
| 208 | +fn failRequest(cmd: anytype) !void { |
| 209 | + const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded; |
| 210 | + var state = &bc.cdp.intercept_state; |
| 211 | + const params = (try cmd.params(struct { |
| 212 | + requestId: []const u8, // "INTERCEPT-{d}" |
| 213 | + errorReason: ErrorReason, |
| 214 | + })) orelse return error.InvalidParams; |
| 215 | + |
| 216 | + const request_id = try idFromRequestId(params.requestId); |
| 217 | + if (state.waiting.fetchSwapRemove(request_id) == null) return error.RequestNotFound; |
| 218 | + |
| 219 | + log.info(.cdp, "Request aborted by intercept", .{ .reason = params.errorReason }); |
| 220 | + return cmd.sendResult(null, .{}); |
| 221 | +} |
| 222 | + |
| 223 | +// Get u64 from requestId which is formatted as: "INTERCEPT-{d}" |
| 224 | +fn idFromRequestId(request_id: []const u8) !u64 { |
| 225 | + if (!std.mem.startsWith(u8, request_id, "INTERCEPT-")) return error.InvalidParams; |
| 226 | + return std.fmt.parseInt(u64, request_id[10..], 10) catch return error.InvalidParams; |
| 227 | +} |
0 commit comments