Commit 22966584 authored by 's avatar

Added literal header support and the addHeader method to allow multiple

headers with the same name.
parent 665972d5
...@@ -84,8 +84,8 @@ ...@@ -84,8 +84,8 @@
############################################################################## ##############################################################################
'''CGI Response Output formatter '''CGI Response Output formatter
$Id: HTTPResponse.py,v 1.5 1999/03/10 00:15:51 klm Exp $''' $Id: HTTPResponse.py,v 1.6 1999/03/11 14:28:24 brian Exp $'''
__version__='$Revision: 1.5 $'[11:-2] __version__='$Revision: 1.6 $'[11:-2]
import string, types, sys, regex import string, types, sys, regex
from string import find, rfind, lower, upper, strip, split, join, translate from string import find, rfind, lower, upper, strip, split, join, translate
...@@ -228,16 +228,27 @@ class HTTPResponse(BaseResponse): ...@@ -228,16 +228,27 @@ class HTTPResponse(BaseResponse):
self.setHeader('Status', "%d %s" % (status,str(reason))) self.setHeader('Status', "%d %s" % (status,str(reason)))
self.errmsg=reason self.errmsg=reason
def setHeader(self, name, value): def setHeader(self, name, value, literal=0):
'''\ '''\
Sets an HTTP return header "name" with value "value", clearing Sets an HTTP return header "name" with value "value", clearing
the previous value set for the header, if one exists. ''' the previous value set for the header, if one exists. If the
n=lower(name) literal flag is true, the case of the header name is preserved,
if accumulate_header(n): otherwise word-capitalization will be performed on the header
name on output.'''
key=lower(name)
if accumulate_header(key):
self.accumulated_headers=( self.accumulated_headers=(
"%s%s: %s\n" % (self.accumulated_headers, name, value)) "%s%s: %s\n" % (self.accumulated_headers, name, value))
else: return
self.headers[n]=value name=literal and name or key
self.headers[name]=value
def addHeader(self, name, value):
'''\
Set a new HTTP return header with the given value, while retaining
any previously set headers with the same name.'''
self.accumulated_headers=(
"%s%s: %s\n" % (self.accumulated_headers, name, value))
__setitem__=setHeader __setitem__=setHeader
...@@ -628,29 +639,24 @@ class HTTPResponse(BaseResponse): ...@@ -628,29 +639,24 @@ class HTTPResponse(BaseResponse):
headersl=[] headersl=[]
append=headersl.append append=headersl.append
# Make sure status comes out first! # status header must come first.
try: append("Status: %s" % headers.get('status', '200 OK'))
v=headers['status'] if headers.has_key('status'):
del headers['status'] del headers['status']
except: v="200 OK" for key, val in headers.items():
append("Status: "+v) if lower(key)==key:
# only change non-literal header names
for k,v in headers.items(): key="%s%s" % (upper(key[:1]), key[1:])
start=0
k=upper(k[:1])+k[1:] l=find(key,'-',start)
while l >= start:
start=0 key="%s-%s%s" % (key[:l],upper(key[l+1:l+2]),key[l+2:])
l=find(k,'-',start) start=l+1
while l >= start: l=find(key,'-',start)
k="%s-%s%s" % (k[:l],upper(k[l+1:l+2]),k[l+2:]) append("%s: %s" % (key, val))
start=l+1
l=find(k,'-',start)
append("%s: %s" % (k,v))
if self.cookies: if self.cookies:
headersl=headersl+self._cookie_list() headersl=headersl+self._cookie_list()
headersl[len(headersl):]=[self.accumulated_headers,body] headersl[len(headersl):]=[self.accumulated_headers, body]
return join(headersl,'\n') return join(headersl,'\n')
def write(self,data): def write(self,data):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment