@@ -30,6 +30,7 @@ const network = @import("network.zig").network;
3030const emulation = @import ("emulation.zig" ).emulation ;
3131const fetch = @import ("fetch.zig" ).fetch ;
3232const performance = @import ("performance.zig" ).performance ;
33+ const IncomingMessage = @import ("msg.zig" ).IncomingMessage ;
3334
3435const log_cdp = std .log .scoped (.cdp );
3536
@@ -70,51 +71,29 @@ pub fn do(
7071 ctx : * Ctx ,
7172) ! []const u8 {
7273
73- // JSON scanner
74- var scanner = std . json . Scanner . initCompleteInput (alloc , s );
75- defer scanner .deinit ();
74+ // incoming message parser
75+ var msg = IncomingMessage . init (alloc , s );
76+ defer msg .deinit ();
7677
77- std .debug .assert (try scanner .next () == .object_begin );
78-
79- // handle 2 possible orders:
80- // - id, method <...>
81- // - method, id <...>
82- var method_key = try nextString (& scanner );
83- var method_token : std.json.Token = undefined ;
84- var id : ? u16 = null ;
85- // check swap order
86- if (std .mem .eql (u8 , method_key , "id" )) {
87- id = try getId (& scanner , method_key );
88- method_key = try nextString (& scanner );
89- method_token = try scanner .next ();
90- } else {
91- method_token = try scanner .next ();
92- }
93- try checkKey (method_key , "method" );
94-
95- // retrieve method
96- if (method_token != .string ) {
97- return error .WrongTokenType ;
98- }
99- const method_name = method_token .string ;
78+ const method = try msg .getMethod ();
10079
10180 // retrieve domain from method
102- var iter = std .mem .splitScalar (u8 , method_name , '.' );
81+ var iter = std .mem .splitScalar (u8 , method , '.' );
10382 const domain = std .meta .stringToEnum (Domains , iter .first ()) orelse
10483 return error .UnknonwDomain ;
10584
10685 // select corresponding domain
10786 const action = iter .next () orelse return error .BadMethod ;
10887 return switch (domain ) {
109- .Browser = > browser (alloc , id , action , & scanner , ctx ),
110- .Target = > target (alloc , id , action , & scanner , ctx ),
111- .Page = > page (alloc , id , action , & scanner , ctx ),
112- .Log = > log (alloc , id , action , & scanner , ctx ),
113- .Runtime = > runtime (alloc , id , action , & scanner , s , ctx ),
114- .Network = > network (alloc , id , action , & scanner , ctx ),
115- .Emulation = > emulation (alloc , id , action , & scanner , ctx ),
116- .Fetch = > fetch (alloc , id , action , & scanner , ctx ),
117- .Performance = > performance (alloc , id , action , & scanner , ctx ),
88+ .Browser = > browser (alloc , & msg , action , ctx ),
89+ .Target = > target (alloc , & msg , action , ctx ),
90+ .Page = > page (alloc , & msg , action , ctx ),
91+ .Log = > log (alloc , & msg , action , ctx ),
92+ .Runtime = > runtime (alloc , & msg , action , ctx ),
93+ .Network = > network (alloc , & msg , action , ctx ),
94+ .Emulation = > emulation (alloc , & msg , action , ctx ),
95+ .Fetch = > fetch (alloc , & msg , action , ctx ),
96+ .Performance = > performance (alloc , & msg , action , ctx ),
11897 };
11998}
12099
@@ -133,14 +112,6 @@ pub const State = struct {
133112// Utils
134113// -----
135114
136- fn nextString (scanner : * std.json.Scanner ) ! []const u8 {
137- const token = try scanner .next ();
138- if (token != .string ) {
139- return error .WrongTokenType ;
140- }
141- return token .string ;
142- }
143-
144115pub fn dumpFile (
145116 alloc : std.mem.Allocator ,
146117 id : u16 ,
@@ -158,10 +129,6 @@ pub fn dumpFile(
158129 defer alloc .free (p );
159130}
160131
161- fn checkKey (key : []const u8 , token : []const u8 ) ! void {
162- if (! std .mem .eql (u8 , key , token )) return error .WrongToken ;
163- }
164-
165132// caller owns the slice returned
166133pub fn stringify (alloc : std.mem.Allocator , res : anytype ) ! []const u8 {
167134 var out = std .ArrayList (u8 ).init (alloc );
@@ -229,97 +196,6 @@ pub fn sendEvent(
229196 try server .sendAsync (ctx , event_msg );
230197}
231198
232- fn getParams (
233- alloc : std.mem.Allocator ,
234- comptime T : type ,
235- scanner : * std.json.Scanner ,
236- key : []const u8 ,
237- ) ! ? T {
238-
239- // check key is "params"
240- if (! std .mem .eql (u8 , "params" , key )) return null ;
241-
242- // skip "params" if not requested
243- if (T == void ) {
244- var finished : usize = 0 ;
245- while (true ) {
246- switch (try scanner .next ()) {
247- .object_begin = > finished += 1 ,
248- .object_end = > finished -= 1 ,
249- else = > continue ,
250- }
251- if (finished == 0 ) break ;
252- }
253- return void {};
254- }
255-
256- // parse "params"
257- const options = std.json.ParseOptions {
258- .max_value_len = scanner .input .len ,
259- .allocate = .alloc_if_needed ,
260- };
261- return try std .json .innerParse (T , alloc , scanner , options );
262- }
263-
264- fn getId (scanner : * std.json.Scanner , key : []const u8 ) ! ? u16 {
265-
266- // check key is "id"
267- if (! std .mem .eql (u8 , "id" , key )) return null ;
268-
269- // parse "id"
270- return try std .fmt .parseUnsigned (u16 , (try scanner .next ()).number , 10 );
271- }
272-
273- fn getSessionId (scanner : * std.json.Scanner , key : []const u8 ) ! ? []const u8 {
274-
275- // check key is "sessionId"
276- if (! std .mem .eql (u8 , "sessionId" , key )) return null ;
277-
278- // parse "sessionId"
279- return try nextString (scanner );
280- }
281-
282- pub fn getMsg (
283- alloc : std.mem.Allocator ,
284- _id : ? u16 ,
285- comptime params_T : type ,
286- scanner : * std.json.Scanner ,
287- ) ! struct { id : u16 , params : ? params_T , sessionID : ? []const u8 } {
288- var id_msg : ? u16 = null ;
289- var params : ? params_T = null ;
290- var sessionID : ? []const u8 = null ;
291-
292- var t : std.json.Token = undefined ;
293-
294- while (true ) {
295- t = try scanner .next ();
296- if (t == .object_end ) break ;
297- if (t != .string ) {
298- return error .WrongTokenType ;
299- }
300- if (_id == null and id_msg == null ) {
301- id_msg = try getId (scanner , t .string );
302- if (id_msg != null ) continue ;
303- }
304- if (params == null ) {
305- params = try getParams (alloc , params_T , scanner , t .string );
306- if (params != null ) continue ;
307- }
308- if (sessionID == null ) {
309- sessionID = try getSessionId (scanner , t .string );
310- }
311- }
312-
313- // end
314- t = try scanner .next ();
315- if (t != .end_of_document ) return error .CDPMsgEnd ;
316-
317- // check id
318- if (_id == null and id_msg == null ) return error .RequestWithoutID ;
319-
320- return .{ .id = _id orelse id_msg .? , .params = params , .sessionID = sessionID };
321- }
322-
323199// Common
324200// ------
325201
0 commit comments