Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
79 lines (63 sloc) 2.37 KB
from urlparse import urlparse
from httplib2 import Http
from pyramid.httpexceptions import (HTTPForbidden, HTTPBadRequest,
HTTPBadGateway, HTTPNotAcceptable)
from pyramid.response import Response
allowed_content_types = (
"application/xml", "text/xml",
"application/vnd.ogc.se_xml", # OGC Service Exception
"application/", # OGC Service Exception
"application/vnd.ogc.success+xml", # OGC Success (SLD Put)
"application/vnd.ogc.wms_xml", # WMS Capabilities
"application/vnd.ogc.context+xml", # WMC
"application/vnd.ogc.gml", # GML
"application/vnd.ogc.sld+xml", # SLD
"application/", # KML
allowed_hosts = (
# list allowed hosts here (no port limiting)
forwarded_headers = (
# The proxy to use to make requests (default: None).
# Example usage:
# views.proxy_info = ProxyInfo(socks.SOCKS5, 'localhost', 1080)
proxy_info = None
def ogcproxy(request):
url = request.params.get("url")
if url is None:
return HTTPBadRequest("Missing url in the query string")
# check for full url
parsed_url = urlparse(url)
if not parsed_url.netloc or parsed_url.scheme not in ("http", "https"):
return HTTPBadRequest("Wrong scheme")
# forward request to target (without Host Header)
http = Http(
headers = {h: request.headers[h] for h in forwarded_headers if h in request.headers}
resp, content = http.request(
url, method=request.method, body=request.body, headers=headers
return HTTPBadGateway()
# check for allowed content types
if "content-type" in resp:
ct = resp["content-type"]
if not ct.split(";")[0] in allowed_content_types:
# allow any content type from allowed hosts (any port)
if parsed_url.netloc not in allowed_hosts:
return HTTPForbidden("Wrong returned content type")
return HTTPNotAcceptable("No returned content type")
response = Response(content, status=resp.status,
headers={"Content-Type": ct})
return response