Chris@0: sendHeaders(); Chris@0: echo $this->getContent(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Send all headers Chris@0: * Chris@0: * Sends any headers specified. If an {@link setHttpResponseCode() HTTP response code} Chris@0: * has been specified, it is sent with the first header. Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function sendHeaders() Chris@0: { Chris@17: if ($this->headers || (200 != $this->statusCode)) { Chris@0: $this->canSendHeaders(true); Chris@0: } elseif (200 == $this->statusCode) { Chris@0: return; Chris@0: } Chris@0: $httpCodeSent = false; Chris@0: foreach ($this->headers as $header) { Chris@12: if (! $httpCodeSent && $this->statusCode) { Chris@0: header($header['name'] . ': ' . $header['value'], $header['replace'], $this->statusCode); Chris@0: $httpCodeSent = true; Chris@0: } else { Chris@0: header($header['name'] . ': ' . $header['value'], $header['replace']); Chris@0: } Chris@0: } Chris@12: if (! $httpCodeSent) { Chris@0: header('HTTP/1.1 ' . $this->statusCode); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set a header Chris@0: * Chris@0: * If $replace is true, replaces any headers already defined with that Chris@0: * $name. Chris@0: * Chris@0: * @param string $name Chris@0: * @param string $value Chris@0: * @param bool $replace Chris@0: * @return \Zend\Feed\PubSubHubbub\HttpResponse Chris@0: */ Chris@0: public function setHeader($name, $value, $replace = false) Chris@0: { Chris@0: $name = $this->_normalizeHeader($name); Chris@0: $value = (string) $value; Chris@0: if ($replace) { Chris@0: foreach ($this->headers as $key => $header) { Chris@0: if ($name == $header['name']) { Chris@0: unset($this->headers[$key]); Chris@0: } Chris@0: } Chris@0: } Chris@0: $this->headers[] = [ Chris@0: 'name' => $name, Chris@0: 'value' => $value, Chris@0: 'replace' => $replace, Chris@0: ]; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check if a specific Header is set and return its value Chris@0: * Chris@0: * @param string $name Chris@0: * @return string|null Chris@0: */ Chris@0: public function getHeader($name) Chris@0: { Chris@0: $name = $this->_normalizeHeader($name); Chris@0: foreach ($this->headers as $header) { Chris@0: if ($header['name'] == $name) { Chris@0: return $header['value']; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return array of headers; see {@link $headers} for format Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getHeaders() Chris@0: { Chris@0: return $this->headers; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Can we send headers? Chris@0: * Chris@0: * @param bool $throw Whether or not to throw an exception if headers have been sent; defaults to false Chris@17: * @return bool Chris@0: * @throws Exception\RuntimeException Chris@0: */ Chris@0: public function canSendHeaders($throw = false) Chris@0: { Chris@0: $ok = headers_sent($file, $line); Chris@0: if ($ok && $throw) { Chris@12: throw new Exception\RuntimeException( Chris@12: 'Cannot send headers; headers already sent in ' . $file . ', line ' . $line Chris@12: ); Chris@0: } Chris@12: return ! $ok; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set HTTP response code to use with headers Chris@0: * Chris@0: * @param int $code Chris@0: * @return HttpResponse Chris@0: * @throws Exception\InvalidArgumentException Chris@0: */ Chris@0: public function setStatusCode($code) Chris@0: { Chris@12: if (! is_int($code) || (100 > $code) || (599 < $code)) { Chris@0: throw new Exception\InvalidArgumentException('Invalid HTTP response' Chris@0: . ' code:' . $code); Chris@0: } Chris@0: $this->statusCode = $code; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieve HTTP response code Chris@0: * Chris@0: * @return int Chris@0: */ Chris@0: public function getStatusCode() Chris@0: { Chris@0: return $this->statusCode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set body content Chris@0: * Chris@0: * @param string $content Chris@0: * @return \Zend\Feed\PubSubHubbub\HttpResponse Chris@0: */ Chris@0: public function setContent($content) Chris@0: { Chris@0: $this->content = (string) $content; Chris@0: $this->setHeader('content-length', strlen($content)); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return the body content Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getContent() Chris@0: { Chris@0: return $this->content; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Normalizes a header name to X-Capitalized-Names Chris@0: * Chris@0: * @param string $name Chris@0: * @return string Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _normalizeHeader($name) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@0: $filtered = str_replace(['-', '_'], ' ', (string) $name); Chris@0: $filtered = ucwords(strtolower($filtered)); Chris@0: $filtered = str_replace(' ', '-', $filtered); Chris@0: return $filtered; Chris@0: } Chris@0: }