[Zope-Checkins] CVS: Zope3/lib/python/Zope/Publisher/HTTP - HTTPResponse.py:1.1.2.13.4.3 IHTTPResponse.py:1.1.2.2
Stephan Richter
srichter@cbu.edu
Fri, 22 Mar 2002 00:22:59 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/Publisher/HTTP
In directory cvs.zope.org:/tmp/cvs-serv31719
Modified Files:
Tag: Zope3-publisher-refactor-branch
HTTPResponse.py IHTTPResponse.py
Log Message:
Started refactoring HTTPResponse.
=== Zope3/lib/python/Zope/Publisher/HTTP/HTTPResponse.py 1.1.2.13.4.2 => 1.1.2.13.4.3 ===
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (ZPL). A copy of the ZPL should accompany this distribution.
@@ -6,11 +9,10 @@
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
-
+##############################################################################
'''HTTP Response Output formatter
$Id$'''
-__version__='$Revision$'[11:-2]
import sys, re
from types import StringType, ClassType
@@ -19,7 +21,8 @@
from Zope.Publisher.BaseResponse import BaseResponse
from Zope.Publisher.Exceptions import Redirect
-status_reasons={
+# Possible HTTP status responses
+status_reasons = {
100: 'Continue',
101: 'Switching Protocols',
102: 'Processing',
@@ -81,7 +84,7 @@
status_codes[name] = 500
-accumulate_header={'set-cookie': 1}.has_key
+accumulate_header = {'set-cookie': 1}.has_key
@@ -106,42 +109,39 @@
passed into the object must be used.
"""
+ __implements__ = IHTTPResponse
+
accumulated_headers = None
base = None
realm = 'Zope'
_error_format = 'text/html'
- payload = None
_wrote_headers = 0
_streaming = 0
- status = 200
+ __status = 200 # The response status (usually an integer)
reason = 'Ok'
- def __init__(self, outstream, header_output=None):
- self.header_output = header_output
+ def __init__(self, outstream, header_output = None):
+ self.__header_output = header_output
super(HTTPResponse, self).__init__(outstream)
- self.headers = {}
- self.cookies = {}
+ self.__headers = {}
+ self.__cookies = {}
- def retry(self):
- """
- Returns a response object to be used in a retry attempt
- """
- return self.__class__(self.outstream,
- self.header_output)
- def getStatus(self):
- return self.status
+ ############################################################
+ # Implementation methods for interface
+ # Zope.Publisher.HTTP.IHTTPResponse.IHTTPResponse
def setStatus(self, status, reason=None):
+ 'See Zope.Publisher.HTTP.IHTTPResponse.IHTTPResponse'
if status is None:
status = 200
else:
if isinstance(status, StringType):
status = status.lower()
if status_codes.has_key(status):
- status=status_codes[status]
+ status = status_codes[status]
else:
status=500
self.status=status
@@ -155,7 +155,14 @@
reason = 'Unknown'
self.reason = reason
+
+ def getStatus(self):
+ 'See Zope.Publisher.HTTP.IHTTPResponse.IHTTPResponse'
+ return self.status
+
+
def setHeader(self, name, value, literal=0):
+ 'See Zope.Publisher.HTTP.IHTTPResponse.IHTTPResponse'
key = name.lower()
if accumulate_header(key):
self.addHeader(name, value)
@@ -163,31 +170,37 @@
name = literal and name or key
self.headers[name]=value
+
def addHeader(self, name, value):
+ 'See Zope.Publisher.HTTP.IHTTPResponse.IHTTPResponse'
accum = self.accumulated_headers
if not accum:
self.accumulated_headers = accum = []
accum.append('%s: %s' % (name, value))
- def setBody(self, body):
- return self.payload.setBody(self, body)
- def __updateContentLength(self):
- blen = str(len(self.body))
- if blen.endswith('L'):
- blen = blen[:-1]
- self.setHeader('content-length', blen)
+ def appendToHeader(self, name, value, delimiter=','):
+ 'See Zope.Publisher.HTTP.IHTTPResponse.IHTTPResponse'
+ headers = self.headers
+ if headers.has_key(name):
+ h = self.header[name]
+ h = "%s%s\r\n\t%s" % (h, delimiter, value)
+ else: h = value
+ self.setHeader(name, h)
- def appendToCookie(self, name, value):
- cookies=self.cookies
- if cookies.has_key(name): cookie=cookies[name]
- else: cookie=cookies[name]={}
+ def appendToCookie(self, name, value):
+ 'See Zope.Publisher.HTTP.IHTTPResponse.IHTTPResponse'
+ cookies = self.cookies
+ if cookies.has_key(name): cookie = cookies[name]
+ else: cookie = cookies[name] = {}
if cookie.has_key('value'):
- cookie['value']='%s:%s' % (cookie['value'], value)
- else: cookie['value']=value
+ cookie['value'] = '%s:%s' % (cookie['value'], value)
+ else: cookie['value'] = value
+
def expireCookie(self, name, **kw):
+ 'See Zope.Publisher.HTTP.IHTTPResponse.IHTTPResponse'
dict={'max_age':0, 'expires':'Wed, 31-Dec-97 23:59:59 GMT'}
for k, v in kw.items():
dict[k]=v
@@ -197,7 +210,9 @@
del cookies[name]
self.setCookie(name, 'deleted', **dict)
+
def setCookie(self, name, value, **kw):
+ 'See Zope.Publisher.HTTP.IHTTPResponse.IHTTPResponse'
cookies=self.cookies
if cookies.has_key(name):
cookie=cookies[name]
@@ -206,13 +221,22 @@
cookie[k]=v
cookie['value']=value
- def appendToHeader(self, name, value, delimiter=","):
- headers=self.headers
- if headers.has_key(name):
- h=self.header[name]
- h="%s%s\r\n\t%s" % (h,delimiter,value)
- else: h=value
- self.setHeader(name,h)
+ #
+ ############################################################
+
+
+ def retry(self):
+ """
+ Returns a response object to be used in a retry attempt
+ """
+ return self.__class__(self.outstream,
+ self.header_output)
+
+ def __updateContentLength(self):
+ blen = str(len(self.body))
+ if blen.endswith('L'):
+ blen = blen[:-1]
+ self.setHeader('content-length', blen)
def redirect(self, location, status=302):
"""Causes a redirection without raising an error"""
@@ -314,15 +338,6 @@
# Write directly to outstream.
s = self.getHeaderText(m)
self.outstream.write(s)
-
-
- def __str__(self):
- """
- Debugging output. Does not include headers added for connection
- control.
- """
- m = self.getHeaders()
- return self.getHeaderText(m) + self.body
def write(self, data):
=== Zope3/lib/python/Zope/Publisher/HTTP/IHTTPResponse.py 1.1.2.1 => 1.1.2.2 ===
#
-# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.