1*5a8d6e48SMichael Große<?php 2*5a8d6e48SMichael Große 3*5a8d6e48SMichael Großenamespace dokuwiki\HTTP; 4*5a8d6e48SMichael Große 5*5a8d6e48SMichael Großedefine('HTTP_NL',"\r\n"); 6*5a8d6e48SMichael Große 7*5a8d6e48SMichael Große 8*5a8d6e48SMichael Große/** 9*5a8d6e48SMichael Große * This class implements a basic HTTP client 10*5a8d6e48SMichael Große * 11*5a8d6e48SMichael Große * It supports POST and GET, Proxy usage, basic authentication, 12*5a8d6e48SMichael Große * handles cookies and referers. It is based upon the httpclient 13*5a8d6e48SMichael Große * function from the VideoDB project. 14*5a8d6e48SMichael Große * 15*5a8d6e48SMichael Große * @link http://www.splitbrain.org/go/videodb 16*5a8d6e48SMichael Große * @author Andreas Goetz <cpuidle@gmx.de> 17*5a8d6e48SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 18*5a8d6e48SMichael Große * @author Tobias Sarnowski <sarnowski@new-thoughts.org> 19*5a8d6e48SMichael Große */ 20*5a8d6e48SMichael Großeclass HTTPClient { 21*5a8d6e48SMichael Große //set these if you like 22*5a8d6e48SMichael Große public $agent; // User agent 23*5a8d6e48SMichael Große public $http; // HTTP version defaults to 1.0 24*5a8d6e48SMichael Große public $timeout; // read timeout (seconds) 25*5a8d6e48SMichael Große public $cookies; 26*5a8d6e48SMichael Große public $referer; 27*5a8d6e48SMichael Große public $max_redirect; 28*5a8d6e48SMichael Große public $max_bodysize; 29*5a8d6e48SMichael Große public $max_bodysize_abort = true; // if set, abort if the response body is bigger than max_bodysize 30*5a8d6e48SMichael Große public $header_regexp; // if set this RE must match against the headers, else abort 31*5a8d6e48SMichael Große public $headers; 32*5a8d6e48SMichael Große public $debug; 33*5a8d6e48SMichael Große public $start = 0.0; // for timings 34*5a8d6e48SMichael Große public $keep_alive = true; // keep alive rocks 35*5a8d6e48SMichael Große 36*5a8d6e48SMichael Große // don't set these, read on error 37*5a8d6e48SMichael Große public $error; 38*5a8d6e48SMichael Große public $redirect_count; 39*5a8d6e48SMichael Große 40*5a8d6e48SMichael Große // read these after a successful request 41*5a8d6e48SMichael Große public $status; 42*5a8d6e48SMichael Große public $resp_body; 43*5a8d6e48SMichael Große public $resp_headers; 44*5a8d6e48SMichael Große 45*5a8d6e48SMichael Große // set these to do basic authentication 46*5a8d6e48SMichael Große public $user; 47*5a8d6e48SMichael Große public $pass; 48*5a8d6e48SMichael Große 49*5a8d6e48SMichael Große // set these if you need to use a proxy 50*5a8d6e48SMichael Große public $proxy_host; 51*5a8d6e48SMichael Große public $proxy_port; 52*5a8d6e48SMichael Große public $proxy_user; 53*5a8d6e48SMichael Große public $proxy_pass; 54*5a8d6e48SMichael Große public $proxy_ssl; //boolean set to true if your proxy needs SSL 55*5a8d6e48SMichael Große public $proxy_except; // regexp of URLs to exclude from proxy 56*5a8d6e48SMichael Große 57*5a8d6e48SMichael Große // list of kept alive connections 58*5a8d6e48SMichael Große protected static $connections = array(); 59*5a8d6e48SMichael Große 60*5a8d6e48SMichael Große // what we use as boundary on multipart/form-data posts 61*5a8d6e48SMichael Große protected $boundary = '---DokuWikiHTTPClient--4523452351'; 62*5a8d6e48SMichael Große 63*5a8d6e48SMichael Große /** 64*5a8d6e48SMichael Große * Constructor. 65*5a8d6e48SMichael Große * 66*5a8d6e48SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 67*5a8d6e48SMichael Große */ 68*5a8d6e48SMichael Große public function __construct(){ 69*5a8d6e48SMichael Große $this->agent = 'Mozilla/4.0 (compatible; DokuWiki HTTP Client; '.PHP_OS.')'; 70*5a8d6e48SMichael Große $this->timeout = 15; 71*5a8d6e48SMichael Große $this->cookies = array(); 72*5a8d6e48SMichael Große $this->referer = ''; 73*5a8d6e48SMichael Große $this->max_redirect = 3; 74*5a8d6e48SMichael Große $this->redirect_count = 0; 75*5a8d6e48SMichael Große $this->status = 0; 76*5a8d6e48SMichael Große $this->headers = array(); 77*5a8d6e48SMichael Große $this->http = '1.0'; 78*5a8d6e48SMichael Große $this->debug = false; 79*5a8d6e48SMichael Große $this->max_bodysize = 0; 80*5a8d6e48SMichael Große $this->header_regexp= ''; 81*5a8d6e48SMichael Große if(extension_loaded('zlib')) $this->headers['Accept-encoding'] = 'gzip'; 82*5a8d6e48SMichael Große $this->headers['Accept'] = 'text/xml,application/xml,application/xhtml+xml,'. 83*5a8d6e48SMichael Große 'text/html,text/plain,image/png,image/jpeg,image/gif,*/*'; 84*5a8d6e48SMichael Große $this->headers['Accept-Language'] = 'en-us'; 85*5a8d6e48SMichael Große } 86*5a8d6e48SMichael Große 87*5a8d6e48SMichael Große 88*5a8d6e48SMichael Große /** 89*5a8d6e48SMichael Große * Simple function to do a GET request 90*5a8d6e48SMichael Große * 91*5a8d6e48SMichael Große * Returns the wanted page or false on an error; 92*5a8d6e48SMichael Große * 93*5a8d6e48SMichael Große * @param string $url The URL to fetch 94*5a8d6e48SMichael Große * @param bool $sloppy304 Return body on 304 not modified 95*5a8d6e48SMichael Große * @return false|string response body, false on error 96*5a8d6e48SMichael Große * 97*5a8d6e48SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 98*5a8d6e48SMichael Große */ 99*5a8d6e48SMichael Große public function get($url,$sloppy304=false){ 100*5a8d6e48SMichael Große if(!$this->sendRequest($url)) return false; 101*5a8d6e48SMichael Große if($this->status == 304 && $sloppy304) return $this->resp_body; 102*5a8d6e48SMichael Große if($this->status < 200 || $this->status > 206) return false; 103*5a8d6e48SMichael Große return $this->resp_body; 104*5a8d6e48SMichael Große } 105*5a8d6e48SMichael Große 106*5a8d6e48SMichael Große /** 107*5a8d6e48SMichael Große * Simple function to do a GET request with given parameters 108*5a8d6e48SMichael Große * 109*5a8d6e48SMichael Große * Returns the wanted page or false on an error. 110*5a8d6e48SMichael Große * 111*5a8d6e48SMichael Große * This is a convenience wrapper around get(). The given parameters 112*5a8d6e48SMichael Große * will be correctly encoded and added to the given base URL. 113*5a8d6e48SMichael Große * 114*5a8d6e48SMichael Große * @param string $url The URL to fetch 115*5a8d6e48SMichael Große * @param array $data Associative array of parameters 116*5a8d6e48SMichael Große * @param bool $sloppy304 Return body on 304 not modified 117*5a8d6e48SMichael Große * @return false|string response body, false on error 118*5a8d6e48SMichael Große * 119*5a8d6e48SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 120*5a8d6e48SMichael Große */ 121*5a8d6e48SMichael Große public function dget($url,$data,$sloppy304=false){ 122*5a8d6e48SMichael Große if(strpos($url,'?')){ 123*5a8d6e48SMichael Große $url .= '&'; 124*5a8d6e48SMichael Große }else{ 125*5a8d6e48SMichael Große $url .= '?'; 126*5a8d6e48SMichael Große } 127*5a8d6e48SMichael Große $url .= $this->postEncode($data); 128*5a8d6e48SMichael Große return $this->get($url,$sloppy304); 129*5a8d6e48SMichael Große } 130*5a8d6e48SMichael Große 131*5a8d6e48SMichael Große /** 132*5a8d6e48SMichael Große * Simple function to do a POST request 133*5a8d6e48SMichael Große * 134*5a8d6e48SMichael Große * Returns the resulting page or false on an error; 135*5a8d6e48SMichael Große * 136*5a8d6e48SMichael Große * @param string $url The URL to fetch 137*5a8d6e48SMichael Große * @param array $data Associative array of parameters 138*5a8d6e48SMichael Große * @return false|string response body, false on error 139*5a8d6e48SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 140*5a8d6e48SMichael Große */ 141*5a8d6e48SMichael Große public function post($url,$data){ 142*5a8d6e48SMichael Große if(!$this->sendRequest($url,$data,'POST')) return false; 143*5a8d6e48SMichael Große if($this->status < 200 || $this->status > 206) return false; 144*5a8d6e48SMichael Große return $this->resp_body; 145*5a8d6e48SMichael Große } 146*5a8d6e48SMichael Große 147*5a8d6e48SMichael Große /** 148*5a8d6e48SMichael Große * Send an HTTP request 149*5a8d6e48SMichael Große * 150*5a8d6e48SMichael Große * This method handles the whole HTTP communication. It respects set proxy settings, 151*5a8d6e48SMichael Große * builds the request headers, follows redirects and parses the response. 152*5a8d6e48SMichael Große * 153*5a8d6e48SMichael Große * Post data should be passed as associative array. When passed as string it will be 154*5a8d6e48SMichael Große * sent as is. You will need to setup your own Content-Type header then. 155*5a8d6e48SMichael Große * 156*5a8d6e48SMichael Große * @param string $url - the complete URL 157*5a8d6e48SMichael Große * @param mixed $data - the post data either as array or raw data 158*5a8d6e48SMichael Große * @param string $method - HTTP Method usually GET or POST. 159*5a8d6e48SMichael Große * @return bool - true on success 160*5a8d6e48SMichael Große * 161*5a8d6e48SMichael Große * @author Andreas Goetz <cpuidle@gmx.de> 162*5a8d6e48SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 163*5a8d6e48SMichael Große */ 164*5a8d6e48SMichael Große public function sendRequest($url,$data='',$method='GET'){ 165*5a8d6e48SMichael Große $this->start = $this->time(); 166*5a8d6e48SMichael Große $this->error = ''; 167*5a8d6e48SMichael Große $this->status = 0; 168*5a8d6e48SMichael Große $this->status = 0; 169*5a8d6e48SMichael Große $this->resp_body = ''; 170*5a8d6e48SMichael Große $this->resp_headers = array(); 171*5a8d6e48SMichael Große 172*5a8d6e48SMichael Große // don't accept gzip if truncated bodies might occur 173*5a8d6e48SMichael Große if($this->max_bodysize && 174*5a8d6e48SMichael Große !$this->max_bodysize_abort && 175*5a8d6e48SMichael Große $this->headers['Accept-encoding'] == 'gzip'){ 176*5a8d6e48SMichael Große unset($this->headers['Accept-encoding']); 177*5a8d6e48SMichael Große } 178*5a8d6e48SMichael Große 179*5a8d6e48SMichael Große // parse URL into bits 180*5a8d6e48SMichael Große $uri = parse_url($url); 181*5a8d6e48SMichael Große $server = $uri['host']; 182*5a8d6e48SMichael Große $path = $uri['path']; 183*5a8d6e48SMichael Große if(empty($path)) $path = '/'; 184*5a8d6e48SMichael Große if(!empty($uri['query'])) $path .= '?'.$uri['query']; 185*5a8d6e48SMichael Große if(!empty($uri['port'])) $port = $uri['port']; 186*5a8d6e48SMichael Große if(isset($uri['user'])) $this->user = $uri['user']; 187*5a8d6e48SMichael Große if(isset($uri['pass'])) $this->pass = $uri['pass']; 188*5a8d6e48SMichael Große 189*5a8d6e48SMichael Große // proxy setup 190*5a8d6e48SMichael Große if($this->proxy_host && (!$this->proxy_except || !preg_match('/'.$this->proxy_except.'/i',$url)) ){ 191*5a8d6e48SMichael Große $request_url = $url; 192*5a8d6e48SMichael Große $server = $this->proxy_host; 193*5a8d6e48SMichael Große $port = $this->proxy_port; 194*5a8d6e48SMichael Große if (empty($port)) $port = 8080; 195*5a8d6e48SMichael Große $use_tls = $this->proxy_ssl; 196*5a8d6e48SMichael Große }else{ 197*5a8d6e48SMichael Große $request_url = $path; 198*5a8d6e48SMichael Große if (!isset($port)) $port = ($uri['scheme'] == 'https') ? 443 : 80; 199*5a8d6e48SMichael Große $use_tls = ($uri['scheme'] == 'https'); 200*5a8d6e48SMichael Große } 201*5a8d6e48SMichael Große 202*5a8d6e48SMichael Große // add SSL stream prefix if needed - needs SSL support in PHP 203*5a8d6e48SMichael Große if($use_tls) { 204*5a8d6e48SMichael Große if(!in_array('ssl', stream_get_transports())) { 205*5a8d6e48SMichael Große $this->status = -200; 206*5a8d6e48SMichael Große $this->error = 'This PHP version does not support SSL - cannot connect to server'; 207*5a8d6e48SMichael Große } 208*5a8d6e48SMichael Große $server = 'ssl://'.$server; 209*5a8d6e48SMichael Große } 210*5a8d6e48SMichael Große 211*5a8d6e48SMichael Große // prepare headers 212*5a8d6e48SMichael Große $headers = $this->headers; 213*5a8d6e48SMichael Große $headers['Host'] = $uri['host']; 214*5a8d6e48SMichael Große if(!empty($uri['port'])) $headers['Host'].= ':'.$uri['port']; 215*5a8d6e48SMichael Große $headers['User-Agent'] = $this->agent; 216*5a8d6e48SMichael Große $headers['Referer'] = $this->referer; 217*5a8d6e48SMichael Große 218*5a8d6e48SMichael Große if($method == 'POST'){ 219*5a8d6e48SMichael Große if(is_array($data)){ 220*5a8d6e48SMichael Große if (empty($headers['Content-Type'])) { 221*5a8d6e48SMichael Große $headers['Content-Type'] = null; 222*5a8d6e48SMichael Große } 223*5a8d6e48SMichael Große switch ($headers['Content-Type']) { 224*5a8d6e48SMichael Große case 'multipart/form-data': 225*5a8d6e48SMichael Große $headers['Content-Type'] = 'multipart/form-data; boundary=' . $this->boundary; 226*5a8d6e48SMichael Große $data = $this->postMultipartEncode($data); 227*5a8d6e48SMichael Große break; 228*5a8d6e48SMichael Große default: 229*5a8d6e48SMichael Große $headers['Content-Type'] = 'application/x-www-form-urlencoded'; 230*5a8d6e48SMichael Große $data = $this->postEncode($data); 231*5a8d6e48SMichael Große } 232*5a8d6e48SMichael Große } 233*5a8d6e48SMichael Große }elseif($method == 'GET'){ 234*5a8d6e48SMichael Große $data = ''; //no data allowed on GET requests 235*5a8d6e48SMichael Große } 236*5a8d6e48SMichael Große 237*5a8d6e48SMichael Große $contentlength = strlen($data); 238*5a8d6e48SMichael Große if($contentlength) { 239*5a8d6e48SMichael Große $headers['Content-Length'] = $contentlength; 240*5a8d6e48SMichael Große } 241*5a8d6e48SMichael Große 242*5a8d6e48SMichael Große if($this->user) { 243*5a8d6e48SMichael Große $headers['Authorization'] = 'Basic '.base64_encode($this->user.':'.$this->pass); 244*5a8d6e48SMichael Große } 245*5a8d6e48SMichael Große if($this->proxy_user) { 246*5a8d6e48SMichael Große $headers['Proxy-Authorization'] = 'Basic '.base64_encode($this->proxy_user.':'.$this->proxy_pass); 247*5a8d6e48SMichael Große } 248*5a8d6e48SMichael Große 249*5a8d6e48SMichael Große // already connected? 250*5a8d6e48SMichael Große $connectionId = $this->uniqueConnectionId($server,$port); 251*5a8d6e48SMichael Große $this->debug('connection pool', self::$connections); 252*5a8d6e48SMichael Große $socket = null; 253*5a8d6e48SMichael Große if (isset(self::$connections[$connectionId])) { 254*5a8d6e48SMichael Große $this->debug('reusing connection', $connectionId); 255*5a8d6e48SMichael Große $socket = self::$connections[$connectionId]; 256*5a8d6e48SMichael Große } 257*5a8d6e48SMichael Große if (is_null($socket) || feof($socket)) { 258*5a8d6e48SMichael Große $this->debug('opening connection', $connectionId); 259*5a8d6e48SMichael Große // open socket 260*5a8d6e48SMichael Große $socket = @fsockopen($server,$port,$errno, $errstr, $this->timeout); 261*5a8d6e48SMichael Große if (!$socket){ 262*5a8d6e48SMichael Große $this->status = -100; 263*5a8d6e48SMichael Große $this->error = "Could not connect to $server:$port\n$errstr ($errno)"; 264*5a8d6e48SMichael Große return false; 265*5a8d6e48SMichael Große } 266*5a8d6e48SMichael Große 267*5a8d6e48SMichael Große // try establish a CONNECT tunnel for SSL 268*5a8d6e48SMichael Große try { 269*5a8d6e48SMichael Große if($this->ssltunnel($socket, $request_url)){ 270*5a8d6e48SMichael Große // no keep alive for tunnels 271*5a8d6e48SMichael Große $this->keep_alive = false; 272*5a8d6e48SMichael Große // tunnel is authed already 273*5a8d6e48SMichael Große if(isset($headers['Proxy-Authentication'])) unset($headers['Proxy-Authentication']); 274*5a8d6e48SMichael Große } 275*5a8d6e48SMichael Große } catch (HTTPClientException $e) { 276*5a8d6e48SMichael Große $this->status = $e->getCode(); 277*5a8d6e48SMichael Große $this->error = $e->getMessage(); 278*5a8d6e48SMichael Große fclose($socket); 279*5a8d6e48SMichael Große return false; 280*5a8d6e48SMichael Große } 281*5a8d6e48SMichael Große 282*5a8d6e48SMichael Große // keep alive? 283*5a8d6e48SMichael Große if ($this->keep_alive) { 284*5a8d6e48SMichael Große self::$connections[$connectionId] = $socket; 285*5a8d6e48SMichael Große } else { 286*5a8d6e48SMichael Große unset(self::$connections[$connectionId]); 287*5a8d6e48SMichael Große } 288*5a8d6e48SMichael Große } 289*5a8d6e48SMichael Große 290*5a8d6e48SMichael Große if ($this->keep_alive && !$this->proxy_host) { 291*5a8d6e48SMichael Große // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive 292*5a8d6e48SMichael Große // connection token to a proxy server. We still do keep the connection the 293*5a8d6e48SMichael Große // proxy alive (well except for CONNECT tunnels) 294*5a8d6e48SMichael Große $headers['Connection'] = 'Keep-Alive'; 295*5a8d6e48SMichael Große } else { 296*5a8d6e48SMichael Große $headers['Connection'] = 'Close'; 297*5a8d6e48SMichael Große } 298*5a8d6e48SMichael Große 299*5a8d6e48SMichael Große try { 300*5a8d6e48SMichael Große //set non-blocking 301*5a8d6e48SMichael Große stream_set_blocking($socket, 0); 302*5a8d6e48SMichael Große 303*5a8d6e48SMichael Große // build request 304*5a8d6e48SMichael Große $request = "$method $request_url HTTP/".$this->http.HTTP_NL; 305*5a8d6e48SMichael Große $request .= $this->buildHeaders($headers); 306*5a8d6e48SMichael Große $request .= $this->getCookies(); 307*5a8d6e48SMichael Große $request .= HTTP_NL; 308*5a8d6e48SMichael Große $request .= $data; 309*5a8d6e48SMichael Große 310*5a8d6e48SMichael Große $this->debug('request',$request); 311*5a8d6e48SMichael Große $this->sendData($socket, $request, 'request'); 312*5a8d6e48SMichael Große 313*5a8d6e48SMichael Große // read headers from socket 314*5a8d6e48SMichael Große $r_headers = ''; 315*5a8d6e48SMichael Große do{ 316*5a8d6e48SMichael Große $r_line = $this->readLine($socket, 'headers'); 317*5a8d6e48SMichael Große $r_headers .= $r_line; 318*5a8d6e48SMichael Große }while($r_line != "\r\n" && $r_line != "\n"); 319*5a8d6e48SMichael Große 320*5a8d6e48SMichael Große $this->debug('response headers',$r_headers); 321*5a8d6e48SMichael Große 322*5a8d6e48SMichael Große // check if expected body size exceeds allowance 323*5a8d6e48SMichael Große if($this->max_bodysize && preg_match('/\r?\nContent-Length:\s*(\d+)\r?\n/i',$r_headers,$match)){ 324*5a8d6e48SMichael Große if($match[1] > $this->max_bodysize){ 325*5a8d6e48SMichael Große if ($this->max_bodysize_abort) 326*5a8d6e48SMichael Große throw new HTTPClientException('Reported content length exceeds allowed response size'); 327*5a8d6e48SMichael Große else 328*5a8d6e48SMichael Große $this->error = 'Reported content length exceeds allowed response size'; 329*5a8d6e48SMichael Große } 330*5a8d6e48SMichael Große } 331*5a8d6e48SMichael Große 332*5a8d6e48SMichael Große // get Status 333*5a8d6e48SMichael Große if (!preg_match('/^HTTP\/(\d\.\d)\s*(\d+).*?\n/', $r_headers, $m)) 334*5a8d6e48SMichael Große throw new HTTPClientException('Server returned bad answer '.$r_headers); 335*5a8d6e48SMichael Große 336*5a8d6e48SMichael Große $this->status = $m[2]; 337*5a8d6e48SMichael Große 338*5a8d6e48SMichael Große // handle headers and cookies 339*5a8d6e48SMichael Große $this->resp_headers = $this->parseHeaders($r_headers); 340*5a8d6e48SMichael Große if(isset($this->resp_headers['set-cookie'])){ 341*5a8d6e48SMichael Große foreach ((array) $this->resp_headers['set-cookie'] as $cookie){ 342*5a8d6e48SMichael Große list($cookie) = explode(';',$cookie,2); 343*5a8d6e48SMichael Große list($key,$val) = explode('=',$cookie,2); 344*5a8d6e48SMichael Große $key = trim($key); 345*5a8d6e48SMichael Große if($val == 'deleted'){ 346*5a8d6e48SMichael Große if(isset($this->cookies[$key])){ 347*5a8d6e48SMichael Große unset($this->cookies[$key]); 348*5a8d6e48SMichael Große } 349*5a8d6e48SMichael Große }elseif($key){ 350*5a8d6e48SMichael Große $this->cookies[$key] = $val; 351*5a8d6e48SMichael Große } 352*5a8d6e48SMichael Große } 353*5a8d6e48SMichael Große } 354*5a8d6e48SMichael Große 355*5a8d6e48SMichael Große $this->debug('Object headers',$this->resp_headers); 356*5a8d6e48SMichael Große 357*5a8d6e48SMichael Große // check server status code to follow redirect 358*5a8d6e48SMichael Große if($this->status == 301 || $this->status == 302 ){ 359*5a8d6e48SMichael Große if (empty($this->resp_headers['location'])){ 360*5a8d6e48SMichael Große throw new HTTPClientException('Redirect but no Location Header found'); 361*5a8d6e48SMichael Große }elseif($this->redirect_count == $this->max_redirect){ 362*5a8d6e48SMichael Große throw new HTTPClientException('Maximum number of redirects exceeded'); 363*5a8d6e48SMichael Große }else{ 364*5a8d6e48SMichael Große // close the connection because we don't handle content retrieval here 365*5a8d6e48SMichael Große // that's the easiest way to clean up the connection 366*5a8d6e48SMichael Große fclose($socket); 367*5a8d6e48SMichael Große unset(self::$connections[$connectionId]); 368*5a8d6e48SMichael Große 369*5a8d6e48SMichael Große $this->redirect_count++; 370*5a8d6e48SMichael Große $this->referer = $url; 371*5a8d6e48SMichael Große // handle non-RFC-compliant relative redirects 372*5a8d6e48SMichael Große if (!preg_match('/^http/i', $this->resp_headers['location'])){ 373*5a8d6e48SMichael Große if($this->resp_headers['location'][0] != '/'){ 374*5a8d6e48SMichael Große $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port']. 375*5a8d6e48SMichael Große dirname($uri['path']).'/'.$this->resp_headers['location']; 376*5a8d6e48SMichael Große }else{ 377*5a8d6e48SMichael Große $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port']. 378*5a8d6e48SMichael Große $this->resp_headers['location']; 379*5a8d6e48SMichael Große } 380*5a8d6e48SMichael Große } 381*5a8d6e48SMichael Große // perform redirected request, always via GET (required by RFC) 382*5a8d6e48SMichael Große return $this->sendRequest($this->resp_headers['location'],array(),'GET'); 383*5a8d6e48SMichael Große } 384*5a8d6e48SMichael Große } 385*5a8d6e48SMichael Große 386*5a8d6e48SMichael Große // check if headers are as expected 387*5a8d6e48SMichael Große if($this->header_regexp && !preg_match($this->header_regexp,$r_headers)) 388*5a8d6e48SMichael Große throw new HTTPClientException('The received headers did not match the given regexp'); 389*5a8d6e48SMichael Große 390*5a8d6e48SMichael Große //read body (with chunked encoding if needed) 391*5a8d6e48SMichael Große $r_body = ''; 392*5a8d6e48SMichael Große if( 393*5a8d6e48SMichael Große ( 394*5a8d6e48SMichael Große isset($this->resp_headers['transfer-encoding']) && 395*5a8d6e48SMichael Große $this->resp_headers['transfer-encoding'] == 'chunked' 396*5a8d6e48SMichael Große ) || ( 397*5a8d6e48SMichael Große isset($this->resp_headers['transfer-coding']) && 398*5a8d6e48SMichael Große $this->resp_headers['transfer-coding'] == 'chunked' 399*5a8d6e48SMichael Große ) 400*5a8d6e48SMichael Große ) { 401*5a8d6e48SMichael Große $abort = false; 402*5a8d6e48SMichael Große do { 403*5a8d6e48SMichael Große $chunk_size = ''; 404*5a8d6e48SMichael Große while (preg_match('/^[a-zA-Z0-9]?$/',$byte=$this->readData($socket,1,'chunk'))){ 405*5a8d6e48SMichael Große // read chunksize until \r 406*5a8d6e48SMichael Große $chunk_size .= $byte; 407*5a8d6e48SMichael Große if (strlen($chunk_size) > 128) // set an abritrary limit on the size of chunks 408*5a8d6e48SMichael Große throw new HTTPClientException('Allowed response size exceeded'); 409*5a8d6e48SMichael Große } 410*5a8d6e48SMichael Große $this->readLine($socket, 'chunk'); // readtrailing \n 411*5a8d6e48SMichael Große $chunk_size = hexdec($chunk_size); 412*5a8d6e48SMichael Große 413*5a8d6e48SMichael Große if($this->max_bodysize && $chunk_size+strlen($r_body) > $this->max_bodysize){ 414*5a8d6e48SMichael Große if ($this->max_bodysize_abort) 415*5a8d6e48SMichael Große throw new HTTPClientException('Allowed response size exceeded'); 416*5a8d6e48SMichael Große $this->error = 'Allowed response size exceeded'; 417*5a8d6e48SMichael Große $chunk_size = $this->max_bodysize - strlen($r_body); 418*5a8d6e48SMichael Große $abort = true; 419*5a8d6e48SMichael Große } 420*5a8d6e48SMichael Große 421*5a8d6e48SMichael Große if ($chunk_size > 0) { 422*5a8d6e48SMichael Große $r_body .= $this->readData($socket, $chunk_size, 'chunk'); 423*5a8d6e48SMichael Große $this->readData($socket, 2, 'chunk'); // read trailing \r\n 424*5a8d6e48SMichael Große } 425*5a8d6e48SMichael Große } while ($chunk_size && !$abort); 426*5a8d6e48SMichael Große }elseif(isset($this->resp_headers['content-length']) && !isset($this->resp_headers['transfer-encoding'])){ 427*5a8d6e48SMichael Große /* RFC 2616 428*5a8d6e48SMichael Große * If a message is received with both a Transfer-Encoding header field and a Content-Length 429*5a8d6e48SMichael Große * header field, the latter MUST be ignored. 430*5a8d6e48SMichael Große */ 431*5a8d6e48SMichael Große 432*5a8d6e48SMichael Große // read up to the content-length or max_bodysize 433*5a8d6e48SMichael Große // for keep alive we need to read the whole message to clean up the socket for the next read 434*5a8d6e48SMichael Große if( 435*5a8d6e48SMichael Große !$this->keep_alive && 436*5a8d6e48SMichael Große $this->max_bodysize && 437*5a8d6e48SMichael Große $this->max_bodysize < $this->resp_headers['content-length'] 438*5a8d6e48SMichael Große ) { 439*5a8d6e48SMichael Große $length = $this->max_bodysize + 1; 440*5a8d6e48SMichael Große }else{ 441*5a8d6e48SMichael Große $length = $this->resp_headers['content-length']; 442*5a8d6e48SMichael Große } 443*5a8d6e48SMichael Große 444*5a8d6e48SMichael Große $r_body = $this->readData($socket, $length, 'response (content-length limited)', true); 445*5a8d6e48SMichael Große }elseif( !isset($this->resp_headers['transfer-encoding']) && $this->max_bodysize && !$this->keep_alive){ 446*5a8d6e48SMichael Große $r_body = $this->readData($socket, $this->max_bodysize+1, 'response (content-length limited)', true); 447*5a8d6e48SMichael Große } elseif ((int)$this->status === 204) { 448*5a8d6e48SMichael Große // request has no content 449*5a8d6e48SMichael Große } else{ 450*5a8d6e48SMichael Große // read entire socket 451*5a8d6e48SMichael Große while (!feof($socket)) { 452*5a8d6e48SMichael Große $r_body .= $this->readData($socket, 4096, 'response (unlimited)', true); 453*5a8d6e48SMichael Große } 454*5a8d6e48SMichael Große } 455*5a8d6e48SMichael Große 456*5a8d6e48SMichael Große // recheck body size, we might have read max_bodysize+1 or even the whole body, so we abort late here 457*5a8d6e48SMichael Große if($this->max_bodysize){ 458*5a8d6e48SMichael Große if(strlen($r_body) > $this->max_bodysize){ 459*5a8d6e48SMichael Große if ($this->max_bodysize_abort) { 460*5a8d6e48SMichael Große throw new HTTPClientException('Allowed response size exceeded'); 461*5a8d6e48SMichael Große } else { 462*5a8d6e48SMichael Große $this->error = 'Allowed response size exceeded'; 463*5a8d6e48SMichael Große } 464*5a8d6e48SMichael Große } 465*5a8d6e48SMichael Große } 466*5a8d6e48SMichael Große 467*5a8d6e48SMichael Große } catch (HTTPClientException $err) { 468*5a8d6e48SMichael Große $this->error = $err->getMessage(); 469*5a8d6e48SMichael Große if ($err->getCode()) 470*5a8d6e48SMichael Große $this->status = $err->getCode(); 471*5a8d6e48SMichael Große unset(self::$connections[$connectionId]); 472*5a8d6e48SMichael Große fclose($socket); 473*5a8d6e48SMichael Große return false; 474*5a8d6e48SMichael Große } 475*5a8d6e48SMichael Große 476*5a8d6e48SMichael Große if (!$this->keep_alive || 477*5a8d6e48SMichael Große (isset($this->resp_headers['connection']) && $this->resp_headers['connection'] == 'Close')) { 478*5a8d6e48SMichael Große // close socket 479*5a8d6e48SMichael Große fclose($socket); 480*5a8d6e48SMichael Große unset(self::$connections[$connectionId]); 481*5a8d6e48SMichael Große } 482*5a8d6e48SMichael Große 483*5a8d6e48SMichael Große // decode gzip if needed 484*5a8d6e48SMichael Große if(isset($this->resp_headers['content-encoding']) && 485*5a8d6e48SMichael Große $this->resp_headers['content-encoding'] == 'gzip' && 486*5a8d6e48SMichael Große strlen($r_body) > 10 && substr($r_body,0,3)=="\x1f\x8b\x08"){ 487*5a8d6e48SMichael Große $this->resp_body = @gzinflate(substr($r_body, 10)); 488*5a8d6e48SMichael Große if($this->resp_body === false){ 489*5a8d6e48SMichael Große $this->error = 'Failed to decompress gzip encoded content'; 490*5a8d6e48SMichael Große $this->resp_body = $r_body; 491*5a8d6e48SMichael Große } 492*5a8d6e48SMichael Große }else{ 493*5a8d6e48SMichael Große $this->resp_body = $r_body; 494*5a8d6e48SMichael Große } 495*5a8d6e48SMichael Große 496*5a8d6e48SMichael Große $this->debug('response body',$this->resp_body); 497*5a8d6e48SMichael Große $this->redirect_count = 0; 498*5a8d6e48SMichael Große return true; 499*5a8d6e48SMichael Große } 500*5a8d6e48SMichael Große 501*5a8d6e48SMichael Große /** 502*5a8d6e48SMichael Große * Tries to establish a CONNECT tunnel via Proxy 503*5a8d6e48SMichael Große * 504*5a8d6e48SMichael Große * Protocol, Servername and Port will be stripped from the request URL when a successful CONNECT happened 505*5a8d6e48SMichael Große * 506*5a8d6e48SMichael Große * @param resource &$socket 507*5a8d6e48SMichael Große * @param string &$requesturl 508*5a8d6e48SMichael Große * @throws HTTPClientException when a tunnel is needed but could not be established 509*5a8d6e48SMichael Große * @return bool true if a tunnel was established 510*5a8d6e48SMichael Große */ 511*5a8d6e48SMichael Große protected function ssltunnel(&$socket, &$requesturl){ 512*5a8d6e48SMichael Große if(!$this->proxy_host) return false; 513*5a8d6e48SMichael Große $requestinfo = parse_url($requesturl); 514*5a8d6e48SMichael Große if($requestinfo['scheme'] != 'https') return false; 515*5a8d6e48SMichael Große if(!$requestinfo['port']) $requestinfo['port'] = 443; 516*5a8d6e48SMichael Große 517*5a8d6e48SMichael Große // build request 518*5a8d6e48SMichael Große $request = "CONNECT {$requestinfo['host']}:{$requestinfo['port']} HTTP/1.0".HTTP_NL; 519*5a8d6e48SMichael Große $request .= "Host: {$requestinfo['host']}".HTTP_NL; 520*5a8d6e48SMichael Große if($this->proxy_user) { 521*5a8d6e48SMichael Große $request .= 'Proxy-Authorization: Basic '.base64_encode($this->proxy_user.':'.$this->proxy_pass).HTTP_NL; 522*5a8d6e48SMichael Große } 523*5a8d6e48SMichael Große $request .= HTTP_NL; 524*5a8d6e48SMichael Große 525*5a8d6e48SMichael Große $this->debug('SSL Tunnel CONNECT',$request); 526*5a8d6e48SMichael Große $this->sendData($socket, $request, 'SSL Tunnel CONNECT'); 527*5a8d6e48SMichael Große 528*5a8d6e48SMichael Große // read headers from socket 529*5a8d6e48SMichael Große $r_headers = ''; 530*5a8d6e48SMichael Große do{ 531*5a8d6e48SMichael Große $r_line = $this->readLine($socket, 'headers'); 532*5a8d6e48SMichael Große $r_headers .= $r_line; 533*5a8d6e48SMichael Große }while($r_line != "\r\n" && $r_line != "\n"); 534*5a8d6e48SMichael Große 535*5a8d6e48SMichael Große $this->debug('SSL Tunnel Response',$r_headers); 536*5a8d6e48SMichael Große if(preg_match('/^HTTP\/1\.[01] 200/i',$r_headers)){ 537*5a8d6e48SMichael Große // set correct peer name for verification (enabled since PHP 5.6) 538*5a8d6e48SMichael Große stream_context_set_option($socket, 'ssl', 'peer_name', $requestinfo['host']); 539*5a8d6e48SMichael Große 540*5a8d6e48SMichael Große // SSLv3 is broken, use only TLS connections. 541*5a8d6e48SMichael Große // @link https://bugs.php.net/69195 542*5a8d6e48SMichael Große if (PHP_VERSION_ID >= 50600 && PHP_VERSION_ID <= 50606) { 543*5a8d6e48SMichael Große $cryptoMethod = STREAM_CRYPTO_METHOD_TLS_CLIENT; 544*5a8d6e48SMichael Große } else { 545*5a8d6e48SMichael Große // actually means neither SSLv2 nor SSLv3 546*5a8d6e48SMichael Große $cryptoMethod = STREAM_CRYPTO_METHOD_SSLv23_CLIENT; 547*5a8d6e48SMichael Große } 548*5a8d6e48SMichael Große 549*5a8d6e48SMichael Große if (@stream_socket_enable_crypto($socket, true, $cryptoMethod)) { 550*5a8d6e48SMichael Große $requesturl = $requestinfo['path']. 551*5a8d6e48SMichael Große (!empty($requestinfo['query'])?'?'.$requestinfo['query']:''); 552*5a8d6e48SMichael Große return true; 553*5a8d6e48SMichael Große } 554*5a8d6e48SMichael Große 555*5a8d6e48SMichael Große throw new HTTPClientException( 556*5a8d6e48SMichael Große 'Failed to set up crypto for secure connection to '.$requestinfo['host'], -151 557*5a8d6e48SMichael Große ); 558*5a8d6e48SMichael Große } 559*5a8d6e48SMichael Große 560*5a8d6e48SMichael Große throw new HTTPClientException('Failed to establish secure proxy connection', -150); 561*5a8d6e48SMichael Große } 562*5a8d6e48SMichael Große 563*5a8d6e48SMichael Große /** 564*5a8d6e48SMichael Große * Safely write data to a socket 565*5a8d6e48SMichael Große * 566*5a8d6e48SMichael Große * @param resource $socket An open socket handle 567*5a8d6e48SMichael Große * @param string $data The data to write 568*5a8d6e48SMichael Große * @param string $message Description of what is being read 569*5a8d6e48SMichael Große * @throws HTTPClientException 570*5a8d6e48SMichael Große * 571*5a8d6e48SMichael Große * @author Tom N Harris <tnharris@whoopdedo.org> 572*5a8d6e48SMichael Große */ 573*5a8d6e48SMichael Große protected function sendData($socket, $data, $message) { 574*5a8d6e48SMichael Große // send request 575*5a8d6e48SMichael Große $towrite = strlen($data); 576*5a8d6e48SMichael Große $written = 0; 577*5a8d6e48SMichael Große while($written < $towrite){ 578*5a8d6e48SMichael Große // check timeout 579*5a8d6e48SMichael Große $time_used = $this->time() - $this->start; 580*5a8d6e48SMichael Große if($time_used > $this->timeout) 581*5a8d6e48SMichael Große throw new HTTPClientException(sprintf('Timeout while sending %s (%.3fs)',$message, $time_used), -100); 582*5a8d6e48SMichael Große if(feof($socket)) 583*5a8d6e48SMichael Große throw new HTTPClientException("Socket disconnected while writing $message"); 584*5a8d6e48SMichael Große 585*5a8d6e48SMichael Große // select parameters 586*5a8d6e48SMichael Große $sel_r = null; 587*5a8d6e48SMichael Große $sel_w = array($socket); 588*5a8d6e48SMichael Große $sel_e = null; 589*5a8d6e48SMichael Große // wait for stream ready or timeout (1sec) 590*5a8d6e48SMichael Große if(@stream_select($sel_r,$sel_w,$sel_e,1) === false){ 591*5a8d6e48SMichael Große usleep(1000); 592*5a8d6e48SMichael Große continue; 593*5a8d6e48SMichael Große } 594*5a8d6e48SMichael Große 595*5a8d6e48SMichael Große // write to stream 596*5a8d6e48SMichael Große $nbytes = fwrite($socket, substr($data,$written,4096)); 597*5a8d6e48SMichael Große if($nbytes === false) 598*5a8d6e48SMichael Große throw new HTTPClientException("Failed writing to socket while sending $message", -100); 599*5a8d6e48SMichael Große $written += $nbytes; 600*5a8d6e48SMichael Große } 601*5a8d6e48SMichael Große } 602*5a8d6e48SMichael Große 603*5a8d6e48SMichael Große /** 604*5a8d6e48SMichael Große * Safely read data from a socket 605*5a8d6e48SMichael Große * 606*5a8d6e48SMichael Große * Reads up to a given number of bytes or throws an exception if the 607*5a8d6e48SMichael Große * response times out or ends prematurely. 608*5a8d6e48SMichael Große * 609*5a8d6e48SMichael Große * @param resource $socket An open socket handle in non-blocking mode 610*5a8d6e48SMichael Große * @param int $nbytes Number of bytes to read 611*5a8d6e48SMichael Große * @param string $message Description of what is being read 612*5a8d6e48SMichael Große * @param bool $ignore_eof End-of-file is not an error if this is set 613*5a8d6e48SMichael Große * @throws HTTPClientException 614*5a8d6e48SMichael Große * @return string 615*5a8d6e48SMichael Große * 616*5a8d6e48SMichael Große * @author Tom N Harris <tnharris@whoopdedo.org> 617*5a8d6e48SMichael Große */ 618*5a8d6e48SMichael Große protected function readData($socket, $nbytes, $message, $ignore_eof = false) { 619*5a8d6e48SMichael Große $r_data = ''; 620*5a8d6e48SMichael Große // Does not return immediately so timeout and eof can be checked 621*5a8d6e48SMichael Große if ($nbytes < 0) $nbytes = 0; 622*5a8d6e48SMichael Große $to_read = $nbytes; 623*5a8d6e48SMichael Große do { 624*5a8d6e48SMichael Große $time_used = $this->time() - $this->start; 625*5a8d6e48SMichael Große if ($time_used > $this->timeout) 626*5a8d6e48SMichael Große throw new HTTPClientException( 627*5a8d6e48SMichael Große sprintf('Timeout while reading %s after %d bytes (%.3fs)', $message, 628*5a8d6e48SMichael Große strlen($r_data), $time_used), -100); 629*5a8d6e48SMichael Große if(feof($socket)) { 630*5a8d6e48SMichael Große if(!$ignore_eof) 631*5a8d6e48SMichael Große throw new HTTPClientException("Premature End of File (socket) while reading $message"); 632*5a8d6e48SMichael Große break; 633*5a8d6e48SMichael Große } 634*5a8d6e48SMichael Große 635*5a8d6e48SMichael Große if ($to_read > 0) { 636*5a8d6e48SMichael Große // select parameters 637*5a8d6e48SMichael Große $sel_r = array($socket); 638*5a8d6e48SMichael Große $sel_w = null; 639*5a8d6e48SMichael Große $sel_e = null; 640*5a8d6e48SMichael Große // wait for stream ready or timeout (1sec) 641*5a8d6e48SMichael Große if(@stream_select($sel_r,$sel_w,$sel_e,1) === false){ 642*5a8d6e48SMichael Große usleep(1000); 643*5a8d6e48SMichael Große continue; 644*5a8d6e48SMichael Große } 645*5a8d6e48SMichael Große 646*5a8d6e48SMichael Große $bytes = fread($socket, $to_read); 647*5a8d6e48SMichael Große if($bytes === false) 648*5a8d6e48SMichael Große throw new HTTPClientException("Failed reading from socket while reading $message", -100); 649*5a8d6e48SMichael Große $r_data .= $bytes; 650*5a8d6e48SMichael Große $to_read -= strlen($bytes); 651*5a8d6e48SMichael Große } 652*5a8d6e48SMichael Große } while ($to_read > 0 && strlen($r_data) < $nbytes); 653*5a8d6e48SMichael Große return $r_data; 654*5a8d6e48SMichael Große } 655*5a8d6e48SMichael Große 656*5a8d6e48SMichael Große /** 657*5a8d6e48SMichael Große * Safely read a \n-terminated line from a socket 658*5a8d6e48SMichael Große * 659*5a8d6e48SMichael Große * Always returns a complete line, including the terminating \n. 660*5a8d6e48SMichael Große * 661*5a8d6e48SMichael Große * @param resource $socket An open socket handle in non-blocking mode 662*5a8d6e48SMichael Große * @param string $message Description of what is being read 663*5a8d6e48SMichael Große * @throws HTTPClientException 664*5a8d6e48SMichael Große * @return string 665*5a8d6e48SMichael Große * 666*5a8d6e48SMichael Große * @author Tom N Harris <tnharris@whoopdedo.org> 667*5a8d6e48SMichael Große */ 668*5a8d6e48SMichael Große protected function readLine($socket, $message) { 669*5a8d6e48SMichael Große $r_data = ''; 670*5a8d6e48SMichael Große do { 671*5a8d6e48SMichael Große $time_used = $this->time() - $this->start; 672*5a8d6e48SMichael Große if ($time_used > $this->timeout) 673*5a8d6e48SMichael Große throw new HTTPClientException( 674*5a8d6e48SMichael Große sprintf('Timeout while reading %s (%.3fs) >%s<', $message, $time_used, $r_data), 675*5a8d6e48SMichael Große -100); 676*5a8d6e48SMichael Große if(feof($socket)) 677*5a8d6e48SMichael Große throw new HTTPClientException("Premature End of File (socket) while reading $message"); 678*5a8d6e48SMichael Große 679*5a8d6e48SMichael Große // select parameters 680*5a8d6e48SMichael Große $sel_r = array($socket); 681*5a8d6e48SMichael Große $sel_w = null; 682*5a8d6e48SMichael Große $sel_e = null; 683*5a8d6e48SMichael Große // wait for stream ready or timeout (1sec) 684*5a8d6e48SMichael Große if(@stream_select($sel_r,$sel_w,$sel_e,1) === false){ 685*5a8d6e48SMichael Große usleep(1000); 686*5a8d6e48SMichael Große continue; 687*5a8d6e48SMichael Große } 688*5a8d6e48SMichael Große 689*5a8d6e48SMichael Große $r_data = fgets($socket, 1024); 690*5a8d6e48SMichael Große } while (!preg_match('/\n$/',$r_data)); 691*5a8d6e48SMichael Große return $r_data; 692*5a8d6e48SMichael Große } 693*5a8d6e48SMichael Große 694*5a8d6e48SMichael Große /** 695*5a8d6e48SMichael Große * print debug info 696*5a8d6e48SMichael Große * 697*5a8d6e48SMichael Große * Uses _debug_text or _debug_html depending on the SAPI name 698*5a8d6e48SMichael Große * 699*5a8d6e48SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 700*5a8d6e48SMichael Große * 701*5a8d6e48SMichael Große * @param string $info 702*5a8d6e48SMichael Große * @param mixed $var 703*5a8d6e48SMichael Große */ 704*5a8d6e48SMichael Große protected function debug($info,$var=null){ 705*5a8d6e48SMichael Große if(!$this->debug) return; 706*5a8d6e48SMichael Große if(php_sapi_name() == 'cli'){ 707*5a8d6e48SMichael Große $this->debugText($info, $var); 708*5a8d6e48SMichael Große }else{ 709*5a8d6e48SMichael Große $this->debugHtml($info, $var); 710*5a8d6e48SMichael Große } 711*5a8d6e48SMichael Große } 712*5a8d6e48SMichael Große 713*5a8d6e48SMichael Große /** 714*5a8d6e48SMichael Große * print debug info as HTML 715*5a8d6e48SMichael Große * 716*5a8d6e48SMichael Große * @param string $info 717*5a8d6e48SMichael Große * @param mixed $var 718*5a8d6e48SMichael Große */ 719*5a8d6e48SMichael Große protected function debugHtml($info, $var=null){ 720*5a8d6e48SMichael Große print '<b>'.$info.'</b> '.($this->time() - $this->start).'s<br />'; 721*5a8d6e48SMichael Große if(!is_null($var)){ 722*5a8d6e48SMichael Große ob_start(); 723*5a8d6e48SMichael Große print_r($var); 724*5a8d6e48SMichael Große $content = htmlspecialchars(ob_get_contents()); 725*5a8d6e48SMichael Große ob_end_clean(); 726*5a8d6e48SMichael Große print '<pre>'.$content.'</pre>'; 727*5a8d6e48SMichael Große } 728*5a8d6e48SMichael Große } 729*5a8d6e48SMichael Große 730*5a8d6e48SMichael Große /** 731*5a8d6e48SMichael Große * prints debug info as plain text 732*5a8d6e48SMichael Große * 733*5a8d6e48SMichael Große * @param string $info 734*5a8d6e48SMichael Große * @param mixed $var 735*5a8d6e48SMichael Große */ 736*5a8d6e48SMichael Große protected function debugText($info, $var=null){ 737*5a8d6e48SMichael Große print '*'.$info.'* '.($this->time() - $this->start)."s\n"; 738*5a8d6e48SMichael Große if(!is_null($var)) print_r($var); 739*5a8d6e48SMichael Große print "\n-----------------------------------------------\n"; 740*5a8d6e48SMichael Große } 741*5a8d6e48SMichael Große 742*5a8d6e48SMichael Große /** 743*5a8d6e48SMichael Große * Return current timestamp in microsecond resolution 744*5a8d6e48SMichael Große * 745*5a8d6e48SMichael Große * @return float 746*5a8d6e48SMichael Große */ 747*5a8d6e48SMichael Große protected static function time(){ 748*5a8d6e48SMichael Große list($usec, $sec) = explode(" ", microtime()); 749*5a8d6e48SMichael Große return ((float)$usec + (float)$sec); 750*5a8d6e48SMichael Große } 751*5a8d6e48SMichael Große 752*5a8d6e48SMichael Große /** 753*5a8d6e48SMichael Große * convert given header string to Header array 754*5a8d6e48SMichael Große * 755*5a8d6e48SMichael Große * All Keys are lowercased. 756*5a8d6e48SMichael Große * 757*5a8d6e48SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 758*5a8d6e48SMichael Große * 759*5a8d6e48SMichael Große * @param string $string 760*5a8d6e48SMichael Große * @return array 761*5a8d6e48SMichael Große */ 762*5a8d6e48SMichael Große protected function parseHeaders($string){ 763*5a8d6e48SMichael Große $headers = array(); 764*5a8d6e48SMichael Große $lines = explode("\n",$string); 765*5a8d6e48SMichael Große array_shift($lines); //skip first line (status) 766*5a8d6e48SMichael Große foreach($lines as $line){ 767*5a8d6e48SMichael Große @list($key, $val) = explode(':',$line,2); 768*5a8d6e48SMichael Große $key = trim($key); 769*5a8d6e48SMichael Große $val = trim($val); 770*5a8d6e48SMichael Große $key = strtolower($key); 771*5a8d6e48SMichael Große if(!$key) continue; 772*5a8d6e48SMichael Große if(isset($headers[$key])){ 773*5a8d6e48SMichael Große if(is_array($headers[$key])){ 774*5a8d6e48SMichael Große $headers[$key][] = $val; 775*5a8d6e48SMichael Große }else{ 776*5a8d6e48SMichael Große $headers[$key] = array($headers[$key],$val); 777*5a8d6e48SMichael Große } 778*5a8d6e48SMichael Große }else{ 779*5a8d6e48SMichael Große $headers[$key] = $val; 780*5a8d6e48SMichael Große } 781*5a8d6e48SMichael Große } 782*5a8d6e48SMichael Große return $headers; 783*5a8d6e48SMichael Große } 784*5a8d6e48SMichael Große 785*5a8d6e48SMichael Große /** 786*5a8d6e48SMichael Große * convert given header array to header string 787*5a8d6e48SMichael Große * 788*5a8d6e48SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 789*5a8d6e48SMichael Große * 790*5a8d6e48SMichael Große * @param array $headers 791*5a8d6e48SMichael Große * @return string 792*5a8d6e48SMichael Große */ 793*5a8d6e48SMichael Große protected function buildHeaders($headers){ 794*5a8d6e48SMichael Große $string = ''; 795*5a8d6e48SMichael Große foreach($headers as $key => $value){ 796*5a8d6e48SMichael Große if($value === '') continue; 797*5a8d6e48SMichael Große $string .= $key.': '.$value.HTTP_NL; 798*5a8d6e48SMichael Große } 799*5a8d6e48SMichael Große return $string; 800*5a8d6e48SMichael Große } 801*5a8d6e48SMichael Große 802*5a8d6e48SMichael Große /** 803*5a8d6e48SMichael Große * get cookies as http header string 804*5a8d6e48SMichael Große * 805*5a8d6e48SMichael Große * @author Andreas Goetz <cpuidle@gmx.de> 806*5a8d6e48SMichael Große * 807*5a8d6e48SMichael Große * @return string 808*5a8d6e48SMichael Große */ 809*5a8d6e48SMichael Große protected function getCookies(){ 810*5a8d6e48SMichael Große $headers = ''; 811*5a8d6e48SMichael Große foreach ($this->cookies as $key => $val){ 812*5a8d6e48SMichael Große $headers .= "$key=$val; "; 813*5a8d6e48SMichael Große } 814*5a8d6e48SMichael Große $headers = substr($headers, 0, -2); 815*5a8d6e48SMichael Große if ($headers) $headers = "Cookie: $headers".HTTP_NL; 816*5a8d6e48SMichael Große return $headers; 817*5a8d6e48SMichael Große } 818*5a8d6e48SMichael Große 819*5a8d6e48SMichael Große /** 820*5a8d6e48SMichael Große * Encode data for posting 821*5a8d6e48SMichael Große * 822*5a8d6e48SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 823*5a8d6e48SMichael Große * 824*5a8d6e48SMichael Große * @param array $data 825*5a8d6e48SMichael Große * @return string 826*5a8d6e48SMichael Große */ 827*5a8d6e48SMichael Große protected function postEncode($data){ 828*5a8d6e48SMichael Große return http_build_query($data,'','&'); 829*5a8d6e48SMichael Große } 830*5a8d6e48SMichael Große 831*5a8d6e48SMichael Große /** 832*5a8d6e48SMichael Große * Encode data for posting using multipart encoding 833*5a8d6e48SMichael Große * 834*5a8d6e48SMichael Große * @fixme use of urlencode might be wrong here 835*5a8d6e48SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 836*5a8d6e48SMichael Große * 837*5a8d6e48SMichael Große * @param array $data 838*5a8d6e48SMichael Große * @return string 839*5a8d6e48SMichael Große */ 840*5a8d6e48SMichael Große protected function postMultipartEncode($data){ 841*5a8d6e48SMichael Große $boundary = '--'.$this->boundary; 842*5a8d6e48SMichael Große $out = ''; 843*5a8d6e48SMichael Große foreach($data as $key => $val){ 844*5a8d6e48SMichael Große $out .= $boundary.HTTP_NL; 845*5a8d6e48SMichael Große if(!is_array($val)){ 846*5a8d6e48SMichael Große $out .= 'Content-Disposition: form-data; name="'.urlencode($key).'"'.HTTP_NL; 847*5a8d6e48SMichael Große $out .= HTTP_NL; // end of headers 848*5a8d6e48SMichael Große $out .= $val; 849*5a8d6e48SMichael Große $out .= HTTP_NL; 850*5a8d6e48SMichael Große }else{ 851*5a8d6e48SMichael Große $out .= 'Content-Disposition: form-data; name="'.urlencode($key).'"'; 852*5a8d6e48SMichael Große if($val['filename']) $out .= '; filename="'.urlencode($val['filename']).'"'; 853*5a8d6e48SMichael Große $out .= HTTP_NL; 854*5a8d6e48SMichael Große if($val['mimetype']) $out .= 'Content-Type: '.$val['mimetype'].HTTP_NL; 855*5a8d6e48SMichael Große $out .= HTTP_NL; // end of headers 856*5a8d6e48SMichael Große $out .= $val['body']; 857*5a8d6e48SMichael Große $out .= HTTP_NL; 858*5a8d6e48SMichael Große } 859*5a8d6e48SMichael Große } 860*5a8d6e48SMichael Große $out .= "$boundary--".HTTP_NL; 861*5a8d6e48SMichael Große return $out; 862*5a8d6e48SMichael Große } 863*5a8d6e48SMichael Große 864*5a8d6e48SMichael Große /** 865*5a8d6e48SMichael Große * Generates a unique identifier for a connection. 866*5a8d6e48SMichael Große * 867*5a8d6e48SMichael Große * @param string $server 868*5a8d6e48SMichael Große * @param string $port 869*5a8d6e48SMichael Große * @return string unique identifier 870*5a8d6e48SMichael Große */ 871*5a8d6e48SMichael Große protected function uniqueConnectionId($server, $port) { 872*5a8d6e48SMichael Große return "$server:$port"; 873*5a8d6e48SMichael Große } 874*5a8d6e48SMichael Große} 875