jenssegers / php-proxy

A PHP proxy script with https and post support
https://jenssegers.com
933 stars 266 forks source link

Extreme delay in fread - 'connection: keep-alive' header (?) #47

Closed Justin-Maxwell closed 6 years ago

Justin-Maxwell commented 7 years ago

UPDATED:

First, this probably isn't directly php-proxy, but downstream, but I'm a novice in these PHP PSR-7 parts...

Using the README boilerplate code, I was getting extremely slow responses from $proxy->to().

15 seconds, for http://example.com, several minutes for longer pages. I could see (using charles) that the remote server was returning with status=200 in milliseconds.

Via XDebug profiling (there's nothing like something that makes no sense to mandate learning new tools!) I could see that it gets tied up in fread. I've attached a screenshot, not sure if any use though.

So, anyhow, after a ton of isolating things, and progressively eliminating bits out of ::fromGlobals(), I eventually found that it seems to be the connection: keep-alive header being passed along from the client.

Using almost the stock README example (see below), but modifying the line near the top to:

$request = ServerRequestFactory::fromGlobals()->withHeader('connection', 'close');

Makes everything zippy, (if no longer gzippy [anachronistic humour fail])

<?php

require __DIR__ . '/vendor/autoload.php';

use Proxy\Proxy;
use Proxy\Adapter\Guzzle\GuzzleAdapter;
use Proxy\Filter\RemoveEncodingFilter;
use Zend\Diactoros;
use Zend\Diactoros\ServerRequestFactory;

// Create a PSR7 request based on the current browser request.
$request2 = ServerRequestFactory::fromGlobals()->withHeader('connection', 'close');

// Create a guzzle client
$guzzle = new GuzzleHttp\Client(['proxy' => 'tcp://localhost:8888']);

// Create the proxy instance
$proxy = new Proxy(new GuzzleAdapter($guzzle));

// Add a response filter that removes the encoding headers.
$proxy->filter(new RemoveEncodingFilter());

$request= $request2->withUri(new \Zend\Diactoros\Uri("http://www.manchestereveningnews.co.uk/news/local-news/st-gabriels-rc-high-school-7540058"));
//$request= $request2->withUri(new \Zend\Diactoros\Uri("http://www.example.com"));

// Forward the request
// and get the response.
$response = $proxy
    ->forward($request)
    ->to('http://www.manchestereveningnews.co.uk');

// Output response to the browser.
(new Zend\Diactoros\Response\SapiEmitter)->emit($response);

XDebug/cachegrind profile selection_001

Justin-Maxwell commented 7 years ago

I may have posted too soon. My problem is back. I will update when I've rechecked.

Justin-Maxwell commented 7 years ago

'connection: keep-alive' makes more sense, but shouldn't the proxy still close the connection once the transfer is Status=200, so the client can get the content and get on with retrieving the other page assets etc? I'm new to working with HTTP at this level...