MediaWiki  1.30.0
CurlHttpRequest.php
Go to the documentation of this file.
1 <?php
25  const SUPPORTS_FILE_POSTS = true;
26 
27  protected $curlOptions = [];
28  protected $headerText = "";
29 
35  protected function readHeader( $fh, $content ) {
36  $this->headerText .= $content;
37  return strlen( $content );
38  }
39 
46  public function execute() {
47  $this->prepare();
48 
49  if ( !$this->status->isOK() ) {
50  return Status::wrap( $this->status ); // TODO B/C; move this to callers
51  }
52 
53  $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
54  $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
55 
56  // Only supported in curl >= 7.16.2
57  if ( defined( 'CURLOPT_CONNECTTIMEOUT_MS' ) ) {
58  $this->curlOptions[CURLOPT_CONNECTTIMEOUT_MS] = $this->connectTimeout * 1000;
59  }
60 
61  $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
62  $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
63  $this->curlOptions[CURLOPT_HEADERFUNCTION] = [ $this, "readHeader" ];
64  $this->curlOptions[CURLOPT_MAXREDIRS] = $this->maxRedirects;
65  $this->curlOptions[CURLOPT_ENCODING] = ""; # Enable compression
66 
67  $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders['User-Agent'];
68 
69  $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost ? 2 : 0;
70  $this->curlOptions[CURLOPT_SSL_VERIFYPEER] = $this->sslVerifyCert;
71 
72  if ( $this->caInfo ) {
73  $this->curlOptions[CURLOPT_CAINFO] = $this->caInfo;
74  }
75 
76  if ( $this->headersOnly ) {
77  $this->curlOptions[CURLOPT_NOBODY] = true;
78  $this->curlOptions[CURLOPT_HEADER] = true;
79  } elseif ( $this->method == 'POST' ) {
80  $this->curlOptions[CURLOPT_POST] = true;
82  // Don't interpret POST parameters starting with '@' as file uploads, because this
83  // makes it impossible to POST plain values starting with '@' (and causes security
84  // issues potentially exposing the contents of local files).
85  // The PHP manual says this option was introduced in PHP 5.5 defaults to true in PHP 5.6,
86  // but we support lower versions, and the option doesn't exist in HHVM 5.6.99.
87  if ( defined( 'CURLOPT_SAFE_UPLOAD' ) ) {
88  $this->curlOptions[CURLOPT_SAFE_UPLOAD] = true;
89  } elseif ( is_array( $postData ) ) {
90  // In PHP 5.2 and later, '@' is interpreted as a file upload if POSTFIELDS
91  // is an array, but not if it's a string. So convert $req['body'] to a string
92  // for safety.
94  }
95  $this->curlOptions[CURLOPT_POSTFIELDS] = $postData;
96 
97  // Suppress 'Expect: 100-continue' header, as some servers
98  // will reject it with a 417 and Curl won't auto retry
99  // with HTTP 1.0 fallback
100  $this->reqHeaders['Expect'] = '';
101  } else {
102  $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
103  }
104 
105  $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
106 
107  $curlHandle = curl_init( $this->url );
108 
109  if ( !curl_setopt_array( $curlHandle, $this->curlOptions ) ) {
110  throw new InvalidArgumentException( "Error setting curl options." );
111  }
112 
113  if ( $this->followRedirects && $this->canFollowRedirects() ) {
114  MediaWiki\suppressWarnings();
115  if ( !curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, true ) ) {
116  $this->logger->debug( __METHOD__ . ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
117  "Probably open_basedir is set.\n" );
118  // Continue the processing. If it were in curl_setopt_array,
119  // processing would have halted on its entry
120  }
121  MediaWiki\restoreWarnings();
122  }
123 
124  if ( $this->profiler ) {
125  $profileSection = $this->profiler->scopedProfileIn(
126  __METHOD__ . '-' . $this->profileName
127  );
128  }
129 
130  $curlRes = curl_exec( $curlHandle );
131  if ( curl_errno( $curlHandle ) == CURLE_OPERATION_TIMEOUTED ) {
132  $this->status->fatal( 'http-timed-out', $this->url );
133  } elseif ( $curlRes === false ) {
134  $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) );
135  } else {
136  $this->headerList = explode( "\r\n", $this->headerText );
137  }
138 
139  curl_close( $curlHandle );
140 
141  if ( $this->profiler ) {
142  $this->profiler->scopedProfileOut( $profileSection );
143  }
144 
145  $this->parseHeader();
146  $this->setStatus();
147 
148  return Status::wrap( $this->status ); // TODO B/C; move this to callers
149  }
150 
154  public function canFollowRedirects() {
155  $curlVersionInfo = curl_version();
156  if ( $curlVersionInfo['version_number'] < 0x071304 ) {
157  $this->logger->debug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
158  return false;
159  }
160 
161  if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) {
162  if ( strval( ini_get( 'open_basedir' ) ) !== '' ) {
163  $this->logger->debug( "Cannot follow redirects when open_basedir is set\n" );
164  return false;
165  }
166  }
167 
168  return true;
169  }
170 }
MWHttpRequest\$callback
callable $callback
Definition: MWHttpRequest.php:54
MWHttpRequest\setStatus
setStatus()
Sets HTTPRequest status member to a fatal value with the error message if the returned integer value ...
Definition: MWHttpRequest.php:438
MWHttpRequest\$maxRedirects
$maxRedirects
Definition: MWHttpRequest.php:55
MWHttpRequest\$sslVerifyCert
$sslVerifyCert
Definition: MWHttpRequest.php:47
MWHttpRequest\$content
$content
Definition: MWHttpRequest.php:41
CurlHttpRequest\$headerText
$headerText
Definition: CurlHttpRequest.php:28
CurlHttpRequest
MWHttpRequest implemented using internal curl compiled into PHP.
Definition: CurlHttpRequest.php:24
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MWHttpRequest\parseHeader
parseHeader()
Parses the headers, including the HTTP status code and any Set-Cookie headers.
Definition: MWHttpRequest.php:411
MWHttpRequest\$postData
$postData
Definition: MWHttpRequest.php:43
Status\wrap
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:55
MWHttpRequest\$timeout
int string $timeout
Definition: MWHttpRequest.php:39
CurlHttpRequest\execute
execute()
Definition: CurlHttpRequest.php:46
MWHttpRequest\$method
$method
Definition: MWHttpRequest.php:49
MWHttpRequest
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
Definition: MWHttpRequest.php:33
MWHttpRequest\getHeaderList
getHeaderList()
Get an array of the headers.
Definition: MWHttpRequest.php:319
CurlHttpRequest\$curlOptions
$curlOptions
Definition: CurlHttpRequest.php:27
MWHttpRequest\$caInfo
$caInfo
Definition: MWHttpRequest.php:48
CurlHttpRequest\canFollowRedirects
canFollowRedirects()
Definition: CurlHttpRequest.php:154
CurlHttpRequest\SUPPORTS_FILE_POSTS
const SUPPORTS_FILE_POSTS
Definition: CurlHttpRequest.php:25
CurlHttpRequest\readHeader
readHeader( $fh, $content)
Definition: CurlHttpRequest.php:35
MWHttpRequest\$proxy
$proxy
Definition: MWHttpRequest.php:44
wfArrayToCgi
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
Definition: GlobalFunctions.php:442
MWHttpRequest\prepare
prepare()
Definition: MWHttpRequest.php:388