From 3a19840c8dfe0b73f1efabd14af39f9ca6921424 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Mon, 24 Oct 2022 16:36:57 -0700 Subject: [PATCH] Always send the Host header first --- wpull/namevalue.py | 4 +++- wpull/protocol/http/request.py | 2 +- wpull/protocol/http/request_test.py | 11 +++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/wpull/namevalue.py b/wpull/namevalue.py index dcdb9f9f..134720e2 100644 --- a/wpull/namevalue.py +++ b/wpull/namevalue.py @@ -80,10 +80,12 @@ def __iter__(self): def __len__(self): return len(self._map) - def add(self, name, value): + def add(self, name, value, front=False): '''Append the name-value pair to the record.''' normalized_name = normalize_name(name, self._normalize_overrides) self._map[normalized_name].append(value) + if front: + self._map.move_to_end(normalized_name, last=False) def get_list(self, name): '''Return all the values for given name.''' diff --git a/wpull/protocol/http/request.py b/wpull/protocol/http/request.py index d2adedee..cef21e18 100644 --- a/wpull/protocol/http/request.py +++ b/wpull/protocol/http/request.py @@ -136,7 +136,7 @@ def prepare_for_send(self, full_url=False): url_info = self.url_info if 'Host' not in self.fields: - self.fields['Host'] = url_info.hostname_with_port + self.fields.add('Host', url_info.hostname_with_port, front=True) if not full_url: if url_info.query: diff --git a/wpull/protocol/http/request_test.py b/wpull/protocol/http/request_test.py index 316d6459..98021886 100644 --- a/wpull/protocol/http/request_test.py +++ b/wpull/protocol/http/request_test.py @@ -18,6 +18,17 @@ def test_request(self): b'\r\n'), request.to_bytes() ) + # Host should always be first, and automatically added + request = Request('http://example.com/robots.txt') + request.fields['Accept'] = 'text/plain' + request.prepare_for_send() + self.assertEqual( + (b'GET /robots.txt HTTP/1.1\r\n' + b'Host: example.com\r\n' + b'Accept: text/plain\r\n' + b'\r\n'), + request.to_bytes() + ) def test_request_parse(self): request = Request()