Skip to content
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

Always send the Host header first #472

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion wpull/namevalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'''
Expand Down
2 changes: 1 addition & 1 deletion wpull/protocol/http/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions wpull/protocol/http/request_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down