/** * Send a HTTP request to a URI. * * The body and headers are part of the arguments. The 'body' argument is for the body and will * accept either a string or an array. The 'headers' argument should be an array, but a string * is acceptable. If the 'body' argument is an array, then it will automatically be escaped * using http_build_query(). * * The only URI that are supported in the HTTP Transport implementation are the HTTP and HTTPS * protocols. HTTP and HTTPS are assumed so the server might not know how to handle the send * headers. Other protocols are unsupported and most likely will fail. * * The defaults are 'method', 'timeout', 'redirection', 'httpversion', 'blocking' and * 'user-agent'. * * Accepted 'method' values are 'GET', 'POST', and 'HEAD', some transports technically allow * others, but should not be assumed. The 'timeout' is used to sent how long the connection * should stay open before failing when no response. 'redirection' is used to track how many * redirects were taken and used to sent the amount for other transports, but not all transports * accept setting that value. * * The 'httpversion' option is used to sent the HTTP version and accepted values are '1.0', and * '1.1' and should be a string. Version 1.1 is not supported, because of chunk response. The * 'user-agent' option is the user-agent and is used to replace the default user-agent, which is * 'NXTClass/nxt_Version', where nxt_Version is the value from $nxt_version. * * 'blocking' is the default, which is used to tell the transport, whether it should halt PHP * while it performs the request or continue regardless. Actually, that isn't entirely correct. * Blocking mode really just means whether the fread should just pull what it can whenever it * gets bytes or if it should wait until it has enough in the buffer to read or finishes reading * the entire content. It doesn't actually always mean that PHP will continue going after making * the request. * * @access public * @since 2.7.0 * * @param string $url URI resource. * @param str|array $args Optional. Override the defaults. * @return array containing 'headers', 'body', 'response', 'cookies' */ function request($url, $args = array()) { $defaults = array('method' => 'GET', 'timeout' => apply_filters('http_request_timeout', 5), 'redirection' => apply_filters('http_request_redirection_count', 5), 'httpversion' => apply_filters('http_request_version', '1.0'), 'user-agent' => apply_filters('http_headers_useragent', backpress_get_option('nxt_http_version')), 'blocking' => true, 'headers' => array(), 'cookies' => array(), 'body' => null, 'compress' => false, 'decompress' => true, 'sslverify' => true); $r = nxt_parse_args($args, $defaults); $r = apply_filters('http_request_args', $r, $url); $arrURL = parse_url($url); if ($this->block_request($url)) { return new nxt_Error('http_request_failed', 'User has blocked requests through HTTP.'); } // Determine if this is a https call and pass that on to the transport functions // so that we can blacklist the transports that do not support ssl verification $r['ssl'] = $arrURL['scheme'] == 'https' || $arrURL['scheme'] == 'ssl'; // Determine if this request is to OUR install of NXTClass $homeURL = parse_url(backpress_get_option('application_uri')); $r['local'] = $homeURL['host'] == $arrURL['host'] || 'localhost' == $arrURL['host']; unset($homeURL); if (is_null($r['headers'])) { $r['headers'] = array(); } if (!is_array($r['headers'])) { $processedHeaders = nxt_Http::processHeaders($r['headers']); $r['headers'] = $processedHeaders['headers']; } if (isset($r['headers']['User-Agent'])) { $r['user-agent'] = $r['headers']['User-Agent']; unset($r['headers']['User-Agent']); } if (isset($r['headers']['user-agent'])) { $r['user-agent'] = $r['headers']['user-agent']; unset($r['headers']['user-agent']); } // Construct Cookie: header if any cookies are set nxt_Http::buildCookieHeader($r); if (nxt_Http_Encoding::is_available()) { $r['headers']['Accept-Encoding'] = nxt_Http_Encoding::accept_encoding(); } if (is_null($r['body'])) { // Some servers fail when sending content without the content-length // header being set. $r['headers']['Content-Length'] = 0; $transports = nxt_Http::_getTransport($r); } else { if (is_array($r['body']) || is_object($r['body'])) { if (!version_compare(phpversion(), '5.1.2', '>=')) { $r['body'] = _http_build_query($r['body'], null, '&'); } else { $r['body'] = http_build_query($r['body'], null, '&'); } $r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . backpress_get_option('charset'); $r['headers']['Content-Length'] = strlen($r['body']); } if (!isset($r['headers']['Content-Length']) && !isset($r['headers']['content-length'])) { $r['headers']['Content-Length'] = strlen($r['body']); } $transports = nxt_Http::_postTransport($r); } if (has_action('http_api_debug')) { do_action('http_api_debug', $transports, 'transports_list'); } $response = array('headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array()); foreach ((array) $transports as $transport) { $response = $transport->request($url, $r); if (has_action('http_api_debug')) { do_action('http_api_debug', $response, 'response', get_class($transport)); } if (!is_nxt_error($response)) { return $response; } } return $response; }
/** * Send a HTTP request to a URI. * * The body and headers are part of the arguments. The 'body' argument is for the body and will * accept either a string or an array. The 'headers' argument should be an array, but a string * is acceptable. If the 'body' argument is an array, then it will automatically be escaped * using http_build_query(). * * The only URI that are supported in the HTTP Transport implementation are the HTTP and HTTPS * protocols. HTTP and HTTPS are assumed so the server might not know how to handle the send * headers. Other protocols are unsupported and most likely will fail. * * The defaults are 'method', 'timeout', 'redirection', 'httpversion', 'blocking' and * 'user-agent'. * * Accepted 'method' values are 'GET', 'POST', and 'HEAD', some transports technically allow * others, but should not be assumed. The 'timeout' is used to sent how long the connection * should stay open before failing when no response. 'redirection' is used to track how many * redirects were taken and used to sent the amount for other transports, but not all transports * accept setting that value. * * The 'httpversion' option is used to sent the HTTP version and accepted values are '1.0', and * '1.1' and should be a string. Version 1.1 is not supported, because of chunk response. The * 'user-agent' option is the user-agent and is used to replace the default user-agent, which is * 'NXTClass/nxt_Version', where nxt_Version is the value from $nxt_version. * * 'blocking' is the default, which is used to tell the transport, whether it should halt PHP * while it performs the request or continue regardless. Actually, that isn't entirely correct. * Blocking mode really just means whether the fread should just pull what it can whenever it * gets bytes or if it should wait until it has enough in the buffer to read or finishes reading * the entire content. It doesn't actually always mean that PHP will continue going after making * the request. * * @access public * @since 2.7.0 * @todo Refactor this code. The code in this method extends the scope of its original purpose * and should be refactored to allow for cleaner abstraction and reduce duplication of the * code. One suggestion is to create a class specifically for the arguments, however * preliminary refactoring to this affect has affect more than just the scope of the * arguments. Something to ponder at least. * * @param string $url URI resource. * @param str|array $args Optional. Override the defaults. * @return array|object Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A nxt_Error instance upon error */ function request($url, $args = array()) { global $nxt_version; $defaults = array('method' => 'GET', 'timeout' => apply_filters('http_request_timeout', 5), 'redirection' => apply_filters('http_request_redirection_count', 5), 'httpversion' => apply_filters('http_request_version', '1.0'), 'user-agent' => apply_filters('http_headers_useragent', 'NXTClass/' . $nxt_version . '; ' . get_bloginfo('url')), 'blocking' => true, 'headers' => array(), 'cookies' => array(), 'body' => null, 'compress' => false, 'decompress' => true, 'sslverify' => true, 'stream' => false, 'filename' => null); // Pre-parse for the HEAD checks. $args = nxt_parse_args($args); // By default, Head requests do not cause redirections. if (isset($args['method']) && 'HEAD' == $args['method']) { $defaults['redirection'] = 0; } $r = nxt_parse_args($args, $defaults); $r = apply_filters('http_request_args', $r, $url); // Certain classes decrement this, store a copy of the original value for loop purposes. $r['_redirection'] = $r['redirection']; // Allow plugins to short-circuit the request $pre = apply_filters('pre_http_request', false, $r, $url); if (false !== $pre) { return $pre; } $arrURL = parse_url($url); if (empty($url) || empty($arrURL['scheme'])) { return new nxt_Error('http_request_failed', __('A valid URL was not provided.')); } if ($this->block_request($url)) { return new nxt_Error('http_request_failed', __('User has blocked requests through HTTP.')); } // Determine if this is a https call and pass that on to the transport functions // so that we can blacklist the transports that do not support ssl verification $r['ssl'] = $arrURL['scheme'] == 'https' || $arrURL['scheme'] == 'ssl'; // Determine if this request is to OUR install of NXTClass $homeURL = parse_url(get_bloginfo('url')); $r['local'] = $homeURL['host'] == $arrURL['host'] || 'localhost' == $arrURL['host']; unset($homeURL); // If we are streaming to a file but no filename was given drop it in the nxt temp dir // and pick it's name using the basename of the $url if ($r['stream'] && empty($r['filename'])) { $r['filename'] = get_temp_dir() . basename($url); } // Force some settings if we are streaming to a file and check for existence and perms of destination directory if ($r['stream']) { $r['blocking'] = true; if (!is_writable(dirname($r['filename']))) { return new nxt_Error('http_request_failed', __('Destination directory for file streaming does not exist or is not writable.')); } } if (is_null($r['headers'])) { $r['headers'] = array(); } if (!is_array($r['headers'])) { $processedHeaders = nxt_Http::processHeaders($r['headers']); $r['headers'] = $processedHeaders['headers']; } if (isset($r['headers']['User-Agent'])) { $r['user-agent'] = $r['headers']['User-Agent']; unset($r['headers']['User-Agent']); } if (isset($r['headers']['user-agent'])) { $r['user-agent'] = $r['headers']['user-agent']; unset($r['headers']['user-agent']); } // Construct Cookie: header if any cookies are set nxt_Http::buildCookieHeader($r); if (nxt_Http_Encoding::is_available()) { $r['headers']['Accept-Encoding'] = nxt_Http_Encoding::accept_encoding(); } if (empty($r['body'])) { $r['body'] = null; // Some servers fail when sending content without the content-length header being set. // Also, to fix another bug, we only send when doing POST and PUT and the content-length // header isn't already set. if (($r['method'] == 'POST' || $r['method'] == 'PUT') && !isset($r['headers']['Content-Length'])) { $r['headers']['Content-Length'] = 0; } } else { if (is_array($r['body']) || is_object($r['body'])) { $r['body'] = http_build_query($r['body'], null, '&'); if (!isset($r['headers']['Content-Type'])) { $r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset'); } $r['headers']['Content-Length'] = strlen($r['body']); } if (!isset($r['headers']['Content-Length']) && !isset($r['headers']['content-length'])) { $r['headers']['Content-Length'] = strlen($r['body']); } } return $this->_dispatch_request($url, $r); }