Repository navigation
Http2 keepalive #5144
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
Merged
Merged
Http2 keepalive #5144
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
02ab78d
Example addon for saving streamed data including a small bug fix to m…
EndUser509 94367fa
Revert "Example addon for saving streamed data including a small bug …
EndUser509 adc6402
Merge branch 'mitmproxy:main' into master
EndUser509 b4df53e
Merge branch 'mitmproxy:main' into master
EndUser509 192cb80
Add https_ping_threshold option to enable keep-alive for HTTP/2 serve…
EndUser509 d18344b
Fixed test
EndUser509 015f2bd
Fix test
EndUser509 daf94da
Adding pragma
EndUser509 ced840b
Moved timer logic to _http2.py
EndUser509 935da9f
Merge branch 'main' into http2_keepalive
EndUser509 824be48
Small code improvement
EndUser509 7463718
Merge branch 'http2_keepalive' of github.com:EndUser509/mitmproxy int…
EndUser509 00491ac
Update mitmproxy/options.py
EndUser509 d6e4396
Update mitmproxy/options.py
EndUser509 4af9144
Update mitmproxy/proxy/commands.py
EndUser509 ac74eb1
Update mitmproxy/proxy/commands.py
EndUser509 8f8f9ed
Update mitmproxy/proxy/layers/http/_http2.py
EndUser509 301066c
Sending Wakup back to right client
EndUser509 7069fc0
Update mitmproxy/proxy/server.py
EndUser509 c6977c2
Update mitmproxy/proxy/server.py
EndUser509 b6fde2a
Update mitmproxy/proxy/server.py
EndUser509 ccdc66c
Incorporated suggested changes
EndUser509 8683c56
make `Wakeup` a `CommandCompleted` event.
mhils bece08f
nits
mhils eebf37b
Merge remote-tracking branch 'origin/main' into pr-5144
mhils f9e283b
nits nits
mhils File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,14 @@ | |
| import hpack | ||
| import hyperframe.frame | ||
| import pytest | ||
| import time | ||
| from h2.errors import ErrorCodes | ||
|
|
||
| from mitmproxy.connection import ConnectionState, Server | ||
| from mitmproxy.flow import Error | ||
| from mitmproxy.http import HTTPFlow, Headers, Request | ||
| from mitmproxy.net.http import status_codes | ||
| from mitmproxy.proxy.commands import CloseConnection, Log, OpenConnection, SendData | ||
| from mitmproxy.proxy.commands import CloseConnection, Log, OpenConnection, SendData, RequestWakeup | ||
| from mitmproxy.proxy.context import Context | ||
| from mitmproxy.proxy.events import ConnectionClosed, DataReceived | ||
| from mitmproxy.proxy.layers import http | ||
|
|
@@ -64,8 +65,9 @@ def decode_frames(data: bytes) -> List[hyperframe.frame.Frame]: | |
| return frames | ||
|
|
||
|
|
||
| def start_h2_client(tctx: Context) -> Tuple[Playbook, FrameFactory]: | ||
| def start_h2_client(tctx: Context, keepalive: int = 0) -> Tuple[Playbook, FrameFactory]: | ||
| tctx.client.alpn = b"h2" | ||
| tctx.options.http2_ping_keepalive = keepalive | ||
| frame_factory = FrameFactory() | ||
|
|
||
| playbook = Playbook(http.HttpLayer(tctx, HTTPMode.regular)) | ||
|
|
@@ -679,6 +681,7 @@ def kill(flow: HTTPFlow): | |
|
|
||
| class TestClient: | ||
| def test_no_data_on_closed_stream(self, tctx): | ||
| tctx.options.http2_ping_keepalive = 0 | ||
| frame_factory = FrameFactory() | ||
| req = Request.make("GET", "http://example.com/") | ||
| resp = { | ||
|
|
@@ -780,3 +783,64 @@ def test_request_smuggling_te(tctx): | |
| << CloseConnection(tctx.client) | ||
| ) | ||
| assert b"Connection-specific header field present" in err() | ||
|
|
||
|
|
||
| def test_request_keepalive(tctx, monkeypatch): | ||
| playbook, cff = start_h2_client(tctx, 58) | ||
| flow = Placeholder(HTTPFlow) | ||
| server = Placeholder(Server) | ||
| initial = Placeholder(bytes) | ||
|
|
||
| def advance_time(_): | ||
| t = time.time() | ||
| monkeypatch.setattr(time, "time", lambda: t + 60) | ||
|
|
||
| assert ( | ||
| playbook | ||
| >> DataReceived(tctx.client, | ||
| cff.build_headers_frame(example_request_headers, flags=["END_STREAM"]).serialize()) | ||
| << http.HttpRequestHeadersHook(flow) | ||
| >> reply() | ||
| << http.HttpRequestHook(flow) | ||
| >> reply() | ||
| << OpenConnection(server) | ||
| >> reply(None, side_effect=make_h2) | ||
| << RequestWakeup(58) | ||
| << SendData(server, initial) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should emulate a def test_request_keepalive(monkeypatch, tctx):
...
assert (playbook ...)
t = time.time()
monkeypatch.setattr(time, "time", lambda: t + 60)
assert (playbook >> Wakeup() ...)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll do this at the very end, when the rest is working. :) |
||
| >> reply(to=-2, side_effect=advance_time) | ||
| << SendData(server, b'\x00\x00\x08\x06\x00\x00\x00\x00\x0000000000') # ping frame | ||
| << RequestWakeup(58) | ||
| ) | ||
|
|
||
|
|
||
| def test_keepalive_disconnect(tctx, monkeypatch): | ||
| playbook, cff = start_h2_client(tctx, 58) | ||
| playbook.hooks = False | ||
| sff = FrameFactory() | ||
| server = Placeholder(Server) | ||
| wakeup_command = RequestWakeup(58) | ||
|
|
||
| http_response = ( | ||
| sff.build_headers_frame(example_response_headers).serialize() + | ||
| sff.build_data_frame(b"", flags=["END_STREAM"]).serialize() | ||
| ) | ||
|
|
||
| def advance_time(_): | ||
| t = time.time() | ||
| monkeypatch.setattr(time, "time", lambda: t + 60) | ||
|
|
||
| assert ( | ||
| playbook | ||
| >> DataReceived(tctx.client, | ||
| cff.build_headers_frame(example_request_headers, flags=["END_STREAM"]).serialize()) | ||
| << OpenConnection(server) | ||
| >> reply(None, side_effect=make_h2) | ||
| << wakeup_command | ||
| << SendData(server, Placeholder(bytes)) | ||
| >> DataReceived(server, http_response) | ||
| << SendData(tctx.client, Placeholder(bytes)) | ||
| >> ConnectionClosed(server) | ||
| << CloseConnection(server) | ||
| >> reply(to=wakeup_command, side_effect=advance_time) | ||
| << None | ||
| ) | ||
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.