Skip to content

Commit ee561b0

Browse files
Merge pull request #353 from lightpanda-io/cdp-enable-security
Cdp enable security
2 parents 90fb90b + 82bbe78 commit ee561b0

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

src/cdp/cdp.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const Input = @import("msg.zig").Input;
3535
const inspector = @import("inspector.zig").inspector;
3636
const dom = @import("dom.zig").dom;
3737
const css = @import("css.zig").css;
38+
const security = @import("security.zig").security;
3839

3940
const log_cdp = std.log.scoped(.cdp);
4041

@@ -69,6 +70,7 @@ const Domains = enum {
6970
Emulation,
7071
Fetch,
7172
Performance,
73+
Security,
7274
};
7375

7476
// The caller is responsible for calling `free` on the returned slice.
@@ -112,6 +114,7 @@ pub fn dispatch(
112114
.Emulation => emulation(alloc, msg, action, ctx),
113115
.Fetch => fetch(alloc, msg, action, ctx),
114116
.Performance => performance(alloc, msg, action, ctx),
117+
.Security => security(alloc, msg, action, ctx),
115118
};
116119
}
117120

src/cdp/runtime.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ fn sendInspector(
129129
}
130130

131131
ctx.sendInspector(msg.json);
132-
return "";
132+
133+
if (msg.id == null) return "";
134+
135+
return result(alloc, msg.id.?, null, null, msg.sessionId);
133136
}
134137

135138
pub const AuxData = struct {

src/cdp/security.zig

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
2+
//
3+
// Francis Bouvier <[email protected]>
4+
// Pierre Tachoire <[email protected]>
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU Affero General Public License as
8+
// published by the Free Software Foundation, either version 3 of the
9+
// License, or (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU Affero General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Affero General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
const std = @import("std");
20+
21+
const server = @import("../server.zig");
22+
const Ctx = server.Ctx;
23+
const cdp = @import("cdp.zig");
24+
const result = cdp.result;
25+
const IncomingMessage = @import("msg.zig").IncomingMessage;
26+
const Input = @import("msg.zig").Input;
27+
28+
const log = std.log.scoped(.cdp);
29+
30+
const Methods = enum {
31+
enable,
32+
};
33+
34+
pub fn security(
35+
alloc: std.mem.Allocator,
36+
msg: *IncomingMessage,
37+
action: []const u8,
38+
ctx: *Ctx,
39+
) ![]const u8 {
40+
const method = std.meta.stringToEnum(Methods, action) orelse
41+
return error.UnknownMethod;
42+
43+
return switch (method) {
44+
.enable => enable(alloc, msg, ctx),
45+
};
46+
}
47+
48+
fn enable(
49+
alloc: std.mem.Allocator,
50+
msg: *IncomingMessage,
51+
_: *Ctx,
52+
) ![]const u8 {
53+
// input
54+
const input = try Input(void).get(alloc, msg);
55+
defer input.deinit();
56+
log.debug("Req > id {d}, method {s}", .{ input.id, "security.enable" });
57+
58+
return result(alloc, input.id, null, null, input.sessionId);
59+
}

src/cdp/target.zig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,17 @@ fn sendMessageToTarget(
453453
};
454454
const input = try Input(Params).get(alloc, msg);
455455
defer input.deinit();
456-
log.debug("Req > id {d}, method {s}", .{ input.id, "target.sendMessageToTarget" });
456+
log.debug("Req > id {d}, method {s} ({s})", .{ input.id, "target.sendMessageToTarget", input.params.message });
457457

458458
// get the wrapped message.
459459
var wmsg = IncomingMessage.init(alloc, input.params.message);
460460
defer wmsg.deinit();
461461

462-
const res = try cdp.dispatch(alloc, &wmsg, ctx);
462+
const res = cdp.dispatch(alloc, &wmsg, ctx) catch |e| {
463+
log.err("send message {d} ({s}): {any}", .{ input.id, input.params.message, e });
464+
// TODO dispatch error correctly.
465+
return e;
466+
};
463467

464468
// receivedMessageFromTarget event
465469
const ReceivedMessageFromTarget = struct {

0 commit comments

Comments
 (0)