Changeset - c82ef5ec8dcd
[Not reviewed]
default
0 6 0
Mads Kiilerich - 6 years ago 2019-12-26 16:09:30
mads@kiilerich.com
Grafted from: 41215423565b
lib: refactor _get_access_path as get_path_info

We will need it later when it gets more tricky to get the path from environ ...
6 files changed with 18 insertions and 16 deletions:
0 comments (0 inline, 0 general)
kallithea/controllers/api/__init__.py
Show inline comments
 
@@ -33,16 +33,16 @@ import traceback
 
import types
 

	
 
from tg import Response, TGController, request, response
 
from webob.exc import HTTPError, HTTPException
 

	
 
from kallithea.lib.auth import AuthUser
 
from kallithea.lib.base import _get_access_path
 
from kallithea.lib.base import _get_ip_addr as _get_ip
 
from kallithea.lib.base import get_path_info
 
from kallithea.lib.compat import json
 
from kallithea.lib.utils2 import safe_str, safe_unicode
 
from kallithea.lib.utils2 import safe_str
 
from kallithea.model.db import User
 

	
 

	
 
log = logging.getLogger('JSONRPC')
 

	
 

	
 
@@ -206,13 +206,13 @@ class JSONRPCController(TGController):
 
        self._rpc_args.update(self._request_params)
 
        self._rpc_args['action'] = self._req_method
 
        self._rpc_args['environ'] = environ
 

	
 
        log.info('IP: %s Request to %s time: %.3fs' % (
 
            self._get_ip_addr(environ),
 
            safe_unicode(_get_access_path(environ)), time.time() - start)
 
            get_path_info(environ), time.time() - start)
 
        )
 

	
 
        state.set_action(self._rpc_call, [])
 
        state.set_params(self._rpc_args)
 
        return state
 

	
kallithea/lib/base.py
Show inline comments
 
@@ -94,18 +94,19 @@ def _get_ip_addr(environ):
 
        return _filter_proxy(ip)
 

	
 
    ip = environ.get(def_key, '0.0.0.0')
 
    return _filter_proxy(ip)
 

	
 

	
 
def _get_access_path(environ):
 
    """Return PATH_INFO from environ ... using tg.original_request if available."""
 
def get_path_info(environ):
 
    """Return unicode PATH_INFO from environ ... using tg.original_request if available.
 
    """
 
    org_req = environ.get('tg.original_request')
 
    if org_req is not None:
 
        environ = org_req.environ
 
    return environ.get('PATH_INFO')
 
    return safe_unicode(environ['PATH_INFO'])
 

	
 

	
 
def log_in_user(user, remember, is_external_auth, ip_addr):
 
    """
 
    Log a `User` in and update session and cookies. If `remember` is True,
 
    the session cookie is set to expire in a year; otherwise, it expires at
 
@@ -523,13 +524,13 @@ class BaseController(TGController):
 
            request.authuser = authuser
 
            request.ip_addr = ip_addr
 
            request.needs_csrf_check = needs_csrf_check
 

	
 
            log.info('IP: %s User: %s accessed %s',
 
                request.ip_addr, request.authuser,
 
                safe_unicode(_get_access_path(environ)),
 
                get_path_info(environ),
 
            )
 
            return super(BaseController, self).__call__(environ, context)
 
        except webob.exc.HTTPException as e:
 
            return e
 

	
 

	
kallithea/lib/middleware/permanent_repo_url.py
Show inline comments
 
@@ -18,22 +18,24 @@ kallithea.lib.middleware.permanent_repo_
 
middleware to handle permanent repo URLs, replacing PATH_INFO '/_123/yada' with
 
'/name/of/repo/yada' after looking 123 up in the database.
 
"""
 

	
 

	
 
from kallithea.lib.utils import fix_repo_id_name
 
from kallithea.lib.utils2 import safe_str
 
from kallithea.lib.utils2 import safe_str, safe_unicode
 

	
 

	
 
class PermanentRepoUrl(object):
 

	
 
    def __init__(self, app, config):
 
        self.application = app
 
        self.config = config
 

	
 
    def __call__(self, environ, start_response):
 
        path_info = environ['PATH_INFO']
 
        # Extract path_info as get_path_info does, but do it explicitly because
 
        # we also have to do the reverse operation when patching it back in
 
        path_info = safe_unicode(environ['PATH_INFO'])
 
        if path_info.startswith('/'): # it must
 
            path_info = '/' + safe_str(fix_repo_id_name(path_info[1:]))
 
            environ['PATH_INFO'] = path_info
 

	
 
        return self.application(environ, start_response)
kallithea/lib/middleware/simplegit.py
Show inline comments
 
@@ -28,13 +28,13 @@ Original author and date, and relevant c
 
"""
 

	
 

	
 
import logging
 
import re
 

	
 
from kallithea.lib.base import BaseVCSController
 
from kallithea.lib.base import BaseVCSController, get_path_info
 
from kallithea.lib.hooks import log_pull_action
 
from kallithea.lib.middleware.pygrack import make_wsgi_app
 
from kallithea.lib.utils import make_ui
 
from kallithea.lib.utils2 import safe_unicode
 
from kallithea.model.db import Repository
 

	
 
@@ -54,13 +54,13 @@ cmd_mapping = {
 
class SimpleGit(BaseVCSController):
 

	
 
    scm_alias = 'git'
 

	
 
    @classmethod
 
    def parse_request(cls, environ):
 
        path_info = environ.get('PATH_INFO', '')
 
        path_info = get_path_info(environ)
 
        m = GIT_PROTO_PAT.match(path_info)
 
        if m is None:
 
            return None
 

	
 
        class parsed_request(object):
 
            # See https://git-scm.com/book/en/v2/Git-Internals-Transfer-Protocols#_the_smart_protocol
kallithea/lib/middleware/simplehg.py
Show inline comments
 
@@ -29,13 +29,13 @@ Original author and date, and relevant c
 

	
 

	
 
import logging
 
import os
 
import urllib
 

	
 
from kallithea.lib.base import BaseVCSController
 
from kallithea.lib.base import BaseVCSController, get_path_info
 
from kallithea.lib.utils import make_ui
 
from kallithea.lib.utils2 import safe_str, safe_unicode
 
from kallithea.lib.vcs.utils.hgcompat import hgweb_mod
 

	
 

	
 
log = logging.getLogger(__name__)
 
@@ -96,13 +96,13 @@ class SimpleHg(BaseVCSController):
 

	
 
    @classmethod
 
    def parse_request(cls, environ):
 
        http_accept = environ.get('HTTP_ACCEPT', '')
 
        if not http_accept.startswith('application/mercurial'):
 
            return None
 
        path_info = environ.get('PATH_INFO', '')
 
        path_info = get_path_info(environ)
 
        if not path_info.startswith('/'): # it must!
 
            return None
 

	
 
        class parsed_request(object):
 
            repo_name = safe_unicode(path_info[1:].rstrip('/'))
 

	
kallithea/lib/middleware/wrapper.py
Show inline comments
 
@@ -26,14 +26,13 @@ Original author and date, and relevant c
 
:license: GPLv3, see LICENSE.md for more details.
 
"""
 

	
 
import logging
 
import time
 

	
 
from kallithea.lib.base import _get_access_path, _get_ip_addr
 
from kallithea.lib.utils2 import safe_unicode
 
from kallithea.lib.base import _get_ip_addr, get_path_info
 

	
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
class Meter:
 
@@ -88,13 +87,13 @@ class RequestWrapper(object):
 
        self.config = config
 

	
 
    def __call__(self, environ, start_response):
 
        meter = Meter(start_response)
 
        description = "Request from %s for %s" % (
 
            _get_ip_addr(environ),
 
            safe_unicode(_get_access_path(environ)),
 
            get_path_info(environ),
 
        )
 
        try:
 
            result = self.application(environ, meter.start_response)
 
        finally:
 
            log.info("%s responding after %.3fs", description, meter.duration())
 
        return ResultIter(result, meter, description)
0 comments (0 inline, 0 general)