forked from pgodschalk/swiperproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.py
143 lines (124 loc) · 4.81 KB
/
Util.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
# Copyright (c) 2014-2016 SwiperProxy Team
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish
# distribute, sublicense and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject
# to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import urlparse
import traceback
def remove_PATH_endpoint(path, config):
if path.startswith(config.https_endpoint):
lens = len(config.https_endpoint)
if lens:
path = path[lens-1:]
if path.startswith(config.http_endpoint):
lens = len(config.http_endpoint)
if lens:
path = path[lens-1:]
return path
def rewrite_URL_strip(url, config):
"""
Strip the proxy hostname part of the passed URL.
"""
try:
res = urlparse.urlsplit(url)
if res[1]:
newres = [ item for item in res ]
host = res[1].split(":")[0]
if host.endswith(config.hostname):
host = host.split(config.hostname)[0]
newres[1] = host
newres[2] = remove_PATH_endpoint(res[2], config)
url = urlparse.urlunsplit(newres)
except Exception, e:
pass
return url
def rewrite_URL(url, config, ssl, remote_host):
"""
Rewrite the URL and add the proxy's HTTP or HTTPS ports when
necessary. For absolute URLs without scheme, use the same scheme as
used to access the proxy (using the 'ssl' flag).
"""
try:
# Strip our own hostname for the rewrites to work.
url = rewrite_URL_strip(url,config)
res = urlparse.urlsplit(url)
need_rewrite = False
# Handle rewrites.
for (f, t) in config.rewrites:
if res[1] and res[1].split(":")[0].lower() == f.lower() \
and (res[0] == '' or res[0] == 'http' or res[0] == 'https'):
newres = [ item for item in res ]
host = t
# No scheme, use the scheme used to access proxy.
if res[0] == '': # res[0] == scheme
if ssl:
newres[0]='https'
else:
newres[0]='http'
# Add port of proxy.
if newres[0] == 'http':
port = config.http_port
elif newres[0] == 'https':
port = config.https_port
newres[1] = host + ":" + str(port)
url = urlparse.urlunsplit(newres)
return url
# Handle absolute HTTP or HTTPS URL.
newres = [ item for item in res ]
host = res[1].split(":")[0]
# No scheme, use the scheme used to access proxy.
if res[0] == '': # res[0] == scheme
if ssl:
newres[0]='https'
else:
newres[0]='http'
# Add the endpoint from URL scheme.
if newres[0] == 'http':
endpoint = config.http_endpoint
elif newres[0] == 'https':
endpoint = config.https_endpoint
# Change scheme and port if using a reverse proxy and if scheme
# is different
if using_reverseproxy(config):
if newres[0] != config.reverseproxy_scheme:
newres[0] = config.reverseproxy_scheme
# Add port of proxy.
if newres[0] == 'http':
port = config.http_port
elif newres[0] == 'https':
port = config.https_port
newres[1] = config.hostname + ":" + str(port)
if host == '':
host = remote_host
if str(newres[2]).startswith("/"):
newres[2] = endpoint + host + newres[2]
else:
newres[2] = endpoint + host + "/" + newres[2]
url = urlparse.urlunsplit(newres)
except Exception, e:
pass
return url
def using_reverseproxy(config):
"""
Returns True if we are using a reverse proxy
(config.http_endpoint != config.https_endpoint), False
otherwise.
"""
return not (config.http_endpoint == config.https_endpoint)