Skip to content

Parse request cookies and response set cookies - #111

Closed
jtung23 wants to merge 4 commits into
sitespeedio:mainfrom
jtung23:jtung-parse-cookies-set-cookies
Closed

jtung23 wants to merge 4 commits into
sitespeedio:mainfrom
jtung23:jtung-parse-cookies-set-cookies

Conversation

@jtung23

@jtung23 jtung23 commented Aug 25, 2023 •

Copy link
Copy Markdown

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

jtung23 commented Aug 25, 2023

Copy link
Copy Markdown
Author

lmk what you think @soulgalore!

Comment thread lib/entryFromResponse.js
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);
}

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
Member

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

@jtung23

jtung23 commented Sep 27, 2023

Copy link
Copy Markdown
Author

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!

soulgalore added a commit that referenced this pull request Nov 3, 2026
Chrome sends the real network headers and cookies in separate extra info events. They were handled in a way that gave wrong HARs: headers were appended so the same header showed up twice with different case, response cookies set only in the extra info were missing, and extra info that arrived before the first page event (the normal order with Puppeteer) was dropped, so those requests lost their cookies and most headers.

When the server answered a revalidation with a 304, its few headers replaced the headers of the 200 that Chrome served from cache. That removed headers like content-type and content-encoding, which tools like the Coach use to check compression and caching.

Headers are now merged, with the extra info winning, and extra info for a response with another status is ignored. Timings, sizes and the number of pages and entries do not change.

Based on the first implementation and the test case by jtung23 in #111.

Assisted-by: Claude Opus 5.5 [email protected]
@soulgalore

Copy link
Copy Markdown
Member

Thank you @jtung23 fixed it in #166

@soulgalore soulgalore closed this Nov 3, 2026
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