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 DokuHTTPClient(){
28 // call parent constructor
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);
65 * This class implements a basic HTTP client
67 * It supports POST and GET, Proxy usage, basic authentication,
68 * handles cookies and referers. It is based upon the httpclient
69 * function from the VideoDB project.
71 * @link http://www.splitbrain.org/go/videodb
72 * @author Andreas Goetz <cpuidle@gmx.de>
73 * @author Andreas Gohr <andi@splitbrain.org>
74 * @author Tobias Sarnowski <sarnowski@new-thoughts.org>
77 //set these if you like
78 var $agent; // User agent
79 var $http; // HTTP version defaults to 1.0
80 var $timeout; // read timeout (seconds)
85 var $max_bodysize_abort = true; // if set, abort if the response body is bigger than max_bodysize
86 var $header_regexp; // if set this RE must match against the headers, else abort
89 var $start = 0; // for timings
90 var $keep_alive = true; // keep alive rocks
92 // don't set these, read on error
96 // read these after a successful request
101 // set these to do basic authentication
105 // set these if you need to use a proxy
110 var $proxy_ssl; //boolean set to true if your proxy needs SSL
111 var $proxy_except; // regexp of URLs to exclude from proxy
113 // list of kept alive connections
114 static $connections = array();
116 // what we use as boundary on multipart/form-data posts
117 var $boundary = '---DokuWikiHTTPClient--4523452351';
122 * @author Andreas Gohr <andi@splitbrain.org>
124 function HTTPClient(){
125 $this->agent = 'Mozilla/4.0 (compatible; DokuWiki HTTP Client; '.PHP_OS.')';
127 $this->cookies = array();
129 $this->max_redirect = 3;
130 $this->redirect_count = 0;
132 $this->headers = array();
134 $this->debug = false;
135 $this->max_bodysize = 0;
136 $this->header_regexp= '';
137 if(extension_loaded('zlib')) $this->headers['Accept-encoding'] = 'gzip';
138 $this->headers['Accept'] = 'text/xml,application/xml,application/xhtml+xml,'.
139 'text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
140 $this->headers['Accept-Language'] = 'en-us';
145 * Simple function to do a GET request
147 * Returns the wanted page or false on an error;
149 * @param string $url The URL to fetch
150 * @param bool $sloppy304 Return body on 304 not modified
151 * @author Andreas Gohr <andi@splitbrain.org>
153 function get($url,$sloppy304=false){
154 if(!$this->sendRequest($url)) return false;
155 if($this->status == 304 && $sloppy304) return $this->resp_body;
156 if($this->status < 200 || $this->status > 206) return false;
157 return $this->resp_body;
161 * Simple function to do a GET request with given parameters
163 * Returns the wanted page or false on an error.
165 * This is a convenience wrapper around get(). The given parameters
166 * will be correctly encoded and added to the given base URL.
168 * @param string $url The URL to fetch
169 * @param array $data Associative array of parameters
170 * @param bool $sloppy304 Return body on 304 not modified
171 * @author Andreas Gohr <andi@splitbrain.org>
173 function dget($url,$data,$sloppy304=false){
174 if(strpos($url,'?')){
179 $url .= $this->_postEncode($data);
180 return $this->get($url,$sloppy304);
184 * Simple function to do a POST request
186 * Returns the resulting page or false on an error;
188 * @author Andreas Gohr <andi@splitbrain.org>
190 function post($url,$data){
191 if(!$this->sendRequest($url,$data,'POST')) return false;
192 if($this->status < 200 || $this->status > 206) return false;
193 return $this->resp_body;
197 * Send an HTTP request
199 * This method handles the whole HTTP communication. It respects set proxy settings,
200 * builds the request headers, follows redirects and parses the response.
202 * Post data should be passed as associative array. When passed as string it will be
203 * sent as is. You will need to setup your own Content-Type header then.
205 * @param string $url - the complete URL
206 * @param mixed $data - the post data either as array or raw data
207 * @param string $method - HTTP Method usually GET or POST.
208 * @return bool - true on success
209 * @author Andreas Goetz <cpuidle@gmx.de>
210 * @author Andreas Gohr <andi@splitbrain.org>
212 function sendRequest($url,$data='',$method='GET'){
213 $this->start = $this->_time();
217 // don't accept gzip if truncated bodies might occur
218 if($this->max_bodysize &&
219 !$this->max_bodysize_abort &&
220 $this->headers['Accept-encoding'] == 'gzip'){
221 unset($this->headers['Accept-encoding']);
224 // parse URL into bits
225 $uri = parse_url($url);
226 $server = $uri['host'];
227 $path = $uri['path'];
228 if(empty($path)) $path = '/';
229 if(!empty($uri['query'])) $path .= '?'.$uri['query'];
230 if(isset($uri['port']) && !empty($uri['port'])) $port = $uri['port'];
231 if(isset($uri['user'])) $this->user = $uri['user'];
232 if(isset($uri['pass'])) $this->pass = $uri['pass'];
235 if($this->proxy_host && (!$this->proxy_except || !preg_match('/'.$this->proxy_except.'/i',$url)) ){
237 $server = $this->proxy_host;
238 $port = $this->proxy_port;
239 if (empty($port)) $port = 8080;
241 $request_url = $path;
243 if (!isset($port)) $port = ($uri['scheme'] == 'https') ? 443 : 80;
246 // add SSL stream prefix if needed - needs SSL support in PHP
247 if($port == 443 || $this->proxy_ssl) $server = 'ssl://'.$server;
250 $headers = $this->headers;
251 $headers['Host'] = $uri['host'];
252 if($uri['port']) $headers['Host'].= ':'.$uri['port'];
253 $headers['User-Agent'] = $this->agent;
254 $headers['Referer'] = $this->referer;
255 if ($this->keep_alive) {
256 $headers['Connection'] = 'Keep-Alive';
258 $headers['Connection'] = 'Close';
260 if($method == 'POST'){
262 if($headers['Content-Type'] == 'multipart/form-data'){
263 $headers['Content-Type'] = 'multipart/form-data; boundary='.$this->boundary;
264 $data = $this->_postMultipartEncode($data);
266 $headers['Content-Type'] = 'application/x-www-form-urlencoded';
267 $data = $this->_postEncode($data);
270 $headers['Content-Length'] = strlen($data);
272 }elseif($method == 'GET'){
273 $data = ''; //no data allowed on GET requests
276 $headers['Authorization'] = 'Basic '.base64_encode($this->user.':'.$this->pass);
278 if($this->proxy_user) {
279 $headers['Proxy-Authorization'] = 'Basic '.base64_encode($this->proxy_user.':'.$this->proxy_pass);
285 // already connected?
286 $connectionId = $this->_uniqueConnectionId($server,$port);
287 $this->_debug('connection pool', $this->connections);
289 if (isset($this->connections[$connectionId])) {
290 $this->_debug('reusing connection', $connectionId);
291 $socket = $this->connections[$connectionId];
293 if (is_null($socket) || feof($socket)) {
294 $this->_debug('opening connection', $connectionId);
296 $socket = @fsockopen($server,$port,$errno, $errstr, $this->timeout);
298 $this->status = -100;
299 $this->error = "Could not connect to $server:$port\n$errstr ($errno)";
304 if ($this->keep_alive) {
305 $this->connections[$connectionId] = $socket;
307 unset($this->connections[$connectionId]);
312 stream_set_blocking($socket,1);
315 $request = "$method $request_url HTTP/".$this->http.HTTP_NL;
316 $request .= $this->_buildHeaders($headers);
317 $request .= $this->_getCookies();
321 $this->_debug('request',$request);
325 $sel_w = array($socket);
329 $towrite = strlen($request);
331 while($written < $towrite){
333 if(time()-$start > $this->timeout){
334 $this->status = -100;
335 $this->error = sprintf('Timeout while sending request (%.3fs)',$this->_time() - $this->start);
336 unset($this->connections[$connectionId]);
340 // wait for stream ready or timeout (1sec)
341 if(@stream_select($sel_r,$sel_w,$sel_e,1) === false){
347 $ret = fwrite($socket, substr($request,$written,4096));
349 $this->status = -100;
350 $this->error = 'Failed writing to socket';
351 unset($this->connections[$connectionId]);
357 // continue non-blocking
358 stream_set_blocking($socket,0);
360 // read headers from socket
363 if(time()-$start > $this->timeout){
364 $this->status = -100;
365 $this->error = sprintf('Timeout while reading headers (%.3fs)',$this->_time() - $this->start);
366 unset($this->connections[$connectionId]);
370 $this->error = 'Premature End of File (socket)';
371 unset($this->connections[$connectionId]);
375 $r_headers .= fgets($socket,1024);
376 }while(!preg_match('/\r?\n\r?\n$/',$r_headers));
378 $this->_debug('response headers',$r_headers);
380 // check if expected body size exceeds allowance
381 if($this->max_bodysize && preg_match('/\r?\nContent-Length:\s*(\d+)\r?\n/i',$r_headers,$match)){
382 if($match[1] > $this->max_bodysize){
383 $this->error = 'Reported content length exceeds allowed response size';
384 if ($this->max_bodysize_abort)
385 unset($this->connections[$connectionId]);
391 if (!preg_match('/^HTTP\/(\d\.\d)\s*(\d+).*?\n/', $r_headers, $m)) {
392 $this->error = 'Server returned bad answer';
393 unset($this->connections[$connectionId]);
396 $this->status = $m[2];
398 // handle headers and cookies
399 $this->resp_headers = $this->_parseHeaders($r_headers);
400 if(isset($this->resp_headers['set-cookie'])){
401 foreach ((array) $this->resp_headers['set-cookie'] as $cookie){
402 list($cookie) = explode(';',$cookie,2);
403 list($key,$val) = explode('=',$cookie,2);
405 if($val == 'deleted'){
406 if(isset($this->cookies[$key])){
407 unset($this->cookies[$key]);
410 $this->cookies[$key] = $val;
415 $this->_debug('Object headers',$this->resp_headers);
417 // check server status code to follow redirect
418 if($this->status == 301 || $this->status == 302 ){
419 // close the connection because we don't handle content retrieval here
420 // that's the easiest way to clean up the connection
422 unset($this->connections[$connectionId]);
424 if (empty($this->resp_headers['location'])){
425 $this->error = 'Redirect but no Location Header found';
427 }elseif($this->redirect_count == $this->max_redirect){
428 $this->error = 'Maximum number of redirects exceeded';
431 $this->redirect_count++;
432 $this->referer = $url;
433 // handle non-RFC-compliant relative redirects
434 if (!preg_match('/^http/i', $this->resp_headers['location'])){
435 if($this->resp_headers['location'][0] != '/'){
436 $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port'].
437 dirname($uri['path']).'/'.$this->resp_headers['location'];
439 $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port'].
440 $this->resp_headers['location'];
443 // perform redirected request, always via GET (required by RFC)
444 return $this->sendRequest($this->resp_headers['location'],array(),'GET');
448 // check if headers are as expected
449 if($this->header_regexp && !preg_match($this->header_regexp,$r_headers)){
450 $this->error = 'The received headers did not match the given regexp';
451 unset($this->connections[$connectionId]);
455 //read body (with chunked encoding if needed)
457 if(preg_match('/transfer\-(en)?coding:\s*chunked\r\n/i',$r_headers)){
462 $this->error = 'Premature End of File (socket)';
463 unset($this->connections[$connectionId]);
466 if(time()-$start > $this->timeout){
467 $this->status = -100;
468 $this->error = sprintf('Timeout while reading chunk (%.3fs)',$this->_time() - $this->start);
469 unset($this->connections[$connectionId]);
472 $byte = fread($socket,1);
473 $chunk_size .= $byte;
474 } while (preg_match('/[a-zA-Z0-9]/',$byte)); // read chunksize including \r
476 $byte = fread($socket,1); // readtrailing \n
477 $chunk_size = hexdec($chunk_size);
479 $this_chunk = fread($socket,$chunk_size);
480 $r_body .= $this_chunk;
481 $byte = fread($socket,2); // read trailing \r\n
484 if($this->max_bodysize && strlen($r_body) > $this->max_bodysize){
485 $this->error = 'Allowed response size exceeded';
486 if ($this->max_bodysize_abort){
487 unset($this->connections[$connectionId]);
493 } while ($chunk_size);
495 // read entire socket
496 while (!feof($socket)) {
497 if(time()-$start > $this->timeout){
498 $this->status = -100;
499 $this->error = sprintf('Timeout while reading response (%.3fs)',$this->_time() - $this->start);
500 unset($this->connections[$connectionId]);
503 $r_body .= fread($socket,4096);
504 $r_size = strlen($r_body);
505 if($this->max_bodysize && $r_size > $this->max_bodysize){
506 $this->error = 'Allowed response size exceeded';
507 if ($this->max_bodysize_abort) {
508 unset($this->connections[$connectionId]);
514 if(isset($this->resp_headers['content-length']) &&
515 !isset($this->resp_headers['transfer-encoding']) &&
516 $this->resp_headers['content-length'] == $r_size){
517 // we read the content-length, finish here
523 if (!$this->keep_alive ||
524 (isset($this->resp_headers['connection']) && $this->resp_headers['connection'] == 'Close')) {
526 $status = socket_get_status($socket);
528 unset($this->connections[$connectionId]);
531 // decode gzip if needed
532 if(isset($this->resp_headers['content-encoding']) &&
533 $this->resp_headers['content-encoding'] == 'gzip' &&
534 strlen($r_body) > 10 && substr($r_body,0,3)=="\x1f\x8b\x08"){
535 $this->resp_body = @gzinflate(substr($r_body, 10));
536 if($this->resp_body === false){
537 $this->error = 'Failed to decompress gzip encoded content';
538 $this->resp_body = $r_body;
541 $this->resp_body = $r_body;
544 $this->_debug('response body',$this->resp_body);
545 $this->redirect_count = 0;
552 * @author Andreas Gohr <andi@splitbrain.org>
554 function _debug($info,$var=null){
555 if(!$this->debug) return;
556 print '<b>'.$info.'</b> '.($this->_time() - $this->start).'s<br />';
560 $content = htmlspecialchars(ob_get_contents());
562 print '<pre>'.$content.'</pre>';
567 * Return current timestamp in microsecond resolution
570 list($usec, $sec) = explode(" ", microtime());
571 return ((float)$usec + (float)$sec);
575 * convert given header string to Header array
577 * All Keys are lowercased.
579 * @author Andreas Gohr <andi@splitbrain.org>
581 function _parseHeaders($string){
583 if (!preg_match_all('/^\s*([\w-]+)\s*:\s*([\S \t]+)\s*$/m', $string,
584 $matches, PREG_SET_ORDER)) {
587 foreach($matches as $match){
588 list(, $key, $val) = $match;
589 $key = strtolower($key);
590 if(isset($headers[$key])){
591 if(is_array($headers[$key])){
592 $headers[$key][] = $val;
594 $headers[$key] = array($headers[$key],$val);
597 $headers[$key] = $val;
604 * convert given header array to header string
606 * @author Andreas Gohr <andi@splitbrain.org>
608 function _buildHeaders($headers){
610 foreach($headers as $key => $value){
611 if(empty($value)) continue;
612 $string .= $key.': '.$value.HTTP_NL;
618 * get cookies as http header string
620 * @author Andreas Goetz <cpuidle@gmx.de>
622 function _getCookies(){
624 foreach ($this->cookies as $key => $val){
625 $headers .= "$key=$val; ";
627 $headers = substr($headers, 0, -2);
628 if ($headers !== '') $headers = "Cookie: $headers".HTTP_NL;
633 * Encode data for posting
635 * @author Andreas Gohr <andi@splitbrain.org>
637 function _postEncode($data){
639 foreach($data as $key => $val){
640 if($url) $url .= '&';
641 $url .= urlencode($key).'='.urlencode($val);
647 * Encode data for posting using multipart encoding
649 * @fixme use of urlencode might be wrong here
650 * @author Andreas Gohr <andi@splitbrain.org>
652 function _postMultipartEncode($data){
653 $boundary = '--'.$this->boundary;
655 foreach($data as $key => $val){
656 $out .= $boundary.HTTP_NL;
658 $out .= 'Content-Disposition: form-data; name="'.urlencode($key).'"'.HTTP_NL;
659 $out .= HTTP_NL; // end of headers
663 $out .= 'Content-Disposition: form-data; name="'.urlencode($key).'"';
664 if($val['filename']) $out .= '; filename="'.urlencode($val['filename']).'"';
666 if($val['mimetype']) $out .= 'Content-Type: '.$val['mimetype'].HTTP_NL;
667 $out .= HTTP_NL; // end of headers
668 $out .= $val['body'];
672 $out .= "$boundary--".HTTP_NL;
677 * Generates a unique identifier for a connection.
679 * @return string unique identifier
681 function _uniqueConnectionId($server, $port) {
682 return "$server:$port";
686 //Setup VIM: ex: et ts=4 :