ID: 47684
Comment by: r-ser at yandex dot ru
Reported By: r-ser at yandex dot ru
Status: Feedback
Bug Type: HTTP related
Operating System: Linux 2.6.26
PHP Version: 5.2.9
New Comment:
Add string
php_stream_context_set_option(context, "http", "method", (zval *)
"GET");
Before
stream = php_stream_url_wrap_http_ex(wrapper, new_path, mode, options,
opened_path, context, --redirect_max, 0 STREAMS_CC TSRMLS_CC);
In ext/standard/http_fopen_wrapper.c
Solved problem.
Previous Comments:
------------------------------------------------------------------------
[2009-03-17 16:13:21] r-ser at yandex dot ru
I can write this code as
$header = "Content-type: multipart/form-data,
boundary=$boundary\r\nContent-Length: ".strlen($data)."\r\n";
But nothing is changed.
Becouse error is in _second_ request.
cat test.php
------------
<?php
$context = stream_context_create(array('http' =>
array('method'=>'POST', 'header'=> 'Content-Length: 0', 'content' =>
'')));
echo file_get_contents('http://localhost/1.php', false, $context);
?>
------------
cat 1.php
------------
<?php
header("Location: 2.php");
?>
------------
cat 2.php
------------
<?
echo "Method: ".$_SERVER['REQUEST_METHOD']."\n";
$headers = getallheaders();
foreach ($headers as $header => $value) echo "$header: $value\n";
?>
------------
Note: 1.php and 2.php are located in root of webserver
if change http://localhost/1.php to http://localhost/2.php you can see
differents headers..
This works with Apache web server.
But not work with some elses, like lighttpd/1.5.0 /* like in example in
first message */
------------------------------------------------------------------------
[2009-03-17 11:00:15] [email protected]
Why are you not passing the content-length in there..? I mean, isn't
that the obvious error..? :)
------------------------------------------------------------------------
[2009-03-17 00:15:55] r-ser at yandex dot ru
Description:
------------
When use file_get_contents + stream_context_create to post file to web
server and the page in web server return redirect to another page
("location" field in answer), then file_get_contents requested
redirected page with POST header without "Content-Length:" field in
request and server response "HTTP/1.0 411 Length Required".
I think, it's two ways to fix problem
1) 'method' must be change to GET in requests to redirect pages.
2) Add 'Content-Length: 0' to next POST redirect requests
Reproduce code:
---------------
$argv[1] = "image.jpg";
$boundary = "AaB03x1234567890";
$type = mime_content_type($argv[1]);
$data = "--$boundary\r\nContent-Disposition: form-data;
name=\"fileupload\"; filename=\"".basename($argv[1])."\"
Content-Type: ".$type." \r\nContent-Transfer-Encoding:
binary\r\n\r\n".file_get_contents($argv[1])."\r\n--$boundary--";
$header = "Content-type: multipart/form-data, boundary=$boundary";
$context = stream_context_create(array('http' =>
array('method'=>'POST', 'header'=> $header, 'content' => $data)));
$result = file_get_contents('http://load.imageshack.us', false,
$context);
echo $result;
Expected result:
----------------
failed to open stream: HTTP request failed! HTTP/1.0 411 Length
Required
Actual result:
--------------
Normal redirected page
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=47684&edit=1