LDP/LDP/scrollserver/scrollserver.py

211 lines
5.5 KiB
Python
Raw Normal View History

#!/usr/bin/python2
2001-09-22 02:54:49 +00:00
import os
import string
import commands
import StringIO
2001-09-22 02:54:49 +00:00
import urlparse
import SimpleHTTPServer
import scrollkeeper
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()
def FileContents(filename):
f = open(filename, "r")
text = f.read()
f.close
return text
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 = ""
Anchor = ""
2001-09-22 02:54:49 +00:00
def __init__(self, uri):
self.URI = uri
temp = uri
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):
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":
return self.send_ContentsList()
2001-09-20 03:19:31 +00:00
elif self.path =="/documents.html":
return self.send_DocList()
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):
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
def send_ContentsList(self):
2001-09-22 02:54:49 +00:00
contents_list = commands.getoutput("scrollkeeper-get-content-list C")
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")
FileCache (contents_list, htmlbase + "documents.html", "xsltproc " + xsltparam + " stylesheets/documents.xsl " + contents_list + " > " + htmlbase + "documents.html")
return self.send_File(htmlbase + "documents.html")
def send_DocumentByID(self, docid):
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"
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 + "/*")
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":
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)
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
return StringIO.StringIO(text)
def send_Text(self, text):
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
def ScrollServer():
os.system("rm -rf " + htmlbase + "*")
print "ScrollServer v0.5 -- development version!"
2001-09-22 02:54:49 +00:00
SimpleHTTPServer.test(RequestHandler)
ScrollServer()