Skip to content
Snippets Groups Projects
Commit 533ea5af authored by alvaro's avatar alvaro
Browse files

Refactoring HTTP statuses methods in a new class

Adding user agent LODSPeaKr
parent 44ed6faf
No related branches found
No related tags found
No related merge requests found
lodspeakr - Linked Open Data Simple Publishing Kit
author: Alvaro Graves (alvaro@graves.cl)
version: 20120318
version: 20120323
Simplest Installation
---------------------
......
......@@ -31,6 +31,7 @@ class Endpoint{
$url = $this->sparqlUrl.'?'.http_build_query($params, '', '&');
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_HTTPHEADER, $context);
curl_setopt($c, CURLOPT_USERAGENT, "LODSPeaKr version ".$conf['version']);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$aux = curl_exec($c); // execute the curl command
if($conf['debug']){
......
<?
class HTTPStatus{
public static function send303($uri, $ext){
header("HTTP/1.0 303 See Other");
header("Location: ".$uri);
header("Content-type: ".$ext);
echo $uri."\n\n";
exit(0);
}
public static function send404($uri){
header("HTTP/1.0 404 Not Found");
echo "LODSPeaKr could not find ".$uri." or information about it.\nNo URIs in the triple store, or services configured with that URI\n";
exit(0);
}
public static function send406($uri){
header("HTTP/1.0 406 Not Acceptable");
echo "LODSPeaKr can't find a representation suitable for the content type you accept\n\n";
exit(0);
}
public static function send500($msg = null){
header("HTTP/1.0 500 Internal Server Error");
echo "An internal error ocurred. Please try later\n\n";
if($msg != null){
echo $msg;
}
exit(0);
}
}
?>
......@@ -37,7 +37,7 @@ class Importer{
}elseif(isset($_POST['importtext'])){
$parser->parse(RDF, $_POST['importtext']);
}else{
Utils::send500();
HTTPStatus::send500();
exit(0);
}
$triples = $parser->getTriples();
......
<?
class Utils{
public static function send303($uri, $ext){
header("HTTP/1.0 303 See Other");
header("Location: ".$uri);
header("Content-type: ".$ext);
echo $uri."\n\n";
exit(0);
}
public static function send404($uri){
header("HTTP/1.0 404 Not Found");
echo "LODSPeaKr could not find ".$uri." or information about it.\nNo URIs in the triple store, or services configured with that URI\n";
exit(0);
}
public static function send406($uri){
header("HTTP/1.0 406 Not Acceptable");
echo "LODSPeaKr can't find a representation suitable for the content type you accept\n\n";
exit(0);
}
public static function send500($msg = null){
header("HTTP/1.0 500 Internal Server Error");
echo "An internal error ocurred. Please try later\n\n";
if($msg != null){
echo $msg;
}
exit(0);
}
public static function uri2curie($uri){
global $conf;
$ns = $conf['ns'];
......@@ -268,7 +239,7 @@ class Utils{
}elseif(preg_match("/construct/i", $query)){
return $conf['output']['describe'];
}else{
Utils::send500(null);
HTTPStatus::send500(null);
}
}
......@@ -367,7 +338,7 @@ class Utils{
$vars = compact('uri', 'lodspk', 'models', 'first');
$q = file_get_contents($modelFile);
if($q == false){
Utils::send500("I can't load ".$modelFile." in ".getcwd());
HTTPStatus::send500("I can't load ".$modelFile." in ".getcwd());
}
$fnc = Haanga::compile($q);
$query = $fnc($vars, TRUE);
......@@ -433,13 +404,13 @@ class Utils{
//For now, assuming variables are in the GRAPH ?g
$query = "CONSTRUCT {?g ?x ?y} WHERE{GRAPH ?g{?g ?x ?y}}";
}else{
Utils::send500();
HTTPStatus::send500();
}
}else{
$query = preg_replace('/select\n?.*\n?where/i', 'CONSTRUCT {'.$construct.'} WHERE', $query);
}
}else {
Utils::send500("invalid query: " . $parser->getErrors());
HTTPStatus::send500("invalid query: " . $parser->getErrors());
}
}
if($conf['debug']){
......@@ -512,7 +483,7 @@ class Utils{
}elseif(is_string($conf['mirror_external_uris'])){
$value['value'] = preg_replace("|^".$conf['mirror_external_uris']."|", $conf['basedir'], $value['value']);
}else{
Utils::send500("Error in mirroring configuration");
HTTPStatus::send500("Error in mirroring configuration");
exit(1);
}
}
......
......@@ -20,7 +20,7 @@ class ServiceModule extends abstractModule{
if(isset($aux[1])){
$contentTypes = $conf['http_accept'][$aux[1]];
if($contentTypes == null){
Utils::send406("Content type not acceptable\n");
HTTPStatus::send406("Content type not acceptable\n");
}
$acceptContentType = $contentTypes[0];
}
......@@ -31,6 +31,7 @@ class ServiceModule extends abstractModule{
$viewFile = null;
$lodspk['model'] = $conf['model']['directory'].'/'.$conf['service']['prefix'].'/'.$qArr[0].'/';
$lodspk['view'] = $conf['view']['directory'].'/'.$conf['service']['prefix'].'/'.$qArr[0].'/'.$extension.'.template';
$lodspk['serviceName'] = $qArr[0];
$modelFile = $lodspk['model'].$extension.'.queries';
if(file_exists($lodspk['model'].$extension.'.queries')){
if(!file_exists($lodspk['view'])){
......@@ -49,7 +50,7 @@ class ServiceModule extends abstractModule{
}
return array($modelFile, $viewFile);
}elseif(file_exists($lodspk['model'])){
Utils::send406($uri);
HTTPStatus::send406($uri);
exit(0);
}
return FALSE;
......@@ -149,7 +150,7 @@ class ServiceModule extends abstractModule{
}catch (Exception $ex){
echo $ex->getMessage();
trigger_error($ex->getMessage(), E_ERROR);
Utils::send500($uri);
HTTPStatus::send500($uri);
}
exit(0);
}
......
......@@ -20,12 +20,12 @@ class TypeModule extends abstractModule{
if(Queries::uriExist($uri, $endpoints['local'])){
$page = Queries::createPage($uri, $localUri, $acceptContentType, $metaDb);
if($page == NULL){
Utils::send500("Can't write sqlite database.");
HTTPStatus::send500("Can't write sqlite database.");
}
Utils::send303($page, $acceptContentType);
HTTPStatus::send303($page, $acceptContentType);
exit(0);
}else{
return false; //Utils::send404($uri);
return false;
}
}
return $pair;
......@@ -43,7 +43,7 @@ class TypeModule extends abstractModule{
list($res, $page, $format) = $pair;
//If resource is not the page, send a 303 to the document
if($res == $localUri){
Utils::send303($page, $acceptContentType);
HTTPStatus::send303($page, $acceptContentType);
}
$uri = $res;
......
......@@ -21,12 +21,12 @@ class UriModule extends abstractModule{
if(Queries::uriExist($uri, $endpoints['local'])){
$page = Queries::createPage($uri, $localUri, $acceptContentType, $metaDb);
if($page == NULL){
Utils::send500("Can't write sqlite database.");
HTTPStatus::send500("Can't write sqlite database.");
}
Utils::send303($page, $acceptContentType);
HTTPStatus::send303($page, $acceptContentType);
exit(0);
}else{
return false; //Utils::send404($uri);
return false;
}
}
$extension = Utils::getExtension($pair[2]);
......@@ -63,7 +63,7 @@ class UriModule extends abstractModule{
//If resource is not the page, send a 303 to the document
if($res == $localUri){
Utils::send303($page, $acceptContentType);
HTTPStatus::send303($page, $acceptContentType);
}
$uri = $res;
......
<?
$conf['version'] = '20120323';
$conf['output']['select'] = 'json';
$conf['output']['describe'] = 'rdf';
$conf['endpointParams']['config']['show_inline'] = 0;
......
......@@ -22,6 +22,7 @@ if($conf['debug']){
error_reporting(E_ERROR);
}
include_once('classes/HTTPStatus.php');
include_once('classes/Utils.php');
include_once('classes/Queries.php');
include_once('classes/Endpoint.php');
......@@ -37,7 +38,7 @@ $extension = Utils::getExtension($acceptContentType);
//Check content type is supported by LODSPeaKr
if($acceptContentType == NULL){
Utils::send406($uri);
HTTPStatus::send406($uri);
}
//Export
......@@ -67,7 +68,7 @@ if(isset($conf['mirror_external_uris']) && $conf['mirror_external_uris'] != fals
}elseif(is_string($conf['mirror_external_uris'])){
$uri = $conf['mirror_external_uris'].$_GET['q'];
}else{
Utils::send500("Error in mirroring configuration");
HTTPStatus::send500("Error in mirroring configuration");
exit(1);
}
......@@ -79,7 +80,7 @@ foreach($conf['modules']['available'] as $i){
$className = $i.'Module';
$currentModule = $conf['modules']['directory'].$className.'.php';
if(!is_file($currentModule)){
Utils::send500("<br/>Can't load or error in module <tt>".$currentModule."</tt>" );
HTTPStatus::send500("<br/>Can't load or error in module <tt>".$currentModule."</tt>" );
exit(1);
}
require_once($currentModule);
......@@ -91,5 +92,5 @@ foreach($conf['modules']['available'] as $i){
}
}
Utils::send404($uri);
HTTPStatus::send404($uri);
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment