|
17 | 17 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
18 | 18 |
|
19 | 19 | const std = @import("std"); |
| 20 | +const Notification = @import("../../notification.zig").Notification; |
| 21 | + |
| 22 | +const Allocator = std.mem.Allocator; |
20 | 23 |
|
21 | 24 | pub fn processMessage(cmd: anytype) !void { |
22 | 25 | const action = std.meta.stringToEnum(enum { |
23 | 26 | enable, |
| 27 | + disable, |
24 | 28 | setCacheDisabled, |
25 | 29 | }, cmd.input.action) orelse return error.UnknownMethod; |
26 | 30 |
|
27 | 31 | switch (action) { |
28 | | - .enable => return cmd.sendResult(null, .{}), |
| 32 | + .enable => return enable(cmd), |
| 33 | + .disable => return disable(cmd), |
29 | 34 | .setCacheDisabled => return cmd.sendResult(null, .{}), |
30 | 35 | } |
31 | 36 | } |
| 37 | + |
| 38 | +fn enable(cmd: anytype) !void { |
| 39 | + const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded; |
| 40 | + try bc.networkEnable(); |
| 41 | + return cmd.sendResult(null, .{}); |
| 42 | +} |
| 43 | + |
| 44 | +fn disable(cmd: anytype) !void { |
| 45 | + const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded; |
| 46 | + bc.networkDisable(); |
| 47 | + return cmd.sendResult(null, .{}); |
| 48 | +} |
| 49 | + |
| 50 | +pub fn httpRequestStart(arena: Allocator, bc: anytype, request: *const Notification.RequestStart) !void { |
| 51 | + // Isn't possible to do a network request within a Browser (which our |
| 52 | + // notification is tied to), without a page. |
| 53 | + std.debug.assert(bc.session.page != null); |
| 54 | + |
| 55 | + var cdp = bc.cdp; |
| 56 | + |
| 57 | + // all unreachable because we _have_ to have a page. |
| 58 | + const session_id = bc.session_id orelse unreachable; |
| 59 | + const target_id = bc.target_id orelse unreachable; |
| 60 | + const page = bc.session.currentPage() orelse unreachable; |
| 61 | + |
| 62 | + const document_url = try urlToString(arena, &page.url.uri, .{ |
| 63 | + .scheme = true, |
| 64 | + .authentication = true, |
| 65 | + .authority = true, |
| 66 | + .path = true, |
| 67 | + .query = true, |
| 68 | + }); |
| 69 | + |
| 70 | + const request_url = try urlToString(arena, request.url, .{ |
| 71 | + .scheme = true, |
| 72 | + .authentication = true, |
| 73 | + .authority = true, |
| 74 | + .path = true, |
| 75 | + .query = true, |
| 76 | + }); |
| 77 | + |
| 78 | + const request_fragment = try urlToString(arena, request.url, .{ |
| 79 | + .fragment = true, |
| 80 | + }); |
| 81 | + |
| 82 | + var headers: std.StringArrayHashMapUnmanaged([]const u8) = .empty; |
| 83 | + try headers.ensureTotalCapacity(arena, request.headers.len); |
| 84 | + for (request.headers) |header| { |
| 85 | + headers.putAssumeCapacity(header.name, header.value); |
| 86 | + } |
| 87 | + |
| 88 | + // We're missing a bunch of fields, but, for now, this seems like enough |
| 89 | + try cdp.sendEvent("Network.requestWillBeSent", .{ |
| 90 | + .requestId = try std.fmt.allocPrint(arena, "REQ-{d}", .{request.id}), |
| 91 | + .frameId = target_id, |
| 92 | + .loaderId = bc.loader_id, |
| 93 | + .documentUrl = document_url, |
| 94 | + .request = .{ |
| 95 | + .url = request_url, |
| 96 | + .urlFragment = request_fragment, |
| 97 | + .method = @tagName(request.method), |
| 98 | + .hasPostData = request.has_body, |
| 99 | + .headers = std.json.ArrayHashMap([]const u8){ .map = headers }, |
| 100 | + }, |
| 101 | + }, .{ .session_id = session_id }); |
| 102 | +} |
| 103 | + |
| 104 | +pub fn httpRequestComplete(arena: Allocator, bc: anytype, request: *const Notification.RequestComplete) !void { |
| 105 | + // Isn't possible to do a network request within a Browser (which our |
| 106 | + // notification is tied to), without a page. |
| 107 | + std.debug.assert(bc.session.page != null); |
| 108 | + |
| 109 | + var cdp = bc.cdp; |
| 110 | + |
| 111 | + // all unreachable because we _have_ to have a page. |
| 112 | + const session_id = bc.session_id orelse unreachable; |
| 113 | + const target_id = bc.target_id orelse unreachable; |
| 114 | + |
| 115 | + const url = try urlToString(arena, request.url, .{ |
| 116 | + .scheme = true, |
| 117 | + .authentication = true, |
| 118 | + .authority = true, |
| 119 | + .path = true, |
| 120 | + .query = true, |
| 121 | + }); |
| 122 | + |
| 123 | + var headers: std.StringArrayHashMapUnmanaged([]const u8) = .empty; |
| 124 | + try headers.ensureTotalCapacity(arena, request.headers.len); |
| 125 | + for (request.headers) |header| { |
| 126 | + headers.putAssumeCapacity(header.name, header.value); |
| 127 | + } |
| 128 | + |
| 129 | + // We're missing a bunch of fields, but, for now, this seems like enough |
| 130 | + try cdp.sendEvent("Network.responseReceived", .{ |
| 131 | + .requestId = try std.fmt.allocPrint(arena, "REQ-{d}", .{request.id}), |
| 132 | + .frameId = target_id, |
| 133 | + .loaderId = bc.loader_id, |
| 134 | + .response = .{ |
| 135 | + .url = url, |
| 136 | + .status = request.status, |
| 137 | + .headers = std.json.ArrayHashMap([]const u8){ .map = headers }, |
| 138 | + }, |
| 139 | + }, .{ .session_id = session_id }); |
| 140 | +} |
| 141 | + |
| 142 | +fn urlToString(arena: Allocator, url: *const std.Uri, opts: std.Uri.WriteToStreamOptions) ![]const u8 { |
| 143 | + var buf: std.ArrayListUnmanaged(u8) = .empty; |
| 144 | + try url.writeToStream(opts, buf.writer(arena)); |
| 145 | + return buf.items; |
| 146 | +} |
0 commit comments