[py] Add high-level BiDi network response handler API#17623
Merged
Conversation
2 tasks
Phase 2 of the BiDi protocol API design: add_response_handler / remove_response_handler / clear_response_handlers with a Response object surface (status, headers, mime_type, set_status / set_headers / set_cookies / set_body) at the responseStarted intercept phase. Mutated bodies are delivered via network.provideResponse (carrying over the current status and headers); other mutations via network.continueResponse; untouched responses are continued unmodified so observers never stall the page. If provideResponse is unsupported at this phase (Firefox), the response is continued with the remaining mutations instead of staying blocked. The request/response registries now share a common base, and clear_request_handlers preserves response handlers (and vice versa). The response_started event is overridden to deliver raw dict params, matching the existing auth_required override, because the generated ResponseStartedParameters dataclass drops request/isBlocked/intercepts.
c40c1e7 to
3f73196
Compare
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔗 Related Issues
💥 What does this PR do?
Adds the doc-aligned high-level response interception API to
driver.network(Phase 2 of the BiDi Protocol API Design, following #17619):add_response_handler(callback)/add_response_handler(url_patterns, callback)with the same glob URL patterns as request handlers (*,**,?), plusremove_response_handler(handler_id)andclear_response_handlers()Responseobject at theresponseStartedintercept phase exposingurl,status,reason_phrase,headers,mime_type, withset_status/set_headers/set_cookies/set_bodymutators (BiDi does not expose the original body or parsed cookies at this phase, so those start empty)network.provideResponse(carrying over the current status/headers), other mutations vianetwork.continueResponse, untouched responses are continued unmodified — observer-only handlers never stall the pageprovideResponseat this phase (Firefox only supports thebodyparameter atbeforeRequestSent), the response is continued with the remaining mutations instead of leaving the page blocked🔧 Implementation Notes
_BaseHandlerRegistryinpy/private/_network_handlers.pyparameterized by intercept phase, event key and wrapper class;RequestHandlerRegistrybehavior is unchanged (all pre-existing unit tests pass unmodified)clear_request_handlers()(the legacy clear-everything sweep) now preserves response-handler intercepts and re-establishes the response registry's event subscription;clear_response_handlers()symmetrically only touches response handlersresponse_startedevent is overridden in the enhancement manifest to deliver raw dict params — the same fix the manifest already applies toauth_required/before_request, because the generatedResponseStartedParametersdataclass only keepsresponse, droppingrequest,isBlockedandintercepts(without this, blocked responses were never continued and navigation hung)🤖 AI assistance
Response/registry refactor in_network_handlers.py, manifest glue, and tests, from the BiDi Protocol API Design doc💡 Additional Considerations
test_change_response_bodyxfails on Firefox with the provideResponse phase limitation)cancel(), extra-headers API, Script module alignment🔄 Types of changes