MediaWiki REL1_31
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;
81 $postData = $this->postData;
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 $this->curlOptions[CURLOPT_SAFE_UPLOAD] = true;
86 $this->curlOptions[CURLOPT_POSTFIELDS] = $postData;
87
88 // Suppress 'Expect: 100-continue' header, as some servers
89 // will reject it with a 417 and Curl won't auto retry
90 // with HTTP 1.0 fallback
91 $this->reqHeaders['Expect'] = '';
92 } else {
93 $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
94 }
95
96 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
97
98 $curlHandle = curl_init( $this->url );
99
100 if ( !curl_setopt_array( $curlHandle, $this->curlOptions ) ) {
101 throw new InvalidArgumentException( "Error setting curl options." );
102 }
103
104 if ( $this->followRedirects && $this->canFollowRedirects() ) {
105 Wikimedia\suppressWarnings();
106 if ( !curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, true ) ) {
107 $this->logger->debug( __METHOD__ . ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
108 "Probably open_basedir is set.\n" );
109 // Continue the processing. If it were in curl_setopt_array,
110 // processing would have halted on its entry
111 }
112 Wikimedia\restoreWarnings();
113 }
114
115 if ( $this->profiler ) {
116 $profileSection = $this->profiler->scopedProfileIn(
117 __METHOD__ . '-' . $this->profileName
118 );
119 }
120
121 $curlRes = curl_exec( $curlHandle );
122 if ( curl_errno( $curlHandle ) == CURLE_OPERATION_TIMEOUTED ) {
123 $this->status->fatal( 'http-timed-out', $this->url );
124 } elseif ( $curlRes === false ) {
125 $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) );
126 } else {
127 $this->headerList = explode( "\r\n", $this->headerText );
128 }
129
130 curl_close( $curlHandle );
131
132 if ( $this->profiler ) {
133 $this->profiler->scopedProfileOut( $profileSection );
134 }
135
136 $this->parseHeader();
137 $this->setStatus();
138
139 return Status::wrap( $this->status ); // TODO B/C; move this to callers
140 }
141
145 public function canFollowRedirects() {
146 $curlVersionInfo = curl_version();
147 if ( $curlVersionInfo['version_number'] < 0x071304 ) {
148 $this->logger->debug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
149 return false;
150 }
151
152 if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) {
153 if ( strval( ini_get( 'open_basedir' ) ) !== '' ) {
154 $this->logger->debug( "Cannot follow redirects when open_basedir is set\n" );
155 return false;
156 }
157 }
158
159 return true;
160 }
161}
MWHttpRequest implemented using internal curl compiled into PHP.
readHeader( $fh, $content)
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
setStatus()
Sets HTTPRequest status member to a fatal value with the error message if the returned integer value ...
parseHeader()
Parses the headers, including the HTTP status code and any Set-Cookie headers.
getHeaderList()
Get an array of the headers.
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:37