webRequest: getting both request and response headers

Hi

I am trying to monitor browser network traffic and would like to get for a given requestId both the request the response headers. I can see the request headers listening onSendHeaders and the response headers with onResponseStarted or onHeadersReceived, but not both at the same place. I tried to maintain an object using the request id as the key and the request headers as value, so those headers are available in the response listener, but there is not a perfect match and that object keeps growing with requests I don’t get a response for, so I cannot delete the corresponding entry in the object. I could use a timer to purge the object but I am concerned with overall performance impact on the browser.

In the old addon SDK, there was a channel.visitRequestHeaders() function that could be called in the response listener, but this seems to have disappeared in webextensions.

Any advice on the best way to achieve this ?

Thanks.

you can try to use onCompleted and onErrorOccurred to as triggers to purge requestIds that are not needed anymore.

see my experimental JS class that handles network requests: github file

I also use a request class to contain each request content: github file

This is also kind of semi-off-topic, but just in case you also encounter this, at times I notice onCompleted fire before the full response stream is done, in case you’re also getting the request body via webRequest.filterResponseData.

That was it ! Thank you so much !!!