Eclectic Media Git klaus / 83b7ff8
add Dulwich's smart HTTP Middleware posativ 8 years ago
3 changed file(s) with 53 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
2323 help="webserver port (default: 8080)"),
2424 make_option("--prefix", type=str, default='/', dest="prefix",
2525 help="serve on given sub uri"),
26 make_option("-s", "--smarthttp", action="store_true", dest="smarthttp",
27 help="serve git repositories the smart way", default=False),
2628 make_option("-r", "--use-reloader", action="store_true", dest="reloader",
2729 help=SUPPRESS_HELP, default=False),
2830 make_option("--debug", action="store_true", dest="debug",
4345 print '%r: Not a git repository' % path
4446 args.remove(path)
4547
46 app = make_app(args, options.prefix)
48 app = make_app(args, options.prefix, options.smarthttp)
4749
4850 if options.debug:
4951 from werkzeug.debug import DebuggedApplication
99 from werkzeug.exceptions import HTTPException, NotFound, InternalServerError
1010 from werkzeug.wsgi import SharedDataMiddleware
1111
12 from klaus import views
12 from klaus import views, http
1313 from klaus.utils import query_string_to_dict
1414 from klaus.utils import force_unicode, timesince, shorten_sha1, pygmentize, \
1515 guess_is_binary, guess_is_image, extract_author_name
104104 return self.wsgi_app(environ, start_response)
105105
106106
107 def make_app(repos, prefix='/'):
107 def make_app(repos, prefix='/', smartgit=False):
108108
109109 repos = dict(
110110 (repo.rstrip(os.sep).split(os.sep)[-1].replace('.git', ''), repo)
122122 app.jinja_env.filters['is_image'] = guess_is_image
123123 app.jinja_env.filters['shorten_author'] = extract_author_name
124124
125 if smartgit:
126 app = http.make_app(app, repos)
127
125128 app.wsgi_app = SubUri(app.wsgi_app, prefix=prefix)
126129 app = SharedDataMiddleware(app, {
127130 '/static/': os.path.join(os.path.dirname(__file__), 'static/')
0 # -*- encoding: utf-8 -*-
1
2 from dulwich.repo import Repo
3 from dulwich.server import DictBackend
4 from dulwich.web import HTTPGitRequest
5 from dulwich.web import HTTPGitApplication, GunzipFilter
6
7
8 class AuthenticatedGitApplication(HTTPGitApplication):
9 """Add basic HTTP authentication to ``git push``."""
10
11 def __init__(self, app, backend, dumb=False, handlers=None):
12 super(AuthenticatedGitApplication, self).__init__(backend, dumb, handlers)
13 self.app = app
14
15 def __call__(self, environ, start_response):
16 path = environ['PATH_INFO']
17 method = environ['REQUEST_METHOD']
18 req = HTTPGitRequest(environ, start_response, dumb=self.dumb,
19 handlers=self.handlers)
20 # environ['QUERY_STRING'] has qs args
21 handler = None
22 for smethod, spath in self.services.iterkeys():
23 if smethod != method:
24 continue
25 mat = spath.search(path)
26 if mat:
27 handler = self.services[smethod, spath]
28 break
29 if handler is None:
30 return self.app(environ, start_response)
31 return handler(req, self.backend, mat)
32
33
34 def make_app(app, repos):
35
36 # DictBackend uses keys with a leading slash
37 backend = DictBackend(dict(('/'+k, Repo(v)) for k, v in repos.iteritems()))
38 wsgi_app = app.wsgi_app
39
40 app = AuthenticatedGitApplication(app, backend, dumb=False, handlers=None)
41 app = GunzipFilter(app)
42 app.wsgi_app = wsgi_app
43
44 return app