Skip to content

Instantly share code, notes, and snippets.

@Python1320
Created August 22, 2014 13:29
Show Gist options
  • Save Python1320/bb1d05b41274e845547a to your computer and use it in GitHub Desktop.
Save Python1320/bb1d05b41274e845547a to your computer and use it in GitHub Desktop.
ETags Proxy for GMod http fetching and caching :x (does not work since no headers get transmitted, need to move header responses to body)
import webapp2
from google.appengine.api import memcache
from google.appengine.api import urlfetch
from google.appengine.ext import db
import hashlib
reason_texts = {
414:"fu",
504:"slow",
413:"wtf",
502:"download",
500:"ew"
}
class MainHandler(webapp2.RequestHandler):
def __cacheget(self,url):
self.cachekey = hashlib.md5(url).hexdigest()
data = memcache.get(self.cachekey)
if data is not None:
if isinstance(data, (int, long)):
self._process_response( status_code=data,cached=True)
return True
else:
l = len(data)
ETag = data[0]
furl = data[1]
self._process_response( ETag=ETag,furl=furl,status_code=200,cached=True)
return True
return False
def _process_response(self,ETag=None,furl=None,cached=False,status_code=None):
if status_code<0:
timeout = status_code == -504
self.response.set_status( -status_code, reason_texts.get(-status_code,None) )
self.response.write("")
memcache.add(self.cachekey, status_code, timeout and 30 or 60)
return
if status_code != 200:
data = status_code
if status_code == 404:
self.response.set_status(404,"fok")
else:
self.response.headers.add_header("x-status",str(status_code))
else:
if cached:
self.response.headers.add_header("x-cached","1")
if ETag:
self.response.headers.add_header("x-etag",ETag)
if furl:
self.response.headers.add_header("x-url",furl)
data = [ETag,furl]
self.response.write("")
memcache.add(self.cachekey, data, 60)
def get(self):
url = self.request.headers.get("url",False)
if not url or url=="":
return self.response.set_status(400,"fok off")
if len(url)>1024:
return self.response.set_status(414,"loongcat")
if self.__cacheget(url):
return
# no filesize limit :(
try:
result = urlfetch.fetch(url=url,
method=urlfetch.HEAD,
allow_truncated=True,
follow_redirects=True,
deadline=7,
validate_certificate=False #, headers={'Content-Type': 'application/x-www-form-urlencoded'}
)
except urlfetch.InvalidURLError as e:
return self._process_response(status_code=-414)
except urlfetch.DeadlineExceededError as e:
return self._process_response(status_code=-504)
except urlfetch.ResponseTooLargeError as e:
return self._process_response(status_code=-413)
except urlfetch.DownloadError as e:
return self._process_response(status_code=-502)
except urlfetch.SSLCertificateError as e:
return self._process_response(status_code=-500)
status_code = result.status_code
furl = result.final_url
ETag = result.headers.get("ETag",False)
return self._process_response( status_code=status_code,
ETag=ETag,
furl=furl)
app = webapp2.WSGIApplication([('/', MainHandler)], debug=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment