Activation request returns invalid HTTP
When activating one's jetpack installation the request to xmlrpc.php returns a response without Content-Length and without Transfer-Encoding. This causes varnish (tested with varnish version 4) to dismiss the response body.
According to RFC 2616 one of the two headers are mandatory.
Steps on how to reproduce:
- Install wordpress with jetpack
- Use netcat/telnet to simulate activation request.
- Note that both
Content-LengthandTransfer-Encodingare missing.
Example request (removed activation code and replaced host header):
POST /xmlrpc.php?for=jetpack HTTP/1.0
Host: example.com
User-Agent: Jetpack by WordPress.com
Accept: */*
Content-Length: 198
Content-Type: application/x-www-form-urlencoded
<?xml version="1.0"?>
<methodCall>
<methodName>jetpack.verifyRegistration</methodName>
<params>
<param><value><string>******</string></value></param>
Here is an example response ( I removed the body for readability):
HTTP/1.1 200 OK
Date: Fri, 05 Feb 2016 12:48:37 GMT
Server: Apache
X-Powered-By: PHP/5.6.16
Connection: close
X-Clacks-Overhead: GNU Terry Pratchett
Vary: Accept-Encoding
Content-Type: text/xml; charset=UTF-8
<?xml version="1.0" encoding="UTF-8"?>
[...]
Please let me know if you need any more information or want me to test anything.
Generally, neither Jetpack nor WordPress sends Content-Length headers "manually". Both leave Content-Length/Transfer-Encoding up to the webserver to handle as it sees fit.
Here's a request you can use to test just WordPress (with or without Jetpack installed).
test.xml:
<methodCall>
<methodName>demo.addTwoNumbers</methodName>
<params>
<param>
<value><int>1</int></value>
</param>
<param>
<value><int>2</int></value>
</param>
</params>
</methodCall>
cat test.xml | curl -i -X POST 'https://blogwaffe.com/wordpress/xmlrpc.php' --data @-
HTTP/1.1 200 OK
Server: nginx/1.8.1
Content-Type: text/xml; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Date: Tue, 09 Feb 2016 05:02:29 +0000
Strict-Transport-Security: max-age=15768000
Content-Security-Policy: upgrade-insecure-requests
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<int>3</int>
</value>
</param>
</params>
</methodResponse>
In that blogwaffe.com example, WordPress isn't setting the Transfer-Encoding: chunked header, nginx is.
Could you try running that request on a site where Jetpack doesn't activate? Does Varnish drop the response body?
I suspect this started happening after upgrading to WordPress 4.4: https://core.trac.wordpress.org/changeset/34571/
Prior to then, the Content-Length was calculated manually for XML-RPC requests. It was removed in that changeset because the calculation was fragile. If a theme or plugin accidentally output a BOM or whitespace prior to the XML-RPC response, the message body would be larger than the Content-Length specified (since the length calculation would not include that BOM/whitespace). Clients would then truncate the response, which would yield invalid XML:
Content-Length: 170
Content-Type: text/xml; charset=UTF-8
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<int>3</int>
</value>
</param>
</params>
</methodRespon
(3 bytes of whitespace prior to the intended output means the last 3 bytes of the intended output get truncated due to a too-short Content-Length header.)
There were also issues with PHP's strlen() function, which, depending on the environment, does not actually return the byte length of its input (because PHP is amazing): http://php.net/manual/en/mbstring.overload.php
Rather than make the calculation more robust, it was dropped entirely.
If Varnish doesn't like these responses, can Apache be configured to chunk the responses or add a content length? Alternatively, if you're just using Varnish for caching, you could turn it off for these requests - there's probably not much point in caching XML-RPC POST requests.
As an aside, I don't believe RFC 2616 requires one of Content-Length or Transfer-Encoding header in a response (see Section 4.4 and note that a Connection: close header is sent), though I could be misreading the spec.
Thank you for for detailed answer.
If I run the request you gave me against the WordPress installation via varnish I do get a response body and a Content-Length header.
HTTP/1.1 200 OK
Date: Tue, 09 Feb 2016 10:55:04 GMT
Server: Apache
X-Powered-By: PHP/5.6.16
X-Clacks-Overhead: GNU Terry Pratchett
Vary: Accept-Encoding
Content-Type: text/xml; charset=UTF-8
X-Varnish: 933986523
Age: 0
Via: 1.1 varnish-v4
Accept-Ranges: bytes
Content-Length: 170
Connection: keep-alive
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<int>3</int>
</value>
</param>
</params>
</methodResponse>
However that is just varnish rewriting the headers. Towards the webserver I get Transfer-Encoding: chukned
HTTP/1.1 200 OK
Date: Tue, 09 Feb 2016 10:55:53 GMT
Server: Apache
X-Powered-By: PHP/5.6.16
Connection: close
X-Clacks-Overhead: GNU Terry Pratchett
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/xml; charset=UTF-8
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<int>3</int>
</value>
</param>
</params>
</methodResponse>
I tried multiple of the demo/hello world requests towards WordPress xmlrpc interface, all of them have returned one of the two headers. Could it be related to the output buffering you are using? (Haven't looked that closely at the code so just guessing)
I'm not sure it would be wise to get Apache to calculate the Content-Length, this would probably break streaming applications. It's not a single application webserver.
Would it be possible to set Transfer-Encoding: chunked on all xmlrpc responses?
Could it be related to the output buffering you are using?
I don't see any output buffering that would be interfering here, but that's a good guess, and I may be missing something.
Would it be possible to set
Transfer-Encoding: chunkedon all xmlrpc responses?
Doing this in PHP would not be pretty (and core WordPress doesn't do it, so there must be some other problem elsewhere). We'd have to do the chunking manually ourselves. WordPress lets the webserver decide how to send the data it receives from PHP.
Clearly something is different, but I'm not sure what that difference between Jetpack and core WordPress is. Here's what I see on my local machine:
verify-registration.xml:
<methodCall>
<methodName>jetpack.verifyRegistration</methodName>
<params>
<param>
<value><string>test</string></value>
</param>
</params>
</methodCall>
Jetpack XML-RPC request:
$ cat verify-registration.xml | curl -i --raw -X POST 'http://dakuten.local/wordpress/xmlrpc.php?for=jetpack' --data @-
HTTP/1.1 200 OK
Date: Tue, 09 Feb 2016 18:37:36 GMT
Server: Apache/2.4.16 (Unix) PHP/5.6.18
X-Powered-By: PHP/5.6.18
Connection: close
Content-Length: 432
Content-Type: text/xml; charset=UTF-8
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><int>400</int></value>
</member>
<member>
<name>faultString</name>
<value><string>Jetpack: [verify_secrets_missing] Verification took too long</string></value>
</member>
</struct>
</value>
</fault>
</methodResponse>
(This faultCode response is normal. Jetpack is correctly telling us there is a problem with the verify-registration.xml request we're sending.)
Core WordPress XML-RPC request (using test.xml from previous comment):
$ cat test.xml | curl -i --raw -X POST 'http://dakuten.local/wordpress/xmlrpc.php' --data @-
HTTP/1.1 200 OK
Date: Tue, 09 Feb 2016 18:37:39 GMT
Server: Apache/2.4.16 (Unix) PHP/5.6.18
X-Powered-By: PHP/5.6.18
Connection: close
Content-Length: 170
Content-Type: text/xml; charset=UTF-8
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<int>3</int>
</value>
</param>
</params>
</methodResponse>
In both cases, my local Apache correctly determines the content length and adds the header.
You can also use PHP's built in webserver to serve the requests. That may give some more insight into what PHP is outputting (and so what Apache is receiving). This creates a PHP webserver process in the foreground:
$ php -S 0.0.0.0:8000 xmlrpc.php
PHP 5.6.18 Development Server started at Tue Feb 9 10:59:44 2016
Listening on http://0.0.0.0:8000
Document root is /Users/mdawaffe/Sites/wordpress
Press Ctrl-C to quit.
Jetpack XML-RPC Request:
$ cat verify-registration.xml | curl -i --raw -X POST 'http://127.0.0.1:8000?for=jetpack' --data @-
HTTP/1.1 200 OK
Host: 127.0.0.1:8000
Connection: close
X-Powered-By: PHP/5.6.18
Connection: close
Content-Type: text/xml; charset=UTF-8
Date: Tue, 09 Feb 2016 19:00:31 +0000
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><int>400</int></value>
</member>
<member>
<name>faultString</name>
<value><string>Jetpack: [verify_secrets_missing] Verification took too long</string></value>
</member>
</struct>
</value>
</fault>
</methodResponse>
Core WordPress XML-RPC Request:
$ cat test.xml | curl -i --raw -X POST 'http://127.0.0.1:8000/' --data @-
HTTP/1.1 200 OK
Host: 127.0.0.1:8000
Connection: close
X-Powered-By: PHP/5.6.18
Connection: close
Content-Type: text/xml; charset=UTF-8
Date: Tue, 09 Feb 2016 19:14:50 +0000
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<int>3</int>
</value>
</param>
</params>
</methodResponse>
PHP is not sending a Content-Length header in either case; Apache is doing that calculation. (I don't know enough about the internals of either to say how much different these responses are to the data PHP sends to Apache in the normal setup.)
I'm not sure what configuration differences would cause Apache (or Apache + Varnish) to do one thing or another, though I suspect there's some size/buffer threshold below which Apache will send a Content-Length header and above which Apache will start chunking.
Perhaps compression is the culprit? If you add a --compressed flag (or a -H 'Accept-Encoding: deflate, gzip' option) to those curl requests, does the absence/presence of Content-Type and/or Transfer-Encoding change for either the Jetpack or the WordPress request? (The request/response you show in the original post suggest that compression is not the issue, but I think it's worth checking anyway.)
Are you using mod_php, FastCGI, or PHP-FPM with Apache?
A shot in the dark: Is there anything else in your setup (another WordPress plugin, some PHP configuration, ...) that would be flush()ing data to Apache before the response is complete? (I don't know why that'd make a difference between Jetpack and WordPress responses.)
We are using FastCGI + apache and a varnish in front. The WordPress installation we tested with was pretty much vanilla. It has akismet and jetpack installed.
I just noticed something while testing with curl. When using curl you actually get all the needed headers. It turns out curl is using HTTP/1.1 where as the server that does the original request uses HTTP/1.0.
The HTTP/1.0 specs are pretty clear on closing the connection being a valid alternative to a Content-Length header. So in this case varnish is falsely removing the body. I found a mailing list post where the bug was acknowledged and there is even a workaround in the form of a vcl snipped (it will force the connection towards the webserver to be HTTP/1.1):
sub vcl_backend_fetch {
set bereq.proto = "HTTP/1.1";
}
I can confirm that the workaround works. You can find the original mailing list post here: https://www.varnish-cache.org/lists/pipermail/varnish-misc/2015-December/024674.html
This might also be interesting for your FAQ, that right now advises people to not use varnish. Thank you for helping debugging this issue!
Very interesting. Thanks for figuring out the problem :)
I'll close this issue but will let the rest of the support team know about the HTTP/1.0 issue in Varnish.
**# PROBLEM RESOLVED! first of all i have 3 domain names hosted with one.com and 2 domains works fine with Jetpack but 1 domain have the problem to connect to jetpack, so i have contacted Jetpack team support and they try to make anything but its not fix the problem, i try also to contacted one.com support team and they also try to solve the problem but nothing have changed and i was still have same problem. I have checked that xml link is works fine but still same problem. I try myself to edit the Load.php file like the suggestion but still same problem. I have reset WordPress but same problem. i have delete WordPress and database to default for the 3 domains, but still same problem (2 domains works fine with Jetpack and 1 domain have same problem to connect. But yesterday i was thinking how come this problem is just for 1 domain and other 2 domains is works fine and all domains have same hosting (one.com) but then i remember that this 2 domains i have transfer it 3 month's ago to other company that i can control domains name server because in one.com i don't have option to change the names servers myself and i must contact team support every time i want to change it, and that was the only different between the 2 domains what is works fine with Jetpack and the 1 domain what was have problem to connect that the 2 domains is not point it with one.com but they have hosting with one.com, so i try to point the 3th domain what is have problem to other than one.com and i keep the names servers with one.com and finally all domains works fine. So, if somebody have same problem even after trying all options i noticed so they can transfer the domain name to other than what they have, and they still can keep hosting plan with the company, so only problem fixing is to (TRANSFER THE DOMAIN NAME TO OTHER COMPANY.....ONLY DOMAIN NAME NOT HOSTING) I hope this can help others to solve the problem and sorry for bad English Ramy
**