Move URL into separate file, handle source URL validation for sending webmentions
This commit is contained in:
1 parent
fca08de828
commit
ec83d67bde
8 files changed
+125
-60
No files matched your search
@@ -3,8 +3,11 @@
|
||||
require_once __DIR__ . "/vendor/autoload.php";
|
||||
|
||||
use Lewisdale\Webmentions\Endpoint;
|
||||
use Lewisdale\Webmentions\Exceptions\InvalidSourceException;
|
||||
use Lewisdale\Webmentions\Exceptions\InvalidTargetException;
|
||||
use Lewisdale\Webmentions\Exceptions\InvalidUrlException;
|
||||
use Lewisdale\Webmentions\Exceptions\SourceNotFoundException;
|
||||
use Lewisdale\Webmentions\Exceptions\TargetNotMentionedException;
|
||||
use Lewisdale\Webmentions\Gateways\SqliteGateway;
|
||||
use Lewisdale\Webmentions\Router\Request;
|
||||
use Lewisdale\Webmentions\Router\Response;
|
||||
@@ -18,7 +21,13 @@ $router = new Router();
|
||||
$router->post("/send", function (Request $req, Response $response) {
|
||||
$mentioner = new Webmention();
|
||||
$source = $req->query["source"];
|
||||
$mentioner->sendForPage($source);
|
||||
try {
|
||||
$mentioner->sendForPage($source);
|
||||
return;
|
||||
} catch (InvalidSourceException $e) {
|
||||
$response->status_code = StatusCode::BadRequest;
|
||||
return $e->getMessage();
|
||||
}
|
||||
});
|
||||
|
||||
$router->post("/endpoint", function (Request $req, Response $response) {
|
||||
@@ -29,21 +38,22 @@ $router->post("/endpoint", function (Request $req, Response $response) {
|
||||
|
||||
try {
|
||||
$endpoint->receiveWebmention($source, $target);
|
||||
} catch (InvalidUrlException $e) {
|
||||
} catch (InvalidUrlException) {
|
||||
$response->status_code = StatusCode::BadRequest;
|
||||
return "Source and target must be valid URLs";
|
||||
} catch (InvalidTargetException $e) {
|
||||
} catch (InvalidTargetException) {
|
||||
$response->status_code = StatusCode::BadRequest;
|
||||
return "Target must be on the domain lewisdale.dev";
|
||||
} catch (\Lewisdale\Webmentions\Exceptions\SourceNotFoundException $e) {
|
||||
} catch (SourceNotFoundException $e) {
|
||||
$response->status_code = StatusCode::BadRequest;
|
||||
return "Source URL was unreachable";
|
||||
} catch (\Lewisdale\Webmentions\Exceptions\TargetNotMentionedException) {
|
||||
} catch (TargetNotMentionedException) {
|
||||
$response->status_code = StatusCode::BadRequest;
|
||||
return "Source does not mention the target";
|
||||
}
|
||||
|
||||
$response->status_code = StatusCode::Created;
|
||||
return;
|
||||
});
|
||||
|
||||
$router->get('/', fn($req, $res) => "<h1>Webmention server</h1>");
|
||||
|
||||
Reference in new issue
Block a user