# HG changeset patch # User RhodeCode Admin # Date 2023-07-17 12:07:32 # Node ID f468f658412e8b82aa323d55fa3fadb56f59b907 # Parent 44baf3db5362283d36708c9e15c5b3083ff8f984 http-app: refactor new usage of main http api endpoints diff --git a/vcsserver/http_main.py b/vcsserver/http_main.py --- a/vcsserver/http_main.py +++ b/vcsserver/http_main.py @@ -18,7 +18,6 @@ import io import os import sys -import base64 import locale import logging import uuid @@ -37,11 +36,12 @@ from pyramid.config import Configurator from pyramid.wsgi import wsgiapp from pyramid.response import Response -from vcsserver.base import BinaryEnvelope +from vcsserver.base import BytesEnvelope, BinaryEnvelope from vcsserver.lib.rc_json import json from vcsserver.config.settings_maker import SettingsMaker -from vcsserver.str_utils import safe_int, safe_bytes, safe_str +from vcsserver.str_utils import safe_int from vcsserver.lib.statsd_client import StatsdClient +from vcsserver.tweens.request_wrapper import get_call_context, get_headers_call_context log = logging.getLogger(__name__) @@ -234,6 +234,7 @@ class HTTPApplication(object): self.global_config = global_config self.config.include('vcsserver.lib.rc_cache') + self.config.include('vcsserver.lib.rc_cache.archive_cache') settings_locale = settings.get('locale', '') or 'en_US.UTF-8' vcs = VCS(locale_conf=settings_locale, cache_config=settings) @@ -330,7 +331,7 @@ class HTTPApplication(object): request.registry.vcs_call_context = { 'method': method, - 'repo_name': payload.get('_repo_name') + 'repo_name': payload.get('_repo_name'), } if wire: @@ -345,7 +346,8 @@ class HTTPApplication(object): if log.isEnabledFor(logging.DEBUG): # also we SKIP printing out any of those methods args since they maybe excessive just_args_methods = { - 'commitctx': ('content', 'removed', 'updated') + 'commitctx': ('content', 'removed', 'updated'), + 'commit': ('content', 'removed', 'updated') } if method in just_args_methods: skip_args = just_args_methods[method] @@ -506,10 +508,13 @@ class HTTPApplication(object): def _render(value, system): bin_type = False res = value.get('result') - if res and isinstance(res, BinaryEnvelope): + if isinstance(res, BytesEnvelope): + log.debug('Result is wrapped in BytesEnvelope type') + bin_type = True + elif isinstance(res, BinaryEnvelope): log.debug('Result is wrapped in BinaryEnvelope type') - value['result'] = res.value - bin_type = res.bin_type + value['result'] = res.val + bin_type = True request = system.get('request') if request is not None: @@ -573,9 +578,7 @@ class HTTPApplication(object): @wsgiapp def _hg_stream(environ, start_response): log.debug('http-app: handling hg stream') - - packed_cc = base64.b64decode(environ['HTTP_X_RC_VCS_STREAM_CALL_CONTEXT']) - call_context = msgpack.unpackb(packed_cc) + call_context = get_headers_call_context(environ) repo_path = call_context['repo_path'] repo_name = call_context['repo_name'] @@ -606,8 +609,7 @@ class HTTPApplication(object): def _git_stream(environ, start_response): log.debug('http-app: handling git stream') - packed_cc = base64.b64decode(environ['HTTP_X_RC_VCS_STREAM_CALL_CONTEXT']) - call_context = msgpack.unpackb(packed_cc) + call_context = get_headers_call_context(environ) repo_path = call_context['repo_path'] repo_name = call_context['repo_name'] @@ -649,15 +651,18 @@ class HTTPApplication(object): def handle_vcs_exception(self, exception, request): _vcs_kind = getattr(exception, '_vcs_kind', '') + if _vcs_kind == 'repo_locked': - # Get custom repo-locked status code if present. - status_code = request.headers.get('X-RC-Locked-Status-Code') + headers_call_context = get_headers_call_context(request.environ) + status_code = safe_int(headers_call_context['locked_status_code']) + return HTTPRepoLocked( - title=str(exception), status_code=status_code) + title=str(exception), status_code=status_code, headers=[('X-Rc-Locked', '1')]) elif _vcs_kind == 'repo_branch_protected': # Get custom repo-branch-protected status code if present. - return HTTPRepoBranchProtected(title=str(exception)) + return HTTPRepoBranchProtected( + title=str(exception), headers=[('X-Rc-Branch-Protection', '1')]) exc_info = request.exc_info store_exception(id(exc_info), exc_info) @@ -767,7 +772,7 @@ def main(global_config, **settings): pyramid_app = HTTPApplication(settings=settings, global_config=global_config).wsgi_app() total_time = time.time() - start_time log.info('Pyramid app `%s` created and configured in %.2fs', - getattr(pyramid_app, 'func_name', 'pyramid_app'), total_time) + pyramid_app.__class__, total_time) return pyramid_app