Athlon1600 / php-proxy

A web proxy script written in PHP and built as an alternative to Glype.
https://www.php-proxy.com
MIT License
298 stars 158 forks source link

response->headers->set Location => exit the proxy and call directly the new location #27

Closed domsie closed 8 years ago

domsie commented 8 years ago

Hi, i have a problem with a response-header-location change: I use the HeaderRewritePlugin and change there at script-end the response-header-location [only in a given case]. The proxy continues [hourglass] and the new page is called only very much delayed [one minute]. The second time || call, the same response-header-location change happens immediately. If I exit the response-header-location change [the HeaderRewritePlugin] with an exit you only get a blank site. [and no header location change anymore]. Can i and how can i exit the proxy call, when a file-download [and my header location change] starts? Is there a function implement for break // exit the proxy call?

Athlon1600 commented 8 years ago

I'm not sure what you're asking for here. Can you show me the code you have now?

domsie commented 8 years ago

Hi, sorry for this :/ It is difficult for me to describe this behavior in english ;) Here is the code [from HeaderRewritePlugin.php):

` function onHeadersReceived(ProxyEvent $event){

    // so stupid... onCompleted won't be called on "streaming" responses
    $response = $event['response'];
    $request_url = $event['request']->getUri();

    $code = $response->getStatusCode();
    $text = $response->getStatusText();

    if($code >= 400 && $code <= 600){
        throw new \Exception("Error accessing resource: {$code} - {$text}");
    }

    // we need content-encoding (in case server refuses to serve it in plain text)
    $forward_headers = array('content-type', 'content-length', 'accept-ranges', 'content-range', 'content-disposition', 'location', 'set-cookie');

    foreach($response->headers->all() as $name => $value){

        // is this one of the headers we wish to forward back to the client?
        if(!in_array($name, $forward_headers)){
            $response->headers->remove($name);
        }
    }

//implement by me $response->headers->set('Access-Control-Allow-Origin', '*'); $response->headers->set('Access-Control-Allow-Credentials', 'true'); $response->headers->set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); $response->headers->set('Access-Control-Allow-Headers', 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type');

    if(!$response->headers->has('content-disposition')){

        $url_path = parse_url($request_url, PHP_URL_PATH);
        $filename = basename($url_path);

        $response->headers->set('content-disposition', 'filename="'.$filename.'"');
    }

 //implement by me
    if($response->headers->has('content-disposition')){

        .....
                    .....

        if ($inside == 1 || (string)$inside == "1" || $attachment == 1 || (string)$attachment == "1")
        {
                //File herunterladen
                $ch = curl_init();
                $user_agent = "Mozilla/4.0";
                curl_setopt ($ch, CURLOPT_URL, $request_url);
                curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
                curl_setopt ($ch, CURLOPT_HEADER, 1);
                curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
                curl_setopt ($ch, CURLOPT_TIMEOUT, 500);
                $content = curl_exec($ch);
                curl_close($ch); 
                $uniqid = uniqid();
                $filename = "/srv/www/htdocs/file_downloads/".$uniqid;
                $handle = fopen($filename, "w");
                fwrite($handle, $content);
                fclose($handle);
                //File herunterladen

                //Parameter base64 encoded übergeben
                $request_url_64 = base64_encode($request_url);
                $content_disposition_64 = base64_encode($content_disposition);
                $content_type_64 = base64_encode($content_type);

                $md_5 = md5_file($filename);

                $response->headers->set('location', 'https://..../safe_download.php?unique='.$uniqid.'&uri='.$request_url_64.'&cd='.$content_disposition_64.'&f='.$content_type_64.'&fs='.filesize($filename).' Byte&md5='.$md_5);

                //exit;
        }
        else
        {
            // proxify header location value
            if($response->headers->has('location')){

                $location = $response->headers->get('location');

                // just in case this is a relative url like: /en
                $response->headers->set('location', proxify_url($location, $request_url));
            }
        }

} `

With
$response->headers->set('location', 'https://..../safe_download.php?unique='.$uniqid.'&uri='.$request_url_64.'&cd='.$content_disposition_64.'&f='.$content_type_64.'&fs='.filesize($filename).' Byte&md5='.$md_5); i am want to make a redirect when the download-file match a special content-type; else i set the location with your part ` // proxify header location value if($response->headers->has('location')){

                $location = $response->headers->get('location');

                // just in case this is a relative url like: /en
                $response->headers->set('location', proxify_url($location, $request_url));
            }

` ....

when the header location change runs the first time (so when the file download runs the first time with this proxy) the redirect needs a loooooooot time; the second time it is better. When i am put a exit; after the location change [ i thought that after the location change a few plugins still do a job and want to exit this and only call the redirect] i only become a blank site [no redirect or so].

Athlon1600 commented 8 years ago

I'm still not sure what you're trying to do here... and I think that there must be an easier way to do whatever you're trying to do. Why would you ever need to wait 60 seconds before downloading?