File: proxy.cgi

package info (click to toggle)
openlayers 2.13.1%2Bds2-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,260 kB
  • sloc: javascript: 61,721; xml: 7,435; python: 907; sh: 44; makefile: 17
file content (87 lines) | stat: -rwxr-xr-x 2,778 bytes parent folder | download | duplicates (3)
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
83
84
85
86
87
#!/usr/bin/env python3


"""This is a blind proxy that we use to get around browser
restrictions that prevent the Javascript from loading pages 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.  It only loads pages via http and https, but it can load any
content type. It supports GET and POST requests."""

import cgi
import os
import sys
import urllib

import requests

# Designed to prevent Open Proxy type stuff.

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',
                'maps.wien.gv.at']

default_url = "http://www.openlayers.org"

method = os.environ["REQUEST_METHOD"]

if method == "POST":
    qs = os.environ["QUERY_STRING"]
    d = urllib.parse.parse_qs(qs)
    if "url" in d:
        url = d["url"][0]
    else:
        url = default_url
else:
    fs = cgi.FieldStorage()
    url = fs.getvalue('url', default_url)

try:
    host = url.split("/")[2]
    if allowedHosts and not host in allowedHosts:
        print("Status: 502 Bad Gateway")
        print("Content-Type: text/plain")
        print("")
        print("This proxy does not allow you to access that location (%s)." % (host,))
        print("")
        print(os.environ)
  
    elif url.startswith("http://") or url.startswith("https://"):
    
        if method == "POST":
            content_type = 'text/plain'
            if 'CONTENT_TYPE' in os.environ:
                content_type = os.environ["CONTENT_TYPE"]

            length = int(os.environ["CONTENT_LENGTH"])
            headers = {"Content-Type": content_type}
            body = sys.stdin.read(length)

            r = requests.post(url, data=body, headers=headers)
        else:
            r = requests.get(url)
        
        # print content type header
        if "Content-Type" in r.headers:
            print("Content-Type: %s" % (r.headers["Content-Type"]))
        else:
            print("Content-Type: text/plain")
        print("")
        
        print(r.text)
    else:
        print("Content-Type: text/plain")
        print("")
        print("Illegal request.")

except Exception as E:
    print("Status: 500 Unexpected Error")
    print("Content-Type: text/plain")
    print("")
    print("Some unexpected error occurred. Error text was:", E)