diff --git a/test/modules/md/test_930_host_header.py b/test/modules/md/test_930_host_header.py new file mode 100644 index 00000000000..106351a0acd --- /dev/null +++ b/test/modules/md/test_930_host_header.py @@ -0,0 +1,28 @@ +# test mod_md with requests that carry no Host header + +from pyhttpd.backport import skip_not_backported + +from .md_conf import MDConf + + +class TestNoHost: + + @skip_not_backported("gh-974", "mod_md requests without a Host", + backported_in="2.4.71") + def test_md_930_001(self, env, request): + domain = env.get_request_domain(request) + conf = MDConf(env, local_ca=False, std_vhosts=False) + conf.add("MDRenewMode manual") + conf.add_md([domain]) + # an MD without a matching VirtualHost warns at startup + conf.add_vhost(domain, port=env.http_port) + conf.install() + assert env.apache_restart() == 0, f'{env.apachectl_stderr}' + + resp = env.raw_request("GET /.well-known/acme-challenge/123 HTTP/1.0\r\n\r\n") + assert resp.startswith(b'HTTP/1.1 404'), f'{resp!r}' + resp = env.raw_request("GET /.httpd/certificate-status HTTP/1.0\r\n\r\n") + assert resp.startswith(b'HTTP/1.1 404'), f'{resp!r}' + # control: with a Host the same resource is served + r = env.get_meta(domain, "/.httpd/certificate-status", use_https=False) + assert r.response['status'] == 200 diff --git a/test/pyhttpd/env.py b/test/pyhttpd/env.py index 3c07d1f7552..e3cced69d0e 100644 --- a/test/pyhttpd/env.py +++ b/test/pyhttpd/env.py @@ -5,6 +5,7 @@ import os import signal import shutil +import socket import stat import subprocess import sys @@ -1330,6 +1331,23 @@ def fin_response(response): fin_response(response) return r + def raw_request(self, data, timeout=10): + """Send *data* on a raw socket to the http port and return the whole + response, so tests needing a raw request do not each hand-roll one. + A str is sent UTF-8 encoded; the sending side is closed, so not for + keep-alive.""" + with socket.create_connection((self.http_addr, self.http_port), + timeout=timeout) as sock: + sock.sendall(data.encode() if isinstance(data, str) else data) + sock.shutdown(socket.SHUT_WR) + buf = b'' + while True: + chunk = sock.recv(4096) + if not chunk: + break + buf += chunk + return buf + def curl_raw(self, urls, timeout=10, options=None, insecure=False, force_resolve=True, no_stdout_list=False): if not isinstance(urls, list):