Add user agent to proxy
This commit is contained in:
parent
a389a76027
commit
f0ce51683f
1 changed files with 13 additions and 6 deletions
|
@ -201,8 +201,10 @@ def validate_url(request):
|
||||||
assert referer.hostname == local.hostname
|
assert referer.hostname == local.hostname
|
||||||
assert toproxy.hostname != "localhost"
|
assert toproxy.hostname != "localhost"
|
||||||
assert toproxy.netloc != local.netloc
|
assert toproxy.netloc != local.netloc
|
||||||
assert not socket.gethostbyname(toproxy.hostname).startswith('127.')
|
# clean this when in python 3.4
|
||||||
assert not socket.gethostbyname(toproxy.hostname).startswith('192.168.')
|
ipaddress = socket.gethostbyname(toproxy.hostname)
|
||||||
|
assert not ipaddress.startswith('127.')
|
||||||
|
assert not ipaddress.startswith('192.168.')
|
||||||
return url
|
return url
|
||||||
|
|
||||||
|
|
||||||
|
@ -214,13 +216,18 @@ class AjaxProxy(View):
|
||||||
url = validate_url(self.request)
|
url = validate_url(self.request)
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
return HttpResponseBadRequest()
|
return HttpResponseBadRequest()
|
||||||
|
headers = {
|
||||||
|
'User-Agent': 'uMapProxy +http://wiki.openstreetmap.org/wiki/UMap'
|
||||||
|
}
|
||||||
|
request = urllib2.Request(url, headers=headers)
|
||||||
|
opener = urllib2.build_opener()
|
||||||
try:
|
try:
|
||||||
proxied_request = urllib2.urlopen(url)
|
proxied_request = opener.open(request)
|
||||||
status_code = proxied_request.code
|
|
||||||
mimetype = proxied_request.headers.typeheader or mimetypes.guess_type(url)
|
|
||||||
content = proxied_request.read()
|
|
||||||
except urllib2.HTTPError as e:
|
except urllib2.HTTPError as e:
|
||||||
return HttpResponse(e.msg, status=e.code, mimetype='text/plain')
|
return HttpResponse(e.msg, status=e.code, mimetype='text/plain')
|
||||||
else:
|
else:
|
||||||
|
status_code = proxied_request.code
|
||||||
|
mimetype = proxied_request.headers.typeheader or mimetypes.guess_type(url)
|
||||||
|
content = proxied_request.read()
|
||||||
return HttpResponse(content, status=status_code, mimetype=mimetype)
|
return HttpResponse(content, status=status_code, mimetype=mimetype)
|
||||||
ajax_proxy = AjaxProxy.as_view()
|
ajax_proxy = AjaxProxy.as_view()
|
||||||
|
|
Loading…
Reference in a new issue