Http.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. use MediaWiki\Logger\LoggerFactory;
  21. use MediaWiki\MediaWikiServices;
  22. /**
  23. * Various HTTP related functions
  24. * @deprecated since 1.34
  25. * @ingroup HTTP
  26. */
  27. class Http {
  28. /** @deprecated since 1.34, just use the default engine */
  29. public static $httpEngine = null;
  30. /**
  31. * Perform an HTTP request
  32. *
  33. * @deprecated since 1.34, use HttpRequestFactory::request()
  34. *
  35. * @param string $method HTTP method. Usually GET/POST
  36. * @param string $url Full URL to act on. If protocol-relative, will be expanded to an http:// URL
  37. * @param array $options Options to pass to MWHttpRequest object. See HttpRequestFactory::create
  38. * docs
  39. * @param string $caller The method making this request, for profiling
  40. * @return string|bool (bool)false on failure or a string on success
  41. */
  42. public static function request( $method, $url, array $options = [], $caller = __METHOD__ ) {
  43. $ret = MediaWikiServices::getInstance()->getHttpRequestFactory()->request(
  44. $method, $url, $options, $caller );
  45. return is_string( $ret ) ? $ret : false;
  46. }
  47. /**
  48. * Simple wrapper for Http::request( 'GET' )
  49. *
  50. * @deprecated since 1.34, use HttpRequestFactory::get()
  51. *
  52. * @since 1.25 Second parameter $timeout removed. Second parameter
  53. * is now $options which can be given a 'timeout'
  54. *
  55. * @param string $url
  56. * @param array $options
  57. * @param string $caller The method making this request, for profiling
  58. * @return string|bool false on error
  59. */
  60. public static function get( $url, array $options = [], $caller = __METHOD__ ) {
  61. $args = func_get_args();
  62. if ( isset( $args[1] ) && ( is_string( $args[1] ) || is_numeric( $args[1] ) ) ) {
  63. // Second was used to be the timeout
  64. // And third parameter used to be $options
  65. wfWarn( "Second parameter should not be a timeout.", 2 );
  66. $options = isset( $args[2] ) && is_array( $args[2] ) ?
  67. $args[2] : [];
  68. $options['timeout'] = $args[1];
  69. $caller = __METHOD__;
  70. }
  71. return self::request( 'GET', $url, $options, $caller );
  72. }
  73. /**
  74. * Simple wrapper for Http::request( 'POST' )
  75. *
  76. * @deprecated since 1.34, use HttpRequestFactory::post()
  77. *
  78. * @param string $url
  79. * @param array $options
  80. * @param string $caller The method making this request, for profiling
  81. * @return string|bool false on error
  82. */
  83. public static function post( $url, array $options = [], $caller = __METHOD__ ) {
  84. return self::request( 'POST', $url, $options, $caller );
  85. }
  86. /**
  87. * A standard user-agent we can use for external requests.
  88. *
  89. * @deprecated since 1.34, use HttpRequestFactory::getUserAgent()
  90. * @return string
  91. */
  92. public static function userAgent() {
  93. return MediaWikiServices::getInstance()->getHttpRequestFactory()->getUserAgent();
  94. }
  95. /**
  96. * Check that the given URI is a valid one.
  97. *
  98. * This hardcodes a small set of protocols only, because we want to
  99. * deterministically reject protocols not supported by all HTTP-transport
  100. * methods.
  101. *
  102. * "file://" specifically must not be allowed, for security purpose
  103. * (see <https://www.mediawiki.org/wiki/Special:Code/MediaWiki/r67684>).
  104. *
  105. * @todo FIXME this is wildly inaccurate and fails to actually check most stuff
  106. *
  107. * @deprecated since 1.34, use MWHttpRequest::isValidURI
  108. * @param string $uri URI to check for validity
  109. * @return bool
  110. */
  111. public static function isValidURI( $uri ) {
  112. return MWHttpRequest::isValidURI( $uri );
  113. }
  114. /**
  115. * Gets the relevant proxy from $wgHTTPProxy
  116. *
  117. * @deprecated since 1.34, use $wgHTTPProxy directly
  118. * @return string The proxy address or an empty string if not set.
  119. */
  120. public static function getProxy() {
  121. wfDeprecated( __METHOD__, '1.34' );
  122. global $wgHTTPProxy;
  123. return (string)$wgHTTPProxy;
  124. }
  125. /**
  126. * Get a configured MultiHttpClient
  127. *
  128. * @deprecated since 1.34, construct it directly
  129. * @param array $options
  130. * @return MultiHttpClient
  131. */
  132. public static function createMultiClient( array $options = [] ) {
  133. wfDeprecated( __METHOD__, '1.34' );
  134. global $wgHTTPConnectTimeout, $wgHTTPTimeout, $wgHTTPProxy;
  135. return new MultiHttpClient( $options + [
  136. 'connTimeout' => $wgHTTPConnectTimeout,
  137. 'reqTimeout' => $wgHTTPTimeout,
  138. 'userAgent' => self::userAgent(),
  139. 'proxy' => $wgHTTPProxy,
  140. 'logger' => LoggerFactory::getInstance( 'http' )
  141. ] );
  142. }
  143. }