WordPress.org

Make WordPress Core

Opened 9 years ago

Last modified 2 months ago

#2567 reopened defect (bug)

XML-RPC returns incorrect content length header when using metaWeblog.getRecentPosts

Reported by: dasher@… Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major Version: 1.5
Component: XML-RPC Keywords: xml-rpc has-patch
Focuses: Cc:

Description

metaWeblog.getRecentPosts returns an incorrect content-length header on WordPress Mu (wordpress.com).

I'll attach a dump of the response to this ticket (if it allows me to).

Attachments (5)

8c814def95a8a82-response-3cebadec-41a4-468e-90f1-122ed4ec87ef.xml (87.6 KB) - added by dasher@… 9 years ago.
Dump of the response when calling metaWeblog.getRecentPosts with a postcount of 40
143_Response.txt (87.8 KB) - added by dasher@… 9 years ago.
Sorry - previous attachment didn't include the headers
bad-content-length.txt (15.6 KB) - added by wonderboymusic 4 years ago.
Content returned truncated
2567-xmlrpc-content-length.diff (842 bytes) - added by wpdavis 3 months ago.
2567.diff (595 bytes) - added by SergeyBiryukov 3 months ago.

Download all attachments as: .zip

Change History (25)

@dasher@…9 years ago

Dump of the response when calling metaWeblog.getRecentPosts with a postcount of 40

@dasher@…9 years ago

Sorry - previous attachment didn't include the headers

comment:1 @westi9 years ago

  • Keywords bg|reporter-feedback added

The content-length is returned by IXR based on strlen($xml) in class-IXR.php:

 354      function output($xml) {
 355          $xml = '<?xml version="1.0"?>'."\n".$xml;
 356          $length = strlen($xml);
 357          header('Connection: close');
 358          header('Content-Length: '.$length);
 359          header('Content-Type: text/xml');
 360          header('Date: '.date('r'));
 361          echo $xml;
 362          exit;
 363      }

Not sure how this could fail to return the correct content-length - we could of course remove the content-length header as it is not required.

What content-length are you expecting to be returned in this case.

comment:2 @dasher@…9 years ago

The second attachment (143_response) contains the full response (headers and data).
The content length advertised is about 800 bytes out.

comment:3 @dasher@…9 years ago

Removing the content-length header fixes the problem.

comment:4 @foolswisdom9 years ago

Dasher is this still a problem in the official tree?

comment:5 @foolswisdom9 years ago

  • Keywords xml-rpc added; bg|reporter-feedback removed
  • Priority changed from highest to normal
  • Resolution set to worksforme
  • Status changed from new to closed
  • Summary changed from XMLRPC returns incorrect content length header when using metaWeblog.getRecentPosts to XML-RPC returns incorrect content length header when using metaWeblog.getRecentPosts

Closing WORKSFORME without testing.
No response to my question above.

comment:6 follow-up: @wonderboymusic4 years ago

  • Resolution worksforme deleted
  • Status changed from closed to reopened

This is a problem for me as well - I just imported a bunch of "legacy" content from our old CMS and am getting the wrong content-length when I return content that has a bunch of JavaScript and CSS in it - gross, I know. The XML-RPC requests work when I debug in PHP code, but they break in Java code when the response are returned with wrong Content-Length, typically 12 characters less than actually exist.

Seems like the list of headers should be filterable

comment:7 @SergeyBiryukov4 years ago

  • Milestone set to Awaiting Review

comment:8 in reply to: ↑ 6 @westi4 years ago

  • Keywords WPMU removed

Replying to wonderboymusic:

This is a problem for me as well - I just imported a bunch of "legacy" content from our old CMS and am getting the wrong content-length when I return content that has a bunch of JavaScript and CSS in it - gross, I know. The XML-RPC requests work when I debug in PHP code, but they break in Java code when the response are returned with wrong Content-Length, typically 12 characters less than actually exist.

Seems like the list of headers should be filterable

Could you provide an example (redacted if necessary) post content that can be used to reproduce this error?

@wonderboymusic4 years ago

Content returned truncated

comment:9 @wonderboymusic3 years ago

  • Resolution set to wontfix
  • Status changed from reopened to closed

This shouldn't be fixed in class-IXR.php - you should fix it by subclassing wp_xmlrpc_server which extends IXR_Server:

include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-xmlrpc-server.php' );

add_filter( 'wp_xmlrpc_server_class', function () {
	return 'eMusic_XMLRPC_Server';
} );

class eMusic_XMLRPC_Server extends wp_xmlrpc_server {
	function __construct() {
		parent::__construct();
	}

	function output( $xml ) {
                $xml = '<?xml version="1.0"?>' . PHP_EOL . $xml;
                header( 'Connection: close' );
                header( 'Content-Type: text/xml' );
                header( 'Date: ' . date( 'r' ) );
                echo $xml;
                exit();
        }
}

comment:10 @markoheijnen3 years ago

Is the fix only to add: header( 'Connection: close' );?

comment:11 @wonderboymusic3 years ago

No, removing the Content-Length header - here's the original:

    function output($xml)
    {
        $xml = '<?xml version="1.0"?>'."\n".$xml;
        $length = strlen($xml);
        header('Connection: close');
        header('Content-Length: '.$length);
        header('Content-Type: text/xml');
        header('Date: '.date('r'));
        echo $xml;
        exit;
    }

comment:12 @SergeyBiryukov3 years ago

  • Milestone Awaiting Review deleted

comment:13 @slackbot3 months ago

This ticket was mentioned in Slack in #core by wpdavis. View the logs.

comment:14 @wpdavis3 months ago

  • Keywords has-patch added
  • Resolution wontfix deleted
  • Status changed from closed to reopened

Adding a patch to fix this in core. Just spent quite a while tracking this one back and would like to help others avoid that same unpleasantness if possible.

comment:15 @SergeyBiryukov3 months ago

  • Milestone set to Awaiting Review

comment:16 @wpdavis3 months ago

fyeyes: Using mbstring_binary_safe_encoding() or calling mb_strlen() directly with a variety of encodings does not seem to return the proper strlen, so there must be something else going on here. Continuing my search. But it seems having no content-length would be massively preferable to an incorrect content-length.

@SergeyBiryukov3 months ago

comment:17 @SergeyBiryukov3 months ago

I was thinking of something like 2567.diff. Does that not work as expected?

comment:18 @wpdavis3 months ago

Unfortunately it does not. I'm doing some research as to why.

comment:19 @dd323 months ago

This feels like something might be compressing the response (plugin, php, or server) and not overriding the Content-Length header specified.

comment:20 @wpdavis2 months ago

@dd32 the header is too short, not too long

Note: See TracTickets for help on using tickets.