# HG changeset patch # User Mads Kiilerich # Date 1547087685 -3600 # Node ID b88150a9080442a7c3d12866454bb985a4812194 # Parent d14328af601e69c65ab9133c8eee4166992d4587 middleware: unify Mercurial and Git _handle_request in the VCS base class Finally, it is more clear in what the VCSs are different ... and what generic setup code is needed. diff -r d14328af601e -r b88150a90804 kallithea/lib/base.py --- a/kallithea/lib/base.py Mon Jan 07 02:08:38 2019 +0100 +++ b/kallithea/lib/base.py Thu Jan 10 03:34:45 2019 +0100 @@ -49,11 +49,11 @@ from kallithea.config.routing import url from kallithea.lib.utils2 import str2bool, safe_unicode, AttributeDict, \ - safe_str, safe_int + safe_str, safe_int, get_server_url, _set_extras from kallithea.lib import auth_modules from kallithea.lib.auth import AuthUser, HasPermissionAnyMiddleware from kallithea.lib.compat import json -from kallithea.lib.utils import get_repo_slug +from kallithea.lib.utils import get_repo_slug, is_valid_repo from kallithea.lib.exceptions import UserCreationError from kallithea.lib.vcs.exceptions import RepositoryError, EmptyRepositoryError, ChangesetDoesNotExistError from kallithea.model import meta @@ -306,15 +306,63 @@ def __call__(self, environ, start_response): start = time.time() try: + # try parsing a request for this VCS - if it fails, call the wrapped app parsed_request = self.parse_request(environ) if parsed_request is None: return self.application(environ, start_response) - return self._handle_request(parsed_request, environ, start_response) + + # skip passing error to error controller + environ['pylons.status_code_redirect'] = True + + # quick check if repo exists... + if not is_valid_repo(parsed_request.repo_name, self.basepath, self.scm_alias): + raise webob.exc.HTTPNotFound() + + if parsed_request.action is None: + # Note: the client doesn't get the helpful error message + raise webob.exc.HTTPBadRequest('Unable to detect pull/push action for %r! Are you using a nonstandard command or client?' % parsed_request.repo_name) + + #====================================================================== + # CHECK PERMISSIONS + #====================================================================== + ip_addr = self._get_ip_addr(environ) + user, response_app = self._authorize(environ, parsed_request.action, parsed_request.repo_name, ip_addr) + if response_app is not None: + return response_app(environ, start_response) + + # extras are injected into Mercurial UI object and later available + # in hooks executed by Kallithea + from kallithea import CONFIG + extras = { + 'ip': ip_addr, + 'username': user.username, + 'action': parsed_request.action, + 'repository': parsed_request.repo_name, + 'scm': self.scm_alias, + 'config': CONFIG['__file__'], + 'server_url': get_server_url(environ), + } + + #====================================================================== + # REQUEST HANDLING + #====================================================================== + log.debug('HOOKS extras is %s', extras) + _set_extras(extras) + + try: + log.info('%s action on %s repo "%s" by "%s" from %s', + parsed_request.action, self.scm_alias, parsed_request.repo_name, safe_str(user.username), ip_addr) + app = self._make_app(parsed_request) + return app(environ, start_response) + except Exception: + log.error(traceback.format_exc()) + raise webob.exc.HTTPInternalServerError() + except webob.exc.HTTPException as e: return e(environ, start_response) finally: - log = logging.getLogger('kallithea.' + self.__class__.__name__) - log.debug('Request time: %.3fs', time.time() - start) + log_ = logging.getLogger('kallithea.' + self.__class__.__name__) + log_.debug('Request time: %.3fs', time.time() - start) meta.Session.remove() diff -r d14328af601e -r b88150a90804 kallithea/lib/middleware/simplegit.py --- a/kallithea/lib/middleware/simplegit.py Mon Jan 07 02:08:38 2019 +0100 +++ b/kallithea/lib/middleware/simplegit.py Thu Jan 10 03:34:45 2019 +0100 @@ -30,15 +30,11 @@ import re import logging -import traceback - -from webob.exc import HTTPNotFound, HTTPInternalServerError, HTTPBadRequest from kallithea.model.db import Ui, Repository -from kallithea.lib.utils2 import safe_str, safe_unicode, get_server_url, \ - _set_extras +from kallithea.lib.utils2 import safe_unicode from kallithea.lib.base import BaseVCSController -from kallithea.lib.utils import make_ui, is_valid_repo +from kallithea.lib.utils import make_ui from kallithea.lib.hooks import log_pull_action from kallithea.lib.middleware.pygrack import make_wsgi_app @@ -80,54 +76,6 @@ return parsed_request - def _handle_request(self, parsed_request, environ, start_response): - # skip passing error to error controller - environ['pylons.status_code_redirect'] = True - - # quick check if repo exists... - if not is_valid_repo(parsed_request.repo_name, self.basepath, self.scm_alias): - raise HTTPNotFound() - - if parsed_request.action is None: - # Note: the client doesn't get the helpful error message - raise HTTPBadRequest('Unable to detect pull/push action for %r! Are you using a nonstandard command or client?' % parsed_request.repo_name) - - #====================================================================== - # CHECK PERMISSIONS - #====================================================================== - ip_addr = self._get_ip_addr(environ) - user, response_app = self._authorize(environ, parsed_request.action, parsed_request.repo_name, ip_addr) - if response_app is not None: - return response_app(environ, start_response) - - # extras are injected into Mercurial UI object and later available - # in hooks executed by Kallithea - from kallithea import CONFIG - extras = { - 'ip': ip_addr, - 'username': user.username, - 'action': parsed_request.action, - 'repository': parsed_request.repo_name, - 'scm': self.scm_alias, - 'config': CONFIG['__file__'], - 'server_url': get_server_url(environ), - } - - #====================================================================== - # REQUEST HANDLING - #====================================================================== - log.debug('HOOKS extras is %s', extras) - _set_extras(extras) - - try: - log.info('%s action on %s repo "%s" by "%s" from %s', - parsed_request.action, self.scm_alias, parsed_request.repo_name, safe_str(user.username), ip_addr) - app = self._make_app(parsed_request) - return app(environ, start_response) - except Exception: - log.error(traceback.format_exc()) - raise HTTPInternalServerError() - def _make_app(self, parsed_request): """ Return a pygrack wsgi application. diff -r d14328af601e -r b88150a90804 kallithea/lib/middleware/simplehg.py --- a/kallithea/lib/middleware/simplehg.py Mon Jan 07 02:08:38 2019 +0100 +++ b/kallithea/lib/middleware/simplehg.py Thu Jan 10 03:34:45 2019 +0100 @@ -30,16 +30,12 @@ import os import logging -import traceback import urllib -from webob.exc import HTTPNotFound, HTTPInternalServerError, HTTPBadRequest - -from kallithea.lib.utils2 import safe_str, safe_unicode, get_server_url, \ - _set_extras +from kallithea.lib.utils2 import safe_str, safe_unicode from kallithea.lib.base import BaseVCSController -from kallithea.lib.utils import make_ui, is_valid_repo -from kallithea.lib.vcs.utils.hgcompat import RepoError, hgweb_mod +from kallithea.lib.utils import make_ui +from kallithea.lib.vcs.utils.hgcompat import hgweb_mod log = logging.getLogger(__name__) @@ -135,54 +131,6 @@ return parsed_request - def _handle_request(self, parsed_request, environ, start_response): - # skip passing error to error controller - environ['pylons.status_code_redirect'] = True - - # quick check if repo exists... - if not is_valid_repo(parsed_request.repo_name, self.basepath, self.scm_alias): - raise HTTPNotFound() - - if parsed_request.action is None: - # Note: the client doesn't get the helpful error message - raise HTTPBadRequest('Unable to detect pull/push action for %r! Are you using a nonstandard command or client?' % parsed_request.repo_name) - - #====================================================================== - # CHECK PERMISSIONS - #====================================================================== - ip_addr = self._get_ip_addr(environ) - user, response_app = self._authorize(environ, parsed_request.action, parsed_request.repo_name, ip_addr) - if response_app is not None: - return response_app(environ, start_response) - - # extras are injected into Mercurial UI object and later available - # in hooks executed by Kallithea - from kallithea import CONFIG - extras = { - 'ip': ip_addr, - 'username': user.username, - 'action': parsed_request.action, - 'repository': parsed_request.repo_name, - 'scm': self.scm_alias, - 'config': CONFIG['__file__'], - 'server_url': get_server_url(environ), - } - - #====================================================================== - # REQUEST HANDLING - #====================================================================== - log.debug('HOOKS extras is %s', extras) - _set_extras(extras) - - try: - log.info('%s action on %s repo "%s" by "%s" from %s', - parsed_request.action, self.scm_alias, parsed_request.repo_name, safe_str(user.username), ip_addr) - app = self._make_app(parsed_request) - return app(environ, start_response) - except Exception: - log.error(traceback.format_exc()) - raise HTTPInternalServerError() - def _make_app(self, parsed_request): """ Make an hgweb wsgi application.