From 0549800afeb7b8db389d79a9fd48feecf875ab23 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Mon, 12 Dec 2016 12:58:12 +0100 Subject: Error handling improved, logging included --- src/config/config.inc.php | 10 +++++ src/lib/error.handler.inc.php | 44 ++++++++++++++++++++++ src/lib/functions.http.inc.php | 83 ++++++++++++++++++++++++++++++++++++++++++ src/loadArtifact.php | 34 +++++++++++++++++ src/mvncache.php | 63 ++++++++++++++++++++++---------- 5 files changed, 215 insertions(+), 19 deletions(-) create mode 100644 src/config/config.inc.php create mode 100644 src/lib/error.handler.inc.php create mode 100644 src/lib/functions.http.inc.php create mode 100644 src/loadArtifact.php diff --git a/src/config/config.inc.php b/src/config/config.inc.php new file mode 100644 index 0000000..ac61c27 --- /dev/null +++ b/src/config/config.inc.php @@ -0,0 +1,10 @@ + ['http://repo1.maven.org/maven2', ], + 'logfile' => 'logs/mvncache.log', +]; +?> \ No newline at end of file diff --git a/src/lib/error.handler.inc.php b/src/lib/error.handler.inc.php new file mode 100644 index 0000000..9518e4b --- /dev/null +++ b/src/lib/error.handler.inc.php @@ -0,0 +1,44 @@ + \ No newline at end of file diff --git a/src/lib/functions.http.inc.php b/src/lib/functions.http.inc.php new file mode 100644 index 0000000..44296b5 --- /dev/null +++ b/src/lib/functions.http.inc.php @@ -0,0 +1,83 @@ + $data]; + } + header('Content-Type: application/json'); + sendHttpReturnCodeAndMessage($code, json_encode($data)); +} + +function sendHttpReturnCodeAndMessage($code, $text = '') { + http_response_code($code); + exit($text); +} + +function getOptionalPostParameter($parameterName, $default = NULL) { + $parameter = $_POST[$parameterName]; + if (!isset($parameter) || is_null($parameter) || empty($parameter)) { + $parameter = $default; + } + return $parameter; +} + +function getMandatoryPostParameter($parameterName, $message = '', $json = false) { + $parameter = $_POST[$parameterName]; + if (!isset($parameter) || is_null($parameter) || empty($parameter)) { + if (empty($message)) { + if ($json) { + $message = ['msg' => 'Missing parameter.', 'parameters' => ['missing_parameter' => $parameterName]]; + } else { + $message = 'Missing mandatory parameter "'.$parameterName.'".'; + } + } + if (!$json) { + sendHttpReturnCodeAndMessage(400, $message); + } else { + sendHttpReturnCodeAndJson(400, $message); + } + } + return $parameter; +} +?> \ No newline at end of file diff --git a/src/loadArtifact.php b/src/loadArtifact.php new file mode 100644 index 0000000..6205164 --- /dev/null +++ b/src/loadArtifact.php @@ -0,0 +1,34 @@ +"; +if (!is_dir($localFolder)) { + mkdir($localFolder, 0770, true); +} + +$srcUrl = $mvnBaseUrl.$requestedArtifact; +//echo $srcUrl."
"; +$src = fopen($srcUrl, 'r'); + +$dstPath = $baseLocalFolder.$requestedArtifact; +//echo $dstPath; +$dst = fopen($dstPath, 'w'); + +$filesize = stream_copy_to_stream($src, $dst); + +if (0 == $filesize) { + unlink($dstPath); + unlink($localFolder); +} + +if (is_file($dstPath)) { + chmod($dstPath, 0660); + header('Location: http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]); +} else { + header('HTTP/1.0 404 Not Found'); +} +?> diff --git a/src/mvncache.php b/src/mvncache.php index d296a4f..ef75a16 100644 --- a/src/mvncache.php +++ b/src/mvncache.php @@ -1,29 +1,54 @@ "; -if (!is_dir($localFolder)) { - mkdir($localFolder, 0770, true); -} -$srcUrl = $mvnBaseUrl.$requestedArtifact; -//echo $srcUrl."
"; -$src = fopen($srcUrl, 'r'); - -$dstPath = $baseLocalFolder.$requestedArtifact; -//echo $dstPath; -$dst = fopen($dstPath, 'w'); - -stream_copy_to_stream($src, $dst); +$tempFile = tmpfile(); +foreach ($config['mavenBaseUrls'] as $mvnBaseUrl) { + $srcUrl = $mvnBaseUrl.$requestedArtifact; + //echo $srcUrl."
"; + $src = fopen($srcUrl, 'r'); + $found = FALSE !== $src; + + if ($found) { + $filesize = @stream_copy_to_stream($src, $tempFile); + $found = 0 < $filesize; + } + if ($found) { + trigger_error("Artifact found at $srcUrl", E_USER_NOTICE); + break; + } else { + trigger_error("Artifact NOT found at $srcUrl", E_USER_WARNING); + } +} -if (is_file($dstPath)) { - chmod($dstPath, 0660); - header('Location: http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]); +if (!$found) { + trigger_error("Artifact NOT found at any source", E_USER_WARNING); + sendHttpReturnCodeAndMessage(404, 'Not found'); } else { - header('HTTP/1.0 404 Not Found'); + //echo $localFolder."
"; + if (!is_dir($localFolder)) { + mkdir($localFolder, 0770, true); + } + $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); + header('Location: http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]); + } else { + header('HTTP/1.0 404 Not Found'); + } } ?> -- cgit v1.2.3