php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54137 file_get_contents POST request sends additional line breaks
Submitted: 2011-03-02 14:45 UTC Modified: 2011-05-29 11:23 UTC
Votes:6
Avg. Score:4.8 ± 0.4
Reproduced:6 of 6 (100.0%)
Same Version:3 (50.0%)
Same OS:3 (50.0%)
From: maurice-php at mertinkat dot net Assigned: iliaa (profile)
Status: Closed Package: Streams related
PHP Version: 5.3.5 OS: all
Private report: No CVE-ID: None
 [2011-03-02 14:45 UTC] maurice-php at mertinkat dot net
Description:
------------
When doing HTTP POST requests using file_get_contents and an appropriate stream context (http method = POST, content = ...) two additional line breaks ("\r\n\r\n") are added below the POST message body. This is wrong according to the default encoding "application/x-www-form-urlencoded" and has been seen to upset some webservers (content length is incorrect).

Run the testscript and check the traffic using tcpdump/tcpflow or wireshark. You'll see additional lines at the end of the message body.

Attached is a very simple patch against http_fopen_wrapper.c from PHP 5.3.5 source which removes sending "\r\n\r\n".

Test script:
---------------
<?php

$ctx = stream_context_create(array(
    'http' => array(
        'method' => 'POST',
        'header' => 'Content-Type: application/x-www-form-urlencoded',
        'content' => 'a=1&b=2',
    )
));

$response = file_get_contents('http://www.php.net/', false, $ctx);
echo $response;



Patches

remove-newlines-from-post-body (last revision 2011-03-02 13:46 UTC by maurice-php at mertinkat dot net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-14 18:25 UTC] christophe dot bliard at trux dot info
I have the same problem here, with PHP 5.3.2 and also 5.2.10.

Another thing that may be interesting: if you do the same HTTP POST request on a 
web server on localhost, then there will not be any "\r\n\r\n" after the request.
 [2011-04-26 09:12 UTC] xiezhenye at gmail dot com
I also meet with this problem.
As http protocol, message body size should exactly match the content-length. when there exists unwanted "\r\n\r\n", it maybe cause some unexpected result, as connect resetted by server.
 [2011-05-17 01:08 UTC] spam at abma dot de
is this the same bug https://bugs.launchpad.net/ubuntu/+source/php5/+bug/650779 ?
 [2011-05-29 11:23 UTC] iliaa@php.net
Automatic comment from SVN on behalf of iliaa
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=311542
Log: Fixed bug #54137 (file_get_contents POST request sends additional line break).
 [2011-05-29 11:23 UTC] iliaa@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: iliaa
 [2011-05-29 11:23 UTC] iliaa@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2012-04-18 09:50 UTC] laruence@php.net
Automatic comment on behalf of iliaa
Revision: http://git.php.net/?p=php-src.git;a=commit;h=3ec51648ad440f915b2918e82f1859da168e0439
Log: Fixed bug #54137 (file_get_contents POST request sends additional line break).
 [2012-07-24 23:41 UTC] rasmus@php.net
Automatic comment on behalf of iliaa
Revision: http://git.php.net/?p=php-src.git;a=commit;h=3ec51648ad440f915b2918e82f1859da168e0439
Log: Fixed bug #54137 (file_get_contents POST request sends additional line break).
 [2013-11-17 09:38 UTC] laruence@php.net
Automatic comment on behalf of iliaa
Revision: http://git.php.net/?p=php-src.git;a=commit;h=3ec51648ad440f915b2918e82f1859da168e0439
Log: Fixed bug #54137 (file_get_contents POST request sends additional line break).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 08:01:29 2024 UTC