OpenLayers OpenLayers

root/trunk/openlayers/examples/proxy.cgi

Revision 7992, 2.4 kB (checked in by crschmidt, 2 months ago)

missing comma in list, pointed out by brentp

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