proxy.cgi 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python
  2. """This is a blind proxy that we use to get around browser
  3. restrictions that prevent the Javascript from loading pages not on the
  4. same server as the Javascript. This has several problems: it's less
  5. efficient, it might break some sites, and it's a security risk because
  6. people can use this proxy to browse the web and possibly do bad stuff
  7. with it. It only loads pages via http and https, but it can load any
  8. content type. It supports GET and POST requests."""
  9. import urllib2
  10. import cgi
  11. import sys, os
  12. # Designed to prevent Open Proxy type stuff.
  13. allowedHosts = ['www.openlayers.org', 'openlayers.org',
  14. 'labs.metacarta.com', 'world.freemap.in',
  15. 'prototype.openmnnd.org', 'geo.openplans.org',
  16. 'sigma.openplans.org', 'demo.opengeo.org',
  17. 'www.openstreetmap.org', 'sample.azavea.com',
  18. 'v2.suite.opengeo.org', 'v-swe.uni-muenster.de:8080',
  19. 'vmap0.tiles.osgeo.org', 'www.openrouteservice.org','192.168.1.241:8080']
  20. method = os.environ["REQUEST_METHOD"]
  21. if method == "POST":
  22. qs = os.environ["QUERY_STRING"]
  23. d = cgi.parse_qs(qs)
  24. if d.has_key("url"):
  25. url = d["url"][0]
  26. else:
  27. url = "http://www.openlayers.org"
  28. else:
  29. fs = cgi.FieldStorage()
  30. url = fs.getvalue('url', "http://www.openlayers.org")
  31. try:
  32. host = url.split("/")[2]
  33. if allowedHosts and not host in allowedHosts:
  34. print "Status: 502 Bad Gateway"
  35. print "Content-Type: text/plain"
  36. print
  37. print "This proxy does not allow you to access that location (%s)." % (host,)
  38. print
  39. print os.environ
  40. elif url.startswith("http://") or url.startswith("https://"):
  41. if method == "POST":
  42. length = int(os.environ["CONTENT_LENGTH"])
  43. headers = {"Content-Type": os.environ["CONTENT_TYPE"]}
  44. body = sys.stdin.read(length)
  45. r = urllib2.Request(url, body, headers)
  46. y = urllib2.urlopen(r)
  47. else:
  48. y = urllib2.urlopen(url)
  49. # print content type header
  50. i = y.info()
  51. if i.has_key("Content-Type"):
  52. print "Content-Type: %s" % (i["Content-Type"])
  53. else:
  54. print "Content-Type: text/plain"
  55. print
  56. print y.read()
  57. y.close()
  58. else:
  59. print "Content-Type: text/plain"
  60. print
  61. print "Illegal request."
  62. except Exception, E:
  63. print "Status: 500 Unexpected Error"
  64. print "Content-Type: text/plain"
  65. print
  66. print "Some unexpected error occurred. Error text was:", E