Jump to content

Topic on Extension talk:Math

https://wikimedia.org/api/rest_v1/ returning code 0 and not 200

13
Nyet (talkcontribs)

Using mediawiki 1.31 and Math extension REL_31

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":)

If i change it to allow code 0:

diff --git a/MathRestbaseInterface.php b/MathRestbaseInterface.php
 index 2dc5d45..5355177 100644
 --- a/MathRestbaseInterface.php
 +++ b/MathRestbaseInterface.php
 @@ -364,7 +364,7 @@ class MathRestbaseInterface {
          */
         public function evaluateRestbaseCheckResponse( $response ) {
                 $json = json_decode( $response['body'] );
 -               if ( $response['code'] === 200 ) {
 +               if ( $response['code'] === 200 || $response['code'] === 0) {
                         $headers = $response['headers'];
                         $this->hash = $headers['x-resource-location'];
                         $this->success = $json->success;
 
 

I get

Failed to parse (Conversion error. Server ("https://wikimedia.org/api/rest_") reported: "Cannot get mml. Server problem.")

Is the RESTBase server down?

Jdforrester (WMF) (talkcontribs)

Is the RESTBase server down?

No, but maybe the Mathoid service is?

87.123.120.67 (talkcontribs)

My Math extension have been failing with the same error since a few days.

38.140.29.18 (talkcontribs)

I've had this problem too. After some debugging, it appears that PHP's curl was refusing the SSL certificate of https://wikimedia.org/api/rest_ . I downloaded a cacert.pem file and pointed edited the php.ini to point to under under variable curl.cainfo and openssl.cafile. This fixed my problem.

[curl]

curl.cainfo = "C:\PHP7\cacert.pem"

[openssl]

openssl.cafile="C:\PHP7\cacert.pem"

J Ra Rose (talkcontribs)

Since today I have the same issue. Where did you download the cacert.pem file?

2001:B68:16:80:921B:EFF:FE13:C9A2 (talkcontribs)

We have the same issue as OP. Adding certificates to php.ini did not help.

2001:B68:16:80:921B:EFF:FE13:C9A2 (talkcontribs)

Our problems started after a system update. The issue is probably caused by changes in other packages.


2001:B68:16:80:921B:EFF:FE13:C9A2 (talkcontribs)
Johanngan (talkcontribs)

I had the same issues as OP on MediaWiki 1.32. The HTTP responses were coming back just fine (rendered math and everything); the problem was coming from the MultiHttpClient in the core. It parses the HTTP response code from the header with a regex match that's hard coded to look for HTTP/1.x, when in fact my server was receiving an HTTP/2 response. The code 0 was a default that was never overwritten thanks to the failed match.

I found two workarounds that resolved the issue for me.

  1. Change all the checks in MathRestbaseInterface.php to accept a code 0 response.
  2. Go against Do_not_hack_MediaWiki_core and modify the regex in MultiHttpClient to properly match the HTTP/2 response code.
    • I'm pretty sure I only modified one line of code. Here's what I did:
    1. Locate the file MultiHttpClient.php. It should be on the path: <Your MediaWiki root directory>/includes/libs/MultiHttpClient.php.
    2. Search for the line of code that uses preg_match(), in the getCurlHandle method. For me it was on line 390 (MW 1.33), line 392 (MW 1.32).
    3. Change the argument from "/^(HTTP\/1\.[01]) (\d{3}) (.*)/" to "/^(HTTP\/[12](?:\.[01])?) (\d{3}) (.*)/"
      • Just to be clear, here's the diff:
        diff MultiHttpClient_orig.php MultiHttpClient.php
        
        392c392
        < 				if ( preg_match( "/^(HTTP\/1\.[01]) (\d{3}) (.*)/", $header, $matches ) ) {
        ---
        > 				if ( preg_match( "/^(HTTP\/[12](?:\.[01])?) (\d{3}) (.*)/", $header, $matches ) ) {

I ended up going with the second option. It seems like an overhaul of the MultiHttpClient is in the works with T202352, so maybe this issue will resolve itself in future releases.

British Potato (talkcontribs)

Could you describe in a bit more detail what you did to MultiHttpClient? I might go by your second solution as well.

Johanngan (talkcontribs)

Edited my above comment with more details on what I did.

123.51.111.134 (talkcontribs)

I used the Johangan's hack:

  1. Change all the checks in MathRestbaseInterface.php to accept a code 0 response."

and it worked.

When I have created a patch file to use in our docker container, I'll post it as a gist here...

Dloewenherz2 (talkcontribs)

I did what Johangan recommended as well. Here's my rough patch, which "works".

--- MathRestbaseInterface.php	2019-08-09 19:10:54.000000000 -0500
+++ MathRestbaseInterface.php	2019-08-09 19:23:45.000000000 -0500
@@ -8,6 +8,8 @@

 use MediaWiki\Logger\LoggerFactory;

+const WikiMediaServersReturnInvalidErrorCodes = true;
+
 class MathRestbaseInterface {
 	private $hash = false;
 	private $tex;
@@ -382,7 +384,7 @@
 	 */
 	public function evaluateRestbaseCheckResponse( $response ) {
 		$json = json_decode( $response['body'] );
-		if ( $response['code'] === 200 ) {
+		if ( $response['code'] === 200 || WikiMediaServersReturnInvalidErrorCodes ) {
 			$headers = $response['headers'];
 			$this->hash = $headers['x-resource-location'];
 			$this->success = $json->success;
@@ -439,7 +441,7 @@
 	 * @throws MWException
 	 */
 	private function evaluateContentResponse( $type, array $response, array $request ) {
-		if ( $response['code'] === 200 ) {
+		if ( $response['code'] === 200 || WikiMediaServersReturnInvalidErrorCodes ) {
 			if ( array_key_exists( 'x-mathoid-style', $response['headers'] ) ) {
 				$this->mathoidStyle = $response['headers']['x-mathoid-style'];
 			}
@@ -474,3 +476,4 @@
 		throw new MWException( "Cannot get $type. $detail" );
 	}
 }
+
Reply to "https://wikimedia.org/api/rest_v1/ returning code 0 and not 200"