fixed some test cases
[dokuwiki/radio.git] / inc / HTTPClient.php
blob578d7e7cd7d895294f29b78a7db0b615862445d1
1 <?php
2 /**
3 * HTTP Client
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Goetz <cpuidle@gmx.de>
7 */
10 define('HTTP_NL',"\r\n");
13 /**
14 * Adds DokuWiki specific configs to the HTTP client
16 * @author Andreas Goetz <cpuidle@gmx.de>
18 class DokuHTTPClient extends HTTPClient {
20 /**
21 * Constructor.
23 * @author Andreas Gohr <andi@splitbrain.org>
25 function DokuHTTPClient(){
26 global $conf;
28 // call parent constructor
29 $this->HTTPClient();
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'];
40 /**
41 * Wraps an event around the parent function
43 * @triggers HTTPCLIENT_REQUEST_SEND
44 * @author Andreas Gohr <andi@splitbrain.org>
46 function sendRequest($url,$data='',$method='GET'){
47 $httpdata = array('url' => $url,
48 'data' => $data,
49 'method' => $method);
50 $evt = new Doku_Event('HTTPCLIENT_REQUEST_SEND',$httpdata);
51 if($evt->advise_before()){
52 $url = $httpdata['url'];
53 $data = $httpdata['data'];
54 $method = $httpdata['method'];
56 $evt->advise_after();
57 unset($evt);
58 return parent::sendRequest($url,$data,$method);
63 /**
64 * This class implements a basic HTTP client
66 * It supports POST and GET, Proxy usage, basic authentication,
67 * handles cookies and referers. It is based upon the httpclient
68 * function from the VideoDB project.
70 * @link http://www.splitbrain.org/go/videodb
71 * @author Andreas Goetz <cpuidle@gmx.de>
72 * @author Andreas Gohr <andi@splitbrain.org>
74 class HTTPClient {
75 //set these if you like
76 var $agent; // User agent
77 var $http; // HTTP version defaults to 1.0
78 var $timeout; // read timeout (seconds)
79 var $cookies;
80 var $referer;
81 var $max_redirect;
82 var $max_bodysize;
83 var $max_bodysize_abort = true; // if set, abort if the response body is bigger than max_bodysize
84 var $header_regexp; // if set this RE must match against the headers, else abort
85 var $headers;
86 var $debug;
87 var $start = 0; // for timings
89 // don't set these, read on error
90 var $error;
91 var $redirect_count;
93 // read these after a successful request
94 var $resp_status;
95 var $resp_body;
96 var $resp_headers;
98 // set these to do basic authentication
99 var $user;
100 var $pass;
102 // set these if you need to use a proxy
103 var $proxy_host;
104 var $proxy_port;
105 var $proxy_user;
106 var $proxy_pass;
107 var $proxy_ssl; //boolean set to true if your proxy needs SSL
109 // what we use as boundary on multipart/form-data posts
110 var $boundary = '---DokuWikiHTTPClient--4523452351';
113 * Constructor.
115 * @author Andreas Gohr <andi@splitbrain.org>
117 function HTTPClient(){
118 $this->agent = 'Mozilla/4.0 (compatible; DokuWiki HTTP Client; '.PHP_OS.')';
119 $this->timeout = 15;
120 $this->cookies = array();
121 $this->referer = '';
122 $this->max_redirect = 3;
123 $this->redirect_count = 0;
124 $this->status = 0;
125 $this->headers = array();
126 $this->http = '1.0';
127 $this->debug = false;
128 $this->max_bodysize = 0;
129 $this->header_regexp= '';
130 if(extension_loaded('zlib')) $this->headers['Accept-encoding'] = 'gzip';
131 $this->headers['Accept'] = 'text/xml,application/xml,application/xhtml+xml,'.
132 'text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
133 $this->headers['Accept-Language'] = 'en-us';
138 * Simple function to do a GET request
140 * Returns the wanted page or false on an error;
142 * @param string $url The URL to fetch
143 * @param bool $sloppy304 Return body on 304 not modified
144 * @author Andreas Gohr <andi@splitbrain.org>
146 function get($url,$sloppy304=false){
147 if(!$this->sendRequest($url)) return false;
148 if($this->status == 304 && $sloppy304) return $this->resp_body;
149 if($this->status < 200 || $this->status > 206) return false;
150 return $this->resp_body;
154 * Simple function to do a POST request
156 * Returns the resulting page or false on an error;
158 * @author Andreas Gohr <andi@splitbrain.org>
160 function post($url,$data){
161 if(!$this->sendRequest($url,$data,'POST')) return false;
162 if($this->status < 200 || $this->status > 206) return false;
163 return $this->resp_body;
167 * Send an HTTP request
169 * This method handles the whole HTTP communication. It respects set proxy settings,
170 * builds the request headers, follows redirects and parses the response.
172 * Post data should be passed as associative array. When passed as string it will be
173 * sent as is. You will need to setup your own Content-Type header then.
175 * @param string $url - the complete URL
176 * @param mixed $data - the post data either as array or raw data
177 * @param string $method - HTTP Method usually GET or POST.
178 * @return bool - true on success
179 * @author Andreas Goetz <cpuidle@gmx.de>
180 * @author Andreas Gohr <andi@splitbrain.org>
182 function sendRequest($url,$data='',$method='GET'){
183 $this->start = $this->_time();
184 $this->error = '';
185 $this->status = 0;
187 // don't accept gzip if truncated bodies might occur
188 if($this->max_bodysize &&
189 !$this->max_bodysize_abort &&
190 $this->headers['Accept-encoding'] == 'gzip'){
191 unset($this->headers['Accept-encoding']);
194 // parse URL into bits
195 $uri = parse_url($url);
196 $server = $uri['host'];
197 $path = $uri['path'];
198 if(empty($path)) $path = '/';
199 if(!empty($uri['query'])) $path .= '?'.$uri['query'];
200 $port = $uri['port'];
201 if(isset($uri['user'])) $this->user = $uri['user'];
202 if(isset($uri['pass'])) $this->pass = $uri['pass'];
204 // proxy setup
205 if($this->proxy_host){
206 $request_url = $url;
207 $server = $this->proxy_host;
208 $port = $this->proxy_port;
209 if (empty($port)) $port = 8080;
210 }else{
211 $request_url = $path;
212 $server = $server;
213 if (empty($port)) $port = ($uri['scheme'] == 'https') ? 443 : 80;
216 // add SSL stream prefix if needed - needs SSL support in PHP
217 if($port == 443 || $this->proxy_ssl) $server = 'ssl://'.$server;
219 // prepare headers
220 $headers = $this->headers;
221 $headers['Host'] = $uri['host'];
222 $headers['User-Agent'] = $this->agent;
223 $headers['Referer'] = $this->referer;
224 $headers['Connection'] = 'Close';
225 if($method == 'POST'){
226 if(is_array($data)){
227 if($headers['Content-Type'] == 'multipart/form-data'){
228 $headers['Content-Type'] = 'multipart/form-data; boundary='.$this->boundary;
229 $data = $this->_postMultipartEncode($data);
230 }else{
231 $headers['Content-Type'] = 'application/x-www-form-urlencoded';
232 $data = $this->_postEncode($data);
235 $headers['Content-Length'] = strlen($data);
236 $rmethod = 'POST';
237 }elseif($method == 'GET'){
238 $data = ''; //no data allowed on GET requests
240 if($this->user) {
241 $headers['Authorization'] = 'Basic '.base64_encode($this->user.':'.$this->pass);
243 if($this->proxy_user) {
244 $headers['Proxy-Authorization'] = 'Basic '.base64_encode($this->proxy_user.':'.$this->proxy_pass);
247 // stop time
248 $start = time();
250 // open socket
251 $socket = @fsockopen($server,$port,$errno, $errstr, $this->timeout);
252 if (!$socket){
253 $resp->status = '-100';
254 $this->error = "Could not connect to $server:$port\n$errstr ($errno)";
255 return false;
257 //set non blocking
258 stream_set_blocking($socket,0);
260 // build request
261 $request = "$method $request_url HTTP/".$this->http.HTTP_NL;
262 $request .= $this->_buildHeaders($headers);
263 $request .= $this->_getCookies();
264 $request .= HTTP_NL;
265 $request .= $data;
267 $this->_debug('request',$request);
269 // send request
270 $towrite = strlen($request);
271 $written = 0;
272 while($written < $towrite){
273 $ret = fwrite($socket, substr($request,$written));
274 if($ret === false){
275 $this->status = -100;
276 $this->error = 'Failed writing to socket';
277 return false;
279 $written += $ret;
283 // read headers from socket
284 $r_headers = '';
286 if(time()-$start > $this->timeout){
287 $this->status = -100;
288 $this->error = sprintf('Timeout while reading headers (%.3fs)',$this->_time() - $this->start);
289 return false;
291 if(feof($socket)){
292 $this->error = 'Premature End of File (socket)';
293 return false;
295 $r_headers .= fgets($socket,1024);
296 }while(!preg_match('/\r?\n\r?\n$/',$r_headers));
298 $this->_debug('response headers',$r_headers);
300 // check if expected body size exceeds allowance
301 if($this->max_bodysize && preg_match('/\r?\nContent-Length:\s*(\d+)\r?\n/i',$r_headers,$match)){
302 if($match[1] > $this->max_bodysize){
303 $this->error = 'Reported content length exceeds allowed response size';
304 if ($this->max_bodysize_abort)
305 return false;
309 // get Status
310 if (!preg_match('/^HTTP\/(\d\.\d)\s*(\d+).*?\n/', $r_headers, $m)) {
311 $this->error = 'Server returned bad answer';
312 return false;
314 $this->status = $m[2];
316 // handle headers and cookies
317 $this->resp_headers = $this->_parseHeaders($r_headers);
318 if(isset($this->resp_headers['set-cookie'])){
319 foreach ((array) $this->resp_headers['set-cookie'] as $cookie){
320 list($cookie) = explode(';',$cookie,2);
321 list($key,$val) = explode('=',$cookie,2);
322 $key = trim($key);
323 if($val == 'deleted'){
324 if(isset($this->cookies[$key])){
325 unset($this->cookies[$key]);
327 }elseif($key){
328 $this->cookies[$key] = $val;
333 $this->_debug('Object headers',$this->resp_headers);
335 // check server status code to follow redirect
336 if($this->status == 301 || $this->status == 302 ){
337 if (empty($this->resp_headers['location'])){
338 $this->error = 'Redirect but no Location Header found';
339 return false;
340 }elseif($this->redirect_count == $this->max_redirect){
341 $this->error = 'Maximum number of redirects exceeded';
342 return false;
343 }else{
344 $this->redirect_count++;
345 $this->referer = $url;
346 // handle non-RFC-compliant relative redirects
347 if (!preg_match('/^http/i', $this->resp_headers['location'])){
348 if($this->resp_headers['location'][0] != '/'){
349 $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port'].
350 dirname($uri['path']).'/'.$this->resp_headers['location'];
351 }else{
352 $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port'].
353 $this->resp_headers['location'];
356 // perform redirected request, always via GET (required by RFC)
357 return $this->sendRequest($this->resp_headers['location'],array(),'GET');
361 // check if headers are as expected
362 if($this->header_regexp && !preg_match($this->header_regexp,$r_headers)){
363 $this->error = 'The received headers did not match the given regexp';
364 return false;
367 //read body (with chunked encoding if needed)
368 $r_body = '';
369 if(preg_match('/transfer\-(en)?coding:\s*chunked\r\n/i',$r_headers)){
370 do {
371 unset($chunk_size);
372 do {
373 if(feof($socket)){
374 $this->error = 'Premature End of File (socket)';
375 return false;
377 if(time()-$start > $this->timeout){
378 $this->status = -100;
379 $this->error = sprintf('Timeout while reading chunk (%.3fs)',$this->_time() - $this->start);
380 return false;
382 $byte = fread($socket,1);
383 $chunk_size .= $byte;
384 } while (preg_match('/[a-zA-Z0-9]/',$byte)); // read chunksize including \r
386 $byte = fread($socket,1); // readtrailing \n
387 $chunk_size = hexdec($chunk_size);
388 if ($chunk_size) {
389 $this_chunk = fread($socket,$chunk_size);
390 $r_body .= $this_chunk;
391 $byte = fread($socket,2); // read trailing \r\n
394 if($this->max_bodysize && strlen($r_body) > $this->max_bodysize){
395 $this->error = 'Allowed response size exceeded';
396 if ($this->max_bodysize_abort)
397 return false;
398 else
399 break;
401 } while ($chunk_size);
402 }else{
403 // read entire socket
404 while (!feof($socket)) {
405 if(time()-$start > $this->timeout){
406 $this->status = -100;
407 $this->error = sprintf('Timeout while reading response (%.3fs)',$this->_time() - $this->start);
408 return false;
410 $r_body .= fread($socket,4096);
411 $r_size = strlen($r_body);
412 if($this->max_bodysize && $r_size > $this->max_bodysize){
413 $this->error = 'Allowed response size exceeded';
414 if ($this->max_bodysize_abort)
415 return false;
416 else
417 break;
419 if(isset($this->resp_headers['content-length']) &&
420 !isset($this->resp_headers['transfer-encoding']) &&
421 $this->resp_headers['content-length'] == $r_size){
422 // we read the content-length, finish here
423 break;
428 // close socket
429 $status = socket_get_status($socket);
430 fclose($socket);
432 // decode gzip if needed
433 if(isset($this->resp_headers['content-encoding']) &&
434 $this->resp_headers['content-encoding'] == 'gzip' &&
435 strlen($r_body) > 10 && substr($r_body,0,3)=="\x1f\x8b\x08"){
436 $this->resp_body = @gzinflate(substr($r_body, 10));
437 }else{
438 $this->resp_body = $r_body;
441 $this->_debug('response body',$this->resp_body);
442 $this->redirect_count = 0;
443 return true;
447 * print debug info
449 * @author Andreas Gohr <andi@splitbrain.org>
451 function _debug($info,$var=null){
452 if(!$this->debug) return;
453 print '<b>'.$info.'</b> '.($this->_time() - $this->start).'s<br />';
454 if(!is_null($var)){
455 ob_start();
456 print_r($var);
457 $content = htmlspecialchars(ob_get_contents());
458 ob_end_clean();
459 print '<pre>'.$content.'</pre>';
464 * Return current timestamp in microsecond resolution
466 function _time(){
467 list($usec, $sec) = explode(" ", microtime());
468 return ((float)$usec + (float)$sec);
472 * convert given header string to Header array
474 * All Keys are lowercased.
476 * @author Andreas Gohr <andi@splitbrain.org>
478 function _parseHeaders($string){
479 $headers = array();
480 $lines = explode("\n",$string);
481 foreach($lines as $line){
482 list($key,$val) = explode(':',$line,2);
483 $key = strtolower(trim($key));
484 $val = trim($val);
485 if(empty($val)) continue;
486 if(isset($headers[$key])){
487 if(is_array($headers[$key])){
488 $headers[$key][] = $val;
489 }else{
490 $headers[$key] = array($headers[$key],$val);
492 }else{
493 $headers[$key] = $val;
496 return $headers;
500 * convert given header array to header string
502 * @author Andreas Gohr <andi@splitbrain.org>
504 function _buildHeaders($headers){
505 $string = '';
506 foreach($headers as $key => $value){
507 if(empty($value)) continue;
508 $string .= $key.': '.$value.HTTP_NL;
510 return $string;
514 * get cookies as http header string
516 * @author Andreas Goetz <cpuidle@gmx.de>
518 function _getCookies(){
519 $headers = '';
520 foreach ($this->cookies as $key => $val){
521 $headers .= "$key=$val; ";
523 $headers = substr($headers, 0, -2);
524 if ($headers !== '') $headers = "Cookie: $headers".HTTP_NL;
525 return $headers;
529 * Encode data for posting
531 * @author Andreas Gohr <andi@splitbrain.org>
533 function _postEncode($data){
534 foreach($data as $key => $val){
535 if($url) $url .= '&';
536 $url .= urlencode($key).'='.urlencode($val);
538 return $url;
542 * Encode data for posting using multipart encoding
544 * @fixme use of urlencode might be wrong here
545 * @author Andreas Gohr <andi@splitbrain.org>
547 function _postMultipartEncode($data){
548 $boundary = '--'.$this->boundary;
549 $out = '';
550 foreach($data as $key => $val){
551 $out .= $boundary.HTTP_NL;
552 if(!is_array($val)){
553 $out .= 'Content-Disposition: form-data; name="'.urlencode($key).'"'.HTTP_NL;
554 $out .= HTTP_NL; // end of headers
555 $out .= $val;
556 $out .= HTTP_NL;
557 }else{
558 $out .= 'Content-Disposition: form-data; name="'.urlencode($key).'"';
559 if($val['filename']) $out .= '; filename="'.urlencode($val['filename']).'"';
560 $out .= HTTP_NL;
561 if($val['mimetype']) $out .= 'Content-Type: '.$val['mimetype'].HTTP_NL;
562 $out .= HTTP_NL; // end of headers
563 $out .= $val['body'];
564 $out .= HTTP_NL;
567 $out .= "$boundary--".HTTP_NL;
568 return $out;
573 //Setup VIM: ex: et ts=4 enc=utf-8 :