]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/HttpClient.php
Debugging: Warn about empty response
[SourceForge/phpwiki.git] / lib / HttpClient.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3
4 /** 
5    Version 0.9, 6th April 2003 - Simon Willison ( http://simon.incutio.com/ )
6    Manual: http://scripts.incutio.com/httpclient/
7
8    Copyright © 2003 Incutio Limited
9    License: http://www.opensource.org/licenses/artistic-license.php
10
11    File upload and xmlrpc support by Reini Urban for PhpWiki, 2006-12-28 18:12:47 
12    Todo: proxy support
13 */
14
15 class HttpClient {
16     // Request vars
17     var $host;
18     var $port;
19     var $path;
20     var $method;
21     var $postdata = '';
22     var $cookies = array();
23     var $referer;
24     var $accept = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
25     var $accept_encoding = 'gzip';
26     var $accept_language = 'en-us';
27     var $user_agent = 'Incutio HttpClient v1.0';
28     var $boundary = "xYzZY"; // FIXME: check if this string doesn't occur in the data
29     // Options
30     var $timeout = 10;
31     var $use_gzip = true;
32     var $persist_cookies = true;  // If true, received cookies are placed in the $this->cookies array ready for the next request
33                                   // Note: This currently ignores the cookie path (and time) completely. Time is not important, 
34                                   //       but path could possibly lead to security problems.
35     var $persist_referers = true; // For each request, sends path of last request as referer
36     var $debug = false;
37     var $handle_redirects = true; // Auaomtically redirect if Location or URI header is found
38     var $max_redirects = 5;
39     var $headers_only = false;    // If true, stops receiving once headers have been read.
40     // Basic authorization variables
41     var $username;
42     var $password;
43     // Response vars
44     var $status;
45     var $headers = array();
46     var $content = '';
47     var $errormsg;
48     // Tracker variables
49     var $redirect_count = 0;
50     var $cookie_host = '';
51
52     function HttpClient($host='localhost', $port=80) {
53         $this->host = $host;
54         $this->port = $port;
55     }
56     function get($path, $data = false) {
57         $this->path = $path;
58         $this->method = 'GET';
59         if ($data) {
60             $this->path .= '?'.$this->buildQueryString($data);
61         }
62         return $this->doRequest();
63     }
64     function post($path, $data) {
65         $this->path = $path;
66         $this->method = 'POST';
67         $this->postdata = $this->buildQueryString($data);
68         return $this->doRequest();
69     }
70     function postfile($path, $filename) {
71         $this->path = $path;
72         $this->method = 'POST';
73         $boundary = $this->boundary; //"httpclient_boundary";
74         $headers[] = "Content-Type: multipart/form-data; boundary=\"$boundary\"";
75         $basename = basename($filename); 
76         $this->postdata =
77             "\r\n--$boundary\r\n"
78             ."Content-Disposition: form-data; filename=\"$basename\"\r\n"
79             ."Content-Type: application/octet-stream\r\n\r\n";
80         $this->postdata .= join("",file($filename));
81         $this->postdata .= "\r\n\r\n--$boundary--\r\n";
82         return $this->doRequest();
83     }
84     function buildQueryString($data) {
85         $querystring = '';
86         if (is_array($data)) {
87             // Change data in to postable data
88             foreach ($data as $key => $val) {
89                 if (is_array($val)) {
90                     foreach ($val as $val2) {
91                         $querystring .= urlencode($key).'='.urlencode($val2).'&';
92                     }
93                 } else {
94                     $querystring .= urlencode($key).'='.urlencode($val).'&';
95                 }
96             }
97             $querystring = substr($querystring, 0, -1); // Eliminate unnecessary &
98         } else {
99             $querystring = $data;
100         }
101         return $querystring;
102     }
103
104     function doRequest() {
105         // Performs the actual HTTP request, returning true or false depending on outcome
106         // Ensure that the PHP timeout is longer than the socket timeout
107         longer_timeout($this->timeout);
108         if (!$fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout)) {
109             // Set error message
110             switch($errno) {
111             case -3:
112                 $this->errormsg = 'Socket creation failed (-3)';
113             case -4:
114                 $this->errormsg = 'DNS lookup failure (-4)';
115             case -5:
116                 $this->errormsg = 'Connection refused or timed out (-5)';
117             default:
118                 $this->errormsg = 'Connection failed ('.$errno.')';
119                 $this->errormsg .= ' '.$errstr;
120                 $this->debug($this->errormsg);
121             }
122             return false;
123         }
124         if (check_php_version(4,3,0))
125             socket_set_timeout($fp, $this->timeout);
126         if ( $this->method == 'POST' and preg_match("/\<methodCall\>/", $this->postdata))
127             $request = $this->buildRequest("text/xml"); //xmlrpc
128         else if ( $this->method == 'POST' and strstr("\r\nContent-Disposition: form-data; filename=", 
129                                                      $this->postdata)) 
130         {
131             //file upload
132             $boundary = $this->boundary;
133             $request = $this->buildRequest("multipart/form-data; boundary=\"$boundary\"");
134         } else
135             $request = $this->buildRequest();
136         $this->debug('Request', $request);
137         fwrite($fp, $request);
138         // Reset all the variables that should not persist between requests
139         $this->headers = array();
140         $this->content = '';
141         $this->errormsg = '';
142         // Set a couple of flags
143         $inHeaders = true;
144         $atStart = true;
145         // Now start reading back the response
146         while (!feof($fp)) {
147             $line = fgets($fp, 4096);
148             if ($atStart) {
149                 // Deal with first line of returned data
150                 $atStart = false;
151                 if ($line === false) {
152                     $this->errormsg = "Empty ". $this->method. " response";
153                     return false;
154                 }    
155                 if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
156                     $this->errormsg = "Status code line invalid: ".htmlentities($line);
157                     $this->debug($this->errormsg);
158                     return false;
159                 }
160                 $http_version = $m[1]; // not used
161                 $this->status = $m[2];
162                 $status_string = $m[3]; // not used
163                 $this->debug(trim($line));
164                 continue;
165             }
166             if ($inHeaders) {
167                 if (trim($line) == '') {
168                     $inHeaders = false;
169                     $this->debug('Received Headers', $this->headers);
170                     if ($this->headers_only) {
171                         break; // Skip the rest of the input
172                     }
173                     continue;
174                 }
175                 if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
176                     // Skip to the next header
177                     continue;
178                 }
179                 $key = strtolower(trim($m[1]));
180                 $val = trim($m[2]);
181                 // Deal with the possibility of multiple headers of same name
182                 if (isset($this->headers[$key])) {
183                     if (is_array($this->headers[$key])) {
184                         $this->headers[$key][] = $val;
185                     } else {
186                         $this->headers[$key] = array($this->headers[$key], $val);
187                     }
188                 } else {
189                     $this->headers[$key] = $val;
190                 }
191                 continue;
192             }
193             // We're not in the headers, so append the line to the contents
194             $this->content .= $line;
195         }
196         fclose($fp);
197         // If data is compressed, uncompress it
198         if (isset($this->headers['content-encoding']) && $this->headers['content-encoding'] == 'gzip') {
199             $this->debug('Content is gzip encoded, unzipping it');
200             $this->content = substr($this->content, 10); // See http://www.php.net/manual/en/function.gzencode.php
201             $this->content = gzinflate($this->content);
202         }
203         // If $persist_cookies, deal with any cookies
204         if ($this->persist_cookies && isset($this->headers['set-cookie']) && $this->host == $this->cookie_host) {
205             $cookies = $this->headers['set-cookie'];
206             if (!is_array($cookies)) {
207                 $cookies = array($cookies);
208             }
209             foreach ($cookies as $cookie) {
210                 if (preg_match('/([^=]+)=([^;]+);/', $cookie, $m)) {
211                     $this->cookies[$m[1]] = $m[2];
212                 }
213             }
214             // Record domain of cookies for security reasons
215             $this->cookie_host = $this->host;
216         }
217         if ($this->status == '401') {
218             $this->errormsg = '401 ' . $status_string;
219             return false;
220         }
221         // If $persist_referers, set the referer ready for the next request
222         if (isset($this->persist_referers)) {
223             $this->debug('Persisting referer: '.$this->getRequestURL());
224             $this->referer = $this->getRequestURL();
225         }
226         // Finally, if handle_redirects and a redirect is sent, do that
227         if (isset($this->handle_redirects)) {
228             if (++$this->redirect_count >= $this->max_redirects) {
229                 $this->errormsg = 'Number of redirects exceeded maximum ('.$this->max_redirects.')';
230                 $this->debug($this->errormsg);
231                 $this->redirect_count = 0;
232                 return false;
233             }
234             $location = isset($this->headers['location']) ? $this->headers['location'] : '';
235             $uri = isset($this->headers['uri']) ? $this->headers['uri'] : '';
236             if ($location || $uri) {
237                 $url = parse_url($location.$uri);
238                 if ($this->method == 'POST')
239                     return $this->doRequest();
240                 else    
241                     // This will FAIL if redirect is to a different site
242                     return $this->get($url['path']);
243             }
244         }
245         return true;
246     }
247     
248     function buildRequest($ContentType = 'application/x-www-form-urlencoded') {
249         $headers = array();
250         // Using 1.1 leads to all manner of problems, such as "chunked" encoding
251         $headers[] = "{$this->method} {$this->path} HTTP/1.0"; 
252         $headers[] = "Host: {$this->host}";
253         $headers[] = "User-Agent: {$this->user_agent}";
254         $headers[] = "Accept: {$this->accept}";
255         if ($this->use_gzip) {
256             $headers[] = "Accept-encoding: {$this->accept_encoding}";
257         }
258         $headers[] = "Accept-language: {$this->accept_language}";
259         if (!empty($this->referer)) {
260             $headers[] = "Referer: {$this->referer}";
261         }
262         // Cookies
263         if (!empty($this->cookies)) {
264             $cookie = 'Cookie: ';
265             foreach ($this->cookies as $key => $value) {
266                 $cookie .= "$key=$value; ";
267             }
268             $headers[] = $cookie;
269         }
270         // Basic authentication
271         if (!empty($this->username) && !empty($this->password)) {
272             $headers[] = 'Authorization: BASIC '.base64_encode($this->username.':'.$this->password);
273         }
274         // If this is a POST, set the content type and length
275         if ($this->postdata) {
276             $headers[] = 'Content-Type: ' . $ContentType;
277             $headers[] = 'Content-Length: '.strlen($this->postdata);
278         }
279         $request = implode("\r\n", $headers)."\r\n\r\n".$this->postdata;
280         return $request;
281     }
282     function getStatus() {
283         return $this->status;
284     }
285     function getContent() {
286         return $this->content;
287     }
288     function getHeaders() {
289         return $this->headers;
290     }
291     function getHeader($header) {
292         $header = strtolower($header);
293         if (isset($this->headers[$header])) {
294             return $this->headers[$header];
295         } else {
296             return false;
297         }
298     }
299     function getError() {
300         return $this->errormsg;
301     }
302     function getCookies() {
303         return $this->cookies;
304     }
305     function getRequestURL() {
306         $url = 'http://'.$this->host;
307         if ($this->port != 80) {
308             $url = 'https://'.$this->host;
309             $url .= ':'.$this->port;
310         }
311         $url .= $this->path;
312         return $url;
313     }
314     // Setter methods
315     function setUserAgent($string) {
316         $this->user_agent = $string;
317     }
318     function setAuthorization($username, $password) {
319         $this->username = $username;
320         $this->password = $password;
321     }
322     function setCookies($array) {
323         $this->cookies = $array;
324     }
325     // Option setting methods
326     function useGzip($boolean) {
327         $this->use_gzip = $boolean;
328     }
329     function setPersistCookies($boolean) {
330         $this->persist_cookies = $boolean;
331     }
332     function setPersistReferers($boolean) {
333         $this->persist_referers = $boolean;
334     }
335     function setHandleRedirects($boolean) {
336         $this->handle_redirects = $boolean;
337     }
338     function setMaxRedirects($num) {
339         $this->max_redirects = $num;
340     }
341     function setHeadersOnly($boolean) {
342         $this->headers_only = $boolean;
343     }
344     function setDebug($boolean) {
345         $this->debug = $boolean;
346     }
347     // "Quick" static methods
348     function quickGet($url) {
349         $bits = parse_url($url);
350         $host = $bits['host'];
351         $port = isset($bits['port']) ? $bits['port'] : 80;
352         $path = isset($bits['path']) ? $bits['path'] : '/';
353         if (isset($bits['query'])) {
354             $path .= '?'.$bits['query'];
355         }
356         $client = new HttpClient($host, $port);
357         if (!$client->get($path)) {
358             return false;
359         } else {
360             return $client->getContent();
361         }
362     }
363     function quickPost($url, $data) {
364         $bits = parse_url($url);
365         $host = $bits['host'];
366         $port = isset($bits['port']) ? $bits['port'] : 80;
367         $path = isset($bits['path']) ? $bits['path'] : '/';
368         $client = new HttpClient($host, $port);
369         if (!$client->post($path, $data)) {
370             return false;
371         } else {
372             return $client->getContent();
373         }
374     }
375     function debug($msg, $object = false) {
376         if ($this->debug) {
377             print '<div style="border: 1px solid red; padding: 0.5em; margin: 0.5em;"><strong>HttpClient Debug:</strong> '.$msg;
378             if ($object) {
379                 ob_start();
380                 print_r($object);
381                 $content = htmlentities(ob_get_contents());
382                 ob_end_clean();
383                 print '<pre>'.$content.'</pre>';
384             }
385             print '</div>';
386         }
387     }   
388 }
389
390 // For emacs users
391 // Local Variables:
392 // mode: php
393 // tab-width: 8
394 // c-basic-offset: 4
395 // c-hanging-comment-ender-p: nil
396 // indent-tabs-mode: nil
397 // End:
398 ?>