avoids caching 'maven-metadata.xml*' and '*-SNAPSHOT*', avoids delivering already stored files of the same

This commit is contained in:
Tristan 2025-01-13 21:44:01 +01:00
parent 225ae1f92a
commit bfd00d2608
2 changed files with 50 additions and 13 deletions

View file

@ -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]

View file

@ -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,6 +58,21 @@ if (!$found) {
trigger_error("Artifact NOT found at any source", E_USER_WARNING);
sendHttpReturnCodeAndMessage(404, 'Not found');
} else {
// Rest temp file pointer
fseek($tempFile, 0);
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 {
trigger_error("Found artifact to cache: ".$requestedArtifact, E_USER_NOTICE);
//echo $localFolder."<br>";
if (!is_dir($localFolder)) {
mkdir($localFolder, 0770, true);
@ -46,7 +80,7 @@ if (!$found) {
$dstPath = $baseLocalFolder.$requestedArtifact;
//echo $dstPath;
$dst = fopen($dstPath, 'w');
fseek($tempFile, 0);
stream_copy_to_stream($tempFile, $dst);
if (is_file($dstPath)) {
chmod($dstPath, 0660);
@ -55,4 +89,5 @@ if (!$found) {
header('HTTP/1.0 404 Not Found');
}
}
}
?>