2001-09-10 03:14:13 +00:00
|
|
|
#!/usr/bin/python2
|
|
|
|
|
2001-09-22 02:54:49 +00:00
|
|
|
|
2001-09-11 01:40:42 +00:00
|
|
|
import os
|
2001-09-10 03:14:13 +00:00
|
|
|
import string
|
2001-09-17 01:20:40 +00:00
|
|
|
import commands
|
2001-09-10 03:14:13 +00:00
|
|
|
import StringIO
|
2001-09-22 02:54:49 +00:00
|
|
|
import urlparse
|
2001-09-10 03:14:13 +00:00
|
|
|
import SimpleHTTPServer
|
|
|
|
import scrollkeeper
|
2001-09-11 01:40:42 +00:00
|
|
|
|
|
|
|
|
2001-09-20 03:19:31 +00:00
|
|
|
htmlbase = "/var/cache/scrollserver/" # This is the cache directory
|
|
|
|
caching = 1 # set to 1 to enable caching
|
|
|
|
xsltparam = "--timing" # parameters to pass in all xsltproc calls
|
2001-09-22 02:54:49 +00:00
|
|
|
xsltparam = ""
|
|
|
|
|
|
|
|
|
|
|
|
BaseClass = SimpleHTTPServer.SimpleHTTPRequestHandler
|
|
|
|
ScrollKeeper = scrollkeeper.ScrollKeeper()
|
2001-09-11 01:40:42 +00:00
|
|
|
|
2001-09-17 01:20:40 +00:00
|
|
|
def FileContents(filename):
|
|
|
|
f = open(filename, "r")
|
|
|
|
text = f.read()
|
|
|
|
f.close
|
|
|
|
return text
|
2001-09-11 01:40:42 +00:00
|
|
|
|
2001-09-22 02:54:49 +00:00
|
|
|
|
|
|
|
def FileCache(sourcefile, htmlfile, cmd):
|
|
|
|
if not os.path.exists(htmlfile) or caching == 0:
|
|
|
|
os.system(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
class URI:
|
|
|
|
|
|
|
|
URI = ""
|
|
|
|
Protocol = ""
|
|
|
|
Server = ""
|
|
|
|
Port = ""
|
|
|
|
Path = ""
|
|
|
|
Filename = ""
|
|
|
|
Parameter = ""
|
2001-09-22 15:54:01 +00:00
|
|
|
Anchor = ""
|
2001-09-22 02:54:49 +00:00
|
|
|
|
|
|
|
def __init__(self, uri):
|
|
|
|
|
|
|
|
self.URI = uri
|
|
|
|
temp = uri
|
|
|
|
|
2001-09-22 15:54:01 +00:00
|
|
|
temp = string.split(temp,"#")
|
|
|
|
if len(temp) > 1:
|
|
|
|
self.Anchor = temp[1]
|
|
|
|
temp = temp[0]
|
|
|
|
|
2001-09-22 02:54:49 +00:00
|
|
|
temp = string.split(temp,"?")
|
|
|
|
if len(temp) > 1:
|
|
|
|
self.Parameter = temp[1]
|
|
|
|
temp = temp[0]
|
|
|
|
|
|
|
|
temp = string.split(temp,"/")
|
|
|
|
if len(temp) > 1:
|
|
|
|
self.Filename = string.join(temp[len(temp)-1:])
|
|
|
|
temp = string.join(temp[:len(temp)-1],"/")
|
|
|
|
|
|
|
|
if temp[:7] == "http://":
|
|
|
|
self.Protocol = "http://"
|
|
|
|
temp = temp[7:]
|
|
|
|
|
|
|
|
# If the first character is /, there is no server or port.
|
|
|
|
if temp[:1] == "/":
|
|
|
|
self.Path = temp[1:]
|
|
|
|
else:
|
|
|
|
temp = string.split(temp,":")
|
|
|
|
if len(temp) > 1:
|
|
|
|
self.Port = temp[1]
|
|
|
|
self.Server = temp[0]
|
|
|
|
|
|
|
|
# This is a tricky area, so leave this for testing when problems arise
|
|
|
|
# due to strange URIs.
|
|
|
|
# print "URI: " + self.URI
|
|
|
|
# print "Protocol: " + self.Protocol
|
|
|
|
# print "Server: " + self.Server
|
|
|
|
# print "Port: " + self.Port
|
|
|
|
# print "Path: " + self.Path
|
|
|
|
# print "Filename: [" + self.Filename + "]"
|
|
|
|
# print "Parameter: " + self.Parameter
|
|
|
|
|
|
|
|
|
|
|
|
class RequestHandler(BaseClass):
|
2001-09-10 03:14:13 +00:00
|
|
|
|
|
|
|
def do_GET(self):
|
|
|
|
BaseClass.do_GET(self)
|
|
|
|
|
|
|
|
def send_head(self):
|
|
|
|
|
|
|
|
if self.path == "" or self.path == "/" or self.path == "/index.html":
|
2001-09-20 03:19:31 +00:00
|
|
|
return self.send_Home()
|
|
|
|
elif self.path == "/contents.html":
|
2001-09-17 01:20:40 +00:00
|
|
|
return self.send_ContentsList()
|
2001-09-20 03:19:31 +00:00
|
|
|
elif self.path =="/documents.html":
|
|
|
|
return self.send_DocList()
|
2001-09-10 03:14:13 +00:00
|
|
|
else:
|
2001-09-22 02:54:49 +00:00
|
|
|
uri = URI(self.path)
|
|
|
|
|
|
|
|
if uri.Filename == "docid":
|
|
|
|
return self.send_DocumentByID(uri.Parameter)
|
|
|
|
else:
|
|
|
|
return self.send_URI(uri)
|
2001-09-20 03:19:31 +00:00
|
|
|
|
|
|
|
def send_Home(self):
|
2001-09-22 15:54:01 +00:00
|
|
|
FileCache ("", htmlbase+ "index.html", "xsltproc " + xsltparam + " stylesheets/index.xsl stylesheets/documents.xsl > " + htmlbase + "index.html")
|
|
|
|
return self.send_File(htmlbase + "index.html")
|
2001-09-20 03:19:31 +00:00
|
|
|
|
2001-09-17 01:20:40 +00:00
|
|
|
def send_ContentsList(self):
|
2001-09-22 02:54:49 +00:00
|
|
|
contents_list = commands.getoutput("scrollkeeper-get-content-list C")
|
2001-09-22 15:54:01 +00:00
|
|
|
FileCache (contents_list, htmlbase + "contents.html", "xsltproc " + xsltparam + " stylesheets/contents.xsl " + contents_list + " > " + htmlbase + "contents.html")
|
|
|
|
return self.send_File(htmlbase + "contents.html")
|
2001-09-20 03:19:31 +00:00
|
|
|
|
|
|
|
def send_DocList(self):
|
2001-09-22 02:54:49 +00:00
|
|
|
contents_list = commands.getoutput("scrollkeeper-get-content-list C")
|
2001-09-22 15:54:01 +00:00
|
|
|
FileCache (contents_list, htmlbase + "documents.html", "xsltproc " + xsltparam + " stylesheets/documents.xsl " + contents_list + " > " + htmlbase + "documents.html")
|
|
|
|
return self.send_File(htmlbase + "documents.html")
|
2001-09-17 01:20:40 +00:00
|
|
|
|
2001-09-10 03:14:13 +00:00
|
|
|
def send_DocumentByID(self, docid):
|
2001-09-17 01:20:40 +00:00
|
|
|
document = ScrollKeeper.DocumentByID(docid)
|
2001-09-20 03:19:31 +00:00
|
|
|
|
|
|
|
xmlfile = document.SourceFile
|
|
|
|
xmlpath = os.path.dirname(xmlfile)
|
|
|
|
htmlpath = htmlbase + docid
|
|
|
|
htmlfile = htmlpath + "/index.html"
|
2001-09-17 01:20:40 +00:00
|
|
|
|
|
|
|
if document.Format == "text/html":
|
|
|
|
text = '<html><head><meta http-equiv="refresh" content="0; url=' + document.URL + '"></head></html>'
|
|
|
|
return self.send_Text(text)
|
2001-09-22 02:54:49 +00:00
|
|
|
|
|
|
|
FileCache ("", htmlpath, "mkdir " + htmlpath)
|
|
|
|
FileCache (xmlfile, htmlfile, "xsltproc --docbook " + xsltparam + " stylesheets/docbook/docbook.xsl " + xmlfile + " > " + htmlfile)
|
|
|
|
os.system("ln -s --target-directory=" + htmlpath + " " + xmlpath + "/*")
|
2001-09-17 01:20:40 +00:00
|
|
|
return self.send_File(htmlfile)
|
|
|
|
|
2001-09-22 02:54:49 +00:00
|
|
|
def send_URI(self, uri):
|
|
|
|
|
|
|
|
if os.path.isfile(uri.Filename):
|
|
|
|
return self.send_File(uri.Filename)
|
|
|
|
|
|
|
|
referer = self.headers.getheader("Referer")
|
|
|
|
refuri = URI(referer)
|
|
|
|
|
|
|
|
if refuri.Filename == "docid":
|
2001-09-22 15:54:01 +00:00
|
|
|
print refuri.Parameter
|
2001-09-22 02:54:49 +00:00
|
|
|
document = ScrollKeeper.DocumentByID(refuri.Parameter)
|
|
|
|
filename = htmlbase + document.ID + "/" + uri.Path + "/" + uri.Filename
|
|
|
|
return self.send_File(filename)
|
|
|
|
else:
|
|
|
|
text = "Unrecognized request: " + uri.Filename
|
|
|
|
return self.send_Text(text)
|
|
|
|
|
2001-09-17 01:20:40 +00:00
|
|
|
def send_File(self, filename):
|
2001-09-22 02:54:49 +00:00
|
|
|
temp = string.split(filename, ".")
|
|
|
|
if len(temp) > 1:
|
|
|
|
fileext = temp[1]
|
|
|
|
else:
|
|
|
|
fileext = ""
|
|
|
|
if os.path.isfile(filename + ".png"):
|
|
|
|
fileext = "png"
|
|
|
|
elif os.path.isfile(filename + ".jpeg"):
|
|
|
|
fileext = "jpeg"
|
|
|
|
if os.path.isfile(filename + ".jpg"):
|
|
|
|
fileext = "jpg"
|
|
|
|
if os.path.isfile(filename + ".gif"):
|
|
|
|
fileext = "gif"
|
|
|
|
filename += "." + fileext
|
|
|
|
|
2001-09-20 03:19:31 +00:00
|
|
|
if fileext == "html" or fileext == "htm":
|
|
|
|
mimetype = "text/html"
|
|
|
|
elif fileext == "png":
|
|
|
|
mimetype = "image/png"
|
|
|
|
elif fileext == "gif":
|
|
|
|
mimetype = "image/gif"
|
|
|
|
elif fileext == "jpg" or fileext == "jpeg":
|
|
|
|
mimetype = "image/jpeg"
|
|
|
|
elif fileext == "css":
|
|
|
|
mimetype = "text/css"
|
2001-09-22 02:54:49 +00:00
|
|
|
else:
|
|
|
|
mimetype = ""
|
|
|
|
|
|
|
|
if os.path.isfile(filename):
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", mimetype)
|
|
|
|
self.end_headers()
|
|
|
|
text = FileContents(filename)
|
|
|
|
else:
|
|
|
|
text = "Unrecognized file: " + filename
|
|
|
|
|
2001-09-10 03:14:13 +00:00
|
|
|
return StringIO.StringIO(text)
|
|
|
|
|
2001-09-17 01:20:40 +00:00
|
|
|
def send_Text(self, text):
|
2001-09-10 03:14:13 +00:00
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", "text/html")
|
|
|
|
self.end_headers()
|
|
|
|
return StringIO.StringIO(text)
|
2001-09-22 02:54:49 +00:00
|
|
|
|
2001-09-10 03:14:13 +00:00
|
|
|
|
|
|
|
def ScrollServer():
|
2001-09-22 15:54:01 +00:00
|
|
|
os.system("rm -rf " + htmlbase + "*")
|
|
|
|
print "ScrollServer v0.5 -- development version!"
|
2001-09-22 02:54:49 +00:00
|
|
|
SimpleHTTPServer.test(RequestHandler)
|
2001-09-10 03:14:13 +00:00
|
|
|
|
2001-09-11 01:40:42 +00:00
|
|
|
ScrollServer()
|
|
|
|
|