# Copyright (c) 2002 Samuel Brauer. All Rights Reserved. NO WARRANTY.
# $Revision: 1.4 $
import spb.genericHttpTransaction
import os
import string
import sys
import types
import Cookie
import cgi
class cgiHttpTransaction(spb.genericHttpTransaction.Transaction):
def __init__(self):
self.form = cgi.FieldStorage()
self.env = os.environ
self.inCookies = Cookie.SimpleCookie()
if(self.env.has_key("HTTP_COOKIE")):
http_cookie = self.env["HTTP_COOKIE"]
self.inCookies.load(http_cookie)
self.outCookies = None
self.outHeaders = []
self.headersSent = 0
self.out = sys.stdout
def getRawTransaction(self):
return self.form
def getBackendName(self):
return "cgi"
def getRealPath(self):
return self.getEnvironValue("PATH_TRANSLATED")
def getEnvironNames(self):
return self.env.keys()
def getEnvironValue(self, name, default=None):
val = ""
if(self.env.has_key(name)):
val = self.env[name]
val = self.normalizeString(val)
if(not val): val = default
return val
def getFormNames(self):
return self.form.keys()
def getFormValue(self, name, default=None):
if(self.form.has_key(name)):
data = self.form[name]
if type(data) is type([]):
if(len(data) > 0):
val = self.normalizeString(data[0].value)
else:
val = None
elif(hasattr(data, "filename") and data.filename):
val = self.normalizeString(data.filename)
else:
val = self.normalizeString(data.value)
else:
val = None
if(not(val)): val = default
return val
def getFormValues(self, name, keepNulls=0):
"""Returns a (possibly empty) list of strings"""
valList = []
if(self.form.has_key(name)):
data = self.form[name]
if type(data) is type([]):
for item in data:
val = self.normalizeString(item.value)
if(val or keepNulls): valList.append(val)
elif(hasattr(data, "filename") and data.filename):
val = self.normalizeString(data.filename)
if(val or keepNulls): valList.append(val)
else:
val = self.normalizeString(data.value)
if(val or keepNulls): valList.append(val)
return valList
def getFilename(self, name):
if(self.form.has_key(name)):
if(hasattr(self.form[name], "filename")):
return self.normalizeString(self.form[name].filename)
return None
def getFile(self, name):
"""Returns a file handle, or None"""
filename = self.getFilename(name)
if(filename):
return self.form[name].file
else:
return None
def getCookieNames(self):
"""Return names of cookies."""
return self.inCookies.keys()
def getCookieValue(self, name, default=None):
"""Return value of cookie."""
if(self.inCookies.has_key(name)):
val = str(self.inCookies[name].value)
else:
val = None
val = self.normalizeString(val)
if(not val): val = default
return val
def setCookieValue(self, name, val, path=None, expires=None, domain=None, secure=0):
""" If specified, "expires" should be a number of seconds.
"""
if(self.outCookies == None): self.outCookies = Cookie.SimpleCookie()
if(val == None): val = ""
self.outCookies[name] = str(val)
if(path != None): self.outCookies[name]["path"] = path
if(expires != None): self.outCookies[name]["expires"] = expires
if(domain != None): self.outCookies[name]["domain"] = domain
if(secure): self.outCookies[name]["secure"] = 1
def setHeader(self, name, val):
self.outHeaders.append("%s: %s" % (name, val))
def sendRedirect(self, locationString):
self.setHeader("Location", locationString)
self.sendHeaders()
def sendHeaders(self):
if(self.headersSent): raise RuntimeError, "Response headers have already been sent!"
if(self.outCookies != None):
cookies = string.split(str(self.outCookies), "\n")
for cookie in cookies:
# assume each cookie begins with "Set-Cookie: "
self.setHeader("Set-Cookie", cookie[12:].replace('"', ''))
for header in self.outHeaders:
self.out.write(header)
self.out.write("\n")
self.out.write("\n")
self.headersSent = 1
self.finalizeSession()
def setResponseContentType(self, contentType):
self.setHeader("Content-type", contentType)
def writeOutput(self, data):
write_out = data
if(isinstance(data, types.UnicodeType)): write_out = data.encode('utf-8')
self.out.write(write_out)
|