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

URL reconstruction #88

Merged
merged 4 commits into from Nov 29, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 28 additions & 3 deletions brubeck/request.py
Expand Up @@ -2,6 +2,7 @@
import json
import Cookie
import logging
import urlparse
import re

def parse_netstring(ns):
Expand All @@ -25,12 +26,13 @@ def to_unicode(s, enc='utf8'):
class Request(object):
"""Word.
"""
def __init__(self, sender, conn_id, path, headers, body, *args, **kwargs):
def __init__(self, sender, conn_id, path, headers, body, url, *args, **kwargs):
self.sender = sender
self.path = path
self.conn_id = conn_id
self.headers = headers
self.body = body
self.url_parts = urlparse.urlsplit(url) if isinstance(url, basestring) else url

if self.method == 'JSON':
self.data = json.loads(body)
Expand Down Expand Up @@ -179,6 +181,10 @@ def cookies(self):
self.clear_all_cookies()
return self._cookies

@property
def url(self):
return self.url_parts.geturl()

@staticmethod
def parse_msg(msg):
"""Static method for constructing a Request instance out of a
Expand All @@ -188,7 +194,13 @@ def parse_msg(msg):
headers, rest = parse_netstring(rest)
body, _ = parse_netstring(rest)
headers = json.loads(headers)
r = Request(sender, conn_id, path, headers, body)
# construct url from request
scheme = headers.get('URL_SCHEME', 'http')
netloc = headers.get('host')
path = headers.get('PATH')
query = headers.get('QUERY')
url = urlparse.SplitResult(scheme, netloc, path, query, None)
r = Request(sender, conn_id, path, headers, body, url)
r.is_wsgi = False
return r

Expand Down Expand Up @@ -218,7 +230,20 @@ def parse_wsgi_request(environ):
headers['cookie'] = headers['HTTP_COOKIE']
if 'HTTP_CONNECTION' in headers:
headers['connection'] = headers['HTTP_CONNECTION']
r = Request(sender, conn_id, path, headers, body)
# construct url from request
scheme = headers['wsgi.url_scheme']
netloc = headers.get('HTTP_HOST')
if not netloc:
netloc = headers['SERVER_NAME']
port = headers['SERVER_PORT']
if ((scheme == 'https' and port != '443') or
(scheme == 'http' and port != '80')):
netloc += ':' + port
path = headers.get('SCRIPT_NAME', '')
path += headers.get('PATH_INFO', '')
query = headers.get('QUERY_STRING', None)
url = urlparse.SplitResult(scheme, netloc, path, query, None)
r = Request(sender, conn_id, path, headers, body, url)
r.is_wsgi = True
return r

Expand Down