diff --git a/rester/http.py b/rester/http.py index 26efbaa..6dfbb4f 100644 --- a/rester/http.py +++ b/rester/http.py @@ -157,18 +157,26 @@ def run(self): timeout=self._timeout) try: - # Body + + # Body: encode and add Content-length header body_bytes = None if self.request.body: body_bytes = self.request.body.encode(self._encoding) if not self.request.get_header("Content-length"): self.request.headers.append(("Content-length", len(body_bytes))) + + # Insert a host header, if needed. + if not self.request.get_header("host"): + self.request.headers.append(("Host", self.request.host)) + # Method and Path conn.putrequest(self.request.method, self.request.full_path, True, True) + # Headers for key, value in self.request.headers: conn.putheader(key, value) conn.endheaders() + # Body if body_bytes: conn.send(body_bytes) diff --git a/rester/message.py b/rester/message.py index 4897833..90c7fb9 100644 --- a/rester/message.py +++ b/rester/message.py @@ -28,24 +28,13 @@ class Request(Message): def __init__(self): Message.__init__(self) - self._host = None + self.host = None self.protocol = "http" self.method = "GET" self.path = "/" self.port = None self.query = {} - @property - def host(self): - return self._host - - @host.setter - def host(self, host): - self._host = host - # Add a host header, only if none exists. - if not self.get_header("host"): - self.headers.append(("Host", host)) - @property def full_path(self): """Path + query string for the request.""" @@ -62,7 +51,7 @@ def request_line(self): @property def uri(self): """Full URI, including protocol""" - uri = self.protocol + "://" + self._host + uri = self.protocol + "://" + self.host if self.port: uri += ":" + str(self.port) uri += self.full_path diff --git a/rester/parse.py b/rester/parse.py index 2f89c90..2011c6e 100644 --- a/rester/parse.py +++ b/rester/parse.py @@ -50,6 +50,7 @@ def get_request(self, text): for header in default_headers: self.request.headers.append(header) + self.request.host = self.settings.get("host", None) self.request.port = self.settings.get("port", None) self.request.protocol = self.settings.get("protocol", None)