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);
 | 
			
		||||
 | 
			
		||||
    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) => "<h1>Webmention server</h1>");
 | 
			
		||||
 | 
			
		||||
$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
 | 
			
		||||
        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;
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
    //  */
 | 
			
		||||
 | 
			
		||||
@ -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));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user