php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #58249 toMessageTypeObject() throws exception
Submitted: 2008-06-24 16:37 UTC Modified: 2008-07-11 05:53 UTC
From: nicholas at omniti dot com Assigned: mike (profile)
Status: Not a bug Package: pecl_http (PECL)
PHP Version: 5.2.5 OS: Mac OS 10.5
Private report: No CVE-ID: None
 [2008-06-24 16:37 UTC] nicholas at omniti dot com
Description:
------------
I'm not sure if this is a bug, a feature request, or a problem in documentation. Hopefully a solution already exists and I'm just having trouble finding it.

My intention is to cast an HttpMessage object to an HttpResponse object using toMessageTypeObject. I will then call setStream() and send() on the resultant object, presumably sending the response to a client at the other end of said stream.

The trouble is that toMessageTypeObject() seems to assume that the response it is creating will be the response for the script that it is called by.

I am writing an HTTP proxy, and the proxy's output is all dumped to the terminal using echo. Everything works on the request side of things, and I get a correct response back from the server; however, when I try to cast the HttpMessage object that I get from HttpRequest::send() to an HttpResponse object using toMessageObjectType(), I get an exception stating that the headers cannot be modified because of output already sent (the output from echo, print etc.). The HttpResponse will be sent via a stream to the proxy's client.

I've included some rough pseudo-code below to help illustrate my situation.

Reproduce code:
---------------
<?php

...
//Get request from client
$request = fread($streamToClient,1024);

//Parse the $request string using HttpMessage
$HttpMessage1 = new HttpMessage($request);
echo "Original Request:\n".$HttpMessage1->toString();

//Cast HttpMessage to an HttpRequest
$HttpRequest1 = $HttpMessage1->toMessageTypeObject();

//Mess with the Request a bit to remove proxy-specific
//headers
...

//Send the HttpRequest and store the response as an
//HttpMessage
$HttpMessage2 = $HttpRequest1->send();
echo "Sent Request:\n".$HttpRequest1->getRawRequestMessage();
echo "Received Response:\n".$HttpRequest1->getRawResponseMessage();

//Cast the response HttpMessage to an HttpRequest
//This is where the exception occurs due to output from
//previous lines.
$HttpResponse1 = $HttpMessage2->toMessageTypeObject();

//Set the output stream for the response, and send it to
//the client.
$HttpResponse1->setStream($streamToClient);
$HttpResponse1->send();

...


Expected result:
----------------
My script is run via CLI, so I would expect output to go to two different places. Output from echo, print etc would be sent to the terminal screen where the script is running, providing the user running the script with a few of the requests and responses.

The HttpResponse object would be output using the stream identified by setStream() in the above example to output to the proxy's client. You can see that in this example, echo and printed output has nothing to do with the HttpRequest object.

Actual result:
--------------
Fatal error: Uncaught exception 'HttpException' with message 'Cannot modify header information - headers already sent by (output started at /path/to/proxy.php:9)' in /path/to/proxy.php:29
Stack trace:
#0 /path/to/proxy.php(29): HttpMessage->toMessageTypeObject()
#1 {main}
  thrown in /path/to/proxy.php on line 29


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-11 05:53 UTC] mike@php.net
You're confusing the concept of pecl_http's HttpResponse class.

It's solely a static class to manage the HTTP response from the script to the "client" who requested this particular script.

HttpResponse::setStream() accepts the *source* stream to read from for response data.

If you want to pass the foreign server response back to your client/stream $streamToClient, then just fwrite() it there.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 12:01:28 2024 UTC