Skip to content

Commit

Permalink
Allow @host setting to determine which host to communicate with.
Browse files Browse the repository at this point in the history
  • Loading branch information
pjdietz committed Feb 8, 2015
1 parent c6bf37f commit e3eab08
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
10 changes: 9 additions & 1 deletion rester/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 2 additions & 13 deletions rester/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions rester/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit e3eab08

Please sign in to comment.