Files
@ 89f11587b2dc
Branch filter:
Location: kallithea/kallithea/config/middleware/simplehg.py
89f11587b2dc
4.7 KiB
text/x-python
config: move WSGI middleware apps from lib to config
These middlewares are full WSGI applications - that is not so lib-ish. The
middleware is referenced from the application in config - that seems like a
good place for them to live.
These middlewares are full WSGI applications - that is not so lib-ish. The
middleware is referenced from the application in config - that seems like a
good place for them to live.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | # -*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
kallithea.lib.middleware.simplehg
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SimpleHg middleware for handling Mercurial protocol requests (push/clone etc.).
It's implemented with basic auth function
This file was forked by the Kallithea project in July 2014.
Original author and date, and relevant copyright and licensing information is below:
:created_on: Apr 28, 2010
:author: marcink
:copyright: (c) 2013 RhodeCode GmbH, and others.
:license: GPLv3, see LICENSE.md for more details.
"""
import logging
import os
import urllib.parse
import mercurial.hgweb
from kallithea.lib.base import BaseVCSController, get_path_info
from kallithea.lib.utils import make_ui
from kallithea.lib.utils2 import safe_bytes
log = logging.getLogger(__name__)
def get_header_hgarg(environ):
"""Decode the special Mercurial encoding of big requests over multiple headers.
>>> get_header_hgarg({})
''
>>> get_header_hgarg({'HTTP_X_HGARG_0': ' ', 'HTTP_X_HGARG_1': 'a','HTTP_X_HGARG_2': '','HTTP_X_HGARG_3': 'b+c %20'})
'ab+c %20'
"""
chunks = []
i = 1
while True:
v = environ.get('HTTP_X_HGARG_%d' % i)
if v is None:
break
chunks.append(v)
i += 1
return ''.join(chunks)
cmd_mapping = {
# 'batch' is not in this list - it is handled explicitly
'between': 'pull',
'branches': 'pull',
'branchmap': 'pull',
'capabilities': 'pull',
'changegroup': 'pull',
'changegroupsubset': 'pull',
'changesetdata': 'pull',
'clonebundles': 'pull',
'debugwireargs': 'pull',
'filedata': 'pull',
'getbundle': 'pull',
'getlfile': 'pull',
'heads': 'pull',
'hello': 'pull',
'known': 'pull',
'lheads': 'pull',
'listkeys': 'pull',
'lookup': 'pull',
'manifestdata': 'pull',
'narrow_widen': 'pull',
'protocaps': 'pull',
'statlfile': 'pull',
'stream_out': 'pull',
'pushkey': 'push',
'putlfile': 'push',
'unbundle': 'push',
}
class SimpleHg(BaseVCSController):
scm_alias = 'hg'
@classmethod
def parse_request(cls, environ):
http_accept = environ.get('HTTP_ACCEPT', '')
if not http_accept.startswith('application/mercurial'):
return None
path_info = get_path_info(environ)
if not path_info.startswith('/'): # it must!
return None
class parsed_request(object):
repo_name = path_info[1:].rstrip('/')
query_string = environ['QUERY_STRING']
action = None
for qry in query_string.split('&'):
parts = qry.split('=', 1)
if len(parts) == 2 and parts[0] == 'cmd':
cmd = parts[1]
if cmd == 'batch':
hgarg = get_header_hgarg(environ)
if not hgarg.startswith('cmds='):
action = 'push' # paranoid and safe
break
action = 'pull'
for cmd_arg in hgarg[5:].split(';'):
cmd, _args = urllib.parse.unquote_plus(cmd_arg).split(' ', 1)
op = cmd_mapping.get(cmd, 'push')
if op != 'pull':
assert op == 'push'
action = 'push'
break
else:
action = cmd_mapping.get(cmd, 'push')
break # only process one cmd
return parsed_request
def _make_app(self, parsed_request):
"""
Make an hgweb wsgi application.
"""
repo_name = parsed_request.repo_name
repo_path = os.path.join(self.basepath, repo_name)
baseui = make_ui(repo_path=repo_path)
hgweb_app = mercurial.hgweb.hgweb(safe_bytes(repo_path), name=safe_bytes(repo_name), baseui=baseui)
def wrapper_app(environ, start_response):
environ['REPO_NAME'] = repo_name # used by mercurial.hgweb.hgweb
return hgweb_app(environ, start_response)
return wrapper_app
|