-
Notifications
You must be signed in to change notification settings - Fork 2
/
corsproxy.fcgi
executable file
·82 lines (63 loc) · 2.95 KB
/
corsproxy.fcgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
"""This is a blind proxy that we use to get around browser
restrictions that prevent the WebGL from loading textures not on the
same server as the Javascript. This has several problems: it's less
efficient, it might break some sites, and it's a security risk because
people can use this proxy to browse the web and possibly do bad stuff
with it.
Preferred solution for this cross-domain WebGL textures restriction
is usage of CORS HTTP headers on the server side.
"""
import urllib2
import cgi
import sys, os
import memcache
mc = memcache.Client(['127.0.0.1:11211'], debug=0)
# Designed to prevent Open Proxy type stuff.
allowedHosts = ['a.tile.openstreetmap.org','b.tile.openstreetmap.org','c.tile.openstreetmap.org',
'otile1.mqcdn.com','otile2.mqcdn.com','otile3.mqcdn.com','otile4.mqcdn.com',
'oatile1.mqcdn.com','oatile2.mqcdn.com','oatile3.mqcdn.com','oatile4.mqcdn.com',
'webglearth.googlecode.com','demo.mapserver.org',
'vmap0.tiles.osgeo.org',
'api.europeana.eu' ]
def myapp(environ, start_response):
fs = cgi.FieldStorage(environ['wsgi.input'], environ=environ)
url = fs.getvalue('url', "//URL parameter not specified")
try:
host = url.split("/")[2]
if allowedHosts and not host in allowedHosts:
start_response('502 Bad Gateway', [('Content-Type', 'text/plain')])
return ["This proxy does not allow you to access that location (%s)." % (host,) +
"\n\n"+str(environ)]
elif url.startswith("http://") or url.startswith("https://"):
# load from memcached
s = mc.get(url)
if s:
if url.endswith('png'):
start_response('200 OK', [('Content-Type', 'image/png'),('Cache-Control', 'max-age=3600')])
return [s]
else:
start_response('200 OK', [('Content-Type', 'image/jpeg'),('Cache-Control', 'max-age=3600')])
return [s]
y = urllib2.urlopen(url)
# content type header
i = y.info()
if i.has_key("Content-Type"):
start_response('200 OK', [('Content-Type', i["Content-Type"]),('Cache-Control', 'max-age=3600')])
else:
start_response('200 OK', [('Content-Type', 'text/plain')])
s = y.read()
y.close()
# save to memcached
if url.endswith('png') or url.endswith('jpg') or url.endswith('jpeg'):
mc.set(url, s)
return [s]
else:
start_response('404 Not Found', [('Content-Type', 'text/plain')])
return ["Illegal request."]
except Exception, E:
start_response('500 Unexpected Error', [('Content-Type', 'text/plain')])
return ["Some unexpected error occurred. Error text was: %s" % E]
if __name__ == '__main__':
from flup.server.fcgi import WSGIServer
WSGIServer(myapp).run()