-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
374 lines (348 loc) · 14.3 KB
/
web.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# Copyright 2012 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import appengine
import zlib
import logging
import cgi
import urllib
import hashlib
import zlib
import struct
import binascii
import babygit
import repo
import stage
import store
import webapp2
from webapp2_extras import json
import app.common
import app.users # for authentication
#s = babygit.FsStore()
s = appengine.AEStore()
def init_test_repo():
babygit.put_test_repo(s)
r = repo.Repo(s)
stage.checkout(r)
tree = stage.getroot(r)
tree = stage.save(r, 'dir/newfile', 'This is a new file!\n', tree)
tree = stage.save(r, 'dir/anotherfile', 'This is another new file!\n', tree)
tree = stage.save(r, 'dir/newfile', 'Replace contents\n', tree)
stage.add(r, tree)
stage.commit(r, 'Author <[email protected]>', 'Test adding a new file\n')
def packetstr(payload):
return '%04x' % (len(payload) + 4) + payload
class handler(app.users.AuthenticatedHandler):
def __init__(self, request, response):
self.initialize(request, response)
self.store = s
self.repo = repo.Repo(s)
def smart_upload_pack(self, service):
response = [packetstr('# service=' + service + '\n')]
response.append('0000')
if service == 'git-upload-pack':
caps = 'multi_ack_detailed thin-pack no-progress'
elif service == 'git-receive-pack':
caps = 'report-status delete-refs'
inforefs = self.store.getinforefs()
head_sha = inforefs['refs/heads/master']
response.append(packetstr(head_sha + ' HEAD\x00' + caps + '\n'))
for ref, sha in inforefs.iteritems():
response.append(packetstr(sha + ' ' + ref + '\n'))
response.append('0000')
mimetype = 'application/x-' + service + '-advertisement'
self.response.headers['Content-Type'] = str(mimetype)
self.response.out.write(str(''.join(response)))
def get(self, arg):
if arg == 'HEAD':
self.response.out.write('ref: refs/heads/master\n')
elif arg == 'info/refs':
service = self.request.get('service')
if service in ('git-upload-pack', 'git-receive-pack'):
return self.smart_upload_pack(service)
inforefs = self.store.getinforefs()
infostr = ''.join(['%s\t%s\n' % (sha, ref) for
ref, sha in inforefs.iteritems()])
self.response.out.write(infostr)
elif arg.startswith('objects/') and arg[10] == '/':
sha = arg[8:10] + arg[11:49]
compressed = self.store.getobjz(sha)
self.response.headers['Content-Type'] = 'application/octet-stream'
self.response.out.write(compressed)
elif arg.startswith('edit/'):
if not self.has_write_perm:
return app.common.error_403(self)
editurl = arg[5:]
obj = self.repo.traverse(editurl)
if obj is None:
self.response.out.write('404 not found')
elif babygit.obj_type(obj) == 'blob':
contents = babygit.obj_contents(obj)
self.response.out.write('<html><title>Editing: ' + cgi.escape(editurl) + '</title>\n')
self.response.out.write('<body>\n')
self.response.out.write('<h1>Editing ' + cgi.escape(editurl) + '</h1>\n')
self.response.out.write('<form method="post" action="/git/save/' + urllib.quote(editurl) + '">\n')
self.response.out.write('<textarea cols="80" rows="24" name="content">\n')
for line in contents.split('\n'):
self.response.out.write(cgi.escape(line) + '\n')
self.response.out.write('</textarea>')
self.response.out.write('<br />\n')
self.response.out.write('<input type="submit" value="Save">\n')
elif arg.startswith('gc/'):
# TODO: should be POST, this should just serve the form
return self.serve_gc()
else:
# try to serve a raw blob
obj = self.repo.traverse(arg)
if obj is None:
self.response.out.write('404 not found')
elif babygit.obj_type(obj) == 'blob':
self.response.headers['Content-Type'] = 'text/plain; charset=UTF-8';
contents = babygit.obj_contents(obj)
self.response.out.write(contents)
else:
if len(arg) and not arg.endswith('/'):
url = self.request.url + '/'
self.redirect(url)
return
ptree = self.repo.parse_tree(obj)
html = ['<html><ul>']
for mode, name, sha in ptree:
fn = name
if mode == '40000': fn += '/'
html.append('<li><a href="' + fn + '">' + fn + '</a></li>\n')
self.response.out.write(''.join(html))
def parse_pktlines(self, data):
i = 0
result = []
while i < len(data):
size = int(data[i:i+4], 16)
if size == 0:
result.append(None)
i += 4
else:
result.append(data[i+4:i+size])
i += size
return result
# parse up through the first flush (useful for receive-pack)
def parse_pktlist(self, data):
i = 0
result = []
while i < len(data):
size = int(data[i:i+4], 16)
if size == 0:
return result, i + 4
result.append(data[i+4:i+size])
i += size
obj_types = {'commit': 1, 'tree': 2, 'blob': 3, 'tag': 4}
def encode_type_and_size(self, t, size):
t_num = self.obj_types[t]
hibit = 0x80 if size >= 16 else 0
result = [chr(hibit | (t_num << 4) | (size & 15))]
size >>= 4
while size:
hibit = 0x80 if size >= 128 else 0
result.append(chr(hibit | (size & 127)))
size >>= 7
return ''.join(result)
def send_packfile(self, objs, out):
m = hashlib.sha1()
header = 'PACK' + struct.pack('>II', 2, len(objs))
out.write(header)
m.update(header)
for ref, obj in objs:
t = babygit.obj_type(obj)
s = babygit.obj_size(obj)
#if s != len(babygit.obj_contents(obj)):
# logging.debug('size mismatch')
enc_t_s = self.encode_type_and_size(t, s)
out.write(enc_t_s)
m.update(enc_t_s)
compressed = zlib.compress(babygit.obj_contents(obj))
out.write(compressed)
m.update(compressed)
out.write(m.digest())
def parse_type_and_size(self, data, offset):
byte = ord(data[offset])
offset += 1
t = (byte >> 4) & 7
size = (byte & 15)
shift = 4
while byte & 0x80:
byte = ord(data[offset])
offset += 1
size += (byte & 0x7f) << shift
shift += 7
return t, size, offset
def read_zlib(self, data, offset):
result = []
max_block_size = 4096
d = zlib.decompressobj()
while True:
block_size = min(max_block_size, len(data) - offset)
block = d.decompress(data[offset:offset + block_size])
offset += block_size
result.append(block)
unused = d.unused_data
if unused:
offset -= len(unused)
break
return ''.join(result), offset
def process_packfile(self, data, offset):
if data[offset:offset+4] != 'PACK':
return 'bad header'
version, n_objs = struct.unpack('>II', data[offset+4:offset+12])
if version != 2:
return 'unknown version ' + `version`
offset += 12
for i in range(n_objs):
t, s, offset = self.parse_type_and_size(data, offset)
if t < 5:
raw, offset = self.read_zlib(data, offset)
if s != len(raw):
return 'size mismatch'
#logging.debug(`(t, raw)`)
self.store.put(store.obj_types[t], raw)
elif t == 7: # OBJ_REF_DELTA
refsha = binascii.hexlify(data[offset:offset + 20])
offset += 20
refobj = self.store.getobj(refsha)
if not refobj:
return 'reference obj ' + refsha + ' not found'
delta, offset = self.read_zlib(data, offset)
obj = store.patch_delta(refobj, delta)
sha = hashlib.sha1(obj).hexdigest()
self.store.putobj(sha, obj)
else:
return 'delta type ' + `t` + ' nyi'
return 'ok'
def post(self, arg):
if arg == 'git-upload-pack':
o = self.response.out
lines = self.parse_pktlines(self.request.body)
logging.debug(`lines`)
wants = set()
haves = set()
in_haves = False
ready = False
common = None
for l in lines:
if l is None:
if in_haves:
o.write(packetstr('NAK\n'))
logging.debug('NAK')
return
else:
in_haves = True
# TODO: parse have's
elif l.startswith('want '):
wants.add(l.rstrip('\n').split(' ')[1])
elif l.startswith('have '):
sha = l.rstrip('\n').split(' ')[1]
obj = self.repo.store.getobj(sha)
if obj is not None:
haves.add(sha)
common = sha
if not ready:
o.write(packetstr('ACK ' + sha + ' common\n'))
ready = False # TODO: actually compute
if ready:
o.write(packetstr('ACK ' + sha + ' ready\n'))
logging.debug('ACK ' + sha + ' ready')
objs = self.repo.walk(wants, haves)
self.response.headers['Content-Type'] = 'application/x-git-upload-pack-result'
if common is None:
o.write(packetstr('NAK\n'))
logging.debug('NAK')
else:
o.write(packetstr('ACK ' + common + '\n'))
logging.debug('ACK ' + common)
self.send_packfile(objs, o)
elif arg == 'git-receive-pack':
# Do authentication here, as this is the risky transaction
if not self.authenticate():
return
lines, offset = self.parse_pktlist(self.request.body)
#logging.debug(`lines` + ' offset=' + `offset`)
self.response.headers['Content-Type'] = 'application/x-git-upload-pack-result'
o = self.response.out
status = self.process_packfile(self.request.body, offset)
o.write(packetstr('unpack ' + status + '\n'))
if status == 'ok':
for l in lines:
l = l.rstrip('\n').split('\x00')[0]
oldsha, newsha, name = l.split(' ')
success = self.store.updateref(oldsha, newsha, name)
if success:
o.write(packetstr('ok ' + name + '\n'))
else:
o.write(packetstr('ng ' + name + ' fail\n'))
o.write('0000')
elif arg.startswith('save/'):
if not self.has_write_perm:
return app.common.error_403(self)
editurl = arg[5:]
stage.checkout(self.repo)
tree = stage.getroot(self.repo)
tree = stage.save(self.repo, editurl, self.request.get('content'))
stage.add(self.repo, tree)
author = self.userobj.identity
msg = 'Commit from wiki\n'
commitsha = stage.commit(self.repo, author, msg)
self.response.out.write('saved ' + cgi.escape(editurl) + ' with commit ' + commitsha + '\n')
def serve_gc(self):
pack = self.store.start_pack()
idx = {}
off = [0]
def gc_action(sha, obj):
idx[sha] = off[0]
t = babygit.obj_type(obj)
s = babygit.obj_size(obj)
enc_t_s = self.encode_type_and_size(t, s)
pack.write(enc_t_s)
off[0] += len(enc_t_s)
compressed = zlib.compress(babygit.obj_contents(obj))
pack.write(compressed)
off[0] += len(compressed)
head = self.repo.gethead()
self.repo.walk_action([head], [], gc_action)
self.store.finish_pack(pack, json.encode(idx))
o = self.response.out
o.write('<html>Did gc, len(idx) = ' + str(len(idx)) + '\n')
# This is authentication with basic auth, for git clients. Web access
# uses cookies instead (see app/users.py)
def authenticate(self):
if app.users.bypass_local_auth(self.request):
return True
if not app.users.request_secure_enough(self.request):
self.error(403)
return False
authorization = self.request.headers.get('Authorization')
if not authorization:
realm = 'ghilbert'
self.error(401)
self.response.headers['WWW-Authenticate'] = 'Basic realm="' + realm + '"'
return False
method, cookie = authorization.split(' ')
if method != 'Basic':
self.error(401)
return False
username, passwd = binascii.a2b_base64(cookie).split(':', 1)
if not app.users.check_pass(username, passwd):
self.error(401)
return False
if not app.users.check_perms(username, 'write'):
self.error(403)
return False
logging.debug('authorization succeeded: ' + username)
return True