Repository navigation
Add DeflateAlterETag and mod_sed large input filter tests #1102
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
Open
PaulLodge
wants to merge
1
commit into
apache:trunk
Choose a base branch
from
PaulLodge:port2-filters
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import os | ||
| import random | ||
| import string | ||
|
|
||
| import pytest | ||
|
|
||
| from pyhttpd.conf import HttpdConf | ||
|
|
||
| # DeflateAlterETag controls how mod_deflate modifies the ETag header when | ||
| # compression is applied. Three modes exist: | ||
| # AddSuffix -- append a compression tag, so compressed and plain ETags differ | ||
| # NoChange -- leave the ETag alone, so both representations share one | ||
| # Remove -- strip the ETag from compressed responses entirely | ||
|
|
||
|
|
||
| class TestDeflateAlterEtag: | ||
|
|
||
| @pytest.fixture(autouse=True, scope='class') | ||
| def _class_scope(self, env): | ||
| # The ScriptSock path warning is harmless; it fires when the | ||
| # working-tree path is long (e.g. in CI worktrees). | ||
| env.httpd_error_log.add_ignored_lognos(["AH01254"]) | ||
| # A document large enough that mod_deflate actually compresses it. | ||
| doc_dir = os.path.join(env.server_dir, "htdocs", "test1") | ||
| body = ''.join(random.choices(string.ascii_lowercase, k=8000)) | ||
| for name in ("etag1.html", "etag2.html", "etag3.html"): | ||
| with open(os.path.join(doc_dir, name), "w") as f: | ||
| f.write(f"<html><body>{body}</body></html>") | ||
|
|
||
| def configure(self, env, alter_etag): | ||
| doc_root = os.path.join(env.server_dir, | ||
| "htdocs", "test1").replace('\\', '/') | ||
| conf = HttpdConf(env, extras={ | ||
| f"test1.{env.http_tld}": f""" | ||
| <Directory "{doc_root}"> | ||
| AddOutputFilterByType DEFLATE text/html | ||
| </Directory> | ||
| DeflateAlterETag {alter_etag} | ||
| """, | ||
| }) | ||
| conf.add_vhost_test1() | ||
| conf.install() | ||
| assert env.apache_restart() == 0 | ||
|
|
||
| def get_etag(self, env, path, accept_gzip=False): | ||
| url = env.mkurl("http", "test1", path) | ||
| options = ["-H", "Cache-Control: no-cache"] | ||
| if accept_gzip: | ||
| options.extend(["-H", "Accept-Encoding: gzip"]) | ||
| r = env.curl_get(url, options=options) | ||
| assert r.response, f"no response for {path}" | ||
| assert r.response["status"] == 200, \ | ||
| f"status {r.response['status']} for {path}" | ||
| return r.response["header"].get("etag") | ||
|
|
||
| # AddSuffix: the compressed ETag gets a suffix, so it differs from the | ||
| # plain one. | ||
| def test_filters_010_01_addsuffix_etags_differ(self, env): | ||
| self.configure(env, "AddSuffix") | ||
| etag_plain = self.get_etag(env, "/etag1.html", accept_gzip=False) | ||
| etag_gzip = self.get_etag(env, "/etag1.html", accept_gzip=True) | ||
| assert etag_plain is not None, "plain response should have an ETag" | ||
| assert etag_gzip is not None, "gzip response should have an ETag" | ||
| assert etag_plain != etag_gzip, \ | ||
| (f"AddSuffix: ETags should differ between plain and compressed, " | ||
| f"got {etag_plain} and {etag_gzip}") | ||
|
|
||
| # NoChange: the ETag is left as-is, so both representations match. | ||
| def test_filters_010_02_nochange_etags_match(self, env): | ||
| self.configure(env, "NoChange") | ||
| etag_plain = self.get_etag(env, "/etag2.html", accept_gzip=False) | ||
| etag_gzip = self.get_etag(env, "/etag2.html", accept_gzip=True) | ||
| assert etag_plain is not None, "plain response should have an ETag" | ||
| assert etag_gzip is not None, "gzip response should have an ETag" | ||
| assert etag_plain == etag_gzip, \ | ||
| (f"NoChange: ETags should be identical, " | ||
| f"got {etag_plain} and {etag_gzip}") | ||
|
|
||
| # Remove: the compressed response has no ETag at all. | ||
| def test_filters_010_03_remove_etag_stripped(self, env): | ||
| self.configure(env, "Remove") | ||
| etag_plain = self.get_etag(env, "/etag3.html", accept_gzip=False) | ||
| etag_gzip = self.get_etag(env, "/etag3.html", accept_gzip=True) | ||
| assert etag_plain is not None, "plain response should have an ETag" | ||
| assert etag_gzip is None, \ | ||
| f"Remove: gzip response should have no ETag, got {etag_gzip}" |
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 |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| from pyhttpd.conf import HttpdConf | ||
|
|
||
| # mod_sed must handle large request bodies without crashing. A historical | ||
| # issue caused a segmentation fault when a large body was processed through | ||
| # an InputSed filter due to integer overflow in buffer management. This | ||
| # test sends a multi-megabyte POST body through InputSed and verifies that | ||
| # the server responds and stays alive. | ||
|
|
||
| # 8 MB is enough to exercise the large-input paths without making the test | ||
| # impractically slow. | ||
| LARGE_SIZE = 8 * 1024 * 1024 | ||
|
|
||
| # Bound the request so a regression that stops answering fails the test | ||
| # rather than hanging the suite. | ||
| BOUNDED = ["--max-time", "60"] | ||
|
|
||
|
|
||
| class TestSedLargeInput: | ||
|
|
||
| @pytest.fixture(autouse=True, scope='class') | ||
| def _class_scope(self, env): | ||
| # The ScriptSock path warning is harmless; it fires when the | ||
| # working-tree path is long (e.g. in CI worktrees). | ||
| env.httpd_error_log.add_ignored_lognos(["AH01254"]) | ||
| doc_dir = os.path.join(env.server_dir, "htdocs", "test1") | ||
| with open(os.path.join(doc_dir, "target.html"), "w") as f: | ||
| f.write("This is the target page") | ||
| # Pre-create a large file to POST through the InputSed filter. | ||
| large_file = os.path.join(env.server_dir, "large_input.bin") | ||
| with open(large_file, "wb") as f: | ||
| chunk = b"A" * (1024 * 1024) | ||
| for _ in range(LARGE_SIZE // (1024 * 1024)): | ||
| f.write(chunk) | ||
|
|
||
| @staticmethod | ||
| def large_file_path(env): | ||
| return os.path.join(env.server_dir, "large_input.bin") | ||
|
|
||
| def test_filters_011_01_large_body_no_crash(self, env): | ||
| doc_root = os.path.join(env.server_dir, | ||
| "htdocs", "test1").replace('\\', '/') | ||
| conf = HttpdConf(env, extras={ | ||
| f"test1.{env.http_tld}": f""" | ||
| <Directory "{doc_root}"> | ||
| AddInputFilter SED html | ||
| InputSed "s/A/Z/g" | ||
| </Directory> | ||
| """, | ||
| }) | ||
| conf.add_vhost_test1() | ||
| conf.install() | ||
| assert env.apache_restart() == 0 | ||
| # POST the large file through the InputSed filter. | ||
| url = env.mkurl("http", "test1", "/target.html") | ||
| large_file = self.large_file_path(env) | ||
| r = env.curl_raw(url, timeout=60, options=list(BOUNDED) + [ | ||
| "-X", "POST", | ||
| "--data-binary", f"@{large_file}", | ||
| ]) | ||
| assert r.response, "no response: server may have crashed" | ||
| assert r.response["status"] in (200, 400), \ | ||
| f"unexpected status {r.response['status']}" | ||
| # The server must still be running after processing the large input. | ||
| assert env.is_live(), "httpd is not running after large input" | ||
| env.httpd_error_log.ignore_recent( | ||
| matches=[r".*exit signal Segmentation fault.*"]) | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That shouldn't be needed (right?) and we generally do want to tests to fail in that case