[VM] http examples, low-level alternative/hack

Hi SWBPDWGVMTFers,

the "apache cookbook"[1] idea is great. I'm not sure if the 
following is helpful or out of scope, there are probably better
ways to make apache return resource descriptions, but there may
be situations where someone would like to publish an RDF 
vocabulary but doesn't have access to apache's rewrite or 
multiviews switches. However, custom 404s via .htaccess files 
seem to be enabled by almost every provider of hosted webspace.
So, a simple
[[
ErrorDocument 404 /catch_404.php
]]
line in a .htaccess file can be used to easily lift the request
processing to scripting languages like php, perl, or python. A 
sample (PHP) script file that supports basic slash-ont serving 
could contain
[[
<?php

/* requested URI, e.g. /ns/example/classA */
$ru=$_SERVER["REQUEST_URI"];

/* detect ontology and term by looking for "ns" prefix */
if(preg_match("/\/ns\/([^\/]+)[\/]?(.*)/", $ru, $matches)){
  $ont_name=$matches[1];
  $term_name=$matches[2];

  /* check rdf accept headers */
  if(strpos($_SERVER["HTTP_ACCEPT"], "application/rdf+xml")!==false){
    $ont_path="rdf/".$ont_name."rdf";
    $term_path="rdf/".$ont_name."_".$term_name.".rdf";
    if($term_name && file_exists($term_path)){
      header("HTTP/1.0 200 OK");
      header("Content-Type: application/rdf+xml");
      $fp=fopen($term_path, "r");
    }
    elseif(!$term_name && file_exists($ont_path)){
      header("HTTP/1.0 200 OK");
      header("Content-Type: application/rdf+xml");
      $fp=fopen($ont_path, "r");
    }
  }
  /* try html */
  else{
    $html_path="html/".$ont_name.".htm";
    if(file_exists($html_path)){
      header("HTTP/1.0 200 OK");
      header("Content-Type: text/html");
      $fp=fopen($html_path, "r");
    }
  }
  /* serve result */
  if($fp){
    fpassthru($fp);
  }
  else{
    header("HTTP/1.0 404 Not Found");
  }
}

?>
]]

(I didn't test the code above, it may contain errors. The
basic idea works, though. 303s can be implemented in a 
similar way)

This mechanism can also be useful when Rewrite *is* available,
e.g.
[[
RewriteEngine on
RewriteRule \/ns\/ /catch_ont.php
]]
enables a catch_ont script which is not limited to apache's 
options but can for example access a database or scan the local
file system. Different ontology versions can be served based on
parameters, without having to hard-code the pointers in the 
.htaccess files etc.


As I said, the 404 thing is a bit hacky, but it may be a useful
work-around for TAG-friendly vocabulary publishing on cheap, 
hosted web servers. (Although it seems that many hosters are
enabling the rewrite module these days. Not sure about the
multiviews toggle...)

</noise>

benjamin


[1] http://www.w3.org/2001/sw/BestPractices/VM/http-examples/2005-11-18/

--
Benjamin Nowack

Kruppstr. 100
45145 Essen, Germany
http://www.bnode.org/

Received on Tuesday, 22 November 2005 16:28:10 UTC