5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Goetz <cpuidle@gmx.de>
10 define('HTTP_NL',"\r\n");
14 * Adds DokuWiki specific configs to the HTTP client
16 * @author Andreas Goetz <cpuidle@gmx.de>
18 class DokuHTTPClient extends HTTPClient {
23 * @author Andreas Gohr <andi@splitbrain.org>
25 function __construct(){
28 // call parent constructor
29 parent::__construct();
31 // set some values from the config
32 $this->proxy_host = $conf['proxy']['host'];
33 $this->proxy_port = $conf['proxy']['port'];
34 $this->proxy_user = $conf['proxy']['user'];
35 $this->proxy_pass = conf_decodeString($conf['proxy']['pass']);
36 $this->proxy_ssl = $conf['proxy']['ssl'];
37 $this->proxy_except = $conf['proxy']['except'];
42 * Wraps an event around the parent function
44 * @triggers HTTPCLIENT_REQUEST_SEND
45 * @author Andreas Gohr <andi@splitbrain.org>
47 function sendRequest($url,$data='',$method='GET'){
48 $httpdata = array('url' => $url,
51 $evt = new Doku_Event('HTTPCLIENT_REQUEST_SEND',$httpdata);
52 if($evt->advise_before()){
53 $url = $httpdata['url'];
54 $data = $httpdata['data'];
55 $method = $httpdata['method'];
59 return parent::sendRequest($url,$data,$method);
64 class HTTPClientException extends Exception { }
67 * This class implements a basic HTTP client
69 * It supports POST and GET, Proxy usage, basic authentication,
70 * handles cookies and referers. It is based upon the httpclient
71 * function from the VideoDB project.
73 * @link http://www.splitbrain.org/go/videodb
74 * @author Andreas Goetz <cpuidle@gmx.de>
75 * @author Andreas Gohr <andi@splitbrain.org>
76 * @author Tobias Sarnowski <sarnowski@new-thoughts.org>
79 //set these if you like
80 var $agent; // User agent
81 var $http; // HTTP version defaults to 1.0
82 var $timeout; // read timeout (seconds)
87 var $max_bodysize_abort = true; // if set, abort if the response body is bigger than max_bodysize
88 var $header_regexp; // if set this RE must match against the headers, else abort
91 var $start = 0; // for timings
92 var $keep_alive = true; // keep alive rocks
94 // don't set these, read on error
98 // read these after a successful request
103 // set these to do basic authentication
107 // set these if you need to use a proxy
112 var $proxy_ssl; //boolean set to true if your proxy needs SSL
113 var $proxy_except; // regexp of URLs to exclude from proxy
115 // list of kept alive connections
116 static $connections = array();
118 // what we use as boundary on multipart/form-data posts
119 var $boundary = '---DokuWikiHTTPClient--4523452351';
124 * @author Andreas Gohr <andi@splitbrain.org>
126 function __construct(){
127 $this->agent = 'Mozilla/4.0 (compatible; DokuWiki HTTP Client; '.PHP_OS.')';
129 $this->cookies = array();
131 $this->max_redirect = 3;
132 $this->redirect_count = 0;
134 $this->headers = array();
136 $this->debug = false;
137 $this->max_bodysize = 0;
138 $this->header_regexp= '';
139 if(extension_loaded('zlib')) $this->headers['Accept-encoding'] = 'gzip';
140 $this->headers['Accept'] = 'text/xml,application/xml,application/xhtml+xml,'.
141 'text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
142 $this->headers['Accept-Language'] = 'en-us';
147 * Simple function to do a GET request
149 * Returns the wanted page or false on an error;
151 * @param string $url The URL to fetch
152 * @param bool $sloppy304 Return body on 304 not modified
153 * @author Andreas Gohr <andi@splitbrain.org>
155 function get($url,$sloppy304=false){
156 if(!$this->sendRequest($url)) return false;
157 if($this->status == 304 && $sloppy304) return $this->resp_body;
158 if($this->status < 200 || $this->status > 206) return false;
159 return $this->resp_body;
163 * Simple function to do a GET request with given parameters
165 * Returns the wanted page or false on an error.
167 * This is a convenience wrapper around get(). The given parameters
168 * will be correctly encoded and added to the given base URL.
170 * @param string $url The URL to fetch
171 * @param array $data Associative array of parameters
172 * @param bool $sloppy304 Return body on 304 not modified
173 * @author Andreas Gohr <andi@splitbrain.org>
175 function dget($url,$data,$sloppy304=false){
176 if(strpos($url,'?')){
181 $url .= $this->_postEncode($data);
182 return $this->get($url,$sloppy304);
186 * Simple function to do a POST request
188 * Returns the resulting page or false on an error;
190 * @author Andreas Gohr <andi@splitbrain.org>
192 function post($url,$data){
193 if(!$this->sendRequest($url,$data,'POST')) return false;
194 if($this->status < 200 || $this->status > 206) return false;
195 return $this->resp_body;
199 * Send an HTTP request
201 * This method handles the whole HTTP communication. It respects set proxy settings,
202 * builds the request headers, follows redirects and parses the response.
204 * Post data should be passed as associative array. When passed as string it will be
205 * sent as is. You will need to setup your own Content-Type header then.
207 * @param string $url - the complete URL
208 * @param mixed $data - the post data either as array or raw data
209 * @param string $method - HTTP Method usually GET or POST.
210 * @return bool - true on success
211 * @author Andreas Goetz <cpuidle@gmx.de>
212 * @author Andreas Gohr <andi@splitbrain.org>
214 function sendRequest($url,$data='',$method='GET'){
215 $this->start = $this->_time();
219 // don't accept gzip if truncated bodies might occur
220 if($this->max_bodysize &&
221 !$this->max_bodysize_abort &&
222 $this->headers['Accept-encoding'] == 'gzip'){
223 unset($this->headers['Accept-encoding']);
226 // parse URL into bits
227 $uri = parse_url($url);
228 $server = $uri['host'];
229 $path = $uri['path'];
230 if(empty($path)) $path = '/';
231 if(!empty($uri['query'])) $path .= '?'.$uri['query'];
232 if(!empty($uri['port'])) $port = $uri['port'];
233 if(isset($uri['user'])) $this->user = $uri['user'];
234 if(isset($uri['pass'])) $this->pass = $uri['pass'];
237 if($this->proxy_host && (!$this->proxy_except || !preg_match('/'.$this->proxy_except.'/i',$url)) ){
239 $server = $this->proxy_host;
240 $port = $this->proxy_port;
241 if (empty($port)) $port = 8080;
243 $request_url = $path;
245 if (!isset($port)) $port = ($uri['scheme'] == 'https') ? 443 : 80;
248 // add SSL stream prefix if needed - needs SSL support in PHP
249 if($port == 443 || $this->proxy_ssl) $server = 'ssl://'.$server;
252 $headers = $this->headers;
253 $headers['Host'] = $uri['host'];
254 if(!empty($uri['port'])) $headers['Host'].= ':'.$uri['port'];
255 $headers['User-Agent'] = $this->agent;
256 $headers['Referer'] = $this->referer;
257 if ($this->keep_alive) {
258 $headers['Connection'] = 'Keep-Alive';
260 $headers['Connection'] = 'Close';
262 if($method == 'POST'){
264 if($headers['Content-Type'] == 'multipart/form-data'){
265 $headers['Content-Type'] = 'multipart/form-data; boundary='.$this->boundary;
266 $data = $this->_postMultipartEncode($data);
268 $headers['Content-Type'] = 'application/x-www-form-urlencoded';
269 $data = $this->_postEncode($data);
272 $headers['Content-Length'] = strlen($data);
274 }elseif($method == 'GET'){
275 $data = ''; //no data allowed on GET requests
278 $headers['Authorization'] = 'Basic '.base64_encode($this->user.':'.$this->pass);
280 if($this->proxy_user) {
281 $headers['Proxy-Authorization'] = 'Basic '.base64_encode($this->proxy_user.':'.$this->proxy_pass);
284 // already connected?
285 $connectionId = $this->_uniqueConnectionId($server,$port);
286 $this->_debug('connection pool', self::$connections);
288 if (isset(self::$connections[$connectionId])) {
289 $this->_debug('reusing connection', $connectionId);
290 $socket = self::$connections[$connectionId];
292 if (is_null($socket) || feof($socket)) {
293 $this->_debug('opening connection', $connectionId);
295 $socket = @fsockopen($server,$port,$errno, $errstr, $this->timeout);
297 $this->status = -100;
298 $this->error = "Could not connect to $server:$port\n$errstr ($errno)";
303 if ($this->keep_alive) {
304 self::$connections[$connectionId] = $socket;
306 unset(self::$connections[$connectionId]);
312 stream_set_blocking($socket, false);
315 $request = "$method $request_url HTTP/".$this->http.HTTP_NL;
316 $request .= $this->_buildHeaders($headers);
317 $request .= $this->_getCookies();
321 $this->_debug('request',$request);
322 $this->_sendData($socket, $request, 'request');
324 // read headers from socket
327 $r_line = $this->_readLine($socket, 'headers');
328 $r_headers .= $r_line;
329 }while($r_line != "\r\n" && $r_line != "\n");
331 $this->_debug('response headers',$r_headers);
333 // check if expected body size exceeds allowance
334 if($this->max_bodysize && preg_match('/\r?\nContent-Length:\s*(\d+)\r?\n/i',$r_headers,$match)){
335 if($match[1] > $this->max_bodysize){
336 if ($this->max_bodysize_abort)
337 throw new HTTPClientException('Reported content length exceeds allowed response size');
339 $this->error = 'Reported content length exceeds allowed response size';
344 if (!preg_match('/^HTTP\/(\d\.\d)\s*(\d+).*?\n/', $r_headers, $m))
345 throw new HTTPClientException('Server returned bad answer');
347 $this->status = $m[2];
349 // handle headers and cookies
350 $this->resp_headers = $this->_parseHeaders($r_headers);
351 if(isset($this->resp_headers['set-cookie'])){
352 foreach ((array) $this->resp_headers['set-cookie'] as $cookie){
353 list($cookie) = explode(';',$cookie,2);
354 list($key,$val) = explode('=',$cookie,2);
356 if($val == 'deleted'){
357 if(isset($this->cookies[$key])){
358 unset($this->cookies[$key]);
361 $this->cookies[$key] = $val;
366 $this->_debug('Object headers',$this->resp_headers);
368 // check server status code to follow redirect
369 if($this->status == 301 || $this->status == 302 ){
370 if (empty($this->resp_headers['location'])){
371 throw new HTTPClientException('Redirect but no Location Header found');
372 }elseif($this->redirect_count == $this->max_redirect){
373 throw new HTTPClientException('Maximum number of redirects exceeded');
375 // close the connection because we don't handle content retrieval here
376 // that's the easiest way to clean up the connection
378 unset(self::$connections[$connectionId]);
380 $this->redirect_count++;
381 $this->referer = $url;
382 // handle non-RFC-compliant relative redirects
383 if (!preg_match('/^http/i', $this->resp_headers['location'])){
384 if($this->resp_headers['location'][0] != '/'){
385 $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port'].
386 dirname($uri['path']).'/'.$this->resp_headers['location'];
388 $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port'].
389 $this->resp_headers['location'];
392 // perform redirected request, always via GET (required by RFC)
393 return $this->sendRequest($this->resp_headers['location'],array(),'GET');
397 // check if headers are as expected
398 if($this->header_regexp && !preg_match($this->header_regexp,$r_headers))
399 throw new HTTPClientException('The received headers did not match the given regexp');
401 //read body (with chunked encoding if needed)
403 if((isset($this->resp_headers['transfer-encoding']) && $this->resp_headers['transfer-encoding'] == 'chunked')
404 || (isset($this->resp_headers['transfer-coding']) && $this->resp_headers['transfer-coding'] == 'chunked')){
408 while (preg_match('/^[a-zA-Z0-9]?$/',$byte=$this->_readData($socket,1,'chunk'))){
409 // read chunksize until \r
410 $chunk_size .= $byte;
411 if (strlen($chunk_size) > 128) // set an abritrary limit on the size of chunks
412 throw new HTTPClientException('Allowed response size exceeded');
414 $this->_readLine($socket, 'chunk'); // readtrailing \n
415 $chunk_size = hexdec($chunk_size);
417 if($this->max_bodysize && $chunk_size+strlen($r_body) > $this->max_bodysize){
418 if ($this->max_bodysize_abort)
419 throw new HTTPClientException('Allowed response size exceeded');
420 $this->error = 'Allowed response size exceeded';
421 $chunk_size = $this->max_bodysize - strlen($r_body);
425 if ($chunk_size > 0) {
426 $r_body .= $this->_readData($socket, $chunk_size, 'chunk');
427 $byte = $this->_readData($socket, 2, 'chunk'); // read trailing \r\n
429 } while ($chunk_size && !$abort);
430 }elseif($this->max_bodysize){
431 // read just over the max_bodysize
432 $r_body = $this->_readData($socket, $this->max_bodysize+1, 'response', true);
433 if(strlen($r_body) > $this->max_bodysize){
434 if ($this->max_bodysize_abort) {
435 throw new HTTPClientException('Allowed response size exceeded');
437 $this->error = 'Allowed response size exceeded';
440 }elseif(isset($this->resp_headers['content-length']) &&
441 !isset($this->resp_headers['transfer-encoding'])){
442 // read up to the content-length
443 $r_body = $this->_readData($socket, $this->resp_headers['content-length'], 'response', true);
445 // read entire socket
447 while (!feof($socket)) {
448 $r_body .= $this->_readData($socket, 4096, 'response', true);
452 } catch (HTTPClientException $err) {
453 $this->error = $err->getMessage();
455 $this->status = $err->getCode();
456 unset(self::$connections[$connectionId]);
461 if (!$this->keep_alive ||
462 (isset($this->resp_headers['connection']) && $this->resp_headers['connection'] == 'Close')) {
464 $status = socket_get_status($socket);
466 unset(self::$connections[$connectionId]);
469 // decode gzip if needed
470 if(isset($this->resp_headers['content-encoding']) &&
471 $this->resp_headers['content-encoding'] == 'gzip' &&
472 strlen($r_body) > 10 && substr($r_body,0,3)=="\x1f\x8b\x08"){
473 $this->resp_body = @gzinflate(substr($r_body, 10));
474 if($this->resp_body === false){
475 $this->error = 'Failed to decompress gzip encoded content';
476 $this->resp_body = $r_body;
479 $this->resp_body = $r_body;
482 $this->_debug('response body',$this->resp_body);
483 $this->redirect_count = 0;
488 * Safely write data to a socket
490 * @param handle $socket An open socket handle
491 * @param string $data The data to write
492 * @param string $message Description of what is being read
493 * @author Tom N Harris <tnharris@whoopdedo.org>
495 function _sendData($socket, $data, $message) {
498 $sel_w = array($socket);
502 $towrite = strlen($data);
504 while($written < $towrite){
506 $time_used = $this->_time() - $this->start;
507 if($time_used > $this->timeout)
508 throw new HTTPClientException(sprintf('Timeout while sending %s (%.3fs)',$message, $time_used), -100);
510 throw new HTTPClientException("Socket disconnected while writing $message");
512 // wait for stream ready or timeout
513 self::selecttimeout($this->timeout - $time_used, $sec, $usec);
514 if(@stream_select($sel_r, $sel_w, $sel_e, $sec, $usec) !== false){
516 $nbytes = fwrite($socket, substr($data,$written,4096));
517 if($nbytes === false)
518 throw new HTTPClientException("Failed writing to socket while sending $message", -100);
525 * Safely read data from a socket
527 * Reads up to a given number of bytes or throws an exception if the
528 * response times out or ends prematurely.
530 * @param handle $socket An open socket handle in non-blocking mode
531 * @param int $nbytes Number of bytes to read
532 * @param string $message Description of what is being read
533 * @param bool $ignore_eof End-of-file is not an error if this is set
534 * @author Tom N Harris <tnharris@whoopdedo.org>
536 function _readData($socket, $nbytes, $message, $ignore_eof = false) {
538 $sel_r = array($socket);
543 // Does not return immediately so timeout and eof can be checked
544 if ($nbytes < 0) $nbytes = 0;
547 $time_used = $this->_time() - $this->start;
548 if ($time_used > $this->timeout)
549 throw new HTTPClientException(
550 sprintf('Timeout while reading %s (%.3fs)', $message, $time_used),
554 throw new HTTPClientException("Premature End of File (socket) while reading $message");
559 // wait for stream ready or timeout
560 self::selecttimeout($this->timeout - $time_used, $sec, $usec);
561 if(@stream_select($sel_r, $sel_w, $sel_e, $sec, $usec) !== false){
562 $bytes = fread($socket, $to_read);
564 throw new HTTPClientException("Failed reading from socket while reading $message", -100);
566 $to_read -= strlen($bytes);
569 } while ($to_read > 0 && strlen($r_data) < $nbytes);
574 * Safely read a \n-terminated line from a socket
576 * Always returns a complete line, including the terminating \n.
578 * @param handle $socket An open socket handle in non-blocking mode
579 * @param string $message Description of what is being read
580 * @author Tom N Harris <tnharris@whoopdedo.org>
582 function _readLine($socket, $message) {
584 $sel_r = array($socket);
590 $time_used = $this->_time() - $this->start;
591 if ($time_used > $this->timeout)
592 throw new HTTPClientException(
593 sprintf('Timeout while reading %s (%.3fs)', $message, $time_used),
596 throw new HTTPClientException("Premature End of File (socket) while reading $message");
598 // wait for stream ready or timeout
599 self::selecttimeout($this->timeout - $time_used, $sec, $usec);
600 if(@stream_select($sel_r, $sel_w, $sel_e, $sec, $usec) !== false){
601 $r_data = fgets($socket, 1024);
603 } while (!preg_match('/\n$/',$r_data));
610 * @author Andreas Gohr <andi@splitbrain.org>
612 function _debug($info,$var=null){
613 if(!$this->debug) return;
614 print '<b>'.$info.'</b> '.($this->_time() - $this->start).'s<br />';
618 $content = htmlspecialchars(ob_get_contents());
620 print '<pre>'.$content.'</pre>';
625 * Return current timestamp in microsecond resolution
627 static function _time(){
628 list($usec, $sec) = explode(" ", microtime());
629 return ((float)$usec + (float)$sec);
633 * Calculate seconds and microseconds
635 static function selecttimeout($time, &$sec, &$usec){
637 $usec = (int)(($time - $sec) * 1000000);
641 * convert given header string to Header array
643 * All Keys are lowercased.
645 * @author Andreas Gohr <andi@splitbrain.org>
647 function _parseHeaders($string){
649 $lines = explode("\n",$string);
650 array_shift($lines); //skip first line (status)
651 foreach($lines as $line){
652 @list($key, $val) = explode(':',$line,2);
655 $key = strtolower($key);
657 if(isset($headers[$key])){
658 if(is_array($headers[$key])){
659 $headers[$key][] = $val;
661 $headers[$key] = array($headers[$key],$val);
664 $headers[$key] = $val;
671 * convert given header array to header string
673 * @author Andreas Gohr <andi@splitbrain.org>
675 function _buildHeaders($headers){
677 foreach($headers as $key => $value){
678 if(empty($value)) continue;
679 $string .= $key.': '.$value.HTTP_NL;
685 * get cookies as http header string
687 * @author Andreas Goetz <cpuidle@gmx.de>
689 function _getCookies(){
691 foreach ($this->cookies as $key => $val){
692 $headers .= "$key=$val; ";
694 $headers = substr($headers, 0, -2);
695 if ($headers !== '') $headers = "Cookie: $headers".HTTP_NL;
700 * Encode data for posting
702 * @author Andreas Gohr <andi@splitbrain.org>
704 function _postEncode($data){
706 foreach($data as $key => $val){
707 if($url) $url .= '&';
708 $url .= urlencode($key).'='.urlencode($val);
714 * Encode data for posting using multipart encoding
716 * @fixme use of urlencode might be wrong here
717 * @author Andreas Gohr <andi@splitbrain.org>
719 function _postMultipartEncode($data){
720 $boundary = '--'.$this->boundary;
722 foreach($data as $key => $val){
723 $out .= $boundary.HTTP_NL;
725 $out .= 'Content-Disposition: form-data; name="'.urlencode($key).'"'.HTTP_NL;
726 $out .= HTTP_NL; // end of headers
730 $out .= 'Content-Disposition: form-data; name="'.urlencode($key).'"';
731 if($val['filename']) $out .= '; filename="'.urlencode($val['filename']).'"';
733 if($val['mimetype']) $out .= 'Content-Type: '.$val['mimetype'].HTTP_NL;
734 $out .= HTTP_NL; // end of headers
735 $out .= $val['body'];
739 $out .= "$boundary--".HTTP_NL;
744 * Generates a unique identifier for a connection.
746 * @return string unique identifier
748 function _uniqueConnectionId($server, $port) {
749 return "$server:$port";
753 //Setup VIM: ex: et ts=4 :