##// END OF EJS Templates
py3: properly reject non-encoded strings given to hgweb
Ludovic Chabant -
r42395:91104f10 default
parent child Browse files
Show More
@@ -1,107 +1,110 b''
1 1 # hgweb/__init__.py - web interface to a mercurial repository
2 2 #
3 3 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
4 4 # Copyright 2005 Matt Mackall <mpm@selenic.com>
5 5 #
6 6 # This software may be used and distributed according to the terms of the
7 7 # GNU General Public License version 2 or any later version.
8 8
9 9 from __future__ import absolute_import
10 10
11 11 import os
12 12
13 13 from ..i18n import _
14 14
15 15 from .. import (
16 16 error,
17 17 pycompat,
18 18 )
19 19
20 20 from ..utils import (
21 21 procutil,
22 22 )
23 23
24 24 from . import (
25 25 hgweb_mod,
26 26 hgwebdir_mod,
27 27 server,
28 28 )
29 29
30 30 def hgweb(config, name=None, baseui=None):
31 31 '''create an hgweb wsgi object
32 32
33 33 config can be one of:
34 34 - repo object (single repo view)
35 35 - path to repo (single repo view)
36 36 - path to config file (multi-repo view)
37 37 - dict of virtual:real pairs (multi-repo view)
38 38 - list of virtual:real tuples (multi-repo view)
39 39 '''
40 40
41 if isinstance(config, pycompat.unicode):
42 raise error.ProgrammingError(
43 'Mercurial only supports encoded strings: %r' % config)
41 44 if ((isinstance(config, bytes) and not os.path.isdir(config)) or
42 45 isinstance(config, dict) or isinstance(config, list)):
43 46 # create a multi-dir interface
44 47 return hgwebdir_mod.hgwebdir(config, baseui=baseui)
45 48 return hgweb_mod.hgweb(config, name=name, baseui=baseui)
46 49
47 50 def hgwebdir(config, baseui=None):
48 51 return hgwebdir_mod.hgwebdir(config, baseui=baseui)
49 52
50 53 class httpservice(object):
51 54 def __init__(self, ui, app, opts):
52 55 self.ui = ui
53 56 self.app = app
54 57 self.opts = opts
55 58
56 59 def init(self):
57 60 procutil.setsignalhandler()
58 61 self.httpd = server.create_server(self.ui, self.app)
59 62
60 63 if (self.opts['port'] and
61 64 not self.ui.verbose and
62 65 not self.opts['print_url']):
63 66 return
64 67
65 68 if self.httpd.prefix:
66 69 prefix = self.httpd.prefix.strip('/') + '/'
67 70 else:
68 71 prefix = ''
69 72
70 73 port = r':%d' % self.httpd.port
71 74 if port == r':80':
72 75 port = r''
73 76
74 77 bindaddr = self.httpd.addr
75 78 if bindaddr == r'0.0.0.0':
76 79 bindaddr = r'*'
77 80 elif r':' in bindaddr: # IPv6
78 81 bindaddr = r'[%s]' % bindaddr
79 82
80 83 fqaddr = self.httpd.fqaddr
81 84 if r':' in fqaddr:
82 85 fqaddr = r'[%s]' % fqaddr
83 86
84 87 url = 'http://%s%s/%s' % (
85 88 pycompat.sysbytes(fqaddr), pycompat.sysbytes(port), prefix)
86 89 if self.opts['print_url']:
87 90 self.ui.write('%s\n' % url)
88 91 else:
89 92 if self.opts['port']:
90 93 write = self.ui.status
91 94 else:
92 95 write = self.ui.write
93 96 write(_('listening at %s (bound to %s:%d)\n') %
94 97 (url, pycompat.sysbytes(bindaddr), self.httpd.port))
95 98 self.ui.flush() # avoid buffering of status message
96 99
97 100 def run(self):
98 101 self.httpd.serve_forever()
99 102
100 103 def createapp(baseui, repo, webconf):
101 104 if webconf:
102 105 return hgwebdir_mod.hgwebdir(webconf, baseui=baseui)
103 106 else:
104 107 if not repo:
105 108 raise error.RepoError(_("there is no Mercurial repository"
106 109 " here (.hg not found)"))
107 110 return hgweb_mod.hgweb(repo, baseui=baseui)
General Comments 0
You need to be logged in to leave comments. Login now