1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#!/neo/opt/bin/python
import neo_cgi
import sys, os, string
import time
from log import *
# errors thrown...
NoPageName = "NoPageName"
NoDisplayMethod = "NoDisplayMethod"
# errors signaled back to here
Redirected = "Redirected"
DisplayDone = "DisplayDone"
DisplayError = "DisplayError"
class Context:
def __init__ (self):
self.argv = sys.argv
self.stdin = sys.stdin
self.stdout = sys.stdout
self.stderr = sys.stderr
self.environ = os.environ
class CSPage:
def __init__(self, context, pagename=0,readDefaultHDF=1,israwpage=0,**parms):
if pagename == 0:
raise NoPageName, "missing pagename"
self.pagename = pagename
self.readDefaultHDF = readDefaultHDF
self._israwpage = israwpage
self.context = context
self._pageparms = parms
self._error_template = None
self.page_start_time = time.time()
neo_cgi.cgiWrap(context.stdin, context.stdout, context.environ)
neo_cgi.IgnoreEmptyFormVars(1)
self.ncgi = neo_cgi.CGI()
self.ncgi.parse()
self._path_num = 0
domain = self.ncgi.hdf.getValue("CGI.ServerName","")
domain = self.ncgi.hdf.getValue("HTTP.Host", domain)
self.domain = domain
self.subclassinit()
self.setPaths([self.ncgi.hdf.getValue("CGI.DocumentRoot","")])
def subclassinit(self):
pass
def setPaths(self, paths):
for path in paths:
self.ncgi.hdf.setValue("hdf.loadpaths.%d" % self._path_num, path)
self._path_num = self._path_num + 1
def redirectUri(self,redirectTo):
ncgi = self.ncgi
if ncgi.hdf.getIntValue("Cookie.debug",0) == 1:
ncgi.hdf.setValue("CGI.REDIRECT_TO",redirectTo)
ncgi.display("dbg/redirect.cs")
print "<PRE>"
print neo_cgi.htmlEscape(ncgi.hdf.dump())
print "</PRE>"
raise DisplayDone
self.ncgi.redirectUri(redirectTo)
raise Redirected, "redirected To: %s" % redirectTo
## ----------------------------------
## methods to be overridden in subclass when necessary:
def setup(self):
pass
def display(self):
raise NoDisplayMethod, "no display method present in %s" % repr(self)
def main(self):
self.setup()
self.handle_actions()
self.display()
## ----------------------------------
def handle_actions(self):
hdf = self.ncgi.hdf
hdfobj = hdf.getObj("Query.Action")
if hdfobj:
firstchild = hdfobj.child()
if firstchild:
action = firstchild.name()
if firstchild.next():
raise "multiple actions present!!!"
method_name = "Action_%s" % action
method = getattr(self,method_name)
apply(method,[])
def start(self):
SHOULD_DISPLAY = 1
if self._israwpage:
SHOULD_DISPLAY = 0
ncgi = self.ncgi
if self.readDefaultHDF:
try:
if not self.pagename is None:
ncgi.hdf.readFile("%s.hdf" % self.pagename)
except:
log("Error reading HDF file: %s.hdf" % (self.pagename))
DISPLAY_ERROR = 0
ERROR_MESSAGE = ""
# call page main function!
try:
self.main()
except DisplayDone:
SHOULD_DISPLAY = 0
except Redirected:
# catch redirect exceptions
SHOULD_DISPLAY = 0
except DisplayError, num:
ncgi.hdf.setValue("Query.error", str(num))
if self._error_template:
ncgi.hdf.setValue("Content", self._error_template)
else:
DISPLAY_ERROR = 1
except:
SHOULD_DISPLAY = 0
DISPLAY_ERROR = 1
import handle_error
handle_error.handleException("Display Failed!")
ERROR_MESSAGE = handle_error.exceptionString()
if DISPLAY_ERROR:
print "Content-Type: text/html\n\n"
# print the page
print "<H1> Error in Page </H1>"
print "A copy of this error report has been submitted to the developers. "
print "The details of the error report are below."
print "<PRE>"
print neo_cgi.htmlEscape(ERROR_MESSAGE)
print "</PRE>\n"
# print debug info always on page error...
print "<HR>\n"
print "<PRE>"
print neo_cgi.htmlEscape(ncgi.hdf.dump())
print "</PRE>"
etime = time.time() - self.page_start_time
ncgi.hdf.setValue("CGI.debug.execute_time","%f" % (etime))
if SHOULD_DISPLAY and self.pagename:
debug_output = ncgi.hdf.getIntValue("page.debug",ncgi.hdf.getIntValue("Cookie.debug",0))
# hijack the built-in debug output method...
if ncgi.hdf.getValue("Query.debug","") == ncgi.hdf.getValue("Config.DebugPassword","1"):
ncgi.hdf.setValue("Config.DebugPassword","CSPage.py DEBUG hijack (%s)" %
ncgi.hdf.getValue("Config.DebugPassword",""))
debug_output = 1
if not debug_output:
ncgi.hdf.setValue("Config.CompressionEnabled","1")
# default display
template_name = ncgi.hdf.getValue("Content","%s.cs" % self.pagename)
# ncgi.hdf.setValue ("cgiout.charset", "utf-8");
try:
ncgi.display(template_name)
except:
print "Content-Type: text/html\n\n"
print "CSPage: Error occured"
import handle_error
print "<pre>" + handle_error.exceptionString() + "</pre>"
debug_output = 1
# debug output
if debug_output:
print "<HR>\n"
print "CSPage Debug, Execution Time: %5.3f<BR><HR>" % (etime)
print "<PRE>"
print neo_cgi.htmlEscape(ncgi.hdf.dump())
print "</PRE>"
# ncgi.hdf.setValue("hdf.DEBUG",ncgi.hdf.dump())
# ncgi.display("debug.cs")
script_name = ncgi.hdf.getValue("CGI.ScriptName","")
if script_name:
script_name = string.split(script_name,"/")[-1]
log ("[%s] etime/dtime: %5.3f/%5.3f %s (%s)" % (self.domain, etime, time.time() - etime - self.page_start_time, script_name, self.pagename))
# a protected output function to catch the output errors that occur when
# the server is either restarted or the user pushes the stop button on the
# browser
def output(self, str):
try:
self.context.stdout.write(str)
except IOError, reason:
log("IOError: %s" % (repr(reason)))
raise DisplayDone
def allQuery (self, s):
l = []
if self.ncgi.hdf.getValue ("Query.%s.0" % s, ""):
obj = self.ncgi.hdf.getChild ("Query.%s" % s)
while obj:
l.append(obj.value())
obj = obj.next()
else:
t = self.ncgi.hdf.getValue ("Query.%s" % s, "")
if t: l.append(t)
return l
|