forked from ixs/weaveclient-chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
71 lines (59 loc) · 2.34 KB
/
app.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
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import urlfetch
from google.appengine.ext import db
from webob import Request, Response
import logging
SERVER = "https://auth.services.mozilla.com"
class StorageNode(db.Model):
user = db.StringProperty()
url = db.StringProperty()
def getStorageNode(user):
node = list(StorageNode.gql('WHERE user = :1', user))
if node:
return node[0]
# We haven't got an entry, so let's fetch it from the server
url = SERVER + '/user/1.0/' + user + '/node/weave'
result = urlfetch.fetch(url)
node = StorageNode(user=user, url=result.content)
node.put()
logging.debug("Got storage node for user " + user + ": " + node.url)
return node
def weave_app(environ, start_response):
request = Request(environ)
response = Response()
# Poor man's URL dispatch.
path_elements = request.path.split('/')
if (request.path.startswith('/user/1.0/') and
request.path.endswith('/node/weave')):
# Client is asking for the Weave storage node. Tell it it's
# us, but remember the actual storage node for future
# reference.
user = path_elements[3]
getStorageNode(user)
response.body = request.application_url
elif request.path.startswith('/1.0/'):
user = path_elements[2]
node = getStorageNode(user)
url = node.url + request.path_qs
authorization = request.headers.get('Authorization')
if not authorization:
response.status = 401
response.headers['WWW-Authenticate'] = 'Basic realm="weave"'
response.body = "Unauthorized"
else:
copy = ['Authorization', 'Content-Type', 'Accept']
headers = dict((header, request.headers.get(header, ''))
for header in copy)
result = urlfetch.fetch(url, request.body, request.method, headers)
response.body = result.content
response.status = int(result.status_code)
response.headers['Content-Type'] = result.headers.get('Content-Type', '')
#TODO copy X-Weave headers as well if present
else:
response.status = 404
response.body = "Not Found"
return response(environ, start_response)
def main():
run_wsgi_app(weave_app)
if __name__ == "__main__":
main()