$timestamp = microtime(true);
$response = $module->run($input);
$this->profiler->set(microtime(true) - $timestamp, 'content');
- if ($response->getHeaderLine(ICanCreateResponses::X_HEADER) === ICanCreateResponses::TYPE_HTML) {
+ if ($response->getHeaderLine(ICanCreateResponses::X_HEADER) === ICanCreateResponses::TYPE_HTML &&
+ $response->getStatusCode() == 200) {
$page->run($this, $this->baseURL, $this->args, $this->mode, $response, $this->l10n, $this->profiler, $this->config, $pconfig);
} else {
$page->exit($response);
*/
public function exit(ResponseInterface $response)
{
+ header(sprintf("HTTP/%s %i %s",
+ $response->getProtocolVersion(),
+ $response->getStatusCode(),
+ $response->getReasonPhrase())
+ );
+
foreach ($response->getHeaders() as $key => $header) {
if (is_array($header)) {
$header_str = implode(',', $header);
*/
public function setType(string $type, ?string $content_type = null): void;
+ /**
+ * Sets the status and the reason for the response
+ *
+ * @param int $status The HTTP status code
+ * @param null|string $reason Reason phrase (when empty a default will be used based on the status code)
+ *
+ * @return void
+ */
+ public function setStatus(int $status = 200, ?string $reason = null): void;
+
/**
* Creates a PSR-7 compliant interface
* @see https://www.php-fig.org/psr/psr-7/
*/
protected $type = self::TYPE_HTML;
+ protected $status = 200;
+
+ protected $reason = null;
+
/**
* {@inheritDoc}
*/
$this->type = $type;
}
+ /**
+ * {@inheritDoc}
+ */
+ public function setStatus(int $status = 200, ?string $reason = null): void
+ {
+ $this->status = $status;
+ $this->reason = $reason;
+ }
+
/**
* {@inheritDoc}
*/
// Setting the response type as an X-header for direct usage
$this->headers[static::X_HEADER] = $this->type;
- return new \GuzzleHttp\Psr7\Response(200, $this->headers, $this->content);
+ return new \GuzzleHttp\Psr7\Response($this->status, $this->headers, $this->content, $this->reason);
}
}
protected function options(array $request = [])
{
// @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
- $this->response->setHeader('Allow', implode(',', Router::ALLOWED_METHODS));
- $this->response->setHeader(($this->server['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' 204 No Content');
+ $this->response->setHeader(implode(',', Router::ALLOWED_METHODS), 'Allow');
+ $this->response->setStatus(204);
}
}
--- /dev/null
+<?php
+
+namespace Friendica\Test\src\Module\Special;
+
+use Friendica\App\Router;
+use Friendica\Capabilities\ICanCreateResponses;
+use Friendica\DI;
+use Friendica\Module\Special\Options;
+use Friendica\Test\FixtureTest;
+
+class OptionsTest extends FixtureTest
+{
+ public function testOptions()
+ {
+ $response = (new Options(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::OPTIONS]))->run();
+
+ self::assertEmpty((string)$response->getBody());
+ self::assertEquals(204, $response->getStatusCode());
+ self::assertEquals('No Content', $response->getReasonPhrase());
+ self::assertEquals([
+ 'Allow' => [implode(',', Router::ALLOWED_METHODS)],
+ ICanCreateResponses::X_HEADER => ['html'],
+ ], $response->getHeaders());
+ self::assertEquals(implode(',', Router::ALLOWED_METHODS), $response->getHeaderLine('Allow'));
+ }
+}