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,8 +39,14 @@ class REST {
$req->refreshURL();
$class = $this->apis[$api]['class'];
$drv = new $class();
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 {
// sort the API list so the longest URL prefixes come first

View file

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

View file

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