Skip to content

Commit

Permalink
URL is now an object so it's easier to pass it to exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Aug 9, 2024
1 parent 48c7c3f commit 44e440c
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 36 deletions.
14 changes: 6 additions & 8 deletions src/Fetcher/Exceptions/SecurityTxtCannotOpenUrlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@

namespace Spaze\SecurityTxt\Fetcher\Exceptions;

use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherUrl;
use Throwable;

class SecurityTxtCannotOpenUrlException extends SecurityTxtFetcherException
{

/**
* @param list<string> $redirects
*/
public function __construct(string $url, array $redirects, ?Throwable $previous = null)
public function __construct(SecurityTxtFetcherUrl $url, ?Throwable $previous = null)
{
parent::__construct(
func_get_args(),
$redirects ? "Can't open %s (redirects: %s)" : "Can't open %s",
$redirects ? [$url, implode(' => ', $redirects)] : [$url],
$url,
$redirects,
$url->getRedirects() ? "Can't open %s (redirects: %s)" : "Can't open %s",
$url->getRedirects() ? [$url->getUrl(), implode(' => ', $url->getRedirects())] : [$url->getUrl()],
$url->getUrl(),
$url->getRedirects(),
previous: $previous,
);
}
Expand Down
14 changes: 6 additions & 8 deletions src/Fetcher/Exceptions/SecurityTxtCannotReadUrlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@

namespace Spaze\SecurityTxt\Fetcher\Exceptions;

use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherUrl;
use Throwable;

class SecurityTxtCannotReadUrlException extends SecurityTxtFetcherException
{

/**
* @param list<string> $redirects
*/
public function __construct(string $url, array $redirects, ?Throwable $previous = null)
public function __construct(SecurityTxtFetcherUrl $url, ?Throwable $previous = null)
{
parent::__construct(
func_get_args(),
$redirects ? "Can't get contents of %s (redirects: %s)" : "Can't get contents of %s",
$redirects ? [$url, implode(' => ', $redirects)] : [$url],
$url,
$redirects,
$url->getRedirects() ? "Can't get contents of %s (redirects: %s)" : "Can't get contents of %s",
$url->getRedirects() ? [$url->getUrl(), implode(' => ', $url->getRedirects())] : [$url->getUrl()],
$url->getUrl(),
$url->getRedirects(),
previous: $previous,
);
}
Expand Down
14 changes: 6 additions & 8 deletions src/Fetcher/Exceptions/SecurityTxtNoHttpCodeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@

namespace Spaze\SecurityTxt\Fetcher\Exceptions;

use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherUrl;
use Throwable;

class SecurityTxtNoHttpCodeException extends SecurityTxtFetcherException
{

/**
* @param list<string> $redirects
*/
public function __construct(string $url, array $redirects, ?Throwable $previous = null)
public function __construct(SecurityTxtFetcherUrl $url, ?Throwable $previous = null)
{
parent::__construct(
func_get_args(),
$redirects ? "Missing HTTP code when fetching %s (redirects: %s)" : "Missing HTTP code when fetching %s",
$redirects ? [$url, implode(' => ', $redirects)] : [$url],
$url,
$redirects,
$url->getRedirects() ? "Missing HTTP code when fetching %s (redirects: %s)" : "Missing HTTP code when fetching %s",
$url->getRedirects() ? [$url->getUrl(), implode(' => ', $url->getRedirects())] : [$url->getUrl()],
$url->getUrl(),
$url->getRedirects(),
previous: $previous,
);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Fetcher/HttpClients/SecurityTxtFetcherFopenClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use Spaze\SecurityTxt\Fetcher\Exceptions\SecurityTxtCannotReadUrlException;
use Spaze\SecurityTxt\Fetcher\Exceptions\SecurityTxtNoHttpCodeException;
use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherResponse;
use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherUrl;

class SecurityTxtFetcherFopenClient implements SecurityTxtFetcherHttpClient
{

/**
* @param list<string> $redirects
* @throws SecurityTxtCannotReadUrlException
* @throws SecurityTxtCannotOpenUrlException
* @throws SecurityTxtNoHttpCodeException
*/
public function getResponse(string $url, ?string $contextHost, array $redirects): SecurityTxtFetcherResponse
public function getResponse(SecurityTxtFetcherUrl $url, ?string $contextHost): SecurityTxtFetcherResponse
{

$options = [
Expand All @@ -33,13 +33,13 @@ public function getResponse(string $url, ?string $contextHost, array $redirects)
];
$options['http']['header'][] = "Host: {$contextHost}";
}
$fp = @fopen($url, 'r', context: stream_context_create($options)); // intentionally @, converted to exception
$fp = @fopen($url->getUrl(), 'r', context: stream_context_create($options)); // intentionally @, converted to exception
if (!$fp) {
throw new SecurityTxtCannotOpenUrlException($url, $redirects);
throw new SecurityTxtCannotOpenUrlException($url);
}
$contents = stream_get_contents($fp);
if ($contents === false) {
throw new SecurityTxtCannotReadUrlException($url, $redirects);
throw new SecurityTxtCannotReadUrlException($url);
}
$metadata = stream_get_meta_data($fp);
fclose($fp);
Expand All @@ -48,7 +48,7 @@ public function getResponse(string $url, ?string $contextHost, array $redirects)
if (preg_match('~^HTTP/[\d.]+ (\d+)~', $wrapperData[0], $matches)) {
$code = (int)$matches[1];
} else {
throw new SecurityTxtNoHttpCodeException($url, $redirects);
throw new SecurityTxtNoHttpCodeException($url);
}

$headers = [];
Expand Down
6 changes: 2 additions & 4 deletions src/Fetcher/HttpClients/SecurityTxtFetcherHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
namespace Spaze\SecurityTxt\Fetcher\HttpClients;

use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherResponse;
use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherUrl;

interface SecurityTxtFetcherHttpClient
{

/**
* @param list<string> $redirects
*/
public function getResponse(string $url, ?string $contextHost, array $redirects): SecurityTxtFetcherResponse;
public function getResponse(SecurityTxtFetcherUrl $url, ?string $contextHost): SecurityTxtFetcherResponse;

}
2 changes: 1 addition & 1 deletion src/Fetcher/SecurityTxtFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private function getResponse(string $url, string $urlTemplate, string $host, boo
if ($redirects) {
array_unshift($redirects, $builtUrl);
}
$response = $this->httpClient->getResponse($url, $useHostForContextHost ? $host : null, $redirects);
$response = $this->httpClient->getResponse(new SecurityTxtFetcherUrl($url, $redirects), $useHostForContextHost ? $host : null);
if ($response->getHttpCode() >= 400) {
throw new SecurityTxtUrlNotFoundException($url, $response->getHttpCode());
}
Expand Down
33 changes: 33 additions & 0 deletions src/Fetcher/SecurityTxtFetcherUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types = 1);

namespace Spaze\SecurityTxt\Fetcher;

class SecurityTxtFetcherUrl
{

/**
* @param list<string> $redirects
*/
public function __construct(
private readonly string $url,
private readonly array $redirects,
) {
}


public function getUrl(): string
{
return $this->url;
}


/**
* @return list<string>
*/
public function getRedirects(): array
{
return $this->redirects;
}

}
2 changes: 1 addition & 1 deletion tests/Fetcher/SecurityTxtFetcherTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SecurityTxtFetcherTest extends TestCase
private SecurityTxtFetcherResponse $fetcherResponse;


public function getResponse(string $url, ?string $contextHost, array $redirects): SecurityTxtFetcherResponse
public function getResponse(SecurityTxtFetcherUrl $url, ?string $contextHost): SecurityTxtFetcherResponse
{
return $this->fetcherResponse;
}
Expand Down

0 comments on commit 44e440c

Please sign in to comment.