comparison mercurial/hgweb/hgweb_mod.py @ 5993:948a41e77902

hgweb: explicit response status
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Fri, 01 Feb 2008 10:31:13 +0100
parents 9f1e6ab76069
children b913d3aacddc
comparison
equal deleted inserted replaced
5992:30c40ba10963 5993:948a41e77902
9 import os, mimetypes, re 9 import os, mimetypes, re
10 from mercurial.node import * 10 from mercurial.node import *
11 from mercurial import mdiff, ui, hg, util, archival, patch, hook 11 from mercurial import mdiff, ui, hg, util, archival, patch, hook
12 from mercurial import revlog, templater, templatefilters 12 from mercurial import revlog, templater, templatefilters
13 from common import ErrorResponse, get_mtime, style_map, paritygen, get_contact 13 from common import ErrorResponse, get_mtime, style_map, paritygen, get_contact
14 from common import HTTP_OK, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
14 from request import wsgirequest 15 from request import wsgirequest
15 import webcommands, protocol 16 import webcommands, protocol
16 17
17 shortcuts = { 18 shortcuts = {
18 'cl': [('cmd', ['changelog']), ('rev', None)], 19 'cl': [('cmd', ['changelog']), ('rev', None)],
205 if cmd in protocol.__all__: 206 if cmd in protocol.__all__:
206 method = getattr(protocol, cmd) 207 method = getattr(protocol, cmd)
207 method(self, req) 208 method(self, req)
208 else: 209 else:
209 tmpl = self.templater(req) 210 tmpl = self.templater(req)
211 ctype = tmpl('mimetype', encoding=self.encoding)
212 ctype = templater.stringify(ctype)
213
210 if cmd == '': 214 if cmd == '':
211 req.form['cmd'] = [tmpl.cache['default']] 215 req.form['cmd'] = [tmpl.cache['default']]
212 cmd = req.form['cmd'][0] 216 cmd = req.form['cmd'][0]
213 217
214 if cmd not in webcommands.__all__: 218 if cmd not in webcommands.__all__:
215 raise ErrorResponse(400, 'No such method: ' + cmd) 219 msg = 'No such method: %s' % cmd
220 raise ErrorResponse(HTTP_BAD_REQUEST, msg)
216 elif cmd == 'file' and 'raw' in req.form.get('style', []): 221 elif cmd == 'file' and 'raw' in req.form.get('style', []):
222 self.ctype = ctype
217 content = webcommands.rawfile(self, req, tmpl) 223 content = webcommands.rawfile(self, req, tmpl)
218 else: 224 else:
219 content = getattr(webcommands, cmd)(self, req, tmpl) 225 content = getattr(webcommands, cmd)(self, req, tmpl)
226 req.respond(HTTP_OK, ctype)
220 227
221 req.write(content) 228 req.write(content)
222 del tmpl 229 del tmpl
223 230
224 except revlog.LookupError, err: 231 except revlog.LookupError, err:
225 req.respond(404, tmpl( 232 req.respond(HTTP_NOT_FOUND, ctype)
226 'error', error='revision not found: %s' % err.name)) 233 req.write(tmpl('error', error='revision not found: %s' % err.name))
227 except (hg.RepoError, revlog.RevlogError), inst: 234 except (hg.RepoError, revlog.RevlogError), inst:
228 req.respond(500, tmpl('error', error=str(inst))) 235 req.respond(HTTP_SERVER_ERROR, ctype)
236 req.write(tmpl('error', error=str(inst)))
229 except ErrorResponse, inst: 237 except ErrorResponse, inst:
230 req.respond(inst.code, tmpl('error', error=inst.message)) 238 req.respond(inst.code, ctype)
239 req.write(tmpl('error', error=inst.message))
231 240
232 def templater(self, req): 241 def templater(self, req):
233 242
234 # determine scheme, port and server name 243 # determine scheme, port and server name
235 # this is needed to create absolute urls 244 # this is needed to create absolute urls
250 staticurl += '/' 259 staticurl += '/'
251 260
252 # some functions for the templater 261 # some functions for the templater
253 262
254 def header(**map): 263 def header(**map):
255 ctype = tmpl('mimetype', encoding=self.encoding)
256 req.httphdr(templater.stringify(ctype))
257 yield tmpl('header', encoding=self.encoding, **map) 264 yield tmpl('header', encoding=self.encoding, **map)
258 265
259 def footer(**map): 266 def footer(**map):
260 yield tmpl("footer", **map) 267 yield tmpl("footer", **map)
261 268
666 else: 673 else:
667 short = os.path.basename(remain) 674 short = os.path.basename(remain)
668 files[short] = (f, n) 675 files[short] = (f, n)
669 676
670 if not files: 677 if not files:
671 raise ErrorResponse(404, 'Path not found: ' + path) 678 raise ErrorResponse(HTTP_NOT_FOUND, 'Path not found: ' + path)
672 679
673 def filelist(**map): 680 def filelist(**map):
674 fl = files.keys() 681 fl = files.keys()
675 fl.sort() 682 fl.sort()
676 for f in fl: 683 for f in fl:
844 (name, extension)) 851 (name, extension))
845 ] 852 ]
846 if encoding: 853 if encoding:
847 headers.append(('Content-Encoding', encoding)) 854 headers.append(('Content-Encoding', encoding))
848 req.header(headers) 855 req.header(headers)
856 req.respond(HTTP_OK)
849 archival.archive(self.repo, req, cnode, artype, prefix=name) 857 archival.archive(self.repo, req, cnode, artype, prefix=name)
850 858
851 # add tags to things 859 # add tags to things
852 # tags -> list of changesets corresponding to tags 860 # tags -> list of changesets corresponding to tags
853 # find tag, changeset, file 861 # find tag, changeset, file