From 4d8cdc6dc897bf855edac1fa1caea923f19dc221 Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Wed, 17 Apr 2024 14:18:18 +0200 Subject: [PATCH] Handle sessionId in result Signed-off-by: Francis Bouvier --- src/cdp/browser.zig | 4 ++-- src/cdp/cdp.zig | 51 +++++++++++++++++++++++++------------------ src/cdp/emulation.zig | 4 ++-- src/cdp/log.zig | 6 ++--- src/cdp/network.zig | 6 ++--- src/cdp/page.zig | 37 +++++++++++++++++-------------- src/cdp/runtime.zig | 8 +++---- src/cdp/target.zig | 9 ++++---- 8 files changed, 67 insertions(+), 58 deletions(-) diff --git a/src/cdp/browser.zig b/src/cdp/browser.zig index b7071927..5ba2d05b 100644 --- a/src/cdp/browser.zig +++ b/src/cdp/browser.zig @@ -52,7 +52,7 @@ fn browserGetVersion( .userAgent = UserAgent, .jsVersion = JsVersion, }; - return result(alloc, id, Res, res); + return result(alloc, id, Res, res, null); } fn browserSetDownloadBehavior( @@ -69,5 +69,5 @@ fn browserSetDownloadBehavior( }; const params = try getParams(alloc, Params, scanner); std.log.debug("params {any}", .{params}); - return result(alloc, id, null, null); + return result(alloc, id, null, null, null); } diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index c67e933c..c3a93e89 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -78,8 +78,6 @@ fn checkKey(key: []const u8, token: []const u8) !void { if (!std.mem.eql(u8, key, token)) return error.WrongToken; } -const resultNull = "{{\"id\": {d}, \"result\": {{}}}}"; - // caller owns the slice returned pub fn stringify(alloc: std.mem.Allocator, res: anytype) ![]const u8 { var out = std.ArrayList(u8).init(alloc); @@ -94,20 +92,31 @@ pub fn stringify(alloc: std.mem.Allocator, res: anytype) ![]const u8 { return ret; } +const resultNull = "{{\"id\": {d}, \"result\": {{}}}}"; +const resultNullSession = "{{\"id\": {d}, \"result\": {{}}, \"sessionId\": \"{s}\"}}"; + // caller owns the slice returned pub fn result( alloc: std.mem.Allocator, id: u64, comptime T: ?type, res: anytype, + sessionID: ?[]const u8, ) ![]const u8 { - if (T == null) return try std.fmt.allocPrint(alloc, resultNull, .{id}); + if (T == null) { + // No need to stringify a custom JSON msg, just use string templates + if (sessionID) |sID| { + return try std.fmt.allocPrint(alloc, resultNullSession, .{ id, sID }); + } + return try std.fmt.allocPrint(alloc, resultNull, .{id}); + } const Resp = struct { id: u64, result: T.?, + sessionId: ?[]const u8, }; - const resp = Resp{ .id = id, .result = res }; + const resp = Resp{ .id = id, .result = res, .sessionId = sessionID }; return stringify(alloc, resp); } @@ -125,32 +134,32 @@ pub fn getParams( return std.json.innerParse(T, alloc, scanner, options); } -pub fn getSessionID( - alloc: std.mem.Allocator, - scanner: *std.json.Scanner, -) ![]const u8 { - var n = (try scanner.next()).string; +pub fn getSessionID(scanner: *std.json.Scanner) !?[]const u8 { + + // if next token is the end of the object, there is no "sessionId" + const t = try scanner.next(); + if (t == .object_end) return null; + + var n = t.string; + + // if next token is "params" ignore them + // NOTE: will panic if it's not an empty "params" object + // TODO: maybe we should return a custom error here if (std.mem.eql(u8, n, "params")) { // ignore empty params _ = (try scanner.next()).object_begin; _ = (try scanner.next()).object_end; n = (try scanner.next()).string; } - try checkKey("sessionId", n); - const options = std.json.ParseOptions{ - .max_value_len = scanner.input.len, - .allocate = .alloc_if_needed, - }; - return std.json.innerParse([]const u8, alloc, scanner, options); + + // if next token is not "sessionId" there is no "sessionId" + if (!std.mem.eql(u8, n, "sessionId")) return null; + + // parse "sessionId" + return (try scanner.next()).string; } // Common // ------ pub const SessionID = "9559320D92474062597D9875C664CAC0"; - -pub const SessionIDResp = struct { - id: u64, - result: struct {} = .{}, - sessionId: []const u8, -}; diff --git a/src/cdp/emulation.zig b/src/cdp/emulation.zig index c5970a93..4276a17d 100644 --- a/src/cdp/emulation.zig +++ b/src/cdp/emulation.zig @@ -32,7 +32,7 @@ fn setEmulatedMedia( _: *std.json.Scanner, _: *Ctx, ) ![]const u8 { - return result(alloc, id, null, null); + return result(alloc, id, null, null, null); } fn setFocusEmulationEnabled( @@ -41,5 +41,5 @@ fn setFocusEmulationEnabled( _: *std.json.Scanner, _: *Ctx, ) ![]const u8 { - return result(alloc, id, null, null); + return result(alloc, id, null, null, null); } diff --git a/src/cdp/log.zig b/src/cdp/log.zig index b7c46ce6..4bbcb140 100644 --- a/src/cdp/log.zig +++ b/src/cdp/log.zig @@ -31,8 +31,6 @@ fn enable( scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { - return stringify(alloc, cdp.SessionIDResp{ - .id = id, - .sessionId = try cdp.getSessionID(alloc, scanner), - }); + const sessionID = try cdp.getSessionID(scanner); + return result(alloc, id, null, null, sessionID); } diff --git a/src/cdp/network.zig b/src/cdp/network.zig index a81bfdd2..ebc21cca 100644 --- a/src/cdp/network.zig +++ b/src/cdp/network.zig @@ -31,8 +31,6 @@ fn enable( scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { - return stringify(alloc, cdp.SessionIDResp{ - .id = id, - .sessionId = try cdp.getSessionID(alloc, scanner), - }); + const sessionID = try cdp.getSessionID(scanner); + return result(alloc, id, null, null, sessionID); } diff --git a/src/cdp/page.zig b/src/cdp/page.zig index 702b79c7..df490d1a 100644 --- a/src/cdp/page.zig +++ b/src/cdp/page.zig @@ -37,10 +37,8 @@ fn enable( scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { - return stringify(alloc, cdp.SessionIDResp{ - .id = id, - .sessionId = try cdp.getSessionID(alloc, scanner), - }); + const sessionID = try cdp.getSessionID(scanner); + return result(alloc, id, null, null, sessionID); } fn getFrameTree( @@ -50,17 +48,26 @@ fn getFrameTree( _: *Ctx, ) ![]const u8 { // TODO: dummy - return result(alloc, id, null, null); + return result(alloc, id, null, null, null); } fn setLifecycleEventsEnabled( alloc: std.mem.Allocator, id: u64, - _: *std.json.Scanner, + scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { + + // input + const Params = struct { + enabled: bool, + }; + _ = try getParams(alloc, Params, scanner); + const sessionID = try cdp.getSessionID(scanner); + + // output // TODO: dummy - return result(alloc, id, null, null); + return result(alloc, id, null, null, sessionID); } fn addScriptToEvaluateOnNewDocument( @@ -69,20 +76,18 @@ fn addScriptToEvaluateOnNewDocument( scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { + + // input const Params = struct { source: []const u8, worldName: ?[]const u8 = null, }; _ = try getParams(alloc, Params, scanner); + const sessionID = try cdp.getSessionID(scanner); + + // output const Res = struct { - id: u64, - result: struct { - identifier: []const u8 = "1", - } = .{}, - sessionId: []const u8, + identifier: []const u8 = "1", }; - return stringify(alloc, Res{ - .id = id, - .sessionId = try cdp.getSessionID(alloc, scanner), - }); + return result(alloc, id, Res, Res{}, sessionID); } diff --git a/src/cdp/runtime.zig b/src/cdp/runtime.zig index 75fdf60f..4bfec64e 100644 --- a/src/cdp/runtime.zig +++ b/src/cdp/runtime.zig @@ -33,10 +33,8 @@ fn enable( scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { - return stringify(alloc, cdp.SessionIDResp{ - .id = id, - .sessionId = try cdp.getSessionID(alloc, scanner), - }); + const sessionID = try cdp.getSessionID(scanner); + return result(alloc, id, null, null, sessionID); } fn runIfWaitingForDebugger( @@ -45,5 +43,5 @@ fn runIfWaitingForDebugger( _: *std.json.Scanner, _: *Ctx, ) ![]const u8 { - return result(alloc, id, null, null); + return result(alloc, id, null, null, null); } diff --git a/src/cdp/target.zig b/src/cdp/target.zig index 76776f95..168be940 100644 --- a/src/cdp/target.zig +++ b/src/cdp/target.zig @@ -2,9 +2,10 @@ const std = @import("std"); const server = @import("../server.zig"); const Ctx = server.Cmd; -const result = @import("cdp.zig").result; -const getParams = @import("cdp.zig").getParams; -const stringify = @import("cdp.zig").stringify; +const cdp = @import("cdp.zig"); +const result = cdp.result; +const getParams = cdp.getParams; +const stringify = cdp.stringify; const TargetMethods = enum { setAutoAttach, @@ -63,7 +64,7 @@ fn tagetSetAutoAttach( const attached = try stringify(alloc, AttachToTarget{}); try server.sendSync(ctx, attached); - return result(alloc, id, null, null); + return result(alloc, id, null, null, null); } fn tagetGetTargetInfo(