public function createUnsupportedMediaTypeResponse() : HttpResponse { $response = new HttpResponse(Http::UNSUPPORTED_MEDIA_TYPE); $accepted = []; foreach ($this->matches as $match) { foreach ($match->handler->getConsumedMediaTypes() as $type) { $accepted[(string) $type] = true; } } if ($accepted) { \ksort($accepted); $response = $response->withHeader('Content-Type', 'application/json;charset="utf-8"'); $response = $response->withBody(new StringBody(\json_encode(['status' => Http::UNSUPPORTED_MEDIA_TYPE, 'reason' => Http::getReason(Http::UNSUPPORTED_MEDIA_TYPE), 'acceptable' => \array_keys($accepted)], \JSON_UNESCAPED_SLASHES | \JSON_HEX_AMP | \JSON_HEX_APOS | \JSON_HEX_QUOT | \JSON_HEX_TAG))); } return $response; }
/** * Serialize HTTP headers to be sent in an FCGI record. * * @param HttpResponse $response * @param int $size * @return string */ protected function serializeHeaders(HttpResponse $response, int $size = null) : string { $reason = \trim($response->getReasonPhrase()); if ('' === $reason) { $reason = Http::getReason($response->getStatusCode()); } $buffer = \sprintf("Status: %03u%s\r\n", $response->getStatusCode(), \rtrim(' ' . $reason)); if ($size !== null) { $buffer .= "Content-Length: {$size}\r\n"; } foreach ($response->getHeaders() as $name => $header) { $name = Http::normalizeHeaderName($name); foreach ($header as $value) { $buffer .= $name . ': ' . $value . "\r\n"; } } return $buffer; }
/** * Serialize HTTP response headers into a string. */ protected function serializeHeaders(HttpResponse $response, bool &$close, int $size = null, bool $nobody = false, bool $deferred = false) : string { $reason = \trim($response->getReasonPhrase()); if ($reason === '') { $reason = \trim(Http::getReason($response->getStatusCode())); } if (!$response->hasHeader('Connection')) { $response = $response->withHeader('Connection', $close ? 'close' : 'keep-alive'); } if (!$close) { $response = $response->withHeader('Keep-Alive', '30'); } $buffer = \sprintf("HTTP/%s %u%s\r\n", $response->getProtocolVersion(), $response->getStatusCode(), \rtrim(' ' . $reason)); if (!$nobody) { if ($deferred) { $close = true; } else { if ((double) $response->getProtocolVersion() > 1) { if ($size === null) { $buffer .= "Transfer-Encoding: chunked\r\n"; } else { $buffer .= "Content-Length: {$size}\r\n"; } } elseif ($size !== null) { $buffer .= "Content-Length: {$size}\r\n"; } else { $close = true; } } } foreach ($response->getHeaders() as $name => $header) { $name = Http::normalizeHeaderName($name); foreach ($header as $value) { $buffer .= $name . ': ' . $value . "\r\n"; } } return $buffer; }