Skip to content

Commit

Permalink
Merge pull request #10 from digitaltimde/4.x
Browse files Browse the repository at this point in the history
Url based proxy support
  • Loading branch information
Allyans3 authored Jan 30, 2023
2 parents 33ae6a3 + 4daeede commit 137b406
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions src/Engine/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
abstract class Request
{
private $curl;

private $defaultCurlOpts = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLINFO_HEADER_OUT => true,
];

/**
* @param array $proxy
* @param string $cookies
Expand All @@ -28,14 +28,14 @@ abstract class Request
* @throws InvalidClassException
*/
public function makeRequest(array $proxy = [], string $cookies = '', bool $detailed = false, array $curlOpts = [],
bool $multiRequest = false)
bool $multiRequest = false)
{
// if ($multiRequest)
// return self::makeMultiRequest($proxy, $detailed);

return self::makeSingleRequest($proxy, $cookies, $detailed, $curlOpts);
}

/**
* @param array $proxy
* @param string $cookies
Expand All @@ -48,41 +48,43 @@ private function makeSingleRequest(array $proxy = [], string $cookies = '', bool
{
if (!isset($this->curl))
$this->curl = curl_init();

$requestMethod = $this->getRequestMethod();

$postOpts = [];

if ($requestMethod === 'POST')
$postOpts = [CURLOPT_POSTFIELDS => http_build_query($this->getFormData())];

curl_setopt_array($this->curl,
$this->defaultCurlOpts + EngineService::setProxyForSingle($proxy) + $curlOpts + $postOpts +[
CURLOPT_CUSTOMREQUEST => $requestMethod,
CURLOPT_HTTPHEADER => self::mergeHeaders($this->getHeaders()),
CURLOPT_URL => $this->getUrl(),
CURLOPT_URL => (array_key_exists('url', $proxy)) ?
$proxy['url'].$this->getUrl() : $this->getUrl(),
CURLOPT_HEADER => $detailed,
CURLOPT_COOKIE => $cookies,
]
);

return $this->response($detailed ? self::exec() : curl_exec($this->curl), $detailed);
}

/**
* @return array
*/
public function exec(): array
{
$response = curl_exec($this->curl);

$requestHeaders = curl_getinfo($this->curl,CURLINFO_HEADER_OUT);
$headerSize = curl_getinfo($this->curl,CURLINFO_HEADER_SIZE);
$responseHeader = substr($response, 0, $headerSize);

$code = curl_getinfo($this->curl,CURLINFO_HTTP_CODE) ?: '';
$messageCode = array_key_exists($code, Engine::HTTP_CODES) ? Engine::HTTP_CODES[$code] : '';

return [
'request_headers' => self::headersToArray($requestHeaders),
'response_headers' => self::headersToArray($responseHeader),
Expand All @@ -97,7 +99,7 @@ public function exec(): array
'response' => substr($response, $headerSize)
];
}

/**
* @param $response
* @return array
Expand All @@ -106,17 +108,17 @@ private function getCookie($response): array
{
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi',
$response, $match_found);

$cookies = [];

foreach ($match_found[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}

return $cookies;
}

/**
* @param string $header
* @return array
Expand All @@ -125,11 +127,11 @@ private function headersToArray(string $header): array
{
$headers = [];
$headersTmpArray = explode("\r\n", $header);

for ($i = 0 ; $i < count( $headersTmpArray ) ; ++$i) {
// we don't care about the two \r\n lines at the end of the headers
if (strlen( $headersTmpArray[$i] ) > 0) {

// the headers start with HTTP status codes, which do not contain a colon, so we can filter them out too
if (strpos( $headersTmpArray[$i] , ":")) {
$headerName = substr( $headersTmpArray[$i] , 0 , strpos( $headersTmpArray[$i] , ":" ) );
Expand All @@ -138,70 +140,70 @@ private function headersToArray(string $header): array
}
}
}

return $headers;
}

// private function makeMultiRequest(array $proxy = [], bool $detailed = false)
// {
//
// }

/**
* @param $headers
* @return array
*/
private function mergeHeaders($headers): array
{
$mergedHeaders = [];

foreach ($headers as $key => $value) {
$mergedHeaders[] = $key . ': ' . $value;
}

return $mergedHeaders;
}

/**
* @throws InvalidClassException
*/
public function response($data, bool $detailed = false, bool $multiRequest = false)
{
$class = self::getResponseClass();

if (!class_exists($class))
throw new InvalidClassException('Call to undefined Response Class');

return new $class($data, $detailed, $multiRequest);
}

/**
* @return string
*/
private function getResponseClass(): string
{
$reversedArr = explode('\\', strrev(get_called_class()));

$method = strrev($reversedArr[0]);
$folders = '';

unset($reversedArr[0]);

foreach ($reversedArr as $folder) {
if (strrev($folder) === "Requests")
break;

$folders = strrev($folder) . '\\' . $folders;
}

return Engine::RESPONSE_PREFIX . $folders . $method;
}

/**
* Destructor of Request Engine
*/
function __destruct() {
if ($this->curl)
curl_close($this->curl);
}
}
}

0 comments on commit 137b406

Please sign in to comment.