Skip to content

fix(android): sync Cronet and token-refresh requests with CookieManager#73

Open
oferRounds wants to merge 3 commits intomargelo:mainfrom
oferRounds:pr/android-cookiemanager-cold-start
Open

fix(android): sync Cronet and token-refresh requests with CookieManager#73
oferRounds wants to merge 3 commits intomargelo:mainfrom
oferRounds:pr/android-cookiemanager-cold-start

Conversation

@oferRounds
Copy link
Copy Markdown
Contributor

@oferRounds oferRounds commented Apr 11, 2026

Summary

Aligns Android Cronet (NitroFetchClient) and the HttpURLConnection cold-start token-refresh path (AutoPrefetcher) with CookieManager: attach Cookie when the request has no Cookie header, and persist Set-Cookie from responses (including redirects).

Helps SAML/session flows where the session cookie lives in the WebView cookie jar.

Split from #71

This is PR 1 of 2 from the discussion on #71 (atomic PRs per review).

PR 2 (upstream): #74 — cold-start token-refresh outcome → JS (draft until this PR lands; rebase after merge).

Outcome-only diff vs this branch (fork stack): oferRounds#1

How to verify

Exercise cold-start prefetch + token refresh on Android with a session that relies on cookies set via WebView; confirm requests include the expected cookie and refreshed cookies are stored.

- NitroFetchClient: attach Cookie from CookieManager when request has no Cookie header;
 persist Set-Cookie from responses (including redirects).
- AutoPrefetcher: same for HttpURLConnection token refresh; persist Set-Cookie from refresh response.

Helps SAML/session flows where the session cookie lives in the WebView cookie jar.

Made-with: Cursor
@riteshshukla04
Copy link
Copy Markdown
Collaborator

Hey @oferRounds , Thanks for the PR .
The code looks correct. Just few minor changes needed

@oferRounds
Copy link
Copy Markdown
Contributor Author

Sure, let me know what :)

@riteshshukla04
Copy link
Copy Markdown
Collaborator

Sure, let me know what :)

Can you please look at my review comments 😅

}
}
}
cookieManager.flush()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This will run everytime even though we dont have cookies . Maybe we can add check here to run only if actually set-cookie exist

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Also maybe we flush once on onSucceeded . Idk if thats good . Ideally we should not flush on every redirect

for (header in setCookieHeaders) {
cookieManager.setCookie(responseUrl, header.value)
}
cookieManager.flush()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same as above

conn.doInput = true
if (body != null) conn.doOutput = true

var hasCookieHeader = false
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Lets create this in a function like NitroFetchClient.kt . Lets avoid 2 code doing the exact thing but in different ways

@oferRounds
Copy link
Copy Markdown
Contributor Author

good catches! Let me fix them

- NitroCookieSync: shared attach + Set-Cookie helpers; flush only when cookies applied
- Cronet: apply Set-Cookie on redirects without flush; flush once on success when
  redirects or final response stored cookies
- HttpURLConnection token refresh: reuse attach helper; Set-Cookie + flush only if needed

Made-with: Cursor
@oferRounds
Copy link
Copy Markdown
Contributor Author

Addressed the review feedback in a follow-up commit:

  • Set-Cookie / flush: Only flush when at least one Set-Cookie was applied; HttpURLConnection token refresh uses the same rule.
  • Cronet redirects: Apply Set-Cookie on each redirect without flush; one CookieManager.flush() on onSucceeded when any hop (redirect or final) stored cookies—so redirect-only cookies still persist if the final response has no Set-Cookie.
  • Deduped attach logic: New NitroCookieSync.kt with attachCookieFromManagerIfMissing + header checks used by both NitroFetchClient and AutoPrefetcher.

Let me know if you want anything tweaked.

HttpURLConnection follows redirects by default; conn.url returns the
final URL, which is the correct domain to associate Set-Cookie with.

Made-with: Cursor
@riteshshukla04
Copy link
Copy Markdown
Collaborator

Thanks. Looks much better now. Will see to merge tomorrow :)

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.

2 participants