Error handling improved, logging included
This commit is contained in:
parent
13dd3487d7
commit
0549800afe
5 changed files with 215 additions and 19 deletions
10
src/config/config.inc.php
Normal file
10
src/config/config.inc.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
/*
|
||||
* Configuration file for mvn cache
|
||||
*/
|
||||
|
||||
return [
|
||||
'mavenBaseUrls' => ['http://repo1.maven.org/maven2', ],
|
||||
'logfile' => 'logs/mvncache.log',
|
||||
];
|
||||
?>
|
44
src/lib/error.handler.inc.php
Normal file
44
src/lib/error.handler.inc.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
function initTheDevStackErrorHandler($logfile) {
|
||||
// Save the log file name to constant for the error handler
|
||||
define("LOGFILE", $logfile);
|
||||
// Custom error handler that writes to a file
|
||||
set_error_handler("theDevStackErrorHandler");
|
||||
}
|
||||
|
||||
/**
|
||||
* Error handler to write all messages to a dedicated log file.
|
||||
*/
|
||||
function theDevStackErrorHandler($errno, $errstr, $errfile, $errline) {
|
||||
if (!(error_reporting() & $errno)) {
|
||||
// This error code is not included in error_reporting
|
||||
return;
|
||||
}
|
||||
$date = date(DATE_W3C);
|
||||
$str = "$date ";
|
||||
|
||||
switch ($errno) {
|
||||
case E_USER_ERROR:
|
||||
$str .= "ERROR [$errno]: $errstr, Fatal error on line $errline in file $errfile\n";
|
||||
break;
|
||||
|
||||
case E_USER_WARNING:
|
||||
$str .= "WARNING [$errno]: $errstr\n";
|
||||
break;
|
||||
|
||||
case E_USER_NOTICE:
|
||||
$str .= "NOTICE [$errno]: $errstr\n";
|
||||
break;
|
||||
|
||||
default:
|
||||
$str .= "Unknown error type: [$errno] $errstr\n";
|
||||
break;
|
||||
}
|
||||
|
||||
file_put_contents(LOGFILE, $str, FILE_APPEND);
|
||||
|
||||
/* Don't execute PHP internal error handler */
|
||||
return true;
|
||||
}
|
||||
?>
|
83
src/lib/functions.http.inc.php
Normal file
83
src/lib/functions.http.inc.php
Normal file
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
/*
|
||||
*
|
||||
* This file contains functions to be used to
|
||||
* extract information based on http request information.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Inspired by https://github.com/owncloud/core/blob/master/lib/private/appframework/http/request.php#L523
|
||||
*/
|
||||
function getServerProtocol() {
|
||||
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
|
||||
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], ',') !== false) {
|
||||
$parts = explode(',', $_SERVER['HTTP_X_FORWARDED_PROTO']);
|
||||
$proto = strtolower(trim($parts[0]));
|
||||
} else {
|
||||
$proto = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']);
|
||||
}
|
||||
// Verify that the protocol is always HTTP or HTTPS
|
||||
// default to http if an invalid value is provided
|
||||
return $proto === 'https' ? 'https' : 'http';
|
||||
}
|
||||
if (isset($_SERVER['HTTPS'])
|
||||
&& $_SERVER['HTTPS'] !== null
|
||||
&& $_SERVER['HTTPS'] !== 'off'
|
||||
&& $_SERVER['HTTPS'] !== '') {
|
||||
return 'https';
|
||||
}
|
||||
return 'http';
|
||||
}
|
||||
|
||||
function getRequestHostname() {
|
||||
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
|
||||
return strtolower($_SERVER['HTTP_X_FORWARDED_HOST']);
|
||||
}
|
||||
return strtolower($_SERVER['HTTP_HOST']);
|
||||
}
|
||||
|
||||
function getRequestUriWithoutFilename() {
|
||||
return strtolower(substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/') + 1));
|
||||
}
|
||||
|
||||
function sendHttpReturnCodeAndJson($code, $data) {
|
||||
if (!is_array($data)) {
|
||||
$data = ['msg' => $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;
|
||||
}
|
||||
?>
|
34
src/loadArtifact.php
Normal file
34
src/loadArtifact.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
$mvnBaseUrl = 'http://repo1.maven.org/maven2';
|
||||
|
||||
$requestedArtifact = $_SERVER["REQUEST_URI"];
|
||||
$requestedArtifact = str_replace('/mvn', '', $requestedArtifact);
|
||||
$baseLocalFolder = __DIR__;
|
||||
$localFolder = $baseLocalFolder.substr($requestedArtifact, 0, strripos($requestedArtifact, '/'));
|
||||
//echo $localFolder."<br>";
|
||||
if (!is_dir($localFolder)) {
|
||||
mkdir($localFolder, 0770, true);
|
||||
}
|
||||
|
||||
$srcUrl = $mvnBaseUrl.$requestedArtifact;
|
||||
//echo $srcUrl."<br>";
|
||||
$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');
|
||||
}
|
||||
?>
|
|
@ -1,29 +1,54 @@
|
|||
<?php
|
||||
$mvnBaseUrl = 'http://repo1.maven.org/maven2';
|
||||
|
||||
include_once(__DIR__.'/lib/functions.http.inc.php');
|
||||
include_once(__DIR__.'/lib/error.handler.inc.php');
|
||||
// Load configuration
|
||||
$config = require(__DIR__.'/config/config.inc.php');
|
||||
|
||||
initTheDevStackErrorHandler($config["logfile"]);
|
||||
|
||||
$requestedArtifact = $_SERVER["REQUEST_URI"];
|
||||
$requestedArtifact = str_replace('/mvn', '', $requestedArtifact);
|
||||
$baseLocalFolder = __DIR__;
|
||||
$baseLocalFolder = __DIR__.'/..';
|
||||
$localFolder = $baseLocalFolder.substr($requestedArtifact, 0, strripos($requestedArtifact, '/'));
|
||||
//echo $localFolder."<br>";
|
||||
if (!is_dir($localFolder)) {
|
||||
mkdir($localFolder, 0770, true);
|
||||
|
||||
$tempFile = tmpfile();
|
||||
foreach ($config['mavenBaseUrls'] as $mvnBaseUrl) {
|
||||
$srcUrl = $mvnBaseUrl.$requestedArtifact;
|
||||
//echo $srcUrl."<br>";
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
$srcUrl = $mvnBaseUrl.$requestedArtifact;
|
||||
//echo $srcUrl."<br>";
|
||||
$src = fopen($srcUrl, 'r');
|
||||
|
||||
$dstPath = $baseLocalFolder.$requestedArtifact;
|
||||
//echo $dstPath;
|
||||
$dst = fopen($dstPath, 'w');
|
||||
|
||||
stream_copy_to_stream($src, $dst);
|
||||
|
||||
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."<br>";
|
||||
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');
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
Loading…
Reference in a new issue