Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow access to ServerRequest connection for "raw" response? #181

Closed
andig opened this issue Apr 27, 2017 · 7 comments
Closed

Allow access to ServerRequest connection for "raw" response? #181

andig opened this issue Apr 27, 2017 · 7 comments

Comments

@andig
Copy link
Contributor

andig commented Apr 27, 2017

We have a use case where we receive HTTP requests for passing to downstream applications- basically a load balancer. That request needs be parsed and headers modified- so a good case for the HTTP server.

For the downstream application I'm not interested in the actual response semantics, I'd just want to pipe back to the original requester what the downstream sends.

Not sure this is entirely too specific, but would it be possible to return e.g. a stream instead of a Response for use in Server->handleRequest?

@ssipos90
Copy link

+1, interested

@ssipos90
Copy link

I think you can do $downstreamResponse->pipe($originalResponse) or something similar.

@ssipos90
Copy link

The other way around.

@clue
Copy link
Member

clue commented Apr 28, 2017

Thanks for raising this interesting question!

I'm not sure I follow, but it looks like you may want to look into examples/4. The Response class allows you to add any stream as a response body.

I hope this helps 👍

If you feel anything's missing, please provide a simple gist, some pseudo code / overview would be much appreciated and I'm happy to look into this 👍

@andig
Copy link
Contributor Author

andig commented Apr 28, 2017

Thank you @clue for looking into this. I've sketched three drafts I could think of that would allow my use case without reinventing larger parts of the server. Option 1 would require some changes to the server response handling, options 2 and 3 require the additional $requestor parameter:

public function onRelay(ServerRequestInterface $request, Connection $requestor)
{
    // open slave connection
    $stream = @stream_socket_client($slave['host'], $errno, $errstr, $this->timeout);
    $connection = new \React\Socket\Connection($stream, $this->loop);

    // pipe request to slave
    $connection->write(Psr7Implementation\str($request));
    $request->getBody()->pipe($connection);

    // pipe response to master - alternative 1
    return $connection;
    
    // pipe response to master - alternative 2
    $connection->on(
        'data',
        function ($buffer) use ($requestor) {
            $requestor->write($buffer);
        }
    );

    // pipe response to master - alternative 3
    $connection->pipe($requestor);
}

Another valid approach might be subclassing the server instead.

Am I making sense here?

@clue
Copy link
Member

clue commented May 2, 2017

This is very valid use case indeed 👍 However, it's my understanding that this particular code would best be implemented by using an HTTP client in your HTTP request handler so that your server essentially works as a proxy server. The HTTP client will return a "normal" ResponseInterface that can directly be returned (or modified) by the server callback so that this requires literally zero effort on the server side.

This allows the server to follow "normal" HTTP semantics (request-response-cycle), header validation and automatic Transfer-Encoding etc. and to manage the underlying connections to allow persistent connections via #39 in the future.

This will also be significantly easier once the HTTP client will be merged as part of this package via #148.

Do you feel something's missing here or does this cover your use case? 👍

@andig
Copy link
Contributor Author

andig commented May 5, 2017

Do you feel something's missing here or does this cover your use case?

I'm closing this issue now. After client has PSR-7 semantics I'll need to recheck if the approach is feasible especially in the light of async body streams.

@andig andig closed this as completed May 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants