Fix POST argument parsing
This commit is contained in:
parent
4c4fffaf9d
commit
15e9d4c184
@ -44,7 +44,10 @@ $router->post("/endpoint", function (Request $req, Response $response) use ($gat
|
|||||||
$endpoint = new Endpoint(HttpClient::create(), $gateway);
|
$endpoint = new Endpoint(HttpClient::create(), $gateway);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$endpoint->receiveWebmention($source, $target);
|
$id = $endpoint->receiveWebmention($source, $target);
|
||||||
|
|
||||||
|
$response->header('Location', "/webmention/" . $id);
|
||||||
|
$response->status_code = StatusCode::Created;
|
||||||
} catch (InvalidUrlException) {
|
} catch (InvalidUrlException) {
|
||||||
$response->status_code = StatusCode::BadRequest;
|
$response->status_code = StatusCode::BadRequest;
|
||||||
return "Source and target must be valid URLs";
|
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";
|
return "Source does not mention the target";
|
||||||
}
|
}
|
||||||
|
|
||||||
$response->status_code = StatusCode::Created;
|
|
||||||
return;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$router->get('/webmention/(\d+)', [$apiController, "get"]);
|
||||||
|
|
||||||
$router->get('/', fn($req, $res) => "<h1>Webmention server</h1>");
|
$router->get('/', fn($req, $res) => "<h1>Webmention server</h1>");
|
||||||
|
|
||||||
$router->get('/api/webmentions', [$apiController, "list"]);
|
$router->get('/api/webmentions', [$apiController, "list"]);
|
||||||
|
8
requests.http
Normal file
8
requests.http
Normal file
@ -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/
|
||||||
|
|
||||||
|
|
||||||
|
###
|
@ -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
|
// Validate that both source and target are actual domains
|
||||||
if (!Url::validateUrl($source) || !Url::validateUrl($target)) {
|
if (!Url::validateUrl($source) || !Url::validateUrl($target)) {
|
||||||
@ -51,7 +51,7 @@ class Endpoint
|
|||||||
$author = $this->parseAuthor($container);
|
$author = $this->parseAuthor($container);
|
||||||
|
|
||||||
$webmention = new Webmention(null, $target, $source, $type, $content, $author);
|
$webmention = new Webmention(null, $target, $source, $type, $content, $author);
|
||||||
$this->gateway->save($webmention);
|
return $this->gateway->save($webmention);
|
||||||
} else {
|
} else {
|
||||||
throw new SourceNotFoundException();
|
throw new SourceNotFoundException();
|
||||||
}
|
}
|
||||||
@ -69,7 +69,7 @@ class Endpoint
|
|||||||
|
|
||||||
private function parseMentionType(string $target, Crawler $document): MentionType
|
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")) {
|
if (str_contains($class, "u-like-of")) {
|
||||||
return MentionType::Like;
|
return MentionType::Like;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace Lewisdale\Webmentions\Router;
|
namespace Lewisdale\Webmentions\Router;
|
||||||
|
|
||||||
class Response {
|
class Response {
|
||||||
|
private array $headers = [];
|
||||||
public StatusCode $status_code = StatusCode::Ok;
|
public StatusCode $status_code = StatusCode::Ok;
|
||||||
public string $body = "";
|
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 {
|
public function json(mixed $obj): string {
|
||||||
return json_encode($obj);
|
return json_encode($obj);
|
||||||
}
|
}
|
||||||
@ -26,6 +39,12 @@ class Response {
|
|||||||
die();
|
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
|
// * Render a template, with an array of values to provide to the template
|
||||||
// */
|
// */
|
||||||
|
@ -106,7 +106,7 @@ class Router
|
|||||||
private function respond(Response $response)
|
private function respond(Response $response)
|
||||||
{
|
{
|
||||||
http_response_code($response->status_code->code());
|
http_response_code($response->status_code->code());
|
||||||
|
$response->write_headers();
|
||||||
echo $response->body;
|
echo $response->body;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,7 +115,7 @@ class Router
|
|||||||
$uri = $_SERVER['REQUEST_URI'];
|
$uri = $_SERVER['REQUEST_URI'];
|
||||||
$method = Method::from($_SERVER['REQUEST_METHOD']);
|
$method = Method::from($_SERVER['REQUEST_METHOD']);
|
||||||
$query = $_GET;
|
$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));
|
return new Request($params, $uri, $method, $post, $query, array_change_key_case(getallheaders(), CASE_LOWER));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user