From 2651c9ea02f7d365c25df4ed9aabf5308247c5c2 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Wed, 18 Mar 2015 15:56:14 +0100 Subject: [PATCH] Support redirections for remote images, fix #87, fix #90. --- CHttpGet.php | 11 ++++++++++- REVISION.md | 1 + webroot/imgd.php | 11 ++++++++++- webroot/imgp.php | 11 ++++++++++- webroot/imgs.php | 11 ++++++++++- 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/CHttpGet.php b/CHttpGet.php index f81570d..dae7bdc 100644 --- a/CHttpGet.php +++ b/CHttpGet.php @@ -62,7 +62,14 @@ class CHttpGet */ public function parseHeader() { - $header = explode("\r\n", rtrim($this->response['headerRaw'], "\r\n")); + //$header = explode("\r\n", rtrim($this->response['headerRaw'], "\r\n")); + + $rawHeaders = rtrim($this->response['headerRaw'], "\r\n"); + # Handle multiple responses e.g. with redirections (proxies too) + $headerGroups = explode("\r\n\r\n", $rawHeaders); + # We're only interested in the last one + $header = explode("\r\n", end($headerGroups)); + $output = array(); if ('HTTP' === substr($header[0], 0, 4)) { @@ -99,6 +106,8 @@ class CHttpGet CURLINFO_HEADER_OUT => $debug, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 5, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_MAXREDIRS => 2, ); $ch = curl_init(); diff --git a/REVISION.md b/REVISION.md index fdbcab0..6f49275 100644 --- a/REVISION.md +++ b/REVISION.md @@ -8,6 +8,7 @@ Revision history v0.7.0.x (latest) ------------------------------------- +* Support redirections for remote images, fix #87, fix #90. * Improving usage of Travis and Scrutinizer. * Naming cache-file using md5 for remote images, fix #86. * Loading images without depending on filename extension, fix #85. diff --git a/webroot/imgd.php b/webroot/imgd.php index 6c302c0..d1080f5 100644 --- a/webroot/imgd.php +++ b/webroot/imgd.php @@ -99,7 +99,14 @@ class CHttpGet */ public function parseHeader() { - $header = explode("\r\n", rtrim($this->response['headerRaw'], "\r\n")); + //$header = explode("\r\n", rtrim($this->response['headerRaw'], "\r\n")); + + $rawHeaders = rtrim($this->response['headerRaw'], "\r\n"); + # Handle multiple responses e.g. with redirections (proxies too) + $headerGroups = explode("\r\n\r\n", $rawHeaders); + # We're only interested in the last one + $header = explode("\r\n", end($headerGroups)); + $output = array(); if ('HTTP' === substr($header[0], 0, 4)) { @@ -136,6 +143,8 @@ class CHttpGet CURLINFO_HEADER_OUT => $debug, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 5, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_MAXREDIRS => 2, ); $ch = curl_init(); diff --git a/webroot/imgp.php b/webroot/imgp.php index 6591687..89f87c5 100644 --- a/webroot/imgp.php +++ b/webroot/imgp.php @@ -99,7 +99,14 @@ class CHttpGet */ public function parseHeader() { - $header = explode("\r\n", rtrim($this->response['headerRaw'], "\r\n")); + //$header = explode("\r\n", rtrim($this->response['headerRaw'], "\r\n")); + + $rawHeaders = rtrim($this->response['headerRaw'], "\r\n"); + # Handle multiple responses e.g. with redirections (proxies too) + $headerGroups = explode("\r\n\r\n", $rawHeaders); + # We're only interested in the last one + $header = explode("\r\n", end($headerGroups)); + $output = array(); if ('HTTP' === substr($header[0], 0, 4)) { @@ -136,6 +143,8 @@ class CHttpGet CURLINFO_HEADER_OUT => $debug, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 5, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_MAXREDIRS => 2, ); $ch = curl_init(); diff --git a/webroot/imgs.php b/webroot/imgs.php index 6c302c0..d1080f5 100644 --- a/webroot/imgs.php +++ b/webroot/imgs.php @@ -99,7 +99,14 @@ class CHttpGet */ public function parseHeader() { - $header = explode("\r\n", rtrim($this->response['headerRaw'], "\r\n")); + //$header = explode("\r\n", rtrim($this->response['headerRaw'], "\r\n")); + + $rawHeaders = rtrim($this->response['headerRaw'], "\r\n"); + # Handle multiple responses e.g. with redirections (proxies too) + $headerGroups = explode("\r\n\r\n", $rawHeaders); + # We're only interested in the last one + $header = explode("\r\n", end($headerGroups)); + $output = array(); if ('HTTP' === substr($header[0], 0, 4)) { @@ -136,6 +143,8 @@ class CHttpGet CURLINFO_HEADER_OUT => $debug, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 5, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_MAXREDIRS => 2, ); $ch = curl_init();