Edit File by line
/home/zeestwma/redstone.../wp-inclu.../SimplePi.../src/HTTP
File: Psr18Client.php
<?php
[0] Fix | Delete
[1] Fix | Delete
// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
[2] Fix | Delete
// SPDX-License-Identifier: BSD-3-Clause
[3] Fix | Delete
[4] Fix | Delete
declare(strict_types=1);
[5] Fix | Delete
[6] Fix | Delete
namespace SimplePie\HTTP;
[7] Fix | Delete
[8] Fix | Delete
use InvalidArgumentException;
[9] Fix | Delete
use Psr\Http\Client\ClientExceptionInterface;
[10] Fix | Delete
use Psr\Http\Client\ClientInterface;
[11] Fix | Delete
use Psr\Http\Message\RequestFactoryInterface;
[12] Fix | Delete
use Psr\Http\Message\UriFactoryInterface;
[13] Fix | Delete
use Throwable;
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* HTTP Client based on PSR-18 and PSR-17 implementations
[17] Fix | Delete
*
[18] Fix | Delete
* @internal
[19] Fix | Delete
*/
[20] Fix | Delete
final class Psr18Client implements Client
[21] Fix | Delete
{
[22] Fix | Delete
/**
[23] Fix | Delete
* @var ClientInterface
[24] Fix | Delete
*/
[25] Fix | Delete
private $httpClient;
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* @var RequestFactoryInterface
[29] Fix | Delete
*/
[30] Fix | Delete
private $requestFactory;
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* @var UriFactoryInterface
[34] Fix | Delete
*/
[35] Fix | Delete
private $uriFactory;
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* @var int
[39] Fix | Delete
*/
[40] Fix | Delete
private $allowedRedirects = 5;
[41] Fix | Delete
[42] Fix | Delete
public function __construct(ClientInterface $httpClient, RequestFactoryInterface $requestFactory, UriFactoryInterface $uriFactory)
[43] Fix | Delete
{
[44] Fix | Delete
$this->httpClient = $httpClient;
[45] Fix | Delete
$this->requestFactory = $requestFactory;
[46] Fix | Delete
$this->uriFactory = $uriFactory;
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
public function getHttpClient(): ClientInterface
[50] Fix | Delete
{
[51] Fix | Delete
return $this->httpClient;
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
public function getRequestFactory(): RequestFactoryInterface
[55] Fix | Delete
{
[56] Fix | Delete
return $this->requestFactory;
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
public function getUriFactory(): UriFactoryInterface
[60] Fix | Delete
{
[61] Fix | Delete
return $this->uriFactory;
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
/**
[65] Fix | Delete
* send a request and return the response
[66] Fix | Delete
*
[67] Fix | Delete
* @param Client::METHOD_* $method
[68] Fix | Delete
* @param string $url
[69] Fix | Delete
* @param array<string,string|string[]> $headers
[70] Fix | Delete
*
[71] Fix | Delete
* @throws ClientException if anything goes wrong requesting the data
[72] Fix | Delete
*/
[73] Fix | Delete
public function request(string $method, string $url, array $headers = []): Response
[74] Fix | Delete
{
[75] Fix | Delete
if ($method !== self::METHOD_GET) {
[76] Fix | Delete
throw new InvalidArgumentException(sprintf(
[77] Fix | Delete
'%s(): Argument #1 ($method) only supports method "%s".',
[78] Fix | Delete
__METHOD__,
[79] Fix | Delete
self::METHOD_GET
[80] Fix | Delete
), 1);
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
if (preg_match('/^http(s)?:\/\//i', $url)) {
[84] Fix | Delete
return $this->requestUrl($method, $url, $headers);
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
return $this->requestLocalFile($url);
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
/**
[91] Fix | Delete
* @param array<string,string|string[]> $headers
[92] Fix | Delete
*/
[93] Fix | Delete
private function requestUrl(string $method, string $url, array $headers): Response
[94] Fix | Delete
{
[95] Fix | Delete
$permanentUrl = $url;
[96] Fix | Delete
$requestedUrl = $url;
[97] Fix | Delete
$remainingRedirects = $this->allowedRedirects;
[98] Fix | Delete
[99] Fix | Delete
$request = $this->requestFactory->createRequest(
[100] Fix | Delete
$method,
[101] Fix | Delete
$this->uriFactory->createUri($requestedUrl)
[102] Fix | Delete
);
[103] Fix | Delete
[104] Fix | Delete
foreach ($headers as $name => $value) {
[105] Fix | Delete
$request = $request->withHeader($name, $value);
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
do {
[109] Fix | Delete
$followRedirect = false;
[110] Fix | Delete
[111] Fix | Delete
try {
[112] Fix | Delete
$response = $this->httpClient->sendRequest($request);
[113] Fix | Delete
} catch (ClientExceptionInterface $th) {
[114] Fix | Delete
throw new ClientException($th->getMessage(), $th->getCode(), $th);
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
$statusCode = $response->getStatusCode();
[118] Fix | Delete
[119] Fix | Delete
// If we have a redirect
[120] Fix | Delete
if (in_array($statusCode, [300, 301, 302, 303, 307]) && $response->hasHeader('Location')) {
[121] Fix | Delete
// Prevent infinity redirect loops
[122] Fix | Delete
if ($remainingRedirects <= 0) {
[123] Fix | Delete
break;
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
$remainingRedirects--;
[127] Fix | Delete
$followRedirect = true;
[128] Fix | Delete
[129] Fix | Delete
$requestedUrl = $response->getHeaderLine('Location');
[130] Fix | Delete
[131] Fix | Delete
if ($statusCode === 301) {
[132] Fix | Delete
$permanentUrl = $requestedUrl;
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
$request = $request->withUri($this->uriFactory->createUri($requestedUrl));
[136] Fix | Delete
}
[137] Fix | Delete
} while ($followRedirect);
[138] Fix | Delete
[139] Fix | Delete
return new Psr7Response($response, $permanentUrl, $requestedUrl);
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
private function requestLocalFile(string $path): Response
[143] Fix | Delete
{
[144] Fix | Delete
if (!is_readable($path)) {
[145] Fix | Delete
throw new ClientException(sprintf('file "%s" is not readable', $path));
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
try {
[149] Fix | Delete
$raw = file_get_contents($path);
[150] Fix | Delete
} catch (Throwable $th) {
[151] Fix | Delete
throw new ClientException($th->getMessage(), $th->getCode(), $th);
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
if ($raw === false) {
[155] Fix | Delete
throw new ClientException('file_get_contents() could not read the file', 1);
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
return new RawTextResponse($raw, $path);
[159] Fix | Delete
}
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function