Commit graph

84 commits

Author SHA1 Message Date
Umherirrender
8de3b7d324 Use static closures where safe to use
This is micro-optimization of closure code to avoid binding the closure
to $this where it is not needed.

Created by I25a17fb22b6b669e817317a0f45051ae9c608208

Change-Id: I0ffc6200f6c6693d78a3151cb8cea7dce7c21653
2021-02-11 00:13:52 +00:00
Thiemo Kreuz
1fc8d79ac6 Remove documentation that literally repeats the code
For example, documenting the method getUser() with "get the User
object" does not add any information that's not already there.
But I have to read the text first to understand that it doesn't
document anything that's not already obvious from the code.

Some of this is from a time when we had a PHPCS sniff that was
complaining when a line like `@param User $user` doesn't end
with some descriptive text. Some users started adding text like
`@param User $user The User` back then. Let's please remove
this.

Change-Id: I0ea8d051bc732466c73940de9259f87ffb86ce7a
2020-10-27 19:20:26 +00:00
Bill Pirkle
e4890f50bb Add a function to HttpRequestFactory for creating a GuzzleHttp\Client
Guzzle is a vendor library included with MediaWiki, and it is
sometimes useful to directly use a GuzzleHttp\Client instance.
Add a function to HttpRequestFactory for this purpose.

Bug: T263816
Change-Id: I35f4b911676c7a58dfa5a8dd0d104125188dd62c
2020-10-22 23:11:47 -05:00
Michael Große
72f9efd625
Upstream MwHttpRequestToResponseInterfaceAdapter from Wikibase
This makes the PSR-7 interface for the MWHttpRequest available to all
consumers that want to make use of it. This is of general utility and
should not be limited to Wikibase. Also, it allows us to drop the Guzzle
dependency from Wikibase entirely.

As an alternative to moving the adapter (as is done with this patch), it
was considered to refactor MWHttpRequest to support the interface
directly. While that would come without the overhead of an additional
class, it would require adding the interface methods to an already large
class and deprecating the now redundant legacy methods. This would be an
invasive refactoring that would not only affect the classes inheriting
from MWHttpRequest, but also its consumers.

It would seem that a conceptual refactoring of that magnitude would
require a more substantial consensus among contributors (and maybe
consumers) of these classes. Moving this adapter is seen as a step in
building the case for why using standardized interfaces is a good idea
and allowing for the discussion to be grounded of incorporating an existing
adapter into a base class rather than the hypothetical discussion of
adjusting it to an so far unused interface.

Bug: T263989
Depends-On: I56ad52b561460121a8c84313cbd431dc811e2ae1
Change-Id: I2a27dead1d5f1403b8d255c5daf9ba5e7c313476
2020-10-07 10:17:37 +02:00
Tim Starling
d519bef87a Fix GuzzleHttpRequest request headers
The headers passed to Guzzle via the Client constructor are termed
"conditional", meaning that headers in the Request override them. If you
set a "body" option containing a MultipartStream, it sets a default
Content-Type request header, overriding the one from the Client
constructor. So to send a multipart stream with a custom Content-Type,
it is necessary to pass headers to Guzzle in the Request object.

Change-Id: Ie7bfc548ce4211fbf3ec67e3913bbdd3482977c3
2020-09-21 13:57:32 +10:00
Reedy
b346501f7e Updating guzzlehttp/guzzle (6.5.5 => 7.0.1)
Change-Id: I02bdd522844c5174892bc8ead40b8f3dd550c624
Depends-On: I2968abb84303737233d99249f57e212675013f4e
2020-09-18 17:25:06 +00:00
Aryeh Gregor
a24e8a06b5 Mark CONSTRUCTOR_OPTIONS as internal
These were never meant to be part of the public interface and should not
ever have been marked with @since. They're only useful for constructing
the respective objects, which no outside users should be doing.

Change-Id: I86e01272d46fc72af32172d8a12b9180971d4613
2020-08-21 00:18:45 -04:00
daniel
f59bf8a22f Use @internal instead of @private per policy
https://www.mediawiki.org/wiki/Stable_interface_policy mandates the use
of @internal. The semantics of @private was never properly defined.

Bug: T247862
Change-Id: I4c7c6e7b5a80e86456965521f88d1dfa7d698f84
2020-06-26 14:14:23 +02:00
jenkins-bot
0c0a60c87b Merge "Remove terminating line breaks from debug messages" 2020-06-03 02:24:13 +00:00
Tim Starling
47a1619027 Remove terminating line breaks from debug messages
A terminating line break has not been required in wfDebug() since 2014,
however no migration was done. Some of these line breaks found their way
into LoggerInterface::debug() calls, where they mess up the formatting
of the debug log.

So, remove terminating line breaks from wfDebug() and
LoggerInterface::debug() calls.

Also:
* Fix the stripping of leading line breaks from the log header emitted
  by Setup.php. This feature, accidentally broken in 2014, allows
  requests to be distinguished in the log file.
* Avoid using the global variable $self.
* Move the logging of the client IP back to Setup.php. It was moved to
  WebRequest in the hopes that it would not always be needed, however
  $wgRequest->getIP() is now called unconditionally a few lines up in
  Setup.php. This means that it is put in its proper place after the
  "start request" message.
* Wrap the log header code in a closure so that variables like $name do
  not leak into global scope.
* In Linker.php, remove a few instances of an unnecessary second
  parameter to wfDebug().

Change-Id: I96651d3044a95b9d210b51cb8368edc76bebbb9e
2020-06-03 12:01:16 +10:00
Tim Starling
feba7ee306 Use HttpRequestFactory::createMultiClient() in core where possible
* Use createMultiClient() in CdnCacheUpdate. This will reduce the
  timeout from a hard-coded 900s to a configurable 25s. This is not
  used in WMF production.
* Use createMultiClient() in the VirtualRESTServiceClient service. This
  should have no effect in production since the service is broken per
  T175224.
* Use the new createMultiClient() in the old createMultiClient(). The
  configuration is the same except that the maximum timeouts are now
  respected.

Bug: T245170
Change-Id: I63139d29471bc59e9ef60032fd812a1f24644113
2020-06-02 10:51:43 +10:00
Tim Starling
504fe2af11 Respect configured default HTTP timeouts, and introduce max timeouts
* Add HttpRequestFactory::createMultiClient(), which returns a
  MultiHttpClient with configured defaults applied. This is similar to
  the recently-deprecated Http::createMultiClient().
* Introduce $wgHTTPMaxTimeout and $wgHTTPMaxConnectTimeout which, if set
  to a lower value than their defaults of infinity, will limit the
  applied HTTP timeouts, whether configured or passed on a per-request
  basis. This is based on the frequently correct assumption that ops know
  more about timeouts than developers.
* In case developers believe, after becoming aware of this new situation,
  that they actually do know more about timeouts than ops, it is possible
  to override the configured maximum by passing similarly named options
  to HttpRequestFactory::createMultiClient() and
  HttpRequestFactory::create().
* Apply modern standards to HttpRequestFactory by injecting a logger and
  all configuration parameters used by its backends.
* As in Http, the new createMultiClient() will use a MediaWiki/1.35
  User-Agent and the 'http' channel for logging.
* Document that no proxy will be used for createMultiClient().
  Proxy config is weird and was previously a good reason to use
  MultiHttpClient over HttpRequestFactory.
* Deprecate direct construction of MWHttpRequest without a timeout
  parameter

Bug: T245170
Change-Id: I8252f6c854b98059f4916d5460ea71cf4b580149
2020-05-21 09:30:57 +10:00
Reedy
b80a9f4f6a Fix even more PSR12.Properties.ConstantVisibility.NotFound
Change-Id: I5e04824d6fa6a4c36ce489850bb0ed7b4ac588f9
2020-05-16 00:51:14 +01:00
Holger Knust
471d2371ab doxygen: Changed Doxygen tags causing warnings during documentation generation
Updated Doxygen markup in several .php files triggering warnings when mwdocgen.php is executed. Removed
obsolete settings MSCGEN_PATH and TCL_SUBST from Doxyfile. The former would generate a warning in 1.8.16
while TCL support was removed in 1.8.18. Since TCL_SUBST was blank anyway, it was removed prior to getting
to .18 in production. Increased DOT_GRAPH_MAX_NODES from 50 to 200 since Doxygen complained about it being
too low for API and Maintenance.

Bug: T248706
Change-Id: I9c67f0807d1b43089d351263d4f591dee5501f36
2020-04-14 03:25:19 +00:00
Edward Chernenko
116409e279 Support cookies in GuzzleHttpRequest
Currently GuzzleHttpRequest is not sending any cookies.
Furthermore, Guzzle expects its own format of CookieJar,
which is not compatible with CookieJar used by MediaWiki.

Solution is to add Guzzle Middleware that would obtain Cookie header
from MediaWiki CookieJar and add it to outgoing Guzzle requests.

Note: special handling of received cookies (Set-Cookie header
from the server) is unnecessary, this is not Guzzle-specific code
and is already done in MWHttpRequest::parseCookies().

Still, adding both a test of sending cookies and receiving cookies.

Bug: T245644
Change-Id: If69840e65d5671989cf15450964da3c691fd164a
2020-03-18 21:48:26 +03:00
Timo Tijhof
f575721a06 Update all use of $wgVersion to MW_VERSION
Follows-up I04628de4152dd5.

Bug: T212738
Change-Id: I718474ec0d9fd29ac2c05477f0f2493615d8aff5
2020-02-25 02:16:12 +00:00
Daimona Eaytoy
a6b6c18cea build: Upgrade mediawiki-phan-config to 0.9.2
Bug: T244633
Change-Id: If8f35eaf3f2272717d7f12b4068d811b1a23d540
2020-02-21 22:05:24 +00:00
James D. Forrester
0958a0bce4 Coding style: Auto-fix MediaWiki.Usage.IsNull.IsNull
Change-Id: I90cfe8366c0245c9c67e598d17800684897a4e27
2020-01-10 14:17:13 -08:00
James D. Forrester
4f2d1efdda Coding style: Auto-fix MediaWiki.Classes.UnsortedUseStatements.UnsortedUse
Change-Id: I94a0ae83c65e8ee419bbd1ae1e86ab21ed4d8210
2020-01-10 09:32:25 -08:00
Daimona Eaytoy
598c4d7fcb build: Upgrade phan to 0.9.0
Scalar casts are still allowed (for now), because there's a huge amount
of false positives. Ditto for invalid array offsets.

Thoughts about the rest: luckily, many false positives with array offsets
have gone. Moreover, since *Internal issues are suppressed in the base
config, we can remove inline suppressions.

Unfortunately, there are a couple of new issues about array additions
with only false positives, because apparently they don't take
branches into account.

Change-Id: I5a3913c6e762f77bfdae55051a395fae95d1f841
2019-12-07 20:16:19 +00:00
Moritz Schubotz (physikerwelt)
5e3a0e7395 Mimic CURLOPT_POST in GuzzleHttpRequest
The MWHttpRequest is implemented by the
CurlHttpRequest class and also the
GuzzleHttpRequest class. However, curl based rendering set
the CURLOPT_POST which implies that the 'Content-Type'
header defaults to 'application/x-www-form-urlencoded'.
To homgonize the functionality this patch mimics the
curl behaviour in Guzzle.

Bug: T232866
Change-Id: Id60a8de18e5f1e750a3bde23bd8b0deca4071165
2019-12-04 16:02:32 +00:00
Umherirrender
a30bf903c2 Remove unneeded return from MWHttpRequest::setCallback
doSetCallback has no return value

Change-Id: Ifaf5c62d72bf7214a8733f7482adbbe033958f2e
2019-11-08 20:50:50 +01:00
Daimona Eaytoy
95dc119527 Fix new phan errors, part 2
Still mostly doc-only.

Bug: T231636
Change-Id: I65cec6c716ce6859e14da00a12ef71e03603e59a
2019-10-12 10:35:09 +00:00
Derick Alangi
d573f0de1f MWHttpRequest: Avoid usage of deprecated Http class methods
Change-Id: Id0837874896708adf4a950d3bd74ae92f4908851
2019-10-09 09:55:42 +02:00
Umherirrender
c55130118f phan: Removed PhanUndeclaredTypeParameter suppression
Fixed the non-namespaced WebRequest by adding a backslash

Change-Id: I9f1e3e000d9a512a74f93145cd5e66b1b38ab8b2
2019-09-24 21:20:28 +02:00
Umherirrender
a5f1970842 Fix type for timeout and connectTimeout in HttpRequestFactory::create
Both can have a string 'default'

Change-Id: I9c49cf7db762f251b010bc5469d4c1b4a7797f45
2019-09-23 20:09:54 +02:00
Umherirrender
268346e562 phan: Enable PhanTypeMismatchArgument issue
Bug: T231636
Depends-On: I5de4f8f32a47c3f41c990ffe2ebd091fc23d1a58
Change-Id: I34d65fe3ff1916f2af675f0b1f19641b0cdfadc0
2019-09-19 20:11:42 +02:00
Daimona Eaytoy
290ab29617 Declare dynamic properties
This is for classes with a single undeclared property - aside from
BlockManager: I3f51fd3579514b83b567dfe20926df2f0930dc85 removed the
declaration of $permissionManager without actually removing all uses.

Change-Id: Ic2a95f77071312041be6e0633ea9b5325e98de42
2019-09-14 15:21:26 +02:00
Daimona Eaytoy
b5cbb5ab3f Upgrade phan config to 0.7.1
This allows us to remove many suppressions for phan false positives.

Bug: T231636
Depends-On: I82a279e1f7b0fdefd3bb712e46c7d0665429d065
Change-Id: I5c251e9584a1ae9fb1577afcafb5001e0dcd41c7
2019-09-04 08:20:53 +00:00
Daimona Eaytoy
c659bc6308 Unsuppress another phan issue (part 7)
Bug: T231636
Depends-On: I2cd24e73726394e3200a570c45d5e86b6849bfa9
Depends-On: I4fa3e6aad872434ca397325ed7a83f94973661d0
Change-Id: Ie6233561de78457cae5e4e44e220feec2d1272d8
2019-09-03 17:19:21 +00:00
Daimona Eaytoy
327e8ea416 Unsuppress phan issues part 6
Bug: T231636
Depends-On: I50377746f01749b058c39fd8229f9d566224cc43
Change-Id: I2cd24e73726394e3200a570c45d5e86b6849bfa9
2019-09-01 09:48:45 +00:00
Daimona Eaytoy
e70b5b3309 Unsuppress other phan issues (part 4)
Bug: T231636
Depends-On: I58e67c2b38389df874438deada4239510d21654f
Change-Id: I6e5fba7bd273219b1206559420b5bdb78734aa84
2019-08-31 17:13:39 +00:00
daniel
ba2788f62f Return result from HttpRequestFactory get and post methods
Bug: T222935
Change-Id: Idf1d00d04abbcf4e3391e3979bbab97e595916a5
2019-05-13 11:32:17 +02:00
Aryeh Gregor
9018579681 Deprecate the Http class
All methods got moved to HttpRequestFactory or MWHttpRequest or dropped.

I made the return value of the new HttpRequestFactory::request/get/post
methods null on error instead of false, so that when we drop PHP 7
support, we can use a "?string" return value. This could theoretically
change behavior of code that was switched from the old Http methods, but
probably won't. I kept the old behavior for the deprecated methods.

I changed the default value of $wgHTTPProxy from false to ''. This way
it should be usable directly without a trivial wrapper method. For the
benefit of anyone who might have set it to false in LocalSettings.php, I
also recommend casting to string just in case.

Http::$httpEngine is deprecated. Eventually it will be removed along
with the curl and PHP engines, leaving only the Guzlle engine.

I also added deprecation of MWHttpRequest::factory, which occurred in
1.31, to the release notes for 1.34. Now hopefully we can hard-deprecate
it in another couple of versions.

Bug: T214390
Change-Id: I2a316a758d793857f248bd251b90f5e9a6440e3a
2019-05-06 12:07:26 +03:00
Fomafix
9cbb8f104d Use https://www.php.net/ instead of https://secure.php.net/
Change-Id: I0acca592c6909e91b28b904da49dcbd6a43cd2a5
2019-04-12 06:44:48 +02:00
Thiemo Kreuz
5833dda61d Replace strlen() calls with strict string comparisons
Note there is an important difference between the two ways to express
this: strlen() does a string cast, but the `=== ''` and `!== ''`
comparisons will only detect empty strings, but not null, false, or any
other falsy value that becomes an empty string when cast to be one.

I am only touching code where I'm sure the variable is guaranteed to be
a string.

This change is done because I find the strict comparisons much more
readable. The code does exactly one thing now, and no magic casts any
more.

Change-Id: I3e908a0c7c7b6c29b0e5a1414f2ba9062a215b93
2019-03-28 12:32:39 +01:00
Timo Tijhof
69d4b69409 Remove various references to cURL in code comments
Given we now use Guzzle as well, and don't require cURL for any
features at a basic level, our code should not be as assumptious
about the backend being curl. Such assumptions may be wrong
and lead to confusion and/or bugs.

Bug: T137926
Change-Id: I6ad7f76768348e1eb8c1fb46c8125cce9285dc22
2019-03-05 23:55:03 +00:00
Marko Obrovac
9fd7fe0d3c MWHttpRequest: Include the request ID in outgoing HTTP requests
In order to be able to trace requests across the production environment,
we attach the X-Request-Id to outgoing requests.

Bug: T201409
Change-Id: Iadd6cbd51a78a1876a1a318783b51635717d054f
2019-02-08 19:16:13 +00:00
Bill Pirkle
d1256a9560 Change HttpRequestFactory default to guzzle
Activate the changes from T212175 by changing the default
MWHttpRequest-derived type to guzzle.  This is being done
as a separate change so that it is conveniently revertable
if issues arise.

Bug: T214703
Change-Id: I0868d2e7f3eed6d7c6b15334eb3187f3edcd7bec
2019-02-08 01:14:53 +00:00
Kunal Mehta
cc5d9a92a2 build: Updating mediawiki/mediawiki-codesniffer to 24.0.0
Change-Id: I66b1775b7c1d36076d9ca78cbeb42787a743f2aa
2019-02-07 18:39:42 +00:00
Fomafix
cff7dac346 Fix @param tags
* Use 'callable' instead of 'callback'.
* Remove '$' as part of a type name.
* Add missing type.

Change-Id: Ic1b39a7d8cbbee000d8fb2f3a1f71a621bd01993
2019-02-03 16:49:10 +01:00
Bill Pirkle
e8cb9f5f83 http: Support callback functions in GuzzleHttpRequest
Provide backward compatibility for callback functions in
GuzzleHttpRequest, which was missing in T202110, and restore
GuzzleHttpRequest as the default provided by HttpRequestFactory.

Bug: T212175
Depends-On: I4b45e79d35252d13f714f3271b87301ca515121a
Change-Id: I60d1a034b44874f6d24a04058db264eeb565f5e1
2019-01-24 22:05:10 +00:00
Lucas Werkmeister
a3bf4e67e5 Document header case in MWHttpRequest
The lack of this documentation resulted in some confusion in T214032 and
related tasks.

Change-Id: I08fc201b625197614ef5e163ed3686638b630b13
2019-01-21 12:46:36 +01:00
Max Semenik
731ebeea38 Remove a CURLOPT_CONNECTTIMEOUT_MS check
It was introduced in PHP 5.2.3.

Change-Id: I0c7e1684038c98677ceefbcfbb78641e90713002
2019-01-07 18:25:59 -08:00
Bill Pirkle
9cdbf73c9c Fix exception on certain http failures
Task T202110 included a change to recognize an HTTP status code
of 0 (zero) as an error, but it failed to set a status message,
resulting in an exception.  Changed to set a status message of
'Error' so that required value is not empty.

Bug: T212005
Change-Id: I5fb78555bfcaeccdd726432f4dfc70924a385c41
2018-12-14 15:55:28 -06:00
jenkins-bot
ee7e922a24 Merge "Temporarily remove guzzle as default for HttpRequestFactory" 2018-12-13 21:00:54 +00:00
Bill Pirkle
eefe4126a5 Temporarily remove guzzle as default for HttpRequestFactory
Temporarily switching back to curl/php as the default for
HttpRequest objects, to resolve production issues until a
long-term fix for the out-of-memory issue from T211886 is implemented.

Bug: T211886
Change-Id: I6201ac1292b41f82d920a431376d791a666355e8
2018-12-13 11:02:09 -06:00
Bill Pirkle
32d9c56c27 Fix guzzle InvalidArgumentException when body is passed as an array
The postBody option to GuzzleHttpRequest can be passed as an array
or as a string.  We were previously handling the array case incorrectly.

Bug: T211806
Change-Id: I8f40b9de9d40a9361eb45103608bf3aaa943bf73
2018-12-12 18:11:27 -06:00
Bill Pirkle
efd8077aa2 Create GuzzleHttpRequest class as new default for HttpRequestFactory
Create a GuzzleHttpRequest class using the external Guzzle
(docs.guzzlephp.org) library. This will be the new default request type,
but CurlHttpRequest and PhpHttpRequest remain available and accessible
via Http::$httpEngine.

Bug: T202110
Change-Id: Ie720be2628d7baf427b002847f103fd86ee4cff3
Depends-On: I143a6410d111e75f01dbbfd43f300e2e60247451
2018-12-10 00:36:59 -08:00
Fomafix
43244db9a2 Use PHP 7 '??' operator instead of if-then-else
Change-Id: If9d4be5d88c8927f63cbb84dfc8181baf62ea3eb
2018-10-21 21:46:46 +02:00