Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse request cookies and response set cookies #111

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jtung23
Copy link

@jtung23 jtung23 commented Aug 25, 2023

This change properly handles the cookies and set-cookies in requestWillBeSentExtraInfo and responseReceivedExtraInfo respectively. The methods were skipping instead of finding the request entries already stored before the page loads.

My use case is puppeteer where the request structure is

  1. requestWillBeSent
  2. requestWillBeSentExtraInfo
  3. responseReceivedExtraInfo
  4. responseReceived
  5. Page.frameStartedLoading

@jtung23
Copy link
Author

jtung23 commented Aug 25, 2023

lmk what you think @soulgalore!

Comment on lines 54 to 60
// Merging and de-duplicating because extraResponse headers contain the original response headers as well
if (entry.extraResponseInfo.headers) {
const mergedHeaders = new Set(
[...headers, ...entry.extraResponseInfo.headers].map(JSON.stringify)
);
headers = Array.from(mergedHeaders, JSON.parse);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only section that really jumped out to me (not a maintainer).

Is it possible that extraResponseInfo.headers always includes all of headers? if so, we can avoid all of this computation and just use that, yes?

I don't think we can rely on unique header names, so this solution certainly works.
Per RFC7230 3.2.2:

A sender MUST NOT generate multiple header fields with the same field
name in a message unless either the entire field value for that
header field is defined as a comma-separated list [i.e., #(values)]
or the header field is a well-known exception (as noted below).

A recipient MAY combine multiple header fields with the same field
name into one "field-name: field-value" pair, without changing the
semantics of the message, by appending each subsequent field value to
the combined field value in order, separated by a comma. The order
in which header fields with the same field name are received is
therefore significant to the interpretation of the combined field
value; a proxy MUST NOT change the order of these field values when
forwarding a message.

The only thing I could think of that might work better (and here we'd need some performance tests) is to use a Map instead.:

const list = [
  {name: 'a', value: 1},
  {name: 'a', value: 2},
  {name: 'b', value: 1},
  {name: 'b', value: 1},
]

function makeUniqueSet(list) {
  const map = new Map()
  list.forEach(l => map.set(`${l.name}-${l.value}`, l))
  const uniqueSet = [...map.values()]
  return uniqueSet;
}

Example Repl.it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Stephen! Made use of your suggestion and updated the PR. Will leave this comment open though to see if @soulgalore thinks this step is even necessary.

@soulgalore
Copy link
Member

Thanks @jtung23 I'll have a look this weekend.

@jtung23
Copy link
Author

jtung23 commented Sep 27, 2023

Hi @soulgalore, just wanted to follow up and see if you're able to review this PR. Our team would love to be able to view cookies in the HAR for our debugging purposes! Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants