From fca464f0b264a5b1c829e8da8bad98313a748b30 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Tue, 15 Feb 2022 11:43:18 +0100 Subject: [PATCH] Resolve mail redirects https://github.com/the-djmaze/snappymail/issues/234#issuecomment-1040120224#234 --- snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php | 2 ++ snappymail/v/0.0.0/app/libraries/snappymail/http/request.php | 1 + .../v/0.0.0/app/libraries/snappymail/http/request/socket.php | 1 + .../v/0.0.0/app/libraries/snappymail/http/response.php | 5 +++++ 4 files changed, 9 insertions(+) diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php b/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php index 01351bae4..403dcaef3 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php @@ -351,6 +351,7 @@ class ServiceActions \header('X-Content-Location: '.$aData['Url']); $tmp = \tmpfile(); $HTTP = \SnappyMail\HTTP\Request::factory(); + $HTTP->max_redirects = 2; $HTTP->streamBodyTo($tmp); $oResponse = $HTTP->doRequest('GET', $aData['Url']); if ($oResponse && 200 === $oResponse->status @@ -360,6 +361,7 @@ class ServiceActions \header('Content-Type: ' . $oResponse->getHeader('content-type')); \header('Cache-Control: public'); \header('Expires: '.\gmdate('D, j M Y H:i:s', 2592000 + \time()).' UTC'); + \header('X-Content-Redirect-Location: '.$oResponse->final_uri); \rewind($tmp); \fpassthru($tmp); exit; diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/http/request.php b/snappymail/v/0.0.0/app/libraries/snappymail/http/request.php index aa672b992..301d87c34 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/http/request.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/http/request.php @@ -129,6 +129,7 @@ abstract class Request throw new \RuntimeException("Fetching URL not allowed: {$url}"); } + $this->stream && \rewind($this->stream); $result = $this->__doRequest($method, $url, $body, \array_merge($this->headers, $extra_headers)); // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3 diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/http/request/socket.php b/snappymail/v/0.0.0/app/libraries/snappymail/http/request/socket.php index 6d06283f4..f0b65d7a1 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/http/request/socket.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/http/request/socket.php @@ -157,6 +157,7 @@ class Socket extends \SnappyMail\HTTP\Request "\r\n" === \fread($sock, 2); } else { \fwrite($this->stream, \fread($sock, 1024)); +// \stream_copy_to_stream($sock, $this->stream); } } } else { diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/http/response.php b/snappymail/v/0.0.0/app/libraries/snappymail/http/response.php index 2375996fa..aba9be140 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/http/response.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/http/response.php @@ -67,6 +67,11 @@ class Response return null; } + public function getHeaders() : array + { + return $this->headers; + } + public function getRedirectLocation() : ?string { if ($location = $this->getHeader('location')) {