function urlfetch_curl($url, $payload, $method, $headers, $follow_redirects, $deadline, $validate_certificate) { $this->headers = array(); $this->body = ''; $this->body_size = 0; if ($payload) { $headers['content-length'] = strval(strlen($payload)); } $headers['connection'] = 'close'; $curl_opt = array(); $curl_opt[CURLOPT_TIMEOUT] = $deadline; $curl_opt[CURLOPT_CONNECTTIMEOUT] = $deadline; $curl_opt[CURLOPT_RETURNTRANSFER] = true; $curl_opt[CURLOPT_BINARYTRANSFER] = true; $curl_opt[CURLOPT_FAILONERROR] = true; if (!$follow_redirects) { $curl_opt[CURLOPT_FOLLOWLOCATION] = false; } if ($deadline) { $curl_opt[CURLOPT_CONNECTTIMEOUT] = $deadline; $curl_opt[CURLOPT_TIMEOUT] = $deadline; } if (!$validate_certificate) { $curl_opt[CURLOPT_SSL_VERIFYPEER] = false; $curl_opt[CURLOPT_SSL_VERIFYHOST] = false; } switch (strtoupper($method)) { case 'HEAD': $curl_opt[CURLOPT_NOBODY] = true; break; case 'GET': break; case 'POST': $curl_opt[CURLOPT_POST] = true; $curl_opt[CURLOPT_POSTFIELDS] = $payload; break; case 'PUT': case 'DELETE': $curl_opt[CURLOPT_CUSTOMREQUEST] = $method; $curl_opt[CURLOPT_POSTFIELDS] = $payload; break; default: print_notify($method, $url, 501, 'Invalid Method'); exit(-1); } $header_array = array(); foreach ($headers as $key => $value) { if ($key) { $header_array[] = join('-', array_map('ucfirst', explode('-', $key))) . ': ' . $value; } } $curl_opt[CURLOPT_HTTPHEADER] = $header_array; $curl_opt[CURLOPT_HEADER] = false; $curl_opt[CURLOPT_HEADERFUNCTION] = array(&$this, 'urlfetch_curl_readheader'); $curl_opt[CURLOPT_WRITEFUNCTION] = array(&$this, 'urlfetch_curl_readbody'); //error_exit('curl_opt:', $curl_opt); $ch = curl_init($url); curl_setopt_array($ch, $curl_opt); $ret = curl_exec($ch); $this->headers['connection'] = 'close'; $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $errno = curl_errno($ch); if ($errno) { $error = $errno . ': ' . curl_error($ch); } curl_close($ch); $content_length = 1 * $this->headers["content-length"]; if ($status_code == 200 && $this->body_size > $this->body_maxsize && $content_length && $this->body_size < $content_length) { $status_code = 206; $range_start = 0; $range_end = $this->body_size - 1; $this->headers["content-range"] = "bytes {$range_start}-{$range_end}/{$content_length}"; $this->headers["content-length"] = $this->body_size; } //error_exit('urlfetch result:', array('status_code' => $status_code, 'headers' => $this->headers, 'content-size' => $this->body_size, 'error' => $error)); $response = array('status_code' => $status_code, 'headers' => $this->headers, 'content' => $this->body, 'error' => $error); return $response; }
function get() { global $__version__; if (@gzcompress('test') == false) { print_notify('GET', $_SERVER['SCRIPT_FILENAME'], 500, 'need zlib moudle!'); exit(-1); } if (@curl_init('http://www.google.com') == false) { print_notify('GET', $_SERVER['SCRIPT_FILENAME'], 500, 'need curl moudle!'); exit(-1); } echo <<<EOF <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>GoAgent {$__version__} is working now</title> </head> <body> <table width="800" border="0" align="center"> <tr><td align="center"><hr></td></tr> <tr><td align="center"> <b><h1>GoAgent {$__version__} is working now</h1></b> </td></tr> <tr><td align="center"><hr></td></tr> <tr><td align="center"> GoAgent is HTTP Porxy written by python and hosting in GAE/PHP. </td></tr> <tr><td align="center"><hr></td></tr> <tr><td align="center"> For more detail,please refer to <a href="http://code.google.com/p/goagent/">GoAgent Project Homepage</a>. </td></tr> <tr><td align="center"><hr></td></tr> </table> </body> EOF; }