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