开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
develop
分支 (7)
标签 (81)
develop
gh-pages
4.8
master
feature/mailer
refactor/email
feature/queue
v4.7.3
v4.7.2
v4.7.1
v4.7.0
v4.6.5
v4.6.4
v4.6.3
v4.6.2
v4.6.1
v4.6.0
v4.5.8
v4.5.7
v4.5.6
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.8
develop
分支 (7)
标签 (81)
develop
gh-pages
4.8
master
feature/mailer
refactor/email
feature/queue
v4.7.3
v4.7.2
v4.7.1
v4.7.0
v4.6.5
v4.6.4
v4.6.3
v4.6.2
v4.6.1
v4.6.0
v4.5.8
v4.5.7
v4.5.6
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.8
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
develop
分支 (7)
标签 (81)
develop
gh-pages
4.8
master
feature/mailer
refactor/email
feature/queue
v4.7.3
v4.7.2
v4.7.1
v4.7.0
v4.6.5
v4.6.4
v4.6.3
v4.6.2
v4.6.1
v4.6.0
v4.5.8
v4.5.7
v4.5.6
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.8
CodeIgniter4
/
system
/
HTTP
/
CURLRequest.php
CodeIgniter4
/
system
/
HTTP
/
CURLRequest.php
CURLRequest.php 24.04 KB
一键复制 编辑 原始数据 按行查看 历史
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\HTTP;
use CodeIgniter\Exceptions\InvalidArgumentException;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use Config\App;
use Config\CURLRequest as ConfigCURLRequest;
use CurlShareHandle;
use SensitiveParameter;
/**
* A lightweight HTTP client for sending synchronous HTTP requests via cURL.
*
* @see \CodeIgniter\HTTP\CURLRequestTest
*/
class CURLRequest extends OutgoingRequest
{
/**
* The response object associated with this request
*
* @var ResponseInterface|null
*/
protected $response;
/**
* The original response object associated with this request
*
* @var ResponseInterface|null
*/
protected $responseOrig;
/**
* The URI associated with this request
*
* @var URI
*/
protected $baseURI;
/**
* The setting values
*
* @var array
*/
protected $config;
/**
* The default setting values
*
* @var array
*/
protected $defaultConfig = [
'timeout' => 0.0,
'connect_timeout' => 150,
'debug' => false,
'verify' => true,
];
/**
* Default values for when 'allow_redirects'
* option is true.
*
* @var array
*/
protected $redirectDefaults = [
'max' => 5,
'strict' => true,
'protocols' => [
'http',
'https',
],
];
/**
* The number of milliseconds to delay before
* sending the request.
*
* @var float
*/
protected $delay = 0.0;
/**
* The default options from the constructor. Applied to all requests.
*/
private readonly array $defaultOptions;
/**
* Whether share options between requests or not.
*
* If true, all the options won't be reset between requests.
* It may cause an error request with unnecessary headers.
*/
private readonly bool $shareOptions;
/**
* The share connection instance.
*/
protected ?CurlShareHandle $shareConnection = null;
/**
* Takes an array of options to set the following possible class properties:
*
* - baseURI
* - timeout
* - any other request options to use as defaults.
*
* @param array<string, mixed> $options
*/
public function __construct(App $config, URI $uri, ?ResponseInterface $response = null, array $options = [])
{
if (! function_exists('curl_version')) {
throw HTTPException::forMissingCurl(); // @codeCoverageIgnore
}
parent::__construct(Method::GET, $uri);
$this->responseOrig = $response ?? new Response($config);
// Remove the default Content-Type header.
$this->responseOrig->removeHeader('Content-Type');
$this->baseURI = $uri->useRawQueryString();
$this->defaultOptions = $options;
$this->shareOptions = config(ConfigCURLRequest::class)->shareOptions;
$this->config = $this->defaultConfig;
$this->parseOptions($options);
// Share Connection
$optShareConnection = config(ConfigCURLRequest::class)->shareConnectionOptions ?? [ // @phpstan-ignore nullCoalesce.property
CURL_LOCK_DATA_CONNECT,
CURL_LOCK_DATA_DNS,
];
if ($optShareConnection !== []) {
$this->shareConnection = curl_share_init();
foreach (array_unique($optShareConnection) as $opt) {
curl_share_setopt($this->shareConnection, CURLSHOPT_SHARE, $opt);
}
}
}
/**
* Sends an HTTP request to the specified $url. If this is a relative
* URL, it will be merged with $this->baseURI to form a complete URL.
*
* @param string $method HTTP method
*/
public function request($method, string $url, array $options = []): ResponseInterface
{
$this->response = clone $this->responseOrig;
$this->parseOptions($options);
$url = $this->prepareURL($url);
$method = esc(strip_tags($method));
$this->send($method, $url);
if ($this->shareOptions === false) {
$this->resetOptions();
}
return $this->response;
}
/**
* Reset all options to default.
*
* @return void
*/
protected function resetOptions()
{
// Reset headers
$this->headers = [];
$this->headerMap = [];
// Reset body
$this->body = null;
// Reset configs
$this->config = $this->defaultConfig;
// Set the default options for next request
$this->parseOptions($this->defaultOptions);
}
/**
* Convenience method for sending a GET request.
*/
public function get(string $url, array $options = []): ResponseInterface
{
return $this->request(Method::GET, $url, $options);
}
/**
* Convenience method for sending a DELETE request.
*/
public function delete(string $url, array $options = []): ResponseInterface
{
return $this->request('DELETE', $url, $options);
}
/**
* Convenience method for sending a HEAD request.
*/
public function head(string $url, array $options = []): ResponseInterface
{
return $this->request('HEAD', $url, $options);
}
/**
* Convenience method for sending an OPTIONS request.
*/
public function options(string $url, array $options = []): ResponseInterface
{
return $this->request('OPTIONS', $url, $options);
}
/**
* Convenience method for sending a PATCH request.
*/
public function patch(string $url, array $options = []): ResponseInterface
{
return $this->request('PATCH', $url, $options);
}
/**
* Convenience method for sending a POST request.
*/
public function post(string $url, array $options = []): ResponseInterface
{
return $this->request(Method::POST, $url, $options);
}
/**
* Convenience method for sending a PUT request.
*/
public function put(string $url, array $options = []): ResponseInterface
{
return $this->request(Method::PUT, $url, $options);
}
/**
* Set the HTTP Authentication.
*
* @param string $type basic or digest
*
* @return $this
*/
public function setAuth(string $username, #[SensitiveParameter] string $password, string $type = 'basic')
{
$this->config['auth'] = [$username, $password, $type];
return $this;
}
/**
* Set form data to be sent.
*
* @param bool $multipart Set TRUE if you are sending CURLFiles
*
* @return $this
*/
public function setForm(array $params, bool $multipart = false)
{
if ($multipart) {
$this->config['multipart'] = $params;
} else {
$this->config['form_params'] = $params;
}
return $this;
}
/**
* Set JSON data to be sent.
*
* @param array|bool|float|int|object|string|null $data
*
* @return $this
*/
public function setJSON($data)
{
$this->config['json'] = $data;
return $this;
}
/**
* Sets the correct settings based on the options array
* passed in.
*
* @return void
*/
protected function parseOptions(array $options)
{
if (array_key_exists('baseURI', $options)) {
$this->baseURI = $this->baseURI->setURI($options['baseURI']);
unset($options['baseURI']);
}
if (array_key_exists('headers', $options) && is_array($options['headers'])) {
foreach ($options['headers'] as $name => $value) {
$this->setHeader($name, $value);
}
unset($options['headers']);
}
if (array_key_exists('delay', $options)) {
// Convert from the milliseconds passed in
// to the seconds that sleep requires.
$this->delay = (float) $options['delay'] / 1000;
unset($options['delay']);
}
if (array_key_exists('body', $options)) {
$this->setBody($options['body']);
unset($options['body']);
}
foreach ($options as $key => $value) {
$this->config[$key] = $value;
}
}
/**
* If the $url is a relative URL, will attempt to create
* a full URL by prepending $this->baseURI to it.
*/
protected function prepareURL(string $url): string
{
// If it's a full URI, then we have nothing to do here...
if (str_contains($url, '://')) {
return $url;
}
$uri = $this->baseURI->resolveRelativeURI($url);
// Create the string instead of casting to prevent baseURL muddling
return URI::createURIString(
$uri->getScheme(),
$uri->getAuthority(),
$uri->getPath(),
$uri->getQuery(),
$uri->getFragment(),
);
}
/**
* Fires the actual cURL request.
*
* @return ResponseInterface
*/
public function send(string $method, string $url)
{
// Reset our curl options so we're on a fresh slate.
$curlOptions = [];
if (! empty($this->config['query']) && is_array($this->config['query'])) {
// This is likely too naive a solution.
// Should look into handling when $url already
// has query vars on it.
$url .= '?' . http_build_query($this->config['query']);
unset($this->config['query']);
}
$curlOptions[CURLOPT_URL] = $url;
$curlOptions[CURLOPT_RETURNTRANSFER] = true;
if ($this->shareConnection instanceof CurlShareHandle) {
$curlOptions[CURLOPT_SHARE] = $this->shareConnection;
}
$curlOptions[CURLOPT_HEADER] = true;
// Disable @file uploads in post data.
$curlOptions[CURLOPT_SAFE_UPLOAD] = true;
$curlOptions = $this->setCURLOptions($curlOptions, $this->config);
$curlOptions = $this->applyMethod($method, $curlOptions);
$curlOptions = $this->applyRequestHeaders($curlOptions);
// Do we need to delay this request?
if ($this->delay > 0) {
usleep((int) $this->delay * 1_000_000);
}
$output = $this->sendRequest($curlOptions);
// Set the string we want to break our response from
$breakString = "\r\n\r\n";
// Remove all intermediate responses
$output = $this->removeIntermediateResponses($output, $breakString);
// Split out our headers and body
$break = strpos($output, $breakString);
if ($break !== false) {
// Our headers
$headers = explode("\n", substr($output, 0, $break));
$this->setResponseHeaders($headers);
// Our body
$body = substr($output, $break + 4);
$this->response->setBody($body);
} else {
$this->response->setBody($output);
}
return $this->response;
}
/**
* Adds $this->headers to the cURL request.
*/
protected function applyRequestHeaders(array $curlOptions = []): array
{
if (empty($this->headers)) {
return $curlOptions;
}
$set = [];
foreach (array_keys($this->headers) as $name) {
$set[] = $name . ': ' . $this->getHeaderLine($name);
}
$curlOptions[CURLOPT_HTTPHEADER] = $set;
return $curlOptions;
}
/**
* Apply method
*/
protected function applyMethod(string $method, array $curlOptions): array
{
$this->method = $method;
$curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
$size = strlen($this->body ?? '');
// Have content?
if ($size > 0) {
return $this->applyBody($curlOptions);
}
if ($method === Method::PUT || $method === Method::POST) {
// See http://tools.ietf.org/html/rfc7230#section-3.3.2
if ($this->header('content-length') === null && ! isset($this->config['multipart'])) {
$this->setHeader('Content-Length', '0');
}
} elseif ($method === 'HEAD') {
$curlOptions[CURLOPT_NOBODY] = 1;
}
return $curlOptions;
}
/**
* Apply body
*/
protected function applyBody(array $curlOptions = []): array
{
if (! empty($this->body)) {
$curlOptions[CURLOPT_POSTFIELDS] = (string) $this->getBody();
}
return $curlOptions;
}
/**
* Parses the header retrieved from the cURL response into
* our Response object.
*
* @return void
*/
protected function setResponseHeaders(array $headers = [])
{
foreach ($headers as $header) {
if (($pos = strpos($header, ':')) !== false) {
$title = trim(substr($header, 0, $pos));
$value = trim(substr($header, $pos + 1));
if ($this->response instanceof Response) {
$this->response->addHeader($title, $value);
} else {
$this->response->setHeader($title, $value);
}
} elseif (str_starts_with($header, 'HTTP')) {
preg_match('#^HTTP\/([12](?:\.[01])?) (\d+)(?: (.+))?#', $header, $matches);
if (isset($matches[1])) {
$this->response->setProtocolVersion($matches[1]);
}
if (isset($matches[2])) {
$this->response->setStatusCode((int) $matches[2], $matches[3] ?? '');
}
}
}
}
/**
* Set CURL options
*
* @return array
*
* @throws InvalidArgumentException
*/
protected function setCURLOptions(array $curlOptions = [], array $config = [])
{
// Auth Headers
if (! empty($config['auth'])) {
$curlOptions[CURLOPT_USERPWD] = $config['auth'][0] . ':' . $config['auth'][1];
if (! empty($config['auth'][2]) && strtolower($config['auth'][2]) === 'digest') {
$curlOptions[CURLOPT_HTTPAUTH] = CURLAUTH_DIGEST;
} else {
$curlOptions[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
}
}
// Certificate
if (! empty($config['cert'])) {
$cert = $config['cert'];
if (is_array($cert)) {
$curlOptions[CURLOPT_SSLCERTPASSWD] = $cert[1];
$cert = $cert[0];
}
if (! is_file($cert)) {
throw HTTPException::forSSLCertNotFound($cert);
}
$curlOptions[CURLOPT_SSLCERT] = $cert;
}
// SSL Verification
if (isset($config['verify'])) {
if (is_string($config['verify'])) {
$file = realpath($config['verify']) ?: $config['verify'];
if (! is_file($file)) {
throw HTTPException::forInvalidSSLKey($config['verify']);
}
$curlOptions[CURLOPT_CAINFO] = $file;
$curlOptions[CURLOPT_SSL_VERIFYPEER] = true;
$curlOptions[CURLOPT_SSL_VERIFYHOST] = 2;
} elseif (is_bool($config['verify'])) {
$curlOptions[CURLOPT_SSL_VERIFYPEER] = $config['verify'];
$curlOptions[CURLOPT_SSL_VERIFYHOST] = $config['verify'] ? 2 : 0;
}
}
// Proxy
if (isset($config['proxy'])) {
$curlOptions[CURLOPT_HTTPPROXYTUNNEL] = true;
$curlOptions[CURLOPT_PROXY] = $config['proxy'];
}
// Debug
if ($config['debug']) {
$curlOptions[CURLOPT_VERBOSE] = 1;
$curlOptions[CURLOPT_STDERR] = is_string($config['debug']) ? fopen($config['debug'], 'a+b') : fopen('php://stderr', 'wb');
}
// Decode Content
if (! empty($config['decode_content'])) {
$accept = $this->getHeaderLine('Accept-Encoding');
if ($accept !== '') {
$curlOptions[CURLOPT_ENCODING] = $accept;
} else {
$curlOptions[CURLOPT_ENCODING] = '';
$curlOptions[CURLOPT_HTTPHEADER] = 'Accept-Encoding';
}
}
// Allow Redirects
if (array_key_exists('allow_redirects', $config)) {
$settings = $this->redirectDefaults;
if (is_array($config['allow_redirects'])) {
$settings = array_merge($settings, $config['allow_redirects']);
}
if ($config['allow_redirects'] === false) {
$curlOptions[CURLOPT_FOLLOWLOCATION] = 0;
} else {
$curlOptions[CURLOPT_FOLLOWLOCATION] = 1;
$curlOptions[CURLOPT_MAXREDIRS] = $settings['max'];
if ($settings['strict'] === true) {
$curlOptions[CURLOPT_POSTREDIR] = 1 | 2 | 4;
}
$protocols = 0;
foreach ($settings['protocols'] as $proto) {
$protocols += constant('CURLPROTO_' . strtoupper($proto));
}
$curlOptions[CURLOPT_REDIR_PROTOCOLS] = $protocols;
}
}
// DNS Cache Timeout
if (isset($config['dns_cache_timeout']) && is_numeric($config['dns_cache_timeout']) && $config['dns_cache_timeout'] >= -1) {
$curlOptions[CURLOPT_DNS_CACHE_TIMEOUT] = (int) $config['dns_cache_timeout'];
}
// Fresh Connect (default true)
$curlOptions[CURLOPT_FRESH_CONNECT] = isset($config['fresh_connect']) && is_bool($config['fresh_connect'])
? $config['fresh_connect']
: true;
// Timeout
$curlOptions[CURLOPT_TIMEOUT_MS] = (float) $config['timeout'] * 1000;
// Connection Timeout
$curlOptions[CURLOPT_CONNECTTIMEOUT_MS] = (float) $config['connect_timeout'] * 1000;
// Post Data - application/x-www-form-urlencoded
if (! empty($config['form_params']) && is_array($config['form_params'])) {
$postFields = http_build_query($config['form_params']);
$curlOptions[CURLOPT_POSTFIELDS] = $postFields;
// Ensure content-length is set, since CURL doesn't seem to
// calculate it when HTTPHEADER is set.
$this->setHeader('Content-Length', (string) strlen($postFields));
$this->setHeader('Content-Type', 'application/x-www-form-urlencoded');
}
// Post Data - multipart/form-data
if (! empty($config['multipart']) && is_array($config['multipart'])) {
// setting the POSTFIELDS option automatically sets multipart
$curlOptions[CURLOPT_POSTFIELDS] = $config['multipart'];
}
// HTTP Errors
$curlOptions[CURLOPT_FAILONERROR] = array_key_exists('http_errors', $config) ? (bool) $config['http_errors'] : true;
// JSON
if (isset($config['json'])) {
// Will be set as the body in `applyBody()`
$json = json_encode($config['json']);
$this->setBody($json);
$this->setHeader('Content-Type', 'application/json');
$this->setHeader('Content-Length', (string) strlen($json));
}
// Resolve IP
if (array_key_exists('force_ip_resolve', $config)) {
$curlOptions[CURLOPT_IPRESOLVE] = match ($config['force_ip_resolve']) {
'v4' => CURL_IPRESOLVE_V4,
'v6' => CURL_IPRESOLVE_V6,
default => CURL_IPRESOLVE_WHATEVER,
};
}
// version
if (! empty($config['version'])) {
$version = sprintf('%.1F', $config['version']);
if ($version === '1.0') {
$curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
} elseif ($version === '1.1') {
$curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1;
} elseif ($version === '2.0') {
$curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_2_0;
} elseif ($version === '3.0') {
if (! defined('CURL_HTTP_VERSION_3')) {
define('CURL_HTTP_VERSION_3', 30);
}
$curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_3;
}
}
// Cookie
if (isset($config['cookie'])) {
$curlOptions[CURLOPT_COOKIEJAR] = $config['cookie'];
$curlOptions[CURLOPT_COOKIEFILE] = $config['cookie'];
}
// User Agent
if (isset($config['user_agent'])) {
$curlOptions[CURLOPT_USERAGENT] = $config['user_agent'];
}
return $curlOptions;
}
/**
* Does the actual work of initializing cURL, setting the options,
* and grabbing the output.
*
* @param array<int, mixed> $curlOptions
*
* @codeCoverageIgnore
*/
protected function sendRequest(array $curlOptions = []): string
{
$ch = curl_init();
curl_setopt_array($ch, $curlOptions);
// Send the request and wait for a response.
$output = curl_exec($ch);
if ($output === false) {
throw HTTPException::forCurlError((string) curl_errno($ch), curl_error($ch));
}
return $output;
}
private function removeIntermediateResponses(string $output, string $breakString): string
{
while (true) {
// Check if we should remove the current response
if ($this->shouldRemoveCurrentResponse($output, $breakString)) {
$breakStringPos = strpos($output, $breakString);
if ($breakStringPos !== false) {
$output = substr($output, $breakStringPos + 4);
continue;
}
}
// No more intermediate responses to remove
break;
}
return $output;
}
/**
* Check if the current response (at the beginning of output) should be removed.
*/
private function shouldRemoveCurrentResponse(string $output, string $breakString): bool
{
// HTTP/x.x 1xx responses (Continue, Processing, etc.)
if (preg_match('/^HTTP\/\d+(?:\.\d+)?\s+1\d\d\s/', $output)) {
return true;
}
// HTTP/x.x 200 Connection established (proxy responses)
if (preg_match('/^HTTP\/\d+(?:\.\d+)?\s+200\s+Connection\s+established/i', $output)) {
return true;
}
// HTTP/x.x 3xx responses (redirects) - only if redirects are allowed
$allowRedirects = isset($this->config['allow_redirects']) && $this->config['allow_redirects'] !== false;
if ($allowRedirects && preg_match('/^HTTP\/\d+(?:\.\d+)?\s+3\d\d\s/', $output)) {
// Check if there's a Location header
$breakStringPos = strpos($output, $breakString);
if ($breakStringPos !== false) {
$headerSection = substr($output, 0, $breakStringPos);
$headers = explode("\n", $headerSection);
foreach ($headers as $header) {
if (str_starts_with(strtolower($header), 'location:')) {
return true; // Found location header, this is a redirect to remove
}
}
}
}
// Digest auth challenges - only remove if there's another response after
if (isset($this->config['auth'][2]) && $this->config['auth'][2] === 'digest') {
$breakStringPos = strpos($output, $breakString);
if ($breakStringPos !== false) {
$headerSection = substr($output, 0, $breakStringPos);
if (str_contains($headerSection, 'WWW-Authenticate: Digest')) {
$nextBreakPos = strpos($output, $breakString, $breakStringPos + 4);
return $nextBreakPos !== false; // Only remove if there's another response
}
}
}
return false;
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wfdaj/CodeIgniter4.git
git@gitee.com:wfdaj/CodeIgniter4.git
wfdaj
CodeIgniter4
CodeIgniter4
develop
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /