public function render() { if (!$this->_header->offsetExists('content-type')) { $this->_header['content-type'] = JsonApiApplication::$content_type . '; charset=' . JsonApiApplication::$charset; } $this->headers('content-length', (string) $this->content_length()); if (JsonApiApplication::$expose) { $this->headers('user-agent', JsonApiApplication::version()); } if ($this->_cookies) { if (extension_loaded('http')) { $cookies = version_compare(phpversion('http'), '2.0.0', '>=') ? (string) new \http\Cookie($this->_cookies) : http_build_cookie($this->_cookies); $this->_header['set-cookie'] = $cookies; } else { $cookies = array(); foreach ($this->_cookies as $key => $value) { $string = $key . '=' . $value['value'] . '; expires=' . date('l, d M Y H:i:s T', $value['expiration']); $cookies[] = $string; } $this->_header['set-cookie'] = $cookies; } } $output = $this->_protocol . ' ' . $this->_status . ' ' . Response::$messages[$this->_status] . "\r\n"; $output .= (string) $this->_header; $output .= $this->_body; return $output; }
/** * Renders the HTTP_Interaction to a string, producing * * - Protocol * - Headers * - Body * * If there are variables set to the `JsonApiApplication_Request::$_post` * they will override any values set to body. * * @return string */ public function render() { if (!($post = $this->post())) { $body = $this->body(); } else { $this->headers('content-type', 'application/x-www-form-urlencoded; charset=' . JsonApiApplication::$charset); $body = http_build_query($post, NULL, '&'); } // Set the content length $this->headers('content-length', (string) $this->content_length()); // If JsonApiApplication expose, set the user-agent if (JsonApiApplication::$expose) { $this->headers('user-agent', JsonApiApplication::version()); } // Prepare cookies if ($this->_cookies) { $cookie_string = array(); // Parse each foreach ($this->_cookies as $key => $value) { $cookie_string[] = $key . '=' . $value; } // Create the cookie string $this->_header['cookie'] = implode('; ', $cookie_string); } $output = $this->method() . ' ' . $this->uri() . ' ' . $this->protocol() . "\r\n"; $output .= (string) $this->_header; $output .= $body; return $output; }
/** * Sends headers to the php processor, or supplied `$callback` argument. * This method formats the headers correctly for output, re-instating their * capitalization for transmission. * * [!!] if you supply a custom header handler via `$callback`, it is * recommended that `$response` is returned * * @param HTTP_Response $response header to send * @param boolean $replace replace existing value * @param callback $callback optional callback to replace PHP header function * @return mixed * @since 3.2.0 */ public function send_headers(HTTP_Response $response = NULL, $replace = FALSE, $callback = NULL) { $protocol = $response->protocol(); $status = $response->status(); // Create the response header $processed_headers = array($protocol . ' ' . $status . ' ' . Response::$messages[$status]); // Get the headers array $headers = $response->headers()->getArrayCopy(); foreach ($headers as $header => $value) { if (is_array($value)) { $value = implode(', ', $value); } $processed_headers[] = Text::ucfirst($header) . ': ' . $value; } if (!isset($headers['content-type'])) { $processed_headers[] = 'Content-Type: ' . JsonApiApplication::$content_type . '; charset=' . JsonApiApplication::$charset; } if (JsonApiApplication::$expose and !isset($headers['x-powered-by'])) { $processed_headers[] = 'X-Powered-By: ' . JsonApiApplication::version(); } // Get the cookies and apply if ($cookies = $response->cookie()) { $processed_headers['Set-Cookie'] = $cookies; } if (is_callable($callback)) { // Use the callback method to set header return call_user_func($callback, $response, $processed_headers, $replace); } else { $this->_send_headers_to_php($processed_headers, $replace); return $response; } }