3 import os, sys, string, time, string
13 warnings.resetwarnings()
14 warnings.filterwarnings(
"ignore")
18 import neo_cgi, neo_cs, neo_util
20 from pyclearsilver
import CSPage
23 mimetypes.init([
"/etc/mime.types"])
31 if not hasattr(gConfig,
"gRequireUsername"): gConfig.gRequireUsername = 0
32 if not hasattr(gConfig,
"gDataFilePaths"): gConfig.gDataFilePaths = []
37 return string.split(path,
'/')[1:]
46 self.
path = self.context.environ.get(
"PATH_INFO",
'')
48 script_name = self.context.environ.get(
"SCRIPT_NAME",
'')
52 self.
path = script_name
62 if not rpath: rpath =
'/' 64 if rpath ==
"/": rpath = gConfig.gDefaultPage
70 if len(self.
path) == 0:
81 module = gConfig.gDefaultModule
83 if hasattr(gConfig,
"gDataFilePaths"):
84 if self.
path[0]
in gConfig.gDataFilePaths:
85 fn = apply(os.path.join, [cwd,] + self.
path)
88 if gConfig.gRequireUsername:
89 username = self.
path[0]
96 if len(self.
path) > 1:
100 moduleRootPath = apply(os.path.join, [
"mod", module])
101 handlerRoot = apply(os.path.join, [
"mod", module,
"cgibin"])
102 moduleTemplatePath = apply(os.path.join, [cwd,
"mod", module,
"templates"])
103 systemTemplatePath = apply(os.path.join, [cwd,
"templates"])
104 systemJLIBPath = apply(os.path.join, [cwd,
"jslib"])
106 fn = apply(os.path.join, [cwd, moduleRootPath,] + self.
path[n:])
110 if os.path.isfile(fn):
114 sys.path.insert(0, os.path.abspath(cwd))
115 sys.path.insert(0, os.path.abspath(moduleRootPath))
117 debug(
"sys.path", sys.path)
124 for m
in range(len(self.
path)-1, n-2, -1):
125 handlerPath = apply(os.path.join, [handlerRoot, ] + self.
path[n:m+1])
128 if os.path.isdir(handlerPath):
129 sys.path.insert(0, handlerPath)
130 if os.path.isfile(handlerPath):
break 131 if os.path.isdir(handlerPath):
break 133 if m+1 == len(self.
path):
136 pathinfo = apply(os.path.join, self.
path[m+1:])
138 if os.path.isdir(handlerPath):
139 modulePath = handlerPath
140 moduleFilename = module +
"_index.py" 141 handlerPath = os.path.join(modulePath, moduleFilename)
143 modulePath, moduleFilename = os.path.split(handlerPath)
145 debug(handlerPath, pathinfo)
149 if not os.path.isfile(handlerPath):
150 self.
error(404, handlerPath +
" doesn't exist")
155 moduleName, ext = os.path.splitext(moduleFilename)
158 module = __import__(
"mod.%s.cgibin.%s" % (module, moduleName), {}, {}, (
None,))
162 page = module.run(self.
context)
163 page.ncgi.hdf.setValue(
"CGI.BaseURI", gConfig.gBaseURL)
165 if gConfig.gRequireUsername:
166 page.ncgi.hdf.setValue(
"CGI.Username", username)
167 page.username = username
169 if hasattr(page,
"checkLoginCookie"):
170 if not page._pageparms.has_key(
"nologin"):
172 page.checkLoginCookie()
176 page.ncgi.hdf.setValue(
"CGI.PathInfo", pathinfo)
178 page.setPaths([moduleTemplatePath, systemTemplatePath, systemJLIBPath])
183 except AttributeError:
pass 189 def error(self, ecode, reason=None):
191 message = httpResponses.gHTTPResponses[ecode]
193 template = httpResponses.errorMessage_Default
195 template = httpResponses.errorMessage_404
198 hdf.setValue(
"code", str(ecode))
199 if message: hdf.setValue(
"message", message)
200 if reason: hdf.setValue(
"reason", reason)
202 for key,val
in self.context.environ.items():
203 hdf.setValue(
"environ." + key, str(val))
205 self.context.stdout.write(
"Content-Type: text/html\r\n")
206 self.context.setStatus(
None, ecode)
207 self.context.stdout.write(
"Status: %s\r\n" % ecode)
208 self.context.stdout.write(
"\r\n")
211 cs.parseStr(template)
214 self.context.stdout.write(page)
216 warn(
"Error", message, reason)
224 context.setStatus(
None, 200)
226 imagetype, encoding = mimetypes.guess_type(fn)
227 debug(
"imagetype = %s fn = %s" % (imagetype,fn))
232 lines.append(
"Content-Type: %s" % imagetype)
234 lines.append(
"Content-Length: %d" % len(data))
237 mtime = stat.st_mtime
238 mod_str = time.strftime(
"%a, %d %b %Y %H:%M:%S", time.gmtime(mtime))
240 lines.append(
'Last-Modified: %s GMT' % mod_str)
242 expire_time = time.gmtime(time.time() + (360*24*3600))
243 expire_str = time.strftime(
"%a, %d %b %Y %H:%M:%S", expire_time)
244 lines.append(
'Expires: %s GMT' % expire_str)
248 headers = string.join(lines,
"\r\n")
249 context.stdout.write(headers)
251 context.stdout.write(data)
258 apache.log_error(s, apache.APLOG_STARTUP)
263 from mod_python
import apache
269 env = apache.build_cgi_env(req)
273 scriptFilename = self.environ.get(
"SCRIPT_FILENAME",
"")
275 path, fn = os.path.split(scriptFilename)
280 request[
'status'] = str(status)
287 from mod_python
import apache
292 page.mod_python_req = req
306 def main(argv, stdout, environ):
312 if __name__ ==
"__main__":
313 main(sys.argv, sys.stdout, os.environ)
path
handle the case where the site is located at '/'
def __init__(self, context)
def setStatus(self, request, status)
def main(argv, stdout, environ)
def outputFile(context, fn)
def error(self, ecode, reason=None)