Last active
August 29, 2015 14:15
-
-
Save auscompgeek/efdf68ec73e03192294b to your computer and use it in GitHub Desktop.
An App Engine proxy to the old NCSS Challenge site.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This Source Code Form is subject to the terms of the Mozilla Public | |
# License, v. 2.0. If a copy of the MPL was not distributed with this | |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
"A Google App Engine proxy to the old NCSS Challenge website." | |
import re | |
import webapp2 | |
from google.appengine.api import memcache, urlfetch | |
from urllib import urlencode | |
EXPIRATION_DELTA_SECONDS = 3600 | |
IGNORE_HEADERS = frozenset([ | |
# Ignore hop-by-hop headers | |
'connection', | |
'keep-alive', | |
'proxy-authenticate', | |
'proxy-authorization', | |
'te', | |
'trailers', | |
'transfer-encoding', | |
'upgrade', | |
]) | |
class MainHandler(webapp2.RequestHandler): | |
def get(self): | |
url = "http://challenge.ncss.edu.au" + self.request.path | |
if self.request.query_string: | |
url += "?" + self.request.query_string | |
data = urlfetch.fetch(url=url, headers=self.gen_request_headers()) | |
if data is None: | |
return self.error(404) | |
self.respond(data.content) | |
def post(self): | |
postdata = {} | |
for key in self.request.arguments(): | |
postdata[key] = self.request.get(key) | |
data = urlfetch.fetch( | |
url="http://challenge.ncss.edu.au" + self.request.path, | |
payload=urlencode(postdata), | |
method=urlfetch.POST, | |
headers=self.gen_request_headers(), | |
) | |
if data is None: | |
return self.error(404) | |
self.respond(data.content) | |
def gen_request_headers(self): | |
request_headers = {} | |
for key, value in self.request.headers.iteritems(): | |
lc_key = key.lower() | |
if lc_key not in IGNORE_HEADERS: | |
if lc_key == "referer": | |
request_headers.headers[key] = re.sub(r"https?://aucg-ncssproxy\.appspot\.com", | |
"http://challenge.ncss.edu.au", value) | |
else: | |
request_headers.headers[key] = value | |
return request_headers | |
def respond(self, content): | |
content = re.sub(r"http://(?:static\.)?challenge\.ncss\.edu\.au", | |
"", content) | |
for key, value in data.headers.iteritems(): | |
if key.lower() not in IGNORE_HEADERS: | |
self.response.headers[key] = value | |
self.response.out.write(content) | |
class StaticHandler(webapp2.RequestHandler): | |
def get(self): | |
url = "http://static.challenge.ncss.edu.au" + self.request.path | |
# memcache static to reduce load | |
data = memcache.get(url) | |
if data is None: | |
data = urlfetch.fetch(url) | |
memcache.add(url, data) | |
if data is None: | |
return self.error(404) | |
data.content = re.sub(r"http://(?:static\.)?challenge\.ncss\.edu\.au", | |
"", data.content) | |
self.response.headers["content-type"] = data.headers.get("content-type") | |
self.response.out.write(data.content) | |
app = webapp2.WSGIApplication([ | |
(r"/static/.*", StaticHandler), | |
(r"/.*", MainHandler) | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment