fix(httpx): honor session cookies across HTTP client request paths - #2104
fix(httpx): honor session cookies across HTTP client request paths#2104Ayush7614 wants to merge 7 commits into
Conversation
Httpx send_request/stream now send outbound session cookies like crawl. Impit respects persist_cookies_per_session, keys the client cache by jar identity, and closes cached clients on cleanup. Httpx fingerprint headers are generated from a single profile so Accept and User-Agent stay consistent.
There was a problem hiding this comment.
Pull request overview
This PR fixes inconsistent cookie handling across HTTP client request paths so that session cookies are reliably sent (and optionally persisted) whether requests go through crawler navigation (crawl) or handler-level calls (send_request/stream). It also makes Httpx’s fingerprint-derived headers internally consistent by sourcing Accept, Accept-Language, and User-Agent from a single generated fingerprint profile.
Changes:
- Httpx:
send_request/streamnow attach outbound session cookies via the shared request-building path (matchingcrawlbehavior). - Impit: implements
persist_cookies_per_sessionsemantics, caches clients by(proxy, cookie-jar identity), and adds client closing on cleanup/eviction. - Tests: adds coverage for cookie sending/persistence toggles, single-fingerprint headers, and Impit cleanup cache reset.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/unit/http_clients/test_http_clients.py | Adds unit coverage for session cookie sending in send_request/stream, persistence on/off behavior, single-fingerprint headers, and Impit cache cleanup. |
| src/crawlee/http_clients/_impit.py | Honors cookie persistence flag by using a resolved jar (shared vs copy), introduces client caching keyed by proxy + cookie jar identity, and closes cached clients on cleanup/eviction. |
| src/crawlee/http_clients/_httpx.py | Ensures handler-level requests attach session cookies and derives Accept/Accept-Language/User-Agent from one fingerprint profile. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Mantisus
left a comment
There was a problem hiding this comment.
Thank you for your contributions!
When persist_cookies_per_session is False, _resolve_cookie_jar builds a fresh jar for every request, and since the cache key includes the jar identity, that means a new AsyncClient per request. Consider building the Cookie header directly instead of passing a CookieJar to AsyncClient, then the client can stay cached and shared.
For example:
import urllib.request
from http.cookiejar import CookieJar
def get_cookie_header(jar: CookieJar, url: str, headers: HttpHeaders | None = None) -> str:
request = urllib.request.Request(url, headers=dict(headers) if headers else {})
jar.add_cookie_header(request)
return request.get_header('Cookie', '')Send cookies via Cookie header when persist_cookies_per_session is False so clients stay cached, and use LRUCache.popitem for eviction without fire-and-forget close tasks.
|
Thanks for the review @Mantisus! Addressed in
|
Mantisus
left a comment
There was a problem hiding this comment.
Thanks for the update, just a few more points
Simplify cookie header merge, drop redundant LRU popitem, and move client-specific Httpx/Impit tests into dedicated test modules.
|
Thanks @Mantisus — addressed in
|
Mantisus
left a comment
There was a problem hiding this comment.
Please update the PR description to match the current state, otherwise LGTM
|
Updated the PR description to match the current implementation. Thanks @Mantisus! |
Key httpx clients by session jar so cookies survive redirects, reuse _build_request from crawl, simplify Impit LRU keying by jar identity, deprecate unused HeaderGenerator helpers, and cover redirect plus persist=False cookie-header paths in tests.
Mocking get_specific_headers could not catch a regression that mixes Accept and User-Agent from separate fingerprint generations.
|
Thanks @vdusek Addressed in d4f5fae / 6aaadc4:
|
Per-session client caching pinned jars and grew without bound. Re-apply cookies in the transport on each hop (including redirects) instead so one AsyncClient per proxy stays reusable across sessions.
`async with HttpxHttpClient() as client` widens to HttpClient, so ty rejected access to `_client_by_proxy_url`.
|
Type-check failures on the previous run came from Fixed in bf9a8d0 by binding the |
|
cc: @vdusek |
| headers=dict(headers) if headers else None, | ||
| content=payload, | ||
| extensions={'crawlee_session': session if self._persist_cookies_per_session else None}, | ||
| cookies=session.cookies.jar if session else None, |
There was a problem hiding this comment.
With this new approach that uses transport, cookies=session.cookies.jar doesn't make sense
Also, the session is already being passed to the transport. Use the session's jar no need to pass it separately.
Summary
send_request/stream/crawlshare_build_requestfor outbound session cookies. Clients stay shared per proxy (not per session); cookies are re-applied in_HttpxTransporton every hop so they survive redirects without pinning a client to each jar.persist_cookies_per_session. When persistence is enabled, the session jar is attached to the client; when disabled, existing cookies are sent via aCookieheader so the shared client stays cached. Clients are cached by(proxy, CookieJar)identity.Accept/Accept-Language/User-Agentcome from a single fingerprint profile.HeaderGenerator.get_common_headers/get_random_user_agent_headerare deprecated in favor ofget_specific_headers.Why
context.send_request()silently dropped session cookies under Httpx, breaking auth that worked for navigation (including after redirects). Impit's documentedpersist_cookies_per_session=Falsenever took effect because the session jar was always attached and mutated in place.Test plan
tests/unit/http_clients/test_http_clients.py— shared cookie send/persist/redirect coverage across clientstests/unit/http_clients/test_httpx.py— single-generate()fingerprint headers; proxy-only client cache across sessionstests/unit/http_clients/test_impit.py— Cookie-header path when persist is disabled