I took a crack at it. Since I'm not sure how to test it, I have no idea if
it works. I'm just learning the "str.join()" trick so am not that
comfortable with it yet. At a minimum it returns the openlayers home page if
you simply go to http://127.0.0.1:8000/app/default/proxy as suggested in the
FAQ so that's a start. I also verified that it works if you POST to the
proxy and include the POST var "url=http://labs.metacarta.com".
def proxy():
import urllib, urllib2
import cgi
import sys, os
allowedHosts = ['www.openlayers.org', 'openlayers.org',
'labs.metacarta.com', 'world.freemap.in',
'prototype.openmnnd.org', 'geo.openplans.org',
'sigma.openplans.org', 'demo.opengeo.org',
'www.openstreetmap.org', 'sample.azavea.com',
'v2.suite.opengeo.org', 'v-swe.uni-muenster.de:8080',
'vmap0.tiles.osgeo.org', 'www.openrouteservice.org']
method = request.env.request_method
if method == "POST":
if request.vars.url:
url = request.vars.url
else:
url = "http://www.openlayers.org"
else:
fs = cgi.FieldStorage()
url = fs.getvalue('url', "http://www.openlayers.org")
host = url.split("/")[2]
if allowedHosts and not host in allowedHosts:
response = '\n'.join(["Status: 502 Bad Gateway",
"Content-Type: text/plain",
'',
"This proxy does not allow you to access that location
(%s)." % (host,),
'',
str(request.env)])
return response
elif url.startswith("http://") or url.startswith("https://"):
try:
if method == "POST":
body = urllib.urlencode(request.post_vars)
r = urllib2.Request(url, body)
y = urllib2.urlopen(r)
else:
y = urllib2.urlopen(url)
# print content type header
i = y.info()
if i.has_key("Content-Type"):
response = "Content-Type: %s\n\n%s" % (i["Content-Type"],
y.read())
else:
response = "Content-Type: text/plain\n\n%s" % (y.read(),)
y.close()
return response
except Exception, E:
response = '\n'.join(["Status: 500 Unexpected Error",
"Content-Type: text/plain",
'',
"Some unexpected error occurred. Error text was:",
str(E)])
return response
else:
response = '\n'.join(["Content-Type: text/plain",
'',
"Illegal request."])
return response