protected function add_cors_headers(HTTP_Response &$response) { $response->headers('Access-Control-Allow-Origin', '*'); $response->headers('Access-Control-Allow-Headers', 'Authorization, Content-type'); if (isset($this->_action_map)) { $allow_methods = implode(', ', array_keys($this->_action_map)); $this->response->headers('Allow', $allow_methods); $this->response->headers('Access-Control-Allow-Methods', $allow_methods); } return $response; }
/** * @param Object HTTP_Response object * @return bool */ protected function _isOk(HTTP_Response $response) { if ($response->failed()) { return false; } else { if (preg_match('@<Error>.*?<Code>(.*?)</Code>.*?<Message>(.*?)</Message>.*?</Error>@s', $response->getBody(), $matches)) { $this->_error_code = $matches[1]; $this->_error_message = $matches[2]; return false; } else { return true; } } }
/** * 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: ' . Kohana::$content_type . '; charset=' . Kohana::$charset; } if (Kohana::$expose and !isset($headers['x-powered-by'])) { $processed_headers[] = 'X-Powered-By: ' . Kohana::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; } }
/** * Sends the request * * @access public * @param bool Whether to store response body in Response object property, * set this to false if downloading a LARGE file and using a Listener * @return mixed PEAR error on error, true otherwise */ function sendRequest($saveBody = true) { if (!is_a($this->_url, 'Net_URL')) { return PEAR::raiseError('No URL given', HTTP_REQUEST_ERROR_URL); } $host = isset($this->_proxy_host) ? $this->_proxy_host : $this->_url->host; $port = isset($this->_proxy_port) ? $this->_proxy_port : $this->_url->port; // 4.3.0 supports SSL connections using OpenSSL. The function test determines // we running on at least 4.3.0 if (strcasecmp($this->_url->protocol, 'https') == 0 AND function_exists('file_get_contents') AND extension_loaded('openssl')) { if (isset($this->_proxy_host)) { return PEAR::raiseError('HTTPS proxies are not supported', HTTP_REQUEST_ERROR_PROXY); } $host = 'ssl://' . $host; } // magic quotes may f**k up file uploads and chunked response processing $magicQuotes = ini_get('magic_quotes_runtime'); ini_set('magic_quotes_runtime', false); // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive // connection token to a proxy server... if (isset($this->_proxy_host) && !empty($this->_requestHeaders['connection']) && 'Keep-Alive' == $this->_requestHeaders['connection']) { $this->removeHeader('connection'); } $keepAlive = (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && empty($this->_requestHeaders['connection'])) || (!empty($this->_requestHeaders['connection']) && 'Keep-Alive' == $this->_requestHeaders['connection']); $sockets = &PEAR::getStaticProperty('HTTP_Request', 'sockets'); $sockKey = $host . ':' . $port; unset($this->_sock); // There is a connected socket in the "static" property? if ($keepAlive && !empty($sockets[$sockKey]) && !empty($sockets[$sockKey]->fp)) { $this->_sock =& $sockets[$sockKey]; $err = null; } else { $this->_notify('connect'); $this->_sock = new Net_Socket(); $err = $this->_sock->connect($host, $port, null, $this->_timeout, $this->_socketOptions); } PEAR::isError($err) or $err = $this->_sock->write($this->_buildRequest()); if (!PEAR::isError($err)) { if (!empty($this->_readTimeout)) { $this->_sock->setTimeout($this->_readTimeout[0], $this->_readTimeout[1]); } $this->_notify('sentRequest'); // Read the response $this->_response = new HTTP_Response($this->_sock, $this->_listeners); $err = $this->_response->process( $this->_saveBody && $saveBody, HTTP_REQUEST_METHOD_HEAD != $this->_method ); if ($keepAlive) { $keepAlive = (isset($this->_response->_headers['content-length']) || (isset($this->_response->_headers['transfer-encoding']) && strtolower($this->_response->_headers['transfer-encoding']) == 'chunked')); if ($keepAlive) { if (isset($this->_response->_headers['connection'])) { $keepAlive = strtolower($this->_response->_headers['connection']) == 'keep-alive'; } else { $keepAlive = 'HTTP/'.HTTP_REQUEST_HTTP_VER_1_1 == $this->_response->_protocol; } } } } ini_set('magic_quotes_runtime', $magicQuotes); if (PEAR::isError($err)) { return $err; } if (!$keepAlive) { $this->disconnect(); // Store the connected socket in "static" property } elseif (empty($sockets[$sockKey]) || empty($sockets[$sockKey]->fp)) { $sockets[$sockKey] =& $this->_sock; } // Check for redirection if ( $this->_allowRedirects AND $this->_redirects <= $this->_maxRedirects AND $this->getResponseCode() > 300 AND $this->getResponseCode() < 399 AND !empty($this->_response->_headers['location'])) { $redirect = $this->_response->_headers['location']; // Absolute URL if (preg_match('/^https?:\/\//i', $redirect)) { $this->_url = new Net_URL($redirect); $this->addHeader('Host', $this->_generateHostHeader()); // Absolute path } elseif ($redirect{0} == '/') { $this->_url->path = $redirect; // Relative path } elseif (substr($redirect, 0, 3) == '../' OR substr($redirect, 0, 2) == './') { if (substr($this->_url->path, -1) == '/') { $redirect = $this->_url->path . $redirect; } else { $redirect = dirname($this->_url->path) . '/' . $redirect; } $redirect = Net_URL::resolvePath($redirect); $this->_url->path = $redirect; // Filename, no path } else { if (substr($this->_url->path, -1) == '/') { $redirect = $this->_url->path . $redirect; } else { $redirect = dirname($this->_url->path) . '/' . $redirect; } $this->_url->path = $redirect; } $this->_redirects++; return $this->sendRequest($saveBody); // Too many redirects } elseif ($this->_allowRedirects AND $this->_redirects > $this->_maxRedirects) { return PEAR::raiseError('Too many redirects', HTTP_REQUEST_ERROR_REDIRECTS); } return true; }
protected static function static_add_cors_headers(HTTP_Response &$response) { $response->headers('Access-Control-Allow-Origin', '*'); $response->headers('Access-Control-Allow-Headers', 'Authorization, Content-type'); return $response; }
/** * Sends the request * * @access public * @param bool Whether to store response body in Response object property, * set this to false if downloading a LARGE file and using a Listener * @return mixed PEAR error on error, true otherwise */ function sendRequest($saveBody = true) { if (!is_a($this->_url, 'Net_URL')) { return PEAR::raiseError('No URL given', HTTP_REQUEST_ERROR_URL); } $host = isset($this->_proxy_host) ? $this->_proxy_host : $this->_url->host; $port = isset($this->_proxy_port) ? $this->_proxy_port : $this->_url->port; if (strcasecmp($this->_url->protocol, 'https') == 0) { // Bug #14127, don't try connecting to HTTPS sites without OpenSSL if (version_compare(PHP_VERSION, '4.3.0', '<') || !extension_loaded('openssl')) { return PEAR::raiseError('Need PHP 4.3.0 or later with OpenSSL support for https:// requests', HTTP_REQUEST_ERROR_URL); } elseif (isset($this->_proxy_host)) { return PEAR::raiseError('HTTPS proxies are not supported', HTTP_REQUEST_ERROR_PROXY); } $host = 'ssl://' . $host; } // magic quotes may f**k up file uploads and chunked response processing $magicQuotes = ini_get('magic_quotes_runtime'); ini_set('magic_quotes_runtime', false); // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive // connection token to a proxy server... if (isset($this->_proxy_host) && !empty($this->_requestHeaders['connection']) && 'Keep-Alive' == $this->_requestHeaders['connection']) { $this->removeHeader('connection'); } $keepAlive = HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && empty($this->_requestHeaders['connection']) || !empty($this->_requestHeaders['connection']) && 'Keep-Alive' == $this->_requestHeaders['connection']; $sockets =& PEAR::getStaticProperty('HTTP_Request', 'sockets'); $sockKey = $host . ':' . $port; unset($this->_sock); // There is a connected socket in the "static" property? if ($keepAlive && !empty($sockets[$sockKey]) && !empty($sockets[$sockKey]->fp)) { $this->_sock =& $sockets[$sockKey]; $err = null; } else { $this->_notify('connect'); $this->_sock = new Net_Socket(); $err = $this->_sock->connect($host, $port, null, $this->_timeout, $this->_socketOptions); } PEAR::isError($err) or $err = $this->_sock->write($this->_buildRequest()); if (!PEAR::isError($err)) { if (!empty($this->_readTimeout)) { $this->_sock->setTimeout($this->_readTimeout[0], $this->_readTimeout[1]); } $this->_notify('sentRequest'); // Read the response $this->_response = new HTTP_Response($this->_sock, $this->_listeners); $err = $this->_response->process($this->_saveBody && $saveBody, HTTP_REQUEST_METHOD_HEAD != $this->_method); if ($keepAlive) { $keepAlive = isset($this->_response->_headers['content-length']) || isset($this->_response->_headers['transfer-encoding']) && strtolower($this->_response->_headers['transfer-encoding']) == 'chunked'; if ($keepAlive) { if (isset($this->_response->_headers['connection'])) { $keepAlive = strtolower($this->_response->_headers['connection']) == 'keep-alive'; } else { $keepAlive = 'HTTP/' . HTTP_REQUEST_HTTP_VER_1_1 == $this->_response->_protocol; } } } } ini_set('magic_quotes_runtime', $magicQuotes); if (PEAR::isError($err)) { return $err; } if (!$keepAlive) { $this->disconnect(); // Store the connected socket in "static" property } elseif (empty($sockets[$sockKey]) || empty($sockets[$sockKey]->fp)) { $sockets[$sockKey] =& $this->_sock; } // Check for redirection if ($this->_allowRedirects and $this->_redirects <= $this->_maxRedirects and $this->getResponseCode() > 300 and $this->getResponseCode() < 399 and !empty($this->_response->_headers['location'])) { $redirect = $this->_response->_headers['location']; // Absolute URL if (preg_match('/^https?:\\/\\//i', $redirect)) { $this->_url = new Net_URL($redirect); $this->addHeader('Host', $this->_generateHostHeader()); // Absolute path } elseif ($redirect[0] == '/') { $this->_url->path = $redirect; // Relative path } elseif (substr($redirect, 0, 3) == '../' or substr($redirect, 0, 2) == './') { if (substr($this->_url->path, -1) == '/') { $redirect = $this->_url->path . $redirect; } else { $redirect = dirname($this->_url->path) . '/' . $redirect; } $redirect = Net_URL::resolvePath($redirect); $this->_url->path = $redirect; // Filename, no path } else { if (substr($this->_url->path, -1) == '/') { $redirect = $this->_url->path . $redirect; } else { $redirect = dirname($this->_url->path) . '/' . $redirect; } $this->_url->path = $redirect; } // handle cookes on redirect... if (!empty($this->_response->_cookies)) { foreach ($this->_response->_cookies as $c) { $this->_cookies[] = $c; } } if (isset($this->_requestHeaders['cookie'])) { unset($this->_requestHeaders['cookie']); } //print_r($this->_cookies); $cookies = array(); foreach ($this->_cookies as $c) { if (substr($this->_url->host, -1 * strlen($c['domain']) == $c['domain'])) { $cookies[$c['name']] = $c['value']; } } foreach ($cookies as $k => $v) { $this->addCookie($k, $v); } $this->_redirects++; return $this->sendRequest($saveBody); // Too many redirects } elseif ($this->_allowRedirects and $this->_redirects > $this->_maxRedirects) { return PEAR::raiseError('Too many redirects', HTTP_REQUEST_ERROR_REDIRECTS); } return true; }
function checkout() { //$repo_url, $repo_sub_path, $current_rev) { $repo_url = $this->state->get_repository_root(); $repo_sub_path = $this->state->get_repository_path(); $current_rev = $this->state->get_revision(); $src_path = $repo_url . $repo_sub_path; // figure out where to send our http REPORT request $bits = parse_url($repo_url . "/!svn/vcc/default"); $repo_host = $bits['host']; $repo_port = @$bits['port']; if (!$repo_port) { $repo_port = 80; } $repo_path = $bits['path']; $this->out("Connecting to host {$repo_host}:{$repo_port},<br>sending HTTP REPORT {$repo_path} for src-path {$src_path}.\n"); $xml = '<S:update-report send-all="true" xmlns:S="svn:"><S:src-path>' . $src_path . '</S:src-path><S:entry rev="' . $current_rev . '"></S:entry></S:update-report>'; $xml_len = strlen($xml); $query = "REPORT {$repo_path} HTTP/1.0\nHost: {$repo_host}\nContent-Type: text/xml\nContent-Length: {$xml_len}\nDepth: 0\n\n{$xml}"; $this->out($query); $sock = new Net_Socket(); $this->check_err($sock->connect($repo_host, $repo_port, null, 60, null)); // 60 sec timeout $this->check_err($sock->write($query)); // read response $this->out("Downloading patch ...\n"); $this->fp = fopen($this->diff_fn, "wt"); if (!$this->fp) { throw new Subversion_Failure("can't open {$this->diff_fn} file"); } $listeners = array($this); $this->received_bytes = 0; $resp = new HTTP_Response($sock, $listeners); $this->check_err($resp->process(false)); fclose($this->fp); $this->out("\nFinished downloading update ({$this->received_bytes} bytes).\n"); }