diff --git a/db-name b/db-name new file mode 100644 index 0000000..40731fe Binary files /dev/null and b/db-name differ diff --git a/index.php b/index.php index 61578e5..e45a5e6 100644 --- a/index.php +++ b/index.php @@ -44,7 +44,10 @@ $router->post("/endpoint", function (Request $req, Response $response) use ($gat $endpoint = new Endpoint(HttpClient::create(), $gateway); try { - $endpoint->receiveWebmention($source, $target); + $id = $endpoint->receiveWebmention($source, $target); + + $response->header('Location', "/webmention/" . $id); + $response->status_code = StatusCode::Created; } catch (InvalidUrlException) { $response->status_code = StatusCode::BadRequest; return "Source and target must be valid URLs"; @@ -59,10 +62,10 @@ $router->post("/endpoint", function (Request $req, Response $response) use ($gat return "Source does not mention the target"; } - $response->status_code = StatusCode::Created; - return; }); +$router->get('/webmention/(\d+)', [$apiController, "get"]); + $router->get('/', fn($req, $res) => "

Webmention server

"); $router->get('/api/webmentions', [$apiController, "list"]); diff --git a/requests.http b/requests.http new file mode 100644 index 0000000..e01dd20 --- /dev/null +++ b/requests.http @@ -0,0 +1,8 @@ +POST http://localhost:8080/endpoint +Content-Type: application/x-www-form-urlencoded + +source = https://rknight.me/blog/generating-and-caching-open-graph-images-with-eleventy/ & +target = https://lewisdale.dev/post/adding-statically-generated-open-graph-images/ + + +### diff --git a/src/Endpoint.php b/src/Endpoint.php index 9966a46..e5df51c 100644 --- a/src/Endpoint.php +++ b/src/Endpoint.php @@ -23,7 +23,7 @@ class Endpoint { } - public function receiveWebmention(string $source, string $target): void + public function receiveWebmention(string $source, string $target): int { // Validate that both source and target are actual domains if (!Url::validateUrl($source) || !Url::validateUrl($target)) { @@ -51,7 +51,7 @@ class Endpoint $author = $this->parseAuthor($container); $webmention = new Webmention(null, $target, $source, $type, $content, $author); - $this->gateway->save($webmention); + return $this->gateway->save($webmention); } else { throw new SourceNotFoundException(); } @@ -69,7 +69,7 @@ class Endpoint private function parseMentionType(string $target, Crawler $document): MentionType { - $class = $document->filter('a[href="' . $target . '"]')->attr('class'); + $class = $document->filter('a[href="' . $target . '"]')->attr('class') ?? ''; if (str_contains($class, "u-like-of")) { return MentionType::Like; diff --git a/src/Router/Response.php b/src/Router/Response.php index 3570f48..86661fc 100644 --- a/src/Router/Response.php +++ b/src/Router/Response.php @@ -3,6 +3,7 @@ namespace Lewisdale\Webmentions\Router; class Response { + private array $headers = []; public StatusCode $status_code = StatusCode::Ok; public string $body = ""; @@ -10,6 +11,18 @@ class Response { { } + private function normalize_header_name(string $name): string + { + return ucwords(strtolower($name)); + } + + public function header(string $name, string $value): void + { + $normalised = $this->normalize_header_name($name); + echo $normalised; + $this->headers[$this->normalize_header_name($name)] = $value; + } + public function json(mixed $obj): string { return json_encode($obj); } @@ -26,6 +39,12 @@ class Response { die(); } + public function write_headers() { + foreach ($this->headers as $header => $value) { + header("$header: $value"); + } + } + // /** // * Render a template, with an array of values to provide to the template // */ diff --git a/src/Router/Router.php b/src/Router/Router.php index 1e6498f..46895ce 100644 --- a/src/Router/Router.php +++ b/src/Router/Router.php @@ -106,7 +106,7 @@ class Router private function respond(Response $response) { http_response_code($response->status_code->code()); - + $response->write_headers(); echo $response->body; } @@ -115,7 +115,7 @@ class Router $uri = $_SERVER['REQUEST_URI']; $method = Method::from($_SERVER['REQUEST_METHOD']); $query = $_GET; - $post = $_POST; + $post = json_decode(file_get_contents('php://input'), true); return new Request($params, $uri, $method, $post, $query, array_change_key_case(getallheaders(), CASE_LOWER)); }