#!/usr/bin/env python # -*- coding: utf-8 -*- """ SPARUL adaptor See for the .htaccess recipe """ import sys from urlparse import urlparse import httplib import os env = os.environ filename = env['QUERY_STRING'] #assume 'QUERY_STRING' exists via CGI/1.1 METHOD = env['REQUEST_METHOD'] if METHOD == 'HEAD' or METHOD == 'GET': print "MS-Author-Via: SPARQL" #We basically let apache do the content negotiation. See if filename.endswith(".n3"): print 'Content-Type: text/rdf+n3' elif filename.endswith(".rdf"): print 'Content-Type: application/rdf+xml' if METHOD == 'HEAD': print sys.exit() F = open(filename) print print F.read() F.close() sys.exit() REDIRECT_TO = "http://dig.csail.mit.edu/2007/wiki/tabulator" REDIRECT_TO_RSS = "http://dig.csail.mit.edu/2007/wiki/tabulator" REDIRECT_TO_RSS_ON = True #optional #Two approahces for SPARUL redirection # 1. 302 - This makes more sense but I don't have time to investigate into # browser POST redirection at the momment. # 2. Proxy - with the Content-Location: header. (current implementation, # see the following code) NETLOC, PATH = 1, 2 machine, location = urlparse(REDIRECT_TO)[NETLOC:(PATH+1)] data = sys.stdin.read() if "" in data: #heuristics machine, location = urlparse(REDIRECT_TO_RSS)[NETLOC:(PATH+1)] headers = {"Content-type" : 'application/sparql-query'} try: headers['X-Meta'] = os.environ['HTTP_X_META'] except KeyError: pass conn = httplib.HTTPConnection(machine) conn.request("POST", location, data, headers) response = conn.getresponse() response_data = response.read() print "Status: %s %s" % (response.status, response.reason) print "Content-Type: " + response.getheader("Content-type") print "Content-Location: " + REDIRECT_TO print "MS-Author-Via: SPARQL" print response_data conn.close()