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.
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.
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
20 namespace MediaWiki\Http
;
25 use MediaWiki\Logger\LoggerFactory
;
29 use GuzzleHttpRequest
;
32 * Factory creating MWHttpRequest objects.
34 class HttpRequestFactory
{
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
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"' );
53 if ( !isset( $options['logger'] ) ) {
54 $options['logger'] = LoggerFactory
::getInstance( 'http' );
57 switch ( Http
::$httpEngine ) {
59 return new GuzzleHttpRequest( $url, $options, $caller, Profiler
::instance() );
61 return new CurlHttpRequest( $url, $options, $caller, Profiler
::instance() );
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.'
70 return new PhpHttpRequest( $url, $options, $caller, Profiler
::instance() );
72 throw new DomainException( __METHOD__
. ': The setting of Http::$httpEngine is not valid.' );
77 * Simple function to test if we can make any sort of requests at all, using
81 public function canMakeRequests() {
82 return function_exists( 'curl_init' ) ||
wfIniGetBool( 'allow_url_fopen' );