From bfd00d26081b3a7a035d22ac6a90af6d2bce3b64 Mon Sep 17 00:00:00 2001 From: Tristan Date: Mon, 13 Jan 2025 21:44:01 +0100 Subject: [PATCH] avoids caching 'maven-metadata.xml*' and '*-SNAPSHOT*', avoids delivering already stored files of the same --- .htaccess | 4 +++- src/mvncache.php | 59 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/.htaccess b/.htaccess index 894b08a..a6e423d 100644 --- a/.htaccess +++ b/.htaccess @@ -1,3 +1,5 @@ RewriteEngine On -RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-f [OR] +RewriteCond %{REQUEST_FILENAME} =maven-metadata.xml [OR] +RewriteCond %{REQUEST_URI} -SNAPSHOT RewriteRule ^.*$ src/mvncache.php [L] diff --git a/src/mvncache.php b/src/mvncache.php index a3cfa7c..6b8ebd4 100644 --- a/src/mvncache.php +++ b/src/mvncache.php @@ -26,6 +26,25 @@ foreach ($config['mavenBaseUrls'] as $mvnBaseUrl) { if ($found) { $filesize = @stream_copy_to_stream($src, $tempFile); $found = 0 < $filesize; + $metaData = @stream_get_meta_data($src); + $headers = []; + $headerNames = [ + 'Content-Type', + 'Content-Length', + 'ETag', + 'Last-Modified', + ]; + + if (isset($metaData) && array_key_exists('wrapper_data', $metaData)) { + foreach ($metaData['wrapper_data'] as $header) { + foreach ($headerNames as $headername) { + if (stripos($header, $headername) === 0) { + $headerValue = trim(substr($header, strlen($headername) + 1)); // +1 to remove the colon + $headers[$headername] = $headerValue; + } + } + } + } } if ($found) { trigger_error("Artifact found at $srcUrl", E_USER_NOTICE); @@ -39,20 +58,36 @@ if (!$found) { trigger_error("Artifact NOT found at any source", E_USER_WARNING); sendHttpReturnCodeAndMessage(404, 'Not found'); } else { - //echo $localFolder."
"; - if (!is_dir($localFolder)) { - mkdir($localFolder, 0770, true); - } - $dstPath = $baseLocalFolder.$requestedArtifact; - //echo $dstPath; - $dst = fopen($dstPath, 'w'); + // Rest temp file pointer fseek($tempFile, 0); - stream_copy_to_stream($tempFile, $dst); - if (is_file($dstPath)) { - chmod($dstPath, 0660); - header('Location: '.getServerProtocol().'://'.getRequestHostname().$_SERVER["REQUEST_URI"]); + + if (preg_match('/maven-metadata.xml/', $requestedArtifact) || preg_match('/-SNAPSHOT/', $requestedArtifact)) { + trigger_error("Found artifact not to cache: ".$requestedArtifact, E_USER_NOTICE); + foreach ($headers as $header => $value) { + if (is_array($value)) { + $value = implode(', ', $value); + } + header($header.': '.$value); + } + + stream_copy_to_stream($tempFile, fopen('php://output', 'w')); } else { - header('HTTP/1.0 404 Not Found'); + trigger_error("Found artifact to cache: ".$requestedArtifact, E_USER_NOTICE); + //echo $localFolder."
"; + if (!is_dir($localFolder)) { + mkdir($localFolder, 0770, true); + } + $dstPath = $baseLocalFolder.$requestedArtifact; + //echo $dstPath; + $dst = fopen($dstPath, 'w'); + + stream_copy_to_stream($tempFile, $dst); + if (is_file($dstPath)) { + chmod($dstPath, 0660); + header('Location: '.getServerProtocol().'://'.getRequestHostname().$_SERVER["REQUEST_URI"]); + } else { + header('HTTP/1.0 404 Not Found'); + } } } ?>