comparison mercurial/hgweb/common.py @ 5561:22713dce19f6

hgweb: return meaningful HTTP status codes instead of nonsense
author Bryan O'Sullivan <bos@serpentine.com>
date Wed, 28 Nov 2007 08:38:42 -0800
parents 12e4d9524951
children d61fea133f2d
comparison
equal deleted inserted replaced
5560:e78c24011001 5561:22713dce19f6
4 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> 4 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
5 # 5 #
6 # This software may be used and distributed according to the terms 6 # This software may be used and distributed according to the terms
7 # of the GNU General Public License, incorporated herein by reference. 7 # of the GNU General Public License, incorporated herein by reference.
8 8
9 import os, mimetypes 9 import errno, mimetypes, os
10
11 class ErrorResponse(Exception):
12 def __init__(self, code, message=None):
13 Exception.__init__(self)
14 self.code = code
15 if message is None:
16 from httplib import responses
17 self.message = responses.get(code, 'Error')
18 else:
19 self.message = message
10 20
11 def get_mtime(repo_path): 21 def get_mtime(repo_path):
12 store_path = os.path.join(repo_path, ".hg") 22 store_path = os.path.join(repo_path, ".hg")
13 if not os.path.isdir(os.path.join(store_path, "data")): 23 if not os.path.isdir(os.path.join(store_path, "data")):
14 store_path = os.path.join(store_path, "store") 24 store_path = os.path.join(store_path, "store")
38 os.stat(path) 48 os.stat(path)
39 ct = mimetypes.guess_type(path)[0] or "text/plain" 49 ct = mimetypes.guess_type(path)[0] or "text/plain"
40 req.header([('Content-type', ct), 50 req.header([('Content-type', ct),
41 ('Content-length', str(os.path.getsize(path)))]) 51 ('Content-length', str(os.path.getsize(path)))])
42 return file(path, 'rb').read() 52 return file(path, 'rb').read()
43 except (TypeError, OSError): 53 except TypeError:
44 # illegal fname or unreadable file 54 raise ErrorResponse(500, 'illegal file name')
45 return "" 55 except OSError, err:
56 if err.errno == errno.ENOENT:
57 raise ErrorResponse(404)
58 else:
59 raise ErrorResponse(500, err.strerror)
46 60
47 def style_map(templatepath, style): 61 def style_map(templatepath, style):
48 """Return path to mapfile for a given style. 62 """Return path to mapfile for a given style.
49 63
50 Searches mapfile in the following locations: 64 Searches mapfile in the following locations: