Allow wp_remote_post to send a body consisting of entirely '0', which may be used when PUT'ing or POST'ing data to a API which accepts a raw chunk of data rather than key=value pairs (Such as some REST API's). Fixes #14184

git-svn-id: https://develop.svn.wordpress.org/trunk@22047 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dion Hulse 2012-09-27 06:54:17 +00:00
parent 22b3981229
commit 93ba84c528
1 changed files with 7 additions and 11 deletions

View File

@ -169,21 +169,17 @@ class WP_Http {
if ( WP_Http_Encoding::is_available() )
$r['headers']['Accept-Encoding'] = WP_Http_Encoding::accept_encoding();
if ( empty($r['body']) ) {
$r['body'] = null;
// Some servers fail when sending content without the content-length header being set.
// Also, to fix another bug, we only send when doing POST and PUT and the content-length
// header isn't already set.
if ( ($r['method'] == 'POST' || $r['method'] == 'PUT') && ! isset( $r['headers']['Content-Length'] ) )
$r['headers']['Content-Length'] = 0;
} else {
if ( strlen( $r['body'] ) || 'POST' == $r['method'] || 'PUT' == $r['method'] ) {
if ( is_array( $r['body'] ) || is_object( $r['body'] ) ) {
$r['body'] = http_build_query( $r['body'], null, '&' );
if ( ! isset( $r['headers']['Content-Type'] ) )
$r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' );
$r['headers']['Content-Length'] = strlen( $r['body'] );
}
if ( '' === $r['body'] )
$r['body'] = null;
if ( ! isset( $r['headers']['Content-Length'] ) && ! isset( $r['headers']['content-length'] ) )
$r['headers']['Content-Length'] = strlen( $r['body'] );
}
@ -914,7 +910,7 @@ class WP_Http_Streams {
$arrContext['http']['header'] .= $proxy->authentication_header() . "\r\n";
}
if ( ! empty($r['body'] ) )
if ( ! is_null( $r['body'] ) )
$arrContext['http']['content'] = $r['body'];
$context = stream_context_create($arrContext);
@ -1107,7 +1103,7 @@ class WP_Http_Curl {
break;
default:
curl_setopt( $handle, CURLOPT_CUSTOMREQUEST, $r['method'] );
if ( ! empty( $r['body'] ) )
if ( ! is_null( $r['body'] ) )
curl_setopt( $handle, CURLOPT_POSTFIELDS, $r['body'] );
break;
}