Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Commit

Permalink
add 'status' to return value of LEConnector requests (#76)
Browse files Browse the repository at this point in the history
Fix `Invalid response, header: HTTP/2 200` due to changed production CDN.
  • Loading branch information
a3dho3yn authored and yourivw committed Sep 29, 2019
1 parent 760da6c commit 2fa0f35
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
12 changes: 6 additions & 6 deletions src/LEAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private function createLEAccount($email)

$sign = $this->connector->signRequestJWK(array('contact' => $contact, 'termsOfServiceAgreed' => true), $this->connector->newAccount);
$post = $this->connector->post($this->connector->newAccount, $sign);
if(strpos($post['header'], "201 Created") !== false)
if($post['status'] === 201)
{
if(preg_match('~Location: (\S+)~i', $post['header'], $matches)) return trim($matches[1]);
}
Expand All @@ -113,7 +113,7 @@ private function getLEAccount()
$sign = $this->connector->signRequestJWK(array('onlyReturnExisting' => true), $this->connector->newAccount);
$post = $this->connector->post($this->connector->newAccount, $sign);

if(strpos($post['header'], "200 OK") !== false)
if($post['status'] === 200)
{
if(preg_match('~Location: (\S+)~i', $post['header'], $matches)) return trim($matches[1]);
}
Expand All @@ -127,7 +127,7 @@ private function getLEAccountData()
{
$sign = $this->connector->signRequestKid(array('' => ''), $this->connector->accountURL, $this->connector->accountURL);
$post = $this->connector->post($this->connector->accountURL, $sign);
if(strpos($post['header'], "200 OK") !== false)
if($post['status'] === 200)
{
$this->id = isset($post['body']['id']) ? $post['body']['id'] : '';
$this->key = $post['body']['key'];
Expand Down Expand Up @@ -156,7 +156,7 @@ public function updateAccount($email)

$sign = $this->connector->signRequestKid(array('contact' => $contact), $this->connector->accountURL, $this->connector->accountURL);
$post = $this->connector->post($this->connector->accountURL, $sign);
if(strpos($post['header'], "200 OK") !== false)
if($post['status'] === 200)
{
$this->id = $post['body']['id'];
$this->key = $post['body']['key'];
Expand Down Expand Up @@ -196,7 +196,7 @@ public function changeAccountKeys()
$outerPayload = $this->connector->signRequestJWK($innerPayload, $this->connector->keyChange, $this->accountKeys['private_key'].'.new');
$sign = $this->connector->signRequestKid($outerPayload, $this->connector->accountURL, $this->connector->keyChange);
$post = $this->connector->post($this->connector->keyChange, $sign);
if(strpos($post['header'], "200 OK") !== false)
if($post['status'] === 200)
{
unlink($this->accountKeys['private_key']);
unlink($this->accountKeys['public_key']);
Expand Down Expand Up @@ -227,7 +227,7 @@ public function deactivateAccount()
{
$sign = $this->connector->signRequestKid(array('status' => 'deactivated'), $this->connector->accountURL, $this->connector->accountURL);
$post = $this->connector->post($this->connector->accountURL, $sign);
if(strpos($post['header'], "200 OK") !== false)
if($post['status'] === 200)
{
$this->connector->accountDeactivated = true;
if($this->log instanceof \Psr\Log\LoggerInterface)
Expand Down
4 changes: 2 additions & 2 deletions src/LEAuthorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct($connector, $log, $authorizationURL)
$this->authorizationURL = $authorizationURL;

$get = $this->connector->get($this->authorizationURL);
if(strpos($get['header'], "200 OK") !== false)
if($get['status'] === 200)
{
$this->identifier = $get['body']['identifier'];
$this->status = $get['body']['status'];
Expand All @@ -85,7 +85,7 @@ public function __construct($connector, $log, $authorizationURL)
public function updateData()
{
$get = $this->connector->get($this->authorizationURL);
if(strpos($get['header'], "200 OK") !== false)
if($get['status'] === 200)
{
$this->identifier = $get['body']['identifier'];
$this->status = $get['body']['status'];
Expand Down
28 changes: 17 additions & 11 deletions src/LEConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function getLEDirectory()
*/
private function getNewNonce()
{
if(strpos($this->head($this->newNonce)['header'], "200 OK") == false) throw new \RuntimeException('No new nonce.');
if($this->head($this->newNonce)['status'] !== 200) throw new \RuntimeException('No new nonce.');
}

/**
Expand All @@ -97,7 +97,7 @@ private function getNewNonce()
* @param string $URL The URL or partial URL to make the request to. If it is partial, the baseURL will be prepended.
* @param object $data The body to attach to a POST request. Expected as a JSON encoded string.
*
* @return array Returns an array with the keys 'request', 'header' and 'body'.
* @return array Returns an array with the keys 'request', 'header', 'status' and 'body'.
*/
private function request($method, $URL, $data = null)
{
Expand Down Expand Up @@ -132,20 +132,26 @@ private function request($method, $URL, $data = null)
throw new \RuntimeException('Curl: ' . curl_error($handle));
}

$header_size = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
$headerSize = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
$statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);

$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$header = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
$jsonbody = json_decode($body, true);
$jsonresponse = array('request' => $method . ' ' . $requestURL, 'header' => $header, 'body' => $jsonbody === null ? $body : $jsonbody);
$jsonresponse = array(
'request' => $method . ' ' . $requestURL,
'header' => $header,
'status' => $statusCode,
'body' => $jsonbody === null ? $body : $jsonbody,
);
if($this->log instanceof \Psr\Log\LoggerInterface)
{
$this->log->debug($method . ' response received', $jsonresponse);
}
elseif($this->log >= LEClient::LOG_DEBUG) LEFunctions::log($jsonresponse);

if( (($method == 'POST' OR $method == 'GET') AND strpos($header, "200 OK") === false AND strpos($header, "201 Created") === false) OR
($method == 'HEAD' AND strpos($header, "200 OK") === false))
if((($method == 'POST' OR $method == 'GET') AND $statusCode !== 200 AND $statusCode !== 201) OR
($method == 'HEAD' AND $statusCode !== 200))
{
throw new \RuntimeException('Invalid response, header: ' . $header);
}
Expand All @@ -167,7 +173,7 @@ private function request($method, $URL, $data = null)
*
* @param string $url The URL or partial URL to make the request to. If it is partial, the baseURL will be prepended.
*
* @return array Returns an array with the keys 'request', 'header' and 'body'.
* @return array Returns an array with the keys 'request', 'header', 'status' and 'body'.
*/
public function get($url)
{
Expand All @@ -180,7 +186,7 @@ public function get($url)
* @param string $url The URL or partial URL to make the request to. If it is partial, the baseURL will be prepended.
* @param object $data The body to attach to a POST request. Expected as a json string.
*
* @return array Returns an array with the keys 'request', 'header' and 'body'.
* @return array Returns an array with the keys 'request', 'header', 'status' and 'body'.
*/
public function post($url, $data = null)
{
Expand All @@ -192,7 +198,7 @@ public function post($url, $data = null)
*
* @param string $url The URL or partial URL to make the request to. If it is partial, the baseURL will be prepended.
*
* @return array Returns an array with the keys 'request', 'header' and 'body'.
* @return array Returns an array with the keys 'request', 'header', 'status' and 'body'.
*/
public function head($url)
{
Expand Down
18 changes: 9 additions & 9 deletions src/LEOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function __construct($connector, $log, $certificateKeys, $basename, $doma
if (filter_var($this->orderURL, FILTER_VALIDATE_URL))
{
$get = $this->connector->get($this->orderURL);
if(strpos($get['header'], "200 OK") !== false && $get['body']['status'] != "invalid")
if($get['status'] === 200 && $get['body']['status'] != "invalid")
{
$orderdomains = array_map(function($ident) { return $ident['value']; }, $get['body']['identifiers']);
$diff = array_merge(array_diff($orderdomains, $domains), array_diff($domains, $orderdomains));
Expand Down Expand Up @@ -198,7 +198,7 @@ private function createOrder($domains, $notBefore, $notAfter)
$sign = $this->connector->signRequestKid($payload, $this->connector->accountURL, $this->connector->newOrder);
$post = $this->connector->post($this->connector->newOrder, $sign);

if(strpos($post['header'], "201 Created") !== false)
if($post['status'] === 201)
{
if(preg_match('~Location: (\S+)~i', $post['header'], $matches))
{
Expand Down Expand Up @@ -253,7 +253,7 @@ private function createOrder($domains, $notBefore, $notAfter)
private function updateOrderData()
{
$get = $this->connector->get($this->orderURL);
if(strpos($get['header'], "200 OK") !== false)
if($get['status'] === 200)
{
$this->status = $get['body']['status'];
$this->expires = $get['body']['expires'];
Expand Down Expand Up @@ -398,7 +398,7 @@ public function verifyPendingOrderAuthorization($identifier, $type, $localcheck
{
$sign = $this->connector->signRequestKid(array('keyAuthorization' => $keyAuthorization), $this->connector->accountURL, $challenge['url']);
$post = $this->connector->post($challenge['url'], $sign);
if(strpos($post['header'], "200 OK") !== false)
if($post['status'] === 200)
{
if($localcheck)
{
Expand Down Expand Up @@ -431,7 +431,7 @@ public function verifyPendingOrderAuthorization($identifier, $type, $localcheck
{
$sign = $this->connector->signRequestKid(array('keyAuthorization' => $keyAuthorization), $this->connector->accountURL, $challenge['url']);
$post = $this->connector->post($challenge['url'], $sign);
if(strpos($post['header'], "200 OK") !== false)
if($post['status'] === 200)
{
if($localcheck)
{
Expand Down Expand Up @@ -481,7 +481,7 @@ public function deactivateOrderAuthorization($identifier)
{
$sign = $this->connector->signRequestKid(array('status' => 'deactivated'), $this->connector->accountURL, $auth->authorizationURL);
$post = $this->connector->post($auth->authorizationURL, $sign);
if(strpos($post['header'], "200 OK") !== false)
if($post['status'] === 200)
{
if($this->log instanceof \Psr\Log\LoggerInterface)
{
Expand Down Expand Up @@ -573,7 +573,7 @@ public function finalizeOrder($csr = '')
$csr = trim(LEFunctions::Base64UrlSafeEncode(base64_decode($csr)));
$sign = $this->connector->signRequestKid(array('csr' => $csr), $this->connector->accountURL, $this->finalizeURL);
$post = $this->connector->post($this->finalizeURL, $sign);
if(strpos($post['header'], "200 OK") !== false)
if($post['status'] === 200)
{
$this->status = $post['body']['status'];
$this->expires = $post['body']['expires'];
Expand Down Expand Up @@ -643,7 +643,7 @@ public function getCertificate()
if($this->status == 'valid' && !empty($this->certificateURL))
{
$get = $this->connector->get($this->certificateURL);
if(strpos($get['header'], "200 OK") !== false)
if($get['status'] === 200)
{
if(preg_match_all('~(-----BEGIN\sCERTIFICATE-----[\s\S]+?-----END\sCERTIFICATE-----)~i', $get['body'], $matches))
{
Expand Down Expand Up @@ -718,7 +718,7 @@ public function revokeCertificate($reason = 0)

$sign = $this->connector->signRequestJWK(array('certificate' => $certificate, 'reason' => $reason), $this->connector->revokeCert, $this->certificateKeys['private_key']);
$post = $this->connector->post($this->connector->revokeCert, $sign);
if(strpos($post['header'], "200 OK") !== false)
if($post['status'] === 200)
{
if($this->log instanceof \Psr\Log\LoggerInterface)
{
Expand Down

0 comments on commit 2fa0f35

Please sign in to comment.