1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00

Transparently handle HEAD requests; fixes #114

This commit is contained in:
J. King 2017-09-29 17:40:07 -04:00
parent 1ca5b4b456
commit 00bc7a00a0
3 changed files with 29 additions and 10 deletions

View file

@ -39,7 +39,13 @@ class REST {
$req->refreshURL(); $req->refreshURL();
$class = $this->apis[$api]['class']; $class = $this->apis[$api]['class'];
$drv = new $class(); $drv = new $class();
return $drv->dispatch($req); if ($req->head) {
$res = $drv->dispatch($req);
$res->head = true;
return $res;
} else {
return $drv->dispatch($req);
}
} }
public function apiMatch(string $url, array $map): string { public function apiMatch(string $url, array $map): string {

View file

@ -4,6 +4,7 @@ namespace JKingWeb\Arsse\REST;
class Request { class Request {
public $method = "GET"; public $method = "GET";
public $head = false;
public $url = ""; public $url = "";
public $path =""; public $path ="";
public $paths = []; public $paths = [];
@ -26,6 +27,10 @@ class Request {
$this->url = $url; $this->url = $url;
$this->body = $body; $this->body = $body;
$this->type = $contentType; $this->type = $contentType;
if($this->method=="HEAD") {
$this->head = true;
$this->method = "GET";
}
$this->refreshURL(); $this->refreshURL();
} }

View file

@ -9,6 +9,7 @@ class Response {
const T_XML = "application/xml"; const T_XML = "application/xml";
const T_TEXT = "text/plain"; const T_TEXT = "text/plain";
public $head = false;
public $code; public $code;
public $payload; public $payload;
public $type; public $type;
@ -24,15 +25,11 @@ class Response {
public function output() { public function output() {
if (!headers_sent()) { if (!headers_sent()) {
try { foreach ($this->fields as $field) {
$statusText = Arsse::$lang->msg("HTTP.Status.".$this->code); header($field);
} catch (\JKingWeb\Arsse\Lang\Exception $e) {
$statusText = "";
} }
header("Status: ".$this->code." ".$statusText);
$body = ""; $body = "";
if (!is_null($this->payload)) { if (!is_null($this->payload)) {
header("Content-Type: ".$this->type);
switch ($this->type) { switch ($this->type) {
case self::T_JSON: case self::T_JSON:
$body = (string) json_encode($this->payload, \JSON_PRETTY_PRINT); $body = (string) json_encode($this->payload, \JSON_PRETTY_PRINT);
@ -42,10 +39,21 @@ class Response {
break; break;
} }
} }
foreach ($this->fields as $field) { if (strlen($body)) {
header($field); header("Content-Type: ".$this->type);
header("Content-Length: ".strlen($body));
} elseif ($this->code==200) {
$this->code = 204;
}
try {
$statusText = Arsse::$lang->msg("HTTP.Status.".$this->code);
} catch (\JKingWeb\Arsse\Lang\Exception $e) {
$statusText = "";
}
header("Status: ".$this->code." ".$statusText);
if (!$this->head) {
echo $body;
} }
echo $body;
} else { } else {
throw new REST\Exception("headersSent"); throw new REST\Exception("headersSent");
} }