/** * Gets the http status code from $domain, using $port. * @param string $domain * @param int $port * @return mixed */ function get_response($domain, $port) { global $setting; if (test_domain($domain)) { // retrieves just the http header response /// old code: $headers = @get_headers("http://" . $domain . ":" . $port); $start = microtime(true); $r = @http_head("http://" . $domain . ":" . $port, array('timeout' => $setting["timeout"]), $headers); $end = microtime(true); $code = array("code" => $headers["response_code"], "time" => $end - $start); if ($headers["response_code"] == 0) { return false; } } else { $code = array("code" => "invalid", "time" => 0); } return $code; }
function import_fetch_uri($uri, $more = array()) { # QUESTION: do a HEAD here to check the content-type and file-size ? $max_filesize = ini_get("upload_max_filesize"); # http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes if (preg_match("/^(\\d+)(K|M|G)\$/", $max_filesize, $m)) { $unit = $m[1]; $measure = $m[2]; if ($measure == 'G') { $max_bytes = $unit * 1024 * 1024 * 1024; } else { if ($measure == 'M') { $max_bytes = $unit * 1024 * 1024; } else { $max_bytes = $unit * 1024; } } } else { $max_bytes = $max_filesize; } # This is mostly just to try... # # "If no Accept header field is present, then it is assumed that the client accepts all media types. # If an Accept header field is present, and if the server cannot send a response which is acceptable # according to the combined Accept field value, then the server SHOULD send a 406 (not acceptable) # response." (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) $map = formats_valid_import_map(); $accept = array_keys($map); if (!isset($map['text/plain'])) { $accept[] = 'text/plain'; } $headers = array('Range' => "bytes=0-{$max_bytes}", 'Accept' => implode(", ", $accept)); # # Try to do some basic anity checking before we suck in the entire file # if ($GLOBALS['cfg']['import_by_url_do_head']) { $head_rsp = http_head($uri, $headers); if (!$head_rsp['ok']) { return $head_rsp; } if ($head_rsp['info']['download_content_length'] > $max_bytes) { return array('ok' => 0, 'error' => 'Remote file is too large'); } } # # Go! # $http_rsp = http_get($uri, $headers); if (!$http_rsp['ok']) { return $http_rsp; } # # Am I partial content? # if ($http_rsp['headers']['content-length'] > $max_bytes) { # throw an error ? } # # Write the file to disk # $fname = tempnam("/tmp", getmypid()); $fh = fopen($fname, "w"); if (!$fh) { return array('ok' => 0, 'error' => 'failed to open tmp filehandle'); } if (!trim($http_rsp['body'])) { return array('ok' => 0, 'error' => 'remote service failed to return any data!'); } fwrite($fh, $http_rsp['body']); fclose($fh); if (!filesize($fname)) { return array('ok' => 0, 'error' => 'failed to write any data!'); } # # Ima Viking! # $type = $http_rsp['headers']['content-type']; if ($more['assume_mime_type']) { $type = $more['assume_mime_type']; } $type_map = formats_valid_import_map(); if (!isset($type_map[$type])) { if (preg_match("/\\.([a-z0-9]+)\$/", basename($uri), $m)) { $ext = $m[1]; $ext_map = formats_valid_import_map('key by extension'); if (isset($ext_map[$ext])) { $type = $ext_map[$ext]; } } else { } } return array('ok' => 1, 'type' => $type, 'path' => $fname); }
function request($uri, $method="GET", $headers=null, $body='') { if (!isset($headers)) $headers = array(); if ($method == "POST") { if (!isset($header['Content-Type'])) $headers['Content-Type'] = self::DEFAULT_POST_CONTENT_TYPE; } $is_form_encoded = (isset($headers) and $headers['Content-Type'] == self::DEFAULT_POST_CONTENT_TYPE); $parameters = null; if ($is_form_encoded and isset($body)) parse_str($body,$parameters); $req = Request::from_consumer_and_token($this->consumer, $this->token, $method, $uri, $parameters, $body, $is_form_encoded); $req->sign_request($this->method, $this->consumer, $this->token); $headers = array_merge($headers, $req->to_header()); $parsed = parse_url($uri); $realm = http_build_url( array( "scheme" => $parsed['scheme'], "host" => $parsed['host'], ) ); if ($is_form_encoded) { return http_parse_message( http_post_fields($uri, $parameters, null, array(headers => $headers)) )->body; } elseif ($method == "GET") { $uri = $req->to_url(); return http_get($uri, array(headers => $headers)); } else { $headers = $req->to_header($realm); return http_head($uri, array(headers => $headers)); } return http_request($method, $uri, $body, array(headers => $headers)); }
function http_multi(&$requests) { $handles = array(); $responses = array(); foreach ($requests as $req) { $url = $req['url']; $method = isset($req['method']) ? strtoupper($req['method']) : 'GET'; $body = is_array($req['body']) ? $req['body'] : null; $headers = is_array($req['headers']) ? $req['headers'] : array(); $more = is_array($req['more']) ? $req['more'] : array(); $more['return_curl_handle'] = 1; if ($method == 'HEAD') { $ch = http_head($url, $headers, $more); } else { if ($method == 'GET') { $ch = http_get($url, $headers, $more); } else { if ($method == 'POST') { $ch = http_post($url, $body, $headers, $more); } else { if ($method == 'DELETE') { $ch = http_delete($url, $body, $headers, $more); } else { if ($method == 'PUT') { $ch = http_put($url, $body, $headers, $more); } else { log_warning("http", "unsupported HTTP method : {$method}"); continue; } } } } } $handles[] = $ch; } # http://us.php.net/manual/en/function.curl-multi-init.php $mh = curl_multi_init(); foreach ($handles as $ch) { curl_multi_add_handle($mh, $ch); } $active = null; $start = microtime_ms(); # this syntax makes my eyes bleed but whatever... # (20110822/straup) do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active && $mrc == CURLM_OK) { if (curl_multi_select($mh) != -1) { do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } } $end = microtime_ms(); $GLOBALS['timings']['http_count'] += count($handlers); $GLOBALS['timings']['http_time'] += $end - $start; foreach ($handles as $ch) { $raw = curl_multi_getcontent($ch); $info = curl_getinfo($ch); curl_multi_remove_handle($mh, $ch); $rsp = _http_parse_response($raw, $info); $responses[] = $rsp; } curl_multi_close($mh); return $responses; }
function s3_head($bucket, $object_id, $args = array()) { $defaults = array('expires' => time() + 300, 'method' => 'HEAD'); $args = array_merge($defaults, $args); $url = s3_signed_object_url($bucket, $object_id, $args); $rsp = http_head($url); return $rsp; }
{ $r = new request($url); $r->pool = $pool; $pool->attach($r); return $r; } function onFinish() { ++self::$counter; $this->pool->detach($this); $this->pool->push(); } } function usage() { global $argv; fprintf(STDERR, "Usage: %s -u <URL> -n <requests> -c <concurrency> -e (use libevent)\n", $argv[0]); fprintf(STDERR, "\nDefaults: -u http://localhost/ -n 1000 -c 10\n\n"); exit(-1); } isset($argv) or $argv = $_SERVER['argv']; defined('STDERR') or define('STDERR', fopen('php://stderr', 'w')); $opts = getopt("u:c:n:e"); isset($opts["u"]) or $opts["u"] = "http://localhost/"; isset($opts["c"]) or $opts["c"] = 10; isset($opts["n"]) or $opts["n"] = 1000; http_parse_message(http_head($opts["u"]))->responseCode == 200 or usage(); $time = microtime(true); pool::fetch($opts["u"], $opts["n"], $opts["c"], isset($opts["e"])); printf("\n> %10.6fs\n", microtime(true) - $time); request::$counter == $opts["n"] or printf("\nOnly %d finished\n", request::$counter);
/** * Test Http functions. */ function test_functions() { http_cache_last_modified(); http_chunked_decode(); http_deflate(); http_inflate(); http_build_cookie(); http_date(); http_get_request_body_stream(); http_get_request_body(); http_get_request_headers(); http_match_etag(); http_match_modified(); http_match_request_header(); http_support(); http_negotiate_charset(); http_negotiate_content_type(); http_negotiate_language(); ob_deflatehandler(); ob_etaghandler(); ob_inflatehandler(); http_parse_cookie(); http_parse_headers(); http_parse_message(); http_parse_params(); http_persistent_handles_clean(); http_persistent_handles_count(); http_persistent_handles_ident(); http_get(); http_head(); http_post_data(); http_post_fields(); http_put_data(); http_put_file(); http_put_stream(); http_request_body_encode(); http_request_method_exists(); http_request_method_name(); http_request_method_register(); http_request_method_unregister(); http_request(); http_redirect(); http_send_content_disposition(); http_send_content_type(); http_send_data(); http_send_file(); http_send_last_modified(); http_send_status(); http_send_stream(); http_throttle(); http_build_str(); http_build_url(); }
/** * Execute request * * @param Solarium_Client_Request $request * @return array */ protected function _getData($request) { // @codeCoverageIgnoreStart $uri = $this->getBaseUri() . $request->getUri(); $method = $request->getMethod(); $options = $this->_createOptions($request); if ($method == Solarium_Client_Request::METHOD_POST) { if (!isset($options['headers']['Content-Type'])) { $options['headers']['Content-Type'] = 'text/xml; charset=utf-8'; } $httpResponse = http_post_data($uri, $request->getRawData(), $options); } else { if ($method == Solarium_Client_Request::METHOD_GET) { $httpResponse = http_get($uri, $options); } else { if ($method == Solarium_Client_Request::METHOD_HEAD) { $httpResponse = http_head($uri, $options); } else { throw new Solarium_Exception("unsupported method: {$method}"); } } } $headers = array(); $data = ''; if ($message = http_parse_message($httpResponse)) { $data = $message->body; if ($firstPositionOfCRLF = strpos($httpResponse, "\r\n\r\n")) { $headersAsString = substr($httpResponse, 0, $firstPositionOfCRLF); $headers = explode("\n", $headersAsString); } } return array($data, $headers); // @codeCoverageIgnoreEnd }