Files
upstream-zulip/zerver/lib/outgoing_http.py
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

78 lines
2.6 KiB
Python

from typing import Any
import requests
from django.conf import settings
from typing_extensions import override
from urllib3.util import Retry
from zproject.config import get_config
class OutgoingSession(requests.Session):
"""Reasonable outgoing HTTP request defaults.
This class exists to force requests through the Smokescreen
proxy, enforce that requests have timeouts set, and to provide an
easy way for them to include retries. The 'Role' is currently
unused -- Smokescreen does not currently consume the
X-Smokescreen-Role header, but may do so in the future for
additional ACL'ing of destinations and/or logging.
"""
def __init__(
self,
role: str,
timeout: float,
headers: dict[str, str] | None = None,
max_retries: int | Retry | None = None,
proxies: dict[str, str] | None = None,
) -> None:
super().__init__()
retry: Retry | None = Retry(total=0)
if max_retries is not None:
if isinstance(max_retries, Retry):
retry = max_retries
else:
retry = Retry(total=max_retries, backoff_factor=1)
outgoing_adapter = OutgoingHTTPAdapter(role=role, timeout=timeout, max_retries=retry)
self.mount("http://", outgoing_adapter)
self.mount("https://", outgoing_adapter)
if headers:
self.headers.update(headers)
if proxies is None and not settings.DEVELOPMENT:
proxy_host = get_config("http_proxy", "host", "localhost")
proxy_port = get_config("http_proxy", "port", "4750")
proxy = ""
if proxy_host != "" and proxy_port != "":
proxy = f"http://{proxy_host}:{proxy_port}"
self.proxies.update(
{
"http": proxy,
"https": proxy,
}
)
elif proxies is not None:
self.proxies.update(proxies)
class OutgoingHTTPAdapter(requests.adapters.HTTPAdapter):
role: str
timeout: float
def __init__(self, role: str, timeout: float, max_retries: Retry | None) -> None:
self.role = role
self.timeout = timeout
super().__init__(max_retries=max_retries)
@override
def send(self, *args: Any, **kwargs: Any) -> requests.Response:
if kwargs.get("timeout") is None:
kwargs["timeout"] = self.timeout
return super().send(*args, **kwargs)
@override
def proxy_headers(self, proxy: str) -> dict[str, str]:
return {"X-Smokescreen-Role": self.role}