comparison mercurial/hgweb/webcommands.py @ 5964:1cd1582ef25f

hgweb: centralize req.write() calls
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Mon, 28 Jan 2008 15:10:17 +0100
parents 5be210afe1b8
children 948a41e77902
comparison
equal deleted inserted replaced
5963:5be210afe1b8 5964:1cd1582ef25f
18 'archive', 'static', 18 'archive', 'static',
19 ] 19 ]
20 20
21 def log(web, req, tmpl): 21 def log(web, req, tmpl):
22 if 'file' in req.form and req.form['file'][0]: 22 if 'file' in req.form and req.form['file'][0]:
23 filelog(web, req, tmpl) 23 return filelog(web, req, tmpl)
24 else: 24 else:
25 changelog(web, req, tmpl) 25 return changelog(web, req, tmpl)
26 26
27 def rawfile(web, req, tmpl): 27 def rawfile(web, req, tmpl):
28 path = web.cleanpath(req.form.get('file', [''])[0]) 28 path = web.cleanpath(req.form.get('file', [''])[0])
29 if not path: 29 if not path:
30 req.write(web.manifest(tmpl, web.changectx(req), path)) 30 return web.manifest(tmpl, web.changectx(req), path)
31 return
32 31
33 try: 32 try:
34 fctx = web.filectx(req) 33 fctx = web.filectx(req)
35 except revlog.LookupError: 34 except revlog.LookupError:
36 req.write(web.manifest(tmpl, web.changectx(req), path)) 35 return web.manifest(tmpl, web.changectx(req), path)
37 return
38 36
39 path = fctx.path() 37 path = fctx.path()
40 text = fctx.data() 38 text = fctx.data()
41 mt = mimetypes.guess_type(path)[0] 39 mt = mimetypes.guess_type(path)[0]
42 if mt is None or util.binary(text): 40 if mt is None or util.binary(text):
43 mt = mt or 'application/octet-stream' 41 mt = mt or 'application/octet-stream'
44 42
45 req.httphdr(mt, path, len(text)) 43 req.httphdr(mt, path, len(text))
46 req.write(text) 44 return [text]
47 45
48 def file(web, req, tmpl): 46 def file(web, req, tmpl):
49 path = web.cleanpath(req.form.get('file', [''])[0]) 47 path = web.cleanpath(req.form.get('file', [''])[0])
50 if path: 48 if path:
51 try: 49 try:
52 req.write(web.filerevision(tmpl, web.filectx(req))) 50 return web.filerevision(tmpl, web.filectx(req))
53 return
54 except revlog.LookupError: 51 except revlog.LookupError:
55 pass 52 pass
56 53
57 req.write(web.manifest(tmpl, web.changectx(req), path)) 54 return web.manifest(tmpl, web.changectx(req), path)
58 55
59 def changelog(web, req, tmpl, shortlog = False): 56 def changelog(web, req, tmpl, shortlog = False):
60 if 'node' in req.form: 57 if 'node' in req.form:
61 ctx = web.changectx(req) 58 ctx = web.changectx(req)
62 else: 59 else:
65 else: 62 else:
66 hi = web.repo.changelog.count() - 1 63 hi = web.repo.changelog.count() - 1
67 try: 64 try:
68 ctx = web.repo.changectx(hi) 65 ctx = web.repo.changectx(hi)
69 except hg.RepoError: 66 except hg.RepoError:
70 req.write(web.search(tmpl, hi)) # XXX redirect to 404 page? 67 return web.search(tmpl, hi) # XXX redirect to 404 page?
71 return
72 68
73 req.write(web.changelog(tmpl, ctx, shortlog = shortlog)) 69 return web.changelog(tmpl, ctx, shortlog = shortlog)
74 70
75 def shortlog(web, req, tmpl): 71 def shortlog(web, req, tmpl):
76 changelog(web, req, tmpl, shortlog = True) 72 return changelog(web, req, tmpl, shortlog = True)
77 73
78 def changeset(web, req, tmpl): 74 def changeset(web, req, tmpl):
79 req.write(web.changeset(tmpl, web.changectx(req))) 75 return web.changeset(tmpl, web.changectx(req))
80 76
81 rev = changeset 77 rev = changeset
82 78
83 def manifest(web, req, tmpl): 79 def manifest(web, req, tmpl):
84 req.write(web.manifest(tmpl, web.changectx(req), 80 return web.manifest(tmpl, web.changectx(req),
85 web.cleanpath(req.form['path'][0]))) 81 web.cleanpath(req.form['path'][0]))
86 82
87 def tags(web, req, tmpl): 83 def tags(web, req, tmpl):
88 req.write(web.tags(tmpl)) 84 return web.tags(tmpl)
89 85
90 def summary(web, req, tmpl): 86 def summary(web, req, tmpl):
91 req.write(web.summary(tmpl)) 87 return web.summary(tmpl)
92 88
93 def filediff(web, req, tmpl): 89 def filediff(web, req, tmpl):
94 req.write(web.filediff(tmpl, web.filectx(req))) 90 return web.filediff(tmpl, web.filectx(req))
95 91
96 diff = filediff 92 diff = filediff
97 93
98 def annotate(web, req, tmpl): 94 def annotate(web, req, tmpl):
99 req.write(web.fileannotate(tmpl, web.filectx(req))) 95 return web.fileannotate(tmpl, web.filectx(req))
100 96
101 def filelog(web, req, tmpl): 97 def filelog(web, req, tmpl):
102 req.write(web.filelog(tmpl, web.filectx(req))) 98 return web.filelog(tmpl, web.filectx(req))
103 99
104 def archive(web, req, tmpl): 100 def archive(web, req, tmpl):
105 type_ = req.form['type'][0] 101 type_ = req.form['type'][0]
106 allowed = web.configlist("web", "allow_archive") 102 allowed = web.configlist("web", "allow_archive")
107 if (type_ in web.archives and (type_ in allowed or 103 if (type_ in web.archives and (type_ in allowed or
108 web.configbool("web", "allow" + type_, False))): 104 web.configbool("web", "allow" + type_, False))):
109 web.archive(tmpl, req, req.form['node'][0], type_) 105 web.archive(tmpl, req, req.form['node'][0], type_)
110 return 106 return []
111 107
112 raise ErrorResponse(400, 'Unsupported archive type: %s' % type_) 108 raise ErrorResponse(400, 'Unsupported archive type: %s' % type_)
113 109
114 def static(web, req, tmpl): 110 def static(web, req, tmpl):
115 fname = req.form['file'][0] 111 fname = req.form['file'][0]
116 # a repo owner may set web.static in .hg/hgrc to get any file 112 # a repo owner may set web.static in .hg/hgrc to get any file
117 # readable by the user running the CGI script 113 # readable by the user running the CGI script
118 static = web.config("web", "static", 114 static = web.config("web", "static",
119 os.path.join(web.templatepath, "static"), 115 os.path.join(web.templatepath, "static"),
120 untrusted=False) 116 untrusted=False)
121 req.write(staticfile(static, fname, req)) 117 return [staticfile(static, fname, req)]