Skip to content

Commit

Permalink
Merge pull request #1105 from oat-sa/fix/LSI-2325/backport-for-invalsi
Browse files Browse the repository at this point in the history
Fix/lsi 2325/backport for invalsi
  • Loading branch information
uncleempty authored Feb 5, 2024
2 parents 34c1df1 + 77cf4d6 commit 3e04edb
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions common/http/class.Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ class common_http_Request

const METHOD_GET = 'GET';

private const REDIRECT_CODES = [
301,
302,
303,
307,
308
];

/**
* Creates an request from the current call
*
Expand Down Expand Up @@ -187,12 +195,9 @@ public function getBody()
{
return $this->body;
}
/**
* @return common_http_Response
*/
public function send()
{

public function send(bool $followRedirects = false): common_http_Response
{
$curlHandler = curl_init($this->getUrl());

//set the headers
Expand Down Expand Up @@ -237,16 +242,40 @@ public function send()
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($curlHandler, CURLINFO_HEADER_OUT, 1);
//curl_setopt($curlHandler, CURLOPT_HEADER, true);


//directly setting `FOLLOWLOCATION` to false to make sure next lines would be working as expected
//and we can get the redirect url from curl
curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, 0);

$responseData = curl_exec($curlHandler);
$httpResponse = new common_http_Response();

$httpResponse->httpCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE);
$httpResponse->headerOut = curl_getinfo($curlHandler, CURLINFO_HEADER_OUT);
$httpResponse->effectiveUrl = curl_getinfo($curlHandler, CURLINFO_EFFECTIVE_URL);
$httpResponse->responseData = $responseData;

$redirectUrl = curl_getinfo($curlHandler, CURLINFO_REDIRECT_URL);
$sameDomain = null;
if ($redirectUrl) {
$initialDomain = parse_url($this->getUrl(), PHP_URL_HOST);
$redirectDomain = parse_url($redirectUrl, PHP_URL_HOST);

$sameDomain = ($initialDomain === $redirectDomain);
}

//curl_setopt($curlHandler, );
curl_close($curlHandler);

if (
$followRedirects
&& $sameDomain
&& in_array($httpResponse->httpCode, self::REDIRECT_CODES, true)
) {
$this->url = $redirectUrl;
$httpResponse = $this->send();
}

return $httpResponse;
}

Expand Down

0 comments on commit 3e04edb

Please sign in to comment.