seclerp / dotnet-chrome-protocol

A runtime library and schema code generation tools for Chrome DevTools Protocol support in C#/.NET.
8 stars 1 forks source link

Headers are empty #7

Closed artos027 closed 3 months ago

artos027 commented 3 months ago

Hi, thanx for library But, can you tell me why the headers are empty in all requests/responses?

PS. I tried Network.Requestwillbesend, Fetch.RequestPaused and so on, everywhere they are empty.

seclerp commented 3 months ago

Hi @artos027, may I ask you for a repro code?

artos027 commented 3 months ago

After connecting to Page and navigating to url

await pageClient.FireCommandAsync(Page.Enable()); await pageClient.FireCommandAsync(Network.Enable()); await pageClient.FireCommandAsync(Fetch.Enable()); //If I want to check Fetch.PausedRequest

and then a I tried

pageClient.SubscribeAsync(async req => { //try to catch req.Request.Headers, but in req "{}" empty });

All the information on the request is available, but the headers are empty

I'm launching a browser with a proxy extension, but I tried it without too

seclerp commented 3 months ago

Thanks for the repro, I will take a look. Additionally, I suggest you to setup event subscriptions before enabling the related domain, because otherwise you may skip some events which may happen in between these calls.

artos027 commented 3 months ago

Thank you so much, I'll be waiting

seclerp commented 3 months ago

Seems like it's because of the obscurity of Chrome DevTools Protocol type definitions. The definition for Headers type is

{
  "id": "Headers",
  "description": "Request / response headers as keys / values of JSON object.",
  "type": "object"
},

So it doesn't have any useful type information to properly map it to a dictionary or something else.

What we can do is generate such types which have no properties (HeadersType for example) as a subtype of JObject, which will allow us to preserve JSON context and dynamic properties information, as we are doing in other situations when the exact type is unknown.

The same will be done for "type": "array" using JArray in type definitions.

But please be aware that we are planning to migrate from Newtonsoft.Json to System.Text.Json in 2.0.0, so it will be a breaking change.

seclerp commented 3 months ago

The disadvantage of that approach is that you will need to access properties via indexer, like headers["key"] but I think it's not a huge problem.

artos027 commented 3 months ago

Now it's always an error Newtonsoft.Json.JsonSerializationException and it's impossible to read subscribes

pageClient.SubscribeAsync(async req => { //always error Newtonsoft.Json.JsonSerializationException });

seclerp commented 3 months ago

Please wait a bit before using the fresh sources, I'm still testing it

seclerp commented 3 months ago

Once ready, you will see a new release

artos027 commented 3 months ago

When's the new release?)

seclerp commented 3 months ago

1-2 days I suppose

seclerp commented 3 months ago

@artos027 please try one more time using code from main :)

artos027 commented 3 months ago

new error "System.MissingMethodException" ChromeProtocol.Domains.Network+HeadersType is missing in ObjectTypeConverter.cs in ChromeProtocol.Core

public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
  var jObject = JObject.Load(reader);
  var instance = existingValue ?? Activator.CreateInstance(objectType, jObject); // errror in this line

  return instance;
}
seclerp commented 3 months ago

Are you sure you installed ChromeProtocol.Domains package in all projects where it may be required at runtime? I'm not aware of your solution structure, so repro is required, since it works in my test suite.

seclerp commented 3 months ago

Also, which .NET target framework and runtime do you use? Does your language version cover records?

artos027 commented 3 months ago

I'll try again tomorrow and report back with result. .net fr 4.8 windows 11

seclerp commented 3 months ago

I was able to reproduce it in .NET Core 3.1 app, will investigate. Seems like some runtime inconsistency because of record types used in Domains assembly.

artos027 commented 3 months ago

Yeah, tried it again, same error. The last version didn't have this error.

seclerp commented 3 months ago

Please give it a try once you have time. Now it should work on older runtimes too.

artos027 commented 3 months ago

Hi, I checked everything(every subscribe with headers), it's all working as it should. Thank you so much!

seclerp commented 3 months ago

Thank you for checking! Will release packages soon.