Use https://www.php.net/ instead of https://secure.php.net/
[lhc/web/wiklou.git] / includes / http / HttpRequestFactory.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20 namespace MediaWiki\Http;
21
22 use CurlHttpRequest;
23 use DomainException;
24 use Http;
25 use MediaWiki\Logger\LoggerFactory;
26 use MWHttpRequest;
27 use PhpHttpRequest;
28 use Profiler;
29 use GuzzleHttpRequest;
30
31 /**
32 * Factory creating MWHttpRequest objects.
33 */
34 class HttpRequestFactory {
35
36 /**
37 * Generate a new MWHttpRequest object
38 * @param string $url Url to use
39 * @param array $options (optional) extra params to pass (see Http::request())
40 * @param string $caller The method making this request, for profiling
41 * @throws DomainException
42 * @return MWHttpRequest
43 * @see MWHttpRequest::__construct
44 */
45 public function create( $url, array $options = [], $caller = __METHOD__ ) {
46 if ( !Http::$httpEngine ) {
47 Http::$httpEngine = 'guzzle';
48 } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
49 throw new DomainException( __METHOD__ . ': curl (https://www.php.net/curl) is not ' .
50 'installed, but Http::$httpEngine is set to "curl"' );
51 }
52
53 if ( !isset( $options['logger'] ) ) {
54 $options['logger'] = LoggerFactory::getInstance( 'http' );
55 }
56
57 switch ( Http::$httpEngine ) {
58 case 'guzzle':
59 return new GuzzleHttpRequest( $url, $options, $caller, Profiler::instance() );
60 case 'curl':
61 return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
62 case 'php':
63 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
64 throw new DomainException( __METHOD__ . ': allow_url_fopen ' .
65 'needs to be enabled for pure PHP http requests to ' .
66 'work. If possible, curl should be used instead. See ' .
67 'https://www.php.net/curl.'
68 );
69 }
70 return new PhpHttpRequest( $url, $options, $caller, Profiler::instance() );
71 default:
72 throw new DomainException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' );
73 }
74 }
75
76 /**
77 * Simple function to test if we can make any sort of requests at all, using
78 * cURL or fopen()
79 * @return bool
80 */
81 public function canMakeRequests() {
82 return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
83 }
84
85 }