[xhr] Promote determinism in test for invalid HTTP (#16409)

* [xhr] Promote determinism in test for invalid HTTP

`xhr/response-method.htm` attempts to verify how the body of responses
to HEAD requests is interpreted. Although this is in direct violation of
HTTP [1], the Fetch specification includes instruction for how browsers
should handle it [2].

As originally written, the server script uses a high-level interface to
define the invalid response. As a result, no guarantee could be made
about how the data was segmented into TCP packets. This allowed the test
to violate HTTP in a distinct and unintentional way: data intended to be
interpreted as the body of a HEAD response could instead be interpreted
as the beginning of the response to the subsequent POST request.

Although some UAs happen to tolerate such data, others (e.g. Apple
Safari at the time of this writing) do not. In those contexts, the test
would fail inconsistently. Because this test is not intended to verify
how UAs interpret invalid bytes which *precede* an HTTP response, these
failures do  not indicate non-conformance.

Update the server to more precisely control how bytes are written to the
connection.

[1] https://tools.ietf.org/html/rfc7231#section-4.3.2
[2] https://fetch.spec.whatwg.org/#main-fetch

* fixup! [xhr] Promote determinism in test for invalid HTTP

* fixup! [xhr] Promote determinism in test for invalid HTTP
diff --git a/xhr/resources/echo-method.py b/xhr/resources/echo-method.py
index 5351d19..ccb46b6 100644
--- a/xhr/resources/echo-method.py
+++ b/xhr/resources/echo-method.py
@@ -1,6 +1,17 @@
+# This handler is designed to verify that UAs correctly discard the body of
+# responses to HTTP HEAD requests. If the response body is written to a
+# separate TCP packet, then this behavior cannot be verified. This handler uses
+# the response writer to ensure that the body is transmitted in the same packet
+# as the headers. In this way, non-conforming UAs will consistently fail the
+# associated test.
+
 def main(request, response):
-    response.send_body_for_head_request = True
-    headers = [("Content-type", "text/plain")]
     content = request.method
 
-    return headers, content
+    response.add_required_headers = False
+    response.writer.write('''HTTP/1.1 200 OK
+Content-type: text/plain
+Content-Length: {}
+
+{}'''.format(len(content), content))
+    response.writer.flush()