# Copyright (c) 2001 Samuel Brauer. All Rights Reserved. NO WARRANTY.
# $Revision: 1.4 $
import spb.genericHttpTransaction
import string
import sys
import types
import WebKit.Cookie
class webwareHttpTransaction(spb.genericHttpTransaction.Transaction):
def __init__(self, raw_transaction):
self.raw_transaction = raw_transaction
self.form = self.raw_transaction.request().fields()
self.env = self.raw_transaction.request().serverDictionary()
self.cookies = self.raw_transaction.request().cookies()
self.headersSent = 0
def getRawTransaction(self):
return self.raw_transaction
def getBackendName(self):
return "webware"
def getRealPath(self):
return self.raw_transaction.request().serverSidePath()
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])
else:
val = None
elif(hasattr(data, "filename") and data.filename):
val = self.normalizeString(data.filename)
else:
val = self.normalizeString(data)
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)
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)
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.cookies.keys()
def getCookieValue(self, name, default=None):
"""Return value of cookie."""
if(self.cookies.has_key(name)):
val = str(self.cookies[name])
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(val == None): val = ""
cookie = WebKit.Cookie.Cookie(name, str(val))
if(path != None): cookie.setPath(path)
if(expires != None): cookie.setExpires(expires)
if(domain != None): cookie.setDomain(domain)
if(secure): cookie.setSecure(1)
self.raw_transaction.response().addCookie(cookie)
def setHeader(self, name, val):
self.raw_transaction.response().setHeader(name, val)
def sendRedirect(self, locationString):
self.raw_transaction.response().sendRedirect(locationString)
self.sendHeaders()
def sendHeaders(self):
if(self.headersSent): raise RuntimeError, "Response headers have already been sent!"
self.raw_transaction.response().flush()
self.headersSent = 1
self.finalizeSession()
def setResponseContentType(self, contentType):
self.raw_transaction.response().setHeader("Content-type", contentType)
def writeOutput(self, data):
write_out = data
if(isinstance(data, types.UnicodeType)): write_out = data.encode('utf-8')
self.raw_transaction.response().write(write_out)
|