diff options
author | ianb <devnull@localhost> | 2006-04-03 20:33:27 +0000 |
---|---|---|
committer | ianb <devnull@localhost> | 2006-04-03 20:33:27 +0000 |
commit | cb7bdabc239c4002b380017cfbcdfe2a0e517f51 (patch) | |
tree | 79158e8f37a9d39ac197d3260c4ec8a2530bfb5d /paste/proxy.py | |
parent | 486b3ea1ad9a8be23f0f914aed30d105cee288ed (diff) | |
download | paste-cb7bdabc239c4002b380017cfbcdfe2a0e517f51.tar.gz |
Patch for paste.proxy from Brad Clements
Diffstat (limited to 'paste/proxy.py')
-rw-r--r-- | paste/proxy.py | 61 |
1 files changed, 50 insertions, 11 deletions
diff --git a/paste/proxy.py b/paste/proxy.py index b7c5167..24860ff 100644 --- a/paste/proxy.py +++ b/paste/proxy.py @@ -18,20 +18,43 @@ TODO: * Rewriting body? (Probably not on this one -- that can be done with a different middleware that wraps this middleware) + +* Example:: + + use = egg:Paste#proxy + address = http://server3:8680/exist/rest/db/orgs/sch/config/ + allowed_request_methods = GET + """ import httplib import urlparse +from paste import httpexceptions + +# Remove these headers from response (specify lower case header +# names): +filtered_headers = ( + 'transfer-encoding', +) + class Proxy(object): - def __init__(self, address): + def __init__(self, address, allowed_request_methods=()): self.address = address self.parsed = urlparse.urlsplit(address) self.scheme = self.parsed[0].lower() self.host = self.parsed[1] + self.path = self.parsed[2] + self.allowed_request_methods = [ + x.lower() for x in allowed_request_methods if x] + def __call__(self, environ, start_response): + if (self.allowed_request_methods and + environ['REQUEST_METHOD'].lower() not in self.allowed_request_methods): + return httpexceptions.HTTPBadRequest("Disallowed")(environ, start_response) + if self.scheme == 'http': ConnClass = httplib.HTTPConnection elif self.scheme == 'https': @@ -57,26 +80,42 @@ class Proxy(object): body = environ['wsgi.input'].read(length) else: body = '' - print (environ['REQUEST_METHOD'], - environ['PATH_INFO'], - body, headers) + + if self.path: + request_path = environ['PATH_INFO'] + if request_path[0] == '/': + request_path = request_path[1:] + + path = urlparse.urljoin(self.path, request_path) + else: + path = environ['PATH_INFO'] + conn.request(environ['REQUEST_METHOD'], - environ['PATH_INFO'], + path, body, headers) res = conn.getresponse() - # @@: 2.4ism: - headers_out = res.getheaders() + headers_out = [] + for header, value in res.getheaders(): + if header.lower() not in filtered_headers: + headers_out.append((header, value)) + status = '%s %s' % (res.status, res.reason) start_response(status, headers_out) # @@: Default? length = res.getheader('content-length') - body = res.read(int(length)) + if length is not None: + body = res.read(int(length)) + else: + body = res.read() conn.close() return [body] -def make_proxy(global_conf, address): +def make_proxy(global_conf, address, allowed_request_methods=""): """ Make a WSGI application that proxies to another address -- - 'address' should be the full URL. + 'address' should be the full URL ending with a trailing / + 'allowed_request_methods' is a space seperated list of request methods """ - return Proxy(address) + from paste.deploy.converters import aslist + allowed_request_methods = aslist(allowed_request_methods) + return Proxy(address, allowed_request_methods=allowed_request_methods) |