1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 06:44:57 +00:00
Arsse/lib/Misc/HTTP.php

49 lines
1.8 KiB
PHP
Raw Permalink Normal View History

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;
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) {
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;
}
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
}