2019-09-28 02:38:03 +00:00
|
|
|
<?php
|
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2021-04-14 15:17:01 +00:00
|
|
|
|
2019-09-28 02:38:03 +00:00
|
|
|
namespace JKingWeb\Arsse\Misc;
|
|
|
|
|
|
|
|
use Psr\Http\Message\MessageInterface;
|
2022-08-04 13:26:17 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
2019-09-28 02:38:03 +00:00
|
|
|
|
|
|
|
class HTTP {
|
|
|
|
public static function matchType(MessageInterface $msg, string ...$type): bool {
|
|
|
|
$header = $msg->getHeaderLine("Content-Type") ?? "";
|
|
|
|
foreach ($type as $t) {
|
2022-08-06 02:08:36 +00:00
|
|
|
if (($t[0] ?? "") === "+") {
|
|
|
|
$pattern = "/^[^+;,\s]*".preg_quote(trim($t), "/")."\s*($|;|,)/Di";
|
|
|
|
} else {
|
|
|
|
$pattern = "/^".preg_quote(trim($t), "/")."\s*($|;|,)/Di";
|
|
|
|
}
|
2019-09-28 02:38:03 +00:00
|
|
|
if (preg_match($pattern, $header)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-04 13:26:17 +00:00
|
|
|
|
|
|
|
public static function respEmpty(int $status, ?array $headers = []): ResponseInterface {
|
|
|
|
return new Response($status, $headers ?? []);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function respJson($body, int $status = 200, ?array $headers = []): ResponseInterface {
|
|
|
|
$headers = ($headers ?? []) + ['Content-Type' => "application/json"];
|
|
|
|
return new Response($status, $headers, json_encode($body, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function respText(string $body, int $status = 200, ?array $headers = []): ResponseInterface {
|
|
|
|
$headers = ($headers ?? []) + ['Content-Type' => "text/plain; charset=UTF-8"];
|
|
|
|
return new Response($status, $headers, $body);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function respXml(string $body, int $status = 200, ?array $headers = []): ResponseInterface {
|
|
|
|
$headers = ($headers ?? []) + ['Content-Type' => "application/xml; charset=UTF-8"];
|
|
|
|
return new Response($status, $headers, $body);
|
|
|
|
}
|
2019-09-28 02:38:03 +00:00
|
|
|
}
|