bug 18664, bug 19270: Relative URIs in interwiki table break things (in general:...
[lhc/web/wiklou.git] / includes / HttpFunctions.php
1 <?php
2 /**
3 * @defgroup HTTP HTTP
4 */
5
6 /**
7 * Various HTTP related functions
8 * @ingroup HTTP
9 */
10 class Http {
11 static $httpEngine = false;
12
13 /**
14 * Perform an HTTP request
15 * @param $method string HTTP method. Usually GET/POST
16 * @param $url string Full URL to act on
17 * @param $options options to pass to HttpRequest object
18 * Possible keys for the array:
19 * timeout Timeout length in seconds
20 * postData An array of key-value pairs or a url-encoded form data
21 * proxy The proxy to use. Will use $wgHTTPProxy (if set) otherwise.
22 * noProxy Override $wgHTTPProxy (if set) and don't use any proxy at all.
23 * sslVerifyHost (curl only) Verify the SSL certificate
24 * caInfo (curl only) Provide CA information
25 * maxRedirects Maximum number of redirects to follow (defaults to 5)
26 * followRedirects Whether to follow redirects (defaults to true)
27 * @returns mixed (bool)false on failure or a string on success
28 */
29 public static function request( $method, $url, $options = array() ) {
30 $url = wfExpandUrl( $url );
31 wfDebug( "HTTP: $method: $url" );
32 $options['method'] = strtoupper( $method );
33 if ( !isset( $options['timeout'] ) ) {
34 $options['timeout'] = 'default';
35 }
36 $req = HttpRequest::factory( $url, $options );
37 $status = $req->execute();
38 if ( $status->isOK() ) {
39 return $req->getContent();
40 } else {
41 return false;
42 }
43 }
44
45 /**
46 * Simple wrapper for Http::request( 'GET' )
47 * @see Http::request()
48 */
49 public static function get( $url, $timeout = 'default', $options = array() ) {
50 $options['timeout'] = $timeout;
51 return Http::request( 'GET', $url, $options );
52 }
53
54 /**
55 * Simple wrapper for Http::request( 'POST' )
56 * @see Http::request()
57 */
58 public static function post( $url, $options = array() ) {
59 return Http::request( 'POST', $url, $options );
60 }
61
62 /**
63 * Check if the URL can be served by localhost
64 * @param $url string Full url to check
65 * @return bool
66 */
67 public static function isLocalURL( $url ) {
68 global $wgCommandLineMode, $wgConf;
69 if ( $wgCommandLineMode ) {
70 return false;
71 }
72
73 // Extract host part
74 $matches = array();
75 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
76 $host = $matches[1];
77 // Split up dotwise
78 $domainParts = explode( '.', $host );
79 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
80 $domainParts = array_reverse( $domainParts );
81 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
82 $domainPart = $domainParts[$i];
83 if ( $i == 0 ) {
84 $domain = $domainPart;
85 } else {
86 $domain = $domainPart . '.' . $domain;
87 }
88 if ( $wgConf->isLocalVHost( $domain ) ) {
89 return true;
90 }
91 }
92 }
93 return false;
94 }
95
96 /**
97 * A standard user-agent we can use for external requests.
98 * @returns string
99 */
100 public static function userAgent() {
101 global $wgVersion;
102 return "MediaWiki/$wgVersion";
103 }
104
105 /**
106 * Checks that the given URI is a valid one
107 * @param $uri Mixed: URI to check for validity
108 * @returns bool
109 */
110 public static function isValidURI( $uri ) {
111 return preg_match(
112 '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
113 $uri,
114 $matches
115 );
116 }
117 }
118
119 /**
120 * This wrapper class will call out to curl (if available) or fallback
121 * to regular PHP if necessary for handling internal HTTP requests.
122 */
123 class HttpRequest {
124 protected $content;
125 protected $timeout = 'default';
126 protected $headersOnly = null;
127 protected $postData = null;
128 protected $proxy = null;
129 protected $noProxy = false;
130 protected $sslVerifyHost = true;
131 protected $caInfo = null;
132 protected $method = "GET";
133 protected $reqHeaders = array();
134 protected $url;
135 protected $parsedUrl;
136 protected $callback;
137 protected $maxRedirects = 5;
138 protected $followRedirects = true;
139
140 protected $cookieJar;
141
142 protected $headerList = array();
143 protected $respVersion = "0.9";
144 protected $respStatus = "200 Ok";
145 protected $respHeaders = array();
146
147 public $status;
148
149 /**
150 * @param $url string url to use
151 * @param $options array (optional) extra params to pass (see Http::request())
152 */
153 function __construct( $url, $options = array() ) {
154 global $wgHTTPTimeout;
155
156 $this->url = $url;
157 $this->parsedUrl = parse_url( $url );
158
159 if ( !Http::isValidURI( $this->url ) ) {
160 $this->status = Status::newFatal('http-invalid-url');
161 } else {
162 $this->status = Status::newGood( 100 ); // continue
163 }
164
165 if ( isset($options['timeout']) && $options['timeout'] != 'default' ) {
166 $this->timeout = $options['timeout'];
167 } else {
168 $this->timeout = $wgHTTPTimeout;
169 }
170
171 $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
172 "method", "followRedirects", "maxRedirects" );
173 foreach ( $members as $o ) {
174 if ( isset($options[$o]) ) {
175 $this->$o = $options[$o];
176 }
177 }
178 }
179
180 /**
181 * Generate a new request object
182 * @see HttpRequest::__construct
183 */
184 public static function factory( $url, $options = null ) {
185 if ( !Http::$httpEngine ) {
186 Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
187 } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
188 throw new MWException( __METHOD__.': curl (http://php.net/curl) is not installed, but'.
189 ' Http::$httpEngine is set to "curl"' );
190 }
191
192 switch( Http::$httpEngine ) {
193 case 'curl':
194 return new CurlHttpRequest( $url, $options );
195 case 'php':
196 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
197 throw new MWException( __METHOD__.': allow_url_fopen needs to be enabled for pure PHP'.
198 ' http requests to work. If possible, curl should be used instead. See http://php.net/curl.' );
199 }
200 return new PhpHttpRequest( $url, $options );
201 default:
202 throw new MWException( __METHOD__.': The setting of Http::$httpEngine is not valid.' );
203 }
204 }
205
206 /**
207 * Get the body, or content, of the response to the request
208 * @return string
209 */
210 public function getContent() {
211 return $this->content;
212 }
213
214 /**
215 * Take care of setting up the proxy
216 * (override in subclass)
217 * @return string
218 */
219 public function proxySetup() {
220 global $wgHTTPProxy;
221
222 if ( $this->proxy ) {
223 return;
224 }
225 if ( Http::isLocalURL( $this->url ) ) {
226 $this->proxy = 'http://localhost:80/';
227 } elseif ( $wgHTTPProxy ) {
228 $this->proxy = $wgHTTPProxy ;
229 } elseif ( getenv( "http_proxy" ) ) {
230 $this->proxy = getenv( "http_proxy" );
231 }
232 }
233
234 /**
235 * Set the refererer header
236 */
237 public function setReferer( $url ) {
238 $this->setHeader('Referer', $url);
239 }
240
241 /**
242 * Set the user agent
243 */
244 public function setUserAgent( $UA ) {
245 $this->setHeader('User-Agent', $UA);
246 }
247
248 /**
249 * Set an arbitrary header
250 */
251 public function setHeader($name, $value) {
252 // I feel like I should normalize the case here...
253 $this->reqHeaders[$name] = $value;
254 }
255
256 /**
257 * Get an array of the headers
258 */
259 public function getHeaderList() {
260 $list = array();
261
262 if( $this->cookieJar ) {
263 $this->reqHeaders['Cookie'] =
264 $this->cookieJar->serializeToHttpRequest($this->parsedUrl['path'],
265 $this->parsedUrl['host']);
266 }
267 foreach($this->reqHeaders as $name => $value) {
268 $list[] = "$name: $value";
269 }
270 return $list;
271 }
272
273 /**
274 * Set the callback
275 * @param $callback callback
276 */
277 public function setCallback( $callback ) {
278 $this->callback = $callback;
279 }
280
281 /**
282 * A generic callback to read the body of the response from a remote
283 * server.
284 * @param $fh handle
285 * @param $content string
286 */
287 public function read( $fh, $content ) {
288 $this->content .= $content;
289 return strlen( $content );
290 }
291
292 /**
293 * Take care of whatever is necessary to perform the URI request.
294 * @return Status
295 */
296 public function execute() {
297 global $wgTitle;
298
299 if( strtoupper($this->method) == "HEAD" ) {
300 $this->headersOnly = true;
301 }
302
303 if ( is_array( $this->postData ) ) {
304 $this->postData = wfArrayToCGI( $this->postData );
305 }
306
307 if ( is_object( $wgTitle ) && !isset($this->reqHeaders['Referer']) ) {
308 $this->setReferer( $wgTitle->getFullURL() );
309 }
310
311 if ( !$this->noProxy ) {
312 $this->proxySetup();
313 }
314
315 if ( !$this->callback ) {
316 $this->setCallback( array( $this, 'read' ) );
317 }
318
319 if ( !isset($this->reqHeaders['User-Agent']) ) {
320 $this->setUserAgent(Http::userAgent());
321 }
322 }
323
324 /**
325 * Parses the headers, including the HTTP status code and any
326 * Set-Cookie headers. This function expectes the headers to be
327 * found in an array in the member variable headerList.
328 * @returns nothing
329 */
330 protected function parseHeader() {
331 $lastname = "";
332 foreach( $this->headerList as $header ) {
333 if( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
334 $this->respVersion = $match[1];
335 $this->respStatus = $match[2];
336 } elseif( preg_match( "#^[ \t]#", $header ) ) {
337 $last = count($this->respHeaders[$lastname]) - 1;
338 $this->respHeaders[$lastname][$last] .= "\r\n$header";
339 } elseif( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
340 $this->respHeaders[strtolower( $match[1] )][] = $match[2];
341 $lastname = strtolower( $match[1] );
342 }
343 }
344
345 $this->parseCookies();
346 }
347
348 /**
349 * Sets the member variable status to a fatal status if the HTTP
350 * status code was not 200.
351 * @returns nothing
352 */
353 protected function setStatus() {
354 if( !$this->respHeaders ) {
355 $this->parseHeader();
356 }
357
358 if((int)$this->respStatus !== 200) {
359 list( $code, $message ) = explode(" ", $this->respStatus, 2);
360 $this->status->fatal("http-bad-status", $code, $message );
361 }
362 }
363
364
365 /**
366 * Returns true if the last status code was a redirect.
367 * @return bool
368 */
369 public function isRedirect() {
370 if( !$this->respHeaders ) {
371 $this->parseHeader();
372 }
373
374 $status = (int)$this->respStatus;
375 if ( $status >= 300 && $status < 400 ) {
376 return true;
377 }
378 return false;
379 }
380
381 /**
382 * Returns an associative array of response headers after the
383 * request has been executed. Because some headers
384 * (e.g. Set-Cookie) can appear more than once the, each value of
385 * the associative array is an array of the values given.
386 * @return array
387 */
388 public function getResponseHeaders() {
389 if( !$this->respHeaders ) {
390 $this->parseHeader();
391 }
392 return $this->respHeaders;
393 }
394
395 /**
396 * Returns the value of the given response header.
397 * @param $header string
398 * @return string
399 */
400 public function getResponseHeader($header) {
401 if( !$this->respHeaders ) {
402 $this->parseHeader();
403 }
404 if ( isset( $this->respHeaders[strtolower ( $header ) ] ) ) {
405 $v = $this->respHeaders[strtolower ( $header ) ];
406 return $v[count( $v ) - 1];
407 }
408 return null;
409 }
410
411 /**
412 * Tells the HttpRequest object to use this pre-loaded CookieJar.
413 * @param $jar CookieJar
414 */
415 public function setCookieJar( $jar ) {
416 $this->cookieJar = $jar;
417 }
418
419 /**
420 * Returns the cookie jar in use.
421 * @returns CookieJar
422 */
423 public function getCookieJar() {
424 if( !$this->respHeaders ) {
425 $this->parseHeader();
426 }
427 return $this->cookieJar;
428 }
429
430 /**
431 * Sets a cookie. Used before a request to set up any individual
432 * cookies. Used internally after a request to parse the
433 * Set-Cookie headers.
434 * @see Cookie::set
435 */
436 public function setCookie( $name, $value = null, $attr = null) {
437 if( !$this->cookieJar ) {
438 $this->cookieJar = new CookieJar;
439 }
440 $this->cookieJar->setCookie($name, $value, $attr);
441 }
442
443 /**
444 * Parse the cookies in the response headers and store them in the cookie jar.
445 */
446 protected function parseCookies() {
447 if( !$this->cookieJar ) {
448 $this->cookieJar = new CookieJar;
449 }
450 if( isset( $this->respHeaders['set-cookie'] ) ) {
451 $url = parse_url( $this->getFinalUrl() );
452 foreach( $this->respHeaders['set-cookie'] as $cookie ) {
453 $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
454 }
455 }
456 }
457
458 /**
459 * Returns the final URL after all redirections.
460 * @returns string
461 */
462 public function getFinalUrl() {
463 $location = $this->getResponseHeader("Location");
464 if ( $location ) {
465 return $location;
466 }
467
468 return $this->url;
469 }
470 }
471
472
473 class Cookie {
474 protected $name;
475 protected $value;
476 protected $expires;
477 protected $path;
478 protected $domain;
479 protected $isSessionKey = true;
480 // TO IMPLEMENT protected $secure
481 // TO IMPLEMENT? protected $maxAge (add onto expires)
482 // TO IMPLEMENT? protected $version
483 // TO IMPLEMENT? protected $comment
484
485 function __construct( $name, $value, $attr ) {
486 $this->name = $name;
487 $this->set( $value, $attr );
488 }
489
490 /**
491 * Sets a cookie. Used before a request to set up any individual
492 * cookies. Used internally after a request to parse the
493 * Set-Cookie headers.
494 * @param $name string the name of the cookie
495 * @param $value string the value of the cookie
496 * @param $attr array possible key/values:
497 * expires A date string
498 * path The path this cookie is used on
499 * domain Domain this cookie is used on
500 */
501 public function set( $value, $attr ) {
502 $this->value = $value;
503 if( isset( $attr['expires'] ) ) {
504 $this->isSessionKey = false;
505 $this->expires = strtotime( $attr['expires'] );
506 }
507 if( isset( $attr['path'] ) ) {
508 $this->path = $attr['path'];
509 } else {
510 $this->path = "/";
511 }
512 if( isset( $attr['domain'] ) ) {
513 if( self::validateCookieDomain( $attr['domain'] ) ) {
514 $this->domain = $attr['domain'];
515 }
516 } else {
517 throw new MWException("You must specify a domain.");
518 }
519 }
520
521 /**
522 * Return the true if the cookie is valid is valid. Otherwise,
523 * false. The uses a method similar to IE cookie security
524 * described here:
525 * http://kuza55.blogspot.com/2008/02/understanding-cookie-security.html
526 * A better method might be to use a blacklist like
527 * http://publicsuffix.org/
528 *
529 * @param $domain string the domain to validate
530 * @param $originDomain string (optional) the domain the cookie originates from
531 * @return bool
532 */
533 public static function validateCookieDomain( $domain, $originDomain = null) {
534 // Don't allow a trailing dot
535 if( substr( $domain, -1 ) == "." ) return false;
536
537 $dc = explode(".", $domain);
538
539 // Don't allow cookies for "localhost", "ls" or other dot-less hosts
540 if( count($dc) < 2 ) return false;
541
542 // Only allow full, valid IP addresses
543 if( preg_match( '/^[0-9.]+$/', $domain ) ) {
544 if( count( $dc ) != 4 ) return false;
545
546 if( ip2long( $domain ) === false ) return false;
547
548 if( $originDomain == null || $originDomain == $domain ) return true;
549
550 }
551
552 // Don't allow cookies for "co.uk" or "gov.uk", etc, but allow "supermarket.uk"
553 if( strrpos( $domain, "." ) - strlen( $domain ) == -3 ) {
554 if( (count($dc) == 2 && strlen( $dc[0] ) <= 2 )
555 || (count($dc) == 3 && strlen( $dc[0] ) == "" && strlen( $dc[1] ) <= 2 ) ) {
556 return false;
557 }
558 if( (count($dc) == 2 || (count($dc) == 3 && $dc[0] == "") )
559 && preg_match( '/(com|net|org|gov|edu)\...$/', $domain) ) {
560 return false;
561 }
562 }
563
564 if( $originDomain != null ) {
565 if( substr( $domain, 0, 1 ) != "." && $domain != $originDomain ) {
566 return false;
567 }
568 if( substr( $domain, 0, 1 ) == "."
569 && substr_compare( $originDomain, $domain, -strlen( $domain ),
570 strlen( $domain ), TRUE ) != 0 ) {
571 return false;
572 }
573 }
574
575 return true;
576 }
577
578 /**
579 * Serialize the cookie jar into a format useful for HTTP Request headers.
580 * @param $path string the path that will be used. Required.
581 * @param $domain string the domain that will be used. Required.
582 * @return string
583 */
584 public function serializeToHttpRequest( $path, $domain ) {
585 $ret = "";
586
587 if( $this->canServeDomain( $domain )
588 && $this->canServePath( $path )
589 && $this->isUnExpired() ) {
590 $ret = $this->name ."=". $this->value;
591 }
592
593 return $ret;
594 }
595
596 protected function canServeDomain( $domain ) {
597 if( $domain == $this->domain
598 || ( strlen( $domain) > strlen( $this->domain )
599 && substr( $this->domain, 0, 1) == "."
600 && substr_compare( $domain, $this->domain, -strlen( $this->domain ),
601 strlen( $this->domain ), TRUE ) == 0 ) ) {
602 return true;
603 }
604 return false;
605 }
606
607 protected function canServePath( $path ) {
608 if( $this->path && substr_compare( $this->path, $path, 0, strlen( $this->path ) ) == 0 ) {
609 return true;
610 }
611 return false;
612 }
613
614 protected function isUnExpired() {
615 if( $this->isSessionKey || $this->expires > time() ) {
616 return true;
617 }
618 return false;
619 }
620
621 }
622
623 class CookieJar {
624 private $cookie = array();
625
626 /**
627 * Set a cookie in the cookie jar. Make sure only one cookie per-name exists.
628 * @see Cookie::set()
629 */
630 public function setCookie ($name, $value, $attr) {
631 /* cookies: case insensitive, so this should work.
632 * We'll still send the cookies back in the same case we got them, though.
633 */
634 $index = strtoupper($name);
635 if( isset( $this->cookie[$index] ) ) {
636 $this->cookie[$index]->set( $value, $attr );
637 } else {
638 $this->cookie[$index] = new Cookie( $name, $value, $attr );
639 }
640 }
641
642 /**
643 * @see Cookie::serializeToHttpRequest
644 */
645 public function serializeToHttpRequest( $path, $domain ) {
646 $cookies = array();
647
648 foreach( $this->cookie as $c ) {
649 $serialized = $c->serializeToHttpRequest( $path, $domain );
650 if ( $serialized ) $cookies[] = $serialized;
651 }
652
653 return implode("; ", $cookies);
654 }
655
656 /**
657 * Parse the content of an Set-Cookie HTTP Response header.
658 * @param $cookie string
659 */
660 public function parseCookieResponseHeader ( $cookie, $domain ) {
661 $len = strlen( "Set-Cookie:" );
662 if ( substr_compare( "Set-Cookie:", $cookie, 0, $len, TRUE ) === 0 ) {
663 $cookie = substr( $cookie, $len );
664 }
665
666 $bit = array_map( 'trim', explode( ";", $cookie ) );
667 if ( count($bit) >= 1 ) {
668 list($name, $value) = explode( "=", array_shift( $bit ), 2 );
669 $attr = array();
670 foreach( $bit as $piece ) {
671 $parts = explode( "=", $piece );
672 if( count( $parts ) > 1 ) {
673 $attr[strtolower( $parts[0] )] = $parts[1];
674 } else {
675 $attr[strtolower( $parts[0] )] = true;
676 }
677 }
678
679 if( !isset( $attr['domain'] ) ) {
680 $attr['domain'] = $domain;
681 } elseif ( !Cookie::validateCookieDomain( $attr['domain'], $domain ) ) {
682 return null;
683 }
684
685 $this->setCookie( $name, $value, $attr );
686 }
687 }
688 }
689
690
691 /**
692 * HttpRequest implemented using internal curl compiled into PHP
693 */
694 class CurlHttpRequest extends HttpRequest {
695 static $curlMessageMap = array(
696 6 => 'http-host-unreachable',
697 28 => 'http-timed-out'
698 );
699
700 protected $curlOptions = array();
701 protected $headerText = "";
702
703 protected function readHeader( $fh, $content ) {
704 $this->headerText .= $content;
705 return strlen( $content );
706 }
707
708 public function execute() {
709 parent::execute();
710 if ( !$this->status->isOK() ) {
711 return $this->status;
712 }
713 $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
714 $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
715 $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
716 $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
717 $this->curlOptions[CURLOPT_HEADERFUNCTION] = array($this, "readHeader");
718 $this->curlOptions[CURLOPT_FOLLOWLOCATION] = $this->followRedirects;
719 $this->curlOptions[CURLOPT_MAXREDIRS] = $this->maxRedirects;
720
721 /* not sure these two are actually necessary */
722 if(isset($this->reqHeaders['Referer'])) {
723 $this->curlOptions[CURLOPT_REFERER] = $this->reqHeaders['Referer'];
724 }
725 $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders['User-Agent'];
726
727 if ( $this->sslVerifyHost ) {
728 $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost;
729 }
730
731 if ( $this->caInfo ) {
732 $this->curlOptions[CURLOPT_CAINFO] = $this->caInfo;
733 }
734
735 if ( $this->headersOnly ) {
736 $this->curlOptions[CURLOPT_NOBODY] = true;
737 $this->curlOptions[CURLOPT_HEADER] = true;
738 } elseif ( $this->method == 'POST' ) {
739 $this->curlOptions[CURLOPT_POST] = true;
740 $this->curlOptions[CURLOPT_POSTFIELDS] = $this->postData;
741 // Suppress 'Expect: 100-continue' header, as some servers
742 // will reject it with a 417 and Curl won't auto retry
743 // with HTTP 1.0 fallback
744 $this->reqHeaders['Expect'] = '';
745 } else {
746 $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
747 }
748
749 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
750
751 $curlHandle = curl_init( $this->url );
752 curl_setopt_array( $curlHandle, $this->curlOptions );
753
754 if ( false === curl_exec( $curlHandle ) ) {
755 $code = curl_error( $curlHandle );
756
757 if ( isset( self::$curlMessageMap[$code] ) ) {
758 $this->status->fatal( self::$curlMessageMap[$code] );
759 } else {
760 $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) );
761 }
762 } else {
763 $this->headerList = explode("\r\n", $this->headerText);
764 }
765
766 curl_close( $curlHandle );
767
768 $this->parseHeader();
769 $this->setStatus();
770 return $this->status;
771 }
772 }
773
774 class PhpHttpRequest extends HttpRequest {
775 protected $manuallyRedirect = false;
776
777 protected function urlToTcp( $url ) {
778 $parsedUrl = parse_url( $url );
779
780 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
781 }
782
783 public function execute() {
784 parent::execute();
785
786 // At least on Centos 4.8 with PHP 5.1.6, using max_redirects to follow redirects
787 // causes a segfault
788 if ( version_compare( '5.1.7', phpversion(), '>' ) ) {
789 $this->manuallyRedirect = true;
790 }
791
792 if ( $this->parsedUrl['scheme'] != 'http' ) {
793 $this->status->fatal( 'http-invalid-scheme', $this->parsedUrl['scheme'] );
794 }
795
796 $this->reqHeaders['Accept'] = "*/*";
797 if ( $this->method == 'POST' ) {
798 // Required for HTTP 1.0 POSTs
799 $this->reqHeaders['Content-Length'] = strlen( $this->postData );
800 $this->reqHeaders['Content-type'] = "application/x-www-form-urlencoded";
801 }
802
803 $options = array();
804 if ( $this->proxy && !$this->noProxy ) {
805 $options['proxy'] = $this->urlToTCP( $this->proxy );
806 $options['request_fulluri'] = true;
807 }
808
809 if ( !$this->followRedirects || $this->manuallyRedirect ) {
810 $options['max_redirects'] = 0;
811 } else {
812 $options['max_redirects'] = $this->maxRedirects;
813 }
814
815 $options['method'] = $this->method;
816 $options['header'] = implode("\r\n", $this->getHeaderList());
817 // Note that at some future point we may want to support
818 // HTTP/1.1, but we'd have to write support for chunking
819 // in version of PHP < 5.3.1
820 $options['protocol_version'] = "1.0";
821
822 // This is how we tell PHP we want to deal with 404s (for example) ourselves.
823 // Only works on 5.2.10+
824 $options['ignore_errors'] = true;
825
826 if ( $this->postData ) {
827 $options['content'] = $this->postData;
828 }
829
830 $oldTimeout = false;
831 if ( version_compare( '5.2.1', phpversion(), '>' ) ) {
832 $oldTimeout = ini_set('default_socket_timeout', $this->timeout);
833 } else {
834 $options['timeout'] = $this->timeout;
835 }
836
837 $context = stream_context_create( array( 'http' => $options ) );
838
839 $this->headerList = array();
840 $reqCount = 0;
841 $url = $this->url;
842 do {
843 $again = false;
844 $reqCount++;
845 wfSuppressWarnings();
846 $fh = fopen( $url, "r", false, $context );
847 wfRestoreWarnings();
848 if ( $fh ) {
849 $result = stream_get_meta_data( $fh );
850 $this->headerList = $result['wrapper_data'];
851 $this->parseHeader();
852 $url = $this->getResponseHeader("Location");
853 $again = $this->manuallyRedirect && $this->followRedirects && $url
854 && $this->isRedirect() && $this->maxRedirects > $reqCount;
855 }
856 } while ( $again );
857
858 if ( $oldTimeout !== false ) {
859 ini_set('default_socket_timeout', $oldTimeout);
860 }
861 $this->setStatus();
862
863 if ( $fh === false ) {
864 $this->status->fatal( 'http-request-error' );
865 return $this->status;
866 }
867
868 if ( $result['timed_out'] ) {
869 $this->status->fatal( 'http-timed-out', $this->url );
870 return $this->status;
871 }
872
873 if($this->status->isOK()) {
874 while ( !feof( $fh ) ) {
875 $buf = fread( $fh, 8192 );
876 if ( $buf === false ) {
877 $this->status->fatal( 'http-read-error' );
878 break;
879 }
880 if ( strlen( $buf ) ) {
881 call_user_func( $this->callback, $fh, $buf );
882 }
883 }
884 }
885 fclose( $fh );
886
887 return $this->status;
888 }
889 }