-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.py
executable file
·152 lines (129 loc) · 7.6 KB
/
main.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
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
#
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api.urlfetch import fetch
import datetime
import models
import hutils
import re
################################################################################
# SymPullCDN Configuration #
################################################################################
# #
# 1. Origin #
# The origin server will be mirrored by this instance of SymPullCDN #
# configure a full http:// path with a FQDN, trailing slash included #
origin = "http://replace*me/" #
# #
# 2. Cachable Codes #
# This is a list of HTTP Status Codes that will be cached when sent from #
# the origin. By default only 200 OK codes will be cached. Edit this #
# list only if you have a reason. #
# #
cache_codes = ( 200, ) #
# #
# #
################################################################################
# Compiled Regular Expressions
no_cache_regex = re.compile( "(no-cache|no-store|private)", re.IGNORECASE )
class Entity(db.Model):
uri = db.StringProperty(required=True)
LastModified = db.StringProperty()
headers = models.DictProperty()
expires = db.DateTimeProperty()
status = db.IntegerProperty()
content = db.BlobProperty(required=True)
class MainHandler(webapp.RequestHandler):
def get(self):
############################################################################################
# #
# Getting entity from cache, Passing to the user, possibly revalidating it #
# #
############################################################################################
entity = Entity.all().filter("uri =", self.request.path).get()
if entity:
# Revalidate if required. Note, revalidation here updates the
# request /after/ this one for the given entity.
if entity.expires <= datetime.datetime.now():
request_entity = fetch( origin + self.request.path, method="GET",
headers={"If-Modified-Since" : entity.LastModified} )
# If 304 JUST update the headers.
if request_entity.status_code == 304:
headers = dict(request_entity.headers)
entity.expires = hutils.get_expires( request_entity.headers )
entity.LastModified = hutils.get_header( "Last-Modified", request_entity.headers )
entity.save()
# If 200, update the content too.
elif request_entity.status_code == 200:
headers = dict(request_entity.headers)
entity.expires = hutils.get_expires( request_entity.headers )
entity.LastModified = hutils.get_header( "Last-Modified", request_entity.headers )
entity.content = request_entity.content
entity.save()
#Revalidation failed, send the entity stale and delete from the cache.
else:
for key in iter(entity.headers):
self.response.headers[key] = entity.headers[key]
self.response.set_status(entity.status)
self.response.headers["X-SymPullCDN-Status"] = "Hit[EVALIDFAIL]"
self.response.out.write(entity.content)
entity.delete()
return True
# See if we can send a 304
if "If-Modified-Since" in self.request.headers:
if self.request.headers["If-Modified-Since"] == entity.LastModified:
for key in iter(entity.headers):
self.response.headers[key] = entity.headers[key]
self.response.set_status(304)
self.response.headers["X-SymPullCDN-Status"] = "Hit[304]"
self.response.out.write(None)
return True
for key in iter(entity.headers):
self.response.headers[key] = entity.headers[key]
self.response.set_status(entity.status)
self.response.headers["X-SymPullCDN-Status"] = "Hit[200]"
self.response.out.write(entity.content)
return True
############################################################################################
# #
# Fetching The Entity, Passing it to the user, possibly storing it #
# #
############################################################################################
request_entity = fetch( origin + self.request.path, method="GET", payload=None )
# Respect no-cache and private
if "Cache-Control" in request_entity.headers:
m = no_cache_regex.match( request_entity.headers["Cache-Control"] )
if m:
self.response.headers["X-SymPullCDN-Status"] = "Miss[NoCtrl]"
for key in iter(request_entity.headers):
self.response.headers[key] = request_entity.headers[key]
self.response.out.write(request_entity.content)
return True
# Only Cache Specific Codes
if request_entity.status_code not in cache_codes:
self.response.headers["X-SymPullCDN-Status"] = "Miss[NoCode]"
for key in iter(request_entity.headers):
self.response.headers[key] = request_entity.headers[key]
self.response.set_status(request_entity.status_code)
self.response.out.write(request_entity.content)
return True
# Set up data to store.
entity = Entity(
uri = self.request.path,
headers = dict(request_entity.headers),
expires = hutils.get_expires( request_entity.headers ),
LastModified = hutils.get_header( "Last-Modified", request_entity.headers ),
status = request_entity.status_code,
content = request_entity.content).save()
for key in iter(request_entity.headers):
self.response.headers[key] = request_entity.headers[key]
self.response.headers["X-SymPullCDN-Status"] = "Miss[Cached]"
self.response.out.write(request_entity.content)
def main():
application = webapp.WSGIApplication([('/.*', MainHandler)],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()