> креды по хосту (в нижнем регистре) */ private readonly array $auth; private readonly UrlGuard $guard; /** * @param array> $auth креды по хосту: * ['api.example.com' => ['user' => 'bot', 'pass' => '***']] * или ['api.example.com' => ['type' => 'bearer', 'token' => '***']] * @param bool|null $verifySsl проверять TLS-сертификат; null — из AI_VERIFY_SSL (по умолчанию нет) * @param string|null $caInfo путь к CA-бандлу; null — из AI_CAINFO * @param Proxy|null $proxy прокси; null — из AI_PROXY (и NO_PROXY из окружения) */ public function __construct( array $auth = [], bool $allowPrivate = false, private readonly int $defaultChars = 4000, private readonly float $timeout = 10.0, private readonly float $maxTimeout = 60.0, private readonly ?bool $verifySsl = null, private readonly ?string $caInfo = null, private readonly ?Proxy $proxy = null, ) { $normalized = []; foreach ($auth as $host => $credentials) { $normalized[strtolower((string) $host)] = $credentials; } $this->auth = $normalized; $this->guard = new UrlGuard($allowPrivate); } public function name(): string { return 'http_request'; } public function description(): string { return 'Выполнить HTTP-запрос: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS. ' . 'query добавляется к URL, body-объект уходит как JSON (или form при content_type), ' . 'auth — Basic (user/pass) или Bearer (token). ' . 'По умолчанию только публичные адреса.'; } public function parameters(): array { return [ 'type' => 'object', 'properties' => [ 'url' => ['type' => 'string', 'description' => 'Полный адрес, например https://api.example.com/orders.'], 'method' => [ 'type' => 'string', 'enum' => HttpRequest::METHODS, 'description' => 'HTTP-метод, по умолчанию GET.', ], 'query' => [ 'type' => 'object', 'description' => 'Параметры строки запроса, например {"page": 2}.', ], 'body' => [ 'type' => ['object', 'string'], 'description' => 'Тело запроса: объект уйдёт как JSON или form, строка — как есть.', ], 'content_type' => [ 'type' => 'string', 'enum' => ['json', 'form', 'text'], 'description' => 'Как кодировать body при отправке. По умолчанию json.', ], 'headers' => [ 'type' => 'object', 'description' => 'Дополнительные заголовки, например {"X-Trace": "abc"}.', ], 'auth' => [ 'type' => 'object', 'description' => 'Basic: {"user": "...", "pass": "..."} или Bearer: {"type": "bearer", "token": "..."}.', ], 'max_chars' => [ 'type' => 'integer', 'description' => 'Сколько символов ответа вернуть, по умолчанию ' . $this->defaultChars . '.', ], 'timeout' => [ 'type' => 'number', 'description' => Timeout::parameterDescription($this->timeout, $this->maxTimeout), ], ], 'required' => ['url'], 'additionalProperties' => false, ]; } public function handle(array $args): string { $rawUrl = trim((string) ($args['url'] ?? '')); if ($rawUrl === '') { return 'Не указан URL.'; } $maxChars = (int) ($args['max_chars'] ?? $this->defaultChars); $maxChars = max(200, min($maxChars, 20000)); $timeout = Timeout::seconds($args, $this->timeout, $this->maxTimeout); $body = $args['body'] ?? null; if (is_string($body) && strlen($body) > self::MAX_BODY) { return 'Тело запроса слишком большое (максимум ' . self::MAX_BODY . ' байт).'; } try { $request = HttpRequest::build( method: (string) ($args['method'] ?? 'GET'), url: $rawUrl, query: is_array($args['query'] ?? null) ? $args['query'] : [], body: $body, contentType: (string) ($args['content_type'] ?? 'json'), headers: is_array($args['headers'] ?? null) ? $args['headers'] : [], auth: $this->resolveAuth($rawUrl, $args['auth'] ?? null), ); } catch (\InvalidArgumentException $e) { return $e->getMessage(); } if (strlen((string) $request->body) > self::MAX_BODY) { return 'Тело запроса слишком большое (максимум ' . self::MAX_BODY . ' байт).'; } try { $url = $request->url; $response = null; $proxy = $this->proxy ?? Proxy::fromEnv(); for ($hop = 0; $hop <= self::MAX_REDIRECTS; $hop++) { // Через прокси имя резолвит он — локальный DNS может его не знать. $viaProxy = $proxy !== null && $proxy->appliesTo($url); // Проверка и запрос идут на одни и те же адреса: cURL не // резолвит имя второй раз (защита от DNS rebinding). $pinned = $this->guard->pin($url, resolve: !$viaProxy); $response = HttpTransport::request( $url, $timeout, false, self::MAX_BYTES, $request->method, $request->headers, $request->body, $pinned, $this->verifySsl, $this->caInfo, $proxy, ); $isRedirect = in_array($response['status'], [301, 302, 303, 307, 308], true); // Тело POST/PUT/DELETE при редиректе терять нельзя — отдаём как есть. if (!$isRedirect || $response['location'] === null) { break; } if (!in_array($request->method, ['GET', 'HEAD'], true)) { return 'Редирект ' . $response['status'] . ' на ' . $response['location'] . '. Повтори запрос по этому адресу, если он нужен.'; } $url = HttpRequest::normalizeUrl(HttpRequest::resolveRedirect($url, $response['location'])); } } catch (\Throwable $e) { return 'Ошибка: ' . $e->getMessage(); } if ($response === null) { return 'Не удалось получить ответ.'; } return $this->format($request->method, $url, $response, $maxChars); } /** * @param array{status: int, body: string, content_type: string|null, truncated: bool} $response */ private function format(string $method, string $url, array $response, int $maxChars): string { $head = 'HTTP ' . $method . ' ' . $response['status'] . ' — ' . $url; if ($response['content_type'] !== null) { $head .= ' [' . $response['content_type'] . ']'; } $text = trim($response['body']); if ($text === '') { return $head . ' — пустой ответ.'; } $truncated = $response['truncated'] || mb_strlen($text) > $maxChars; $text = mb_substr($text, 0, $maxChars); return $head . ($truncated ? ' (обрезано)' : '') . ":\n" . $text; } /** * Креды: аргумент инструмента важнее карты из конструктора. * * @return array|null */ private function resolveAuth(string $url, mixed $argAuth): ?array { if (is_array($argAuth) && $argAuth !== []) { return array_map(static fn (mixed $value): string => (string) $value, $argAuth); } $host = strtolower((string) (parse_url($url, PHP_URL_HOST) ?? '')); return $this->auth[$host] ?? null; } }