Coverage for mcpgateway / utils / internal_http.py: 100%
9 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-06 00:56 +0100
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-06 00:56 +0100
1# -*- coding: utf-8 -*-
2"""Location: ./mcpgateway/utils/internal_http.py
3Copyright 2026
4SPDX-License-Identifier: Apache-2.0
5Authors: Mihai Criveti
7Helpers for gateway-internal loopback HTTP calls.
9These helpers centralize protocol and TLS verification behavior for
10self-calls to local endpoints like /rpc.
11"""
13# Standard
14import os
16# First-Party
17from mcpgateway.config import settings
20def _is_ssl_enabled() -> bool:
21 """Check whether the gateway is running with SSL enabled.
23 Returns:
24 bool: ``True`` when ``SSL=true`` is set in the environment.
25 """
26 return os.getenv("SSL", "false") == "true"
29def internal_loopback_base_url() -> str:
30 """Return loopback base URL for gateway self-calls.
32 Uses HTTPS when runtime is started with SSL=true, otherwise HTTP.
34 Returns:
35 str: The base URL string (e.g. ``http://127.0.0.1:4444``).
36 """
37 scheme = "https" if _is_ssl_enabled() else "http"
38 return f"{scheme}://127.0.0.1:{settings.port}"
41def internal_loopback_verify() -> bool:
42 """Return TLS verification policy for loopback self-calls.
44 Loopback HTTPS frequently uses a self-signed local cert, so verification
45 is disabled for HTTPS loopback self-calls and enabled otherwise.
47 Returns:
48 bool: ``False`` when the loopback URL is HTTPS, ``True`` otherwise.
49 """
50 return not _is_ssl_enabled()