Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: sitescripts/testpages/web/sitekey_frame.py

Issue 29328589: Issue 3122 - Add testpages.web.sitekey_frame URL handler (Closed)
Left Patch Set: Removed unrelated commit that was accidentally uploaded previously Created Oct. 8, 2015, 11:26 a.m.
Right Patch Set: Further improvements Created Oct. 8, 2015, noon
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import base64 18 import base64
19 import M2Crypto 19 import M2Crypto
20 import os 20 import os
21 import urllib
22 21
23 from sitescripts.utils import get_config, get_template 22 from sitescripts.utils import get_config, get_template
24 from sitescripts.web import url_handler 23 from sitescripts.web import url_handler, request_path
25
26 # https://www.python.org/dev/peps/pep-0333/#url-reconstruction
27 def request_path(environ):
28 path = urllib.quote(environ.get("SCRIPT_NAME", "") +
29 environ.get("PATH_INFO", ""))
30 query_string = environ.get("QUERY_STRING", "")
31 if query_string:
32 path += "?" + urllib.quote(query_string)
33 return path
34 24
35 @url_handler("/sitekey-frame") 25 @url_handler("/sitekey-frame")
36 def sitekey_frame(environ, start_response): 26 def sitekey_frame(environ, start_response):
37 template_path, template_file = os.path.split( 27 template_path, template_file = os.path.split(
38 get_config().get("testpages", "sitekeyFrameTemplate") 28 get_config().get("testpages", "sitekeyFrameTemplate")
39 ) 29 )
40 template = get_template(template_file, template_path=template_path) 30 template = get_template(template_file, template_path=template_path)
41 31
42 key = M2Crypto.EVP.load_key(get_config().get("testpages", "sitekeyPath")) 32 key = M2Crypto.EVP.load_key(get_config().get("testpages", "sitekeyPath"))
43 key.sign_init() 33 key.sign_init()
44 key.sign_update(request_path(environ) + "\0") 34 key.sign_update("\0".join((
45 key.sign_update(environ["HTTP_HOST"] + "\0") 35 request_path(environ), environ["HTTP_HOST"], environ["HTTP_USER_AGENT"]
46 key.sign_update(environ["HTTP_USER_AGENT"]) 36 )))
47 37
48 public_key = base64.b64encode(key.as_der()) 38 public_key = base64.b64encode(key.as_der())
49 signature = base64.b64encode(key.final()) 39 signature = base64.b64encode(key.final())
50 40
51 start_response("200 OK", 41 start_response("200 OK",
52 [("Content-Type", "text/html; charset=utf-8"), 42 [("Content-Type", "text/html; charset=utf-8"),
53 ("X-Adblock-Key", "%s_%s" % (public_key, signature))]) 43 ("X-Adblock-Key", "%s_%s" % (public_key, signature))])
54 return [template.render({"public_key": public_key, 44 return [template.render({"public_key": public_key,
55 "signature": signature}).encode("utf-8")] 45 "signature": signature}).encode("utf-8")]
LEFTRIGHT

Powered by Google App Engine
This is Rietveld