Commit 128f9306 authored by Jim Fulton's avatar Jim Fulton

Added check for responses that look like bogus default object strings:

<some damn instance as 123ab34c>
parent 78dc4b21
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
__doc__='''CGI Response Output formatter __doc__='''CGI Response Output formatter
$Id: Response.py,v 1.20 1997/11/07 14:59:18 jim Exp $''' $Id: Response.py,v 1.21 1997/11/07 19:55:17 jim Exp $'''
# Copyright # Copyright
# #
# Copyright 1996 Digital Creations, L.C., 910 Princess Anne # Copyright 1996 Digital Creations, L.C., 910 Princess Anne
...@@ -50,98 +50,13 @@ $Id: Response.py,v 1.20 1997/11/07 14:59:18 jim Exp $''' ...@@ -50,98 +50,13 @@ $Id: Response.py,v 1.20 1997/11/07 14:59:18 jim Exp $'''
# If you have questions regarding this software, # If you have questions regarding this software,
# contact: # contact:
# #
# Jim Fulton, jim@digicool.com # Digital Creations, info@Digicool.com
#
# (540) 371-6909 # (540) 371-6909
#
# $Log: Response.py,v $
# Revision 1.20 1997/11/07 14:59:18 jim
# Fixed bug in printing tracebacks.
#
# Revision 1.19 1997/10/29 18:46:55 jim
# Fixed leak in exception handler.
#
# Revision 1.18 1997/10/22 14:48:26 jim
# Added simple repr method top support printing requests.
#
# Revision 1.17 1997/09/15 19:20:56 brian
# NS Server apparently chokes on multi-line headers, so bci exception info
# no longer uses ml.
#
# Revision 1.16 1997/04/29 18:29:00 jim
# Changed bobo-exception header code to use tb tail, rather than head
# for reporting errors.
#
# Revision 1.15 1997/04/18 19:46:19 jim
# Brian's changes to try and get file name and line no in exceptions.
#
# Revision 1.14 1997/04/12 17:17:32 jim
# Brian added loggic to set bobo-specific headers to transmit exception
# info.
#
# Revision 1.13 1997/04/11 23:13:23 jim
# Fixed cookies.
#
# Revision 1.12 1997/01/28 22:59:19 jim
# Fixed bug that caused html didling of non-html data
#
# Revision 1.11 1996/09/16 14:43:25 jim
# Changes to make shutdown methods work properly. Now shutdown methods
# can simply sys.exit(0).
#
# Added on-line documentation and debugging support to bobo.
#
# Revision 1.10 1996/09/13 22:52:10 jim
# *** empty log message ***
#
# Revision 1.9 1996/08/30 23:28:29 jfulton
# Added code to map 300 redirects to 302.
#
# Revision 1.8 1996/08/29 22:11:35 jfulton
# Bug fixes.
#
# Revision 1.7 1996/08/05 11:27:59 jfulton
# Added check for asHTML method.
# Added traceback comment quoting.
# Added code to add header of response doesn't contain one.
#
# Revision 1.6 1996/07/25 16:44:24 jfulton
# - Fixed bug in recognizing HTML exception values.
# - Added transaction support.
#
# Revision 1.5 1996/07/10 22:45:57 jfulton
# Made exception handling fussier about exception values.
# Now the value must contain white space to be considered an error
# message.
#
# Revision 1.4 1996/07/08 20:34:09 jfulton
# Many changes, including:
#
# - Butter realm management
# - Automatic type conversion
# - Improved documentation
# - ...
#
# Revision 1.3 1996/07/03 18:25:50 jfulton
# Added support for file upload via newcgi module.
#
# Revision 1.2 1996/07/01 11:51:54 jfulton
# Updated code to:
#
# - Provide a first cut authentication.authorization scheme
# - Fix several bugs
# - Provide better error messages
# - Provide automagic insertion of base
# - Support Fast CGI module publisher.
#
# Revision 1.1 1996/06/17 18:57:18 jfulton
# Almost initial version.
#
#
# #
__version__='$Revision: 1.20 $'[11:-2] __version__='$Revision: 1.21 $'[11:-2]
import string, types, sys, regex, regsub import string, types, sys, regex, regsub
from string import find, rfind, lower, upper, strip, split, join
status_reasons={ status_reasons={
200: 'OK', 200: 'OK',
...@@ -231,6 +146,8 @@ end_of_header_re=regex.compile('</head>',regex.casefold) ...@@ -231,6 +146,8 @@ end_of_header_re=regex.compile('</head>',regex.casefold)
absuri_re=regex.compile("[a-zA-Z0-9+.-]+:[^\0- \"\#<>]+\(#[^\0- \"\#<>]*\)?") absuri_re=regex.compile("[a-zA-Z0-9+.-]+:[^\0- \"\#<>]+\(#[^\0- \"\#<>]*\)?")
bogus_str=regex.compile(" [a-fA-F0-9]+>$")
class Response: class Response:
"""\ """\
An object representation of an HTTP response. An object representation of an HTTP response.
...@@ -280,7 +197,7 @@ class Response: ...@@ -280,7 +197,7 @@ class Response:
ServiceUnavailable } that will be converted to the correct ServiceUnavailable } that will be converted to the correct
integer value. ''' integer value. '''
if type(status) is types.StringType: if type(status) is types.StringType:
status=string.lower(status) status=lower(status)
try: status=status_codes[status] try: status=status_codes[status]
except: status=500 except: status=500
self.status=status self.status=status
...@@ -293,7 +210,7 @@ class Response: ...@@ -293,7 +210,7 @@ class Response:
'''\ '''\
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. '''
self.headers[string.lower(name)]=value self.headers[lower(name)]=value
__setitem__=setHeader __setitem__=setHeader
...@@ -316,6 +233,14 @@ class Response: ...@@ -316,6 +233,14 @@ class Response:
if type(body) is not types.StringType: if type(body) is not types.StringType:
if hasattr(body,'asHTML'): if hasattr(body,'asHTML'):
body=body.asHTML() body=body.asHTML()
body=str(body)
l=len(body)
if (find(body,'>')==l-1 and body[:1]=='<' and l < 200 and
bogus_str.search(body) > 0):
raise 'NotFound', (
"Sorry, the requested document does not exist.<p>"
"\n<!--\n%s\n-->" % body[1:-1])
if(title): if(title):
self.body=('<html>\n<head>\n<title>%s</title>\n</head>\n' self.body=('<html>\n<head>\n<title>%s</title>\n</head>\n'
...@@ -337,7 +262,7 @@ class Response: ...@@ -337,7 +262,7 @@ class Response:
self.insertBase() self.insertBase()
def host(self,base): def host(self,base):
return base[:string.find(base,'/',string.find(base,'//'))] return base[:find(base,'/',find(base,'//'))]
def insertBase(self, def insertBase(self,
base_re=regex.compile('\(<base[\0- ]+\([^>]+\)>\)', base_re=regex.compile('\(<base[\0- ]+\([^>]+\)>\)',
...@@ -367,7 +292,7 @@ class Response: ...@@ -367,7 +292,7 @@ class Response:
href=href[2:] href=href[2:]
elif href[:3]=='../' or href=='..': elif href[:3]=='../' or href=='..':
href=href[3:] href=href[3:]
base=base[:string.rfind(base,'/')] base=base[:rfind(base,'/')]
else: else:
break break
if base: if base:
...@@ -442,8 +367,7 @@ class Response: ...@@ -442,8 +367,7 @@ class Response:
self.setHeader(name,h) self.setHeader(name,h)
def isHTML(self,str): def isHTML(self,str):
return (string.lower(string.strip(str)[:6]) == '<html>' or return lower(strip(str)[:6]) == '<html>' or find(str,'</') > 0
string.find(str,'</') > 0)
def quoteHTML(self,text, def quoteHTML(self,text,
character_entities=( character_entities=(
...@@ -480,14 +404,13 @@ class Response: ...@@ -480,14 +404,13 @@ class Response:
except: pass except: pass
tb = tb.tb_next tb = tb.tb_next
n = n+1 n = n+1
result.append(string.joinfields( result.append(join(traceback.format_exception_only(etype, value), ' '))
traceback.format_exception_only(etype, value), ' '))
sys.exc_type,sys.exc_value,sys.exc_traceback=etype,value,tb sys.exc_type,sys.exc_value,sys.exc_traceback=etype,value,tb
return result return result
def _traceback(self,t,v,tb): def _traceback(self,t,v,tb):
tb=self.format_exception(t,v,tb,200) tb=self.format_exception(t,v,tb,200)
tb=string.joinfields(tb,'\n') tb=join(tb,'\n')
tb=self.quoteHTML(tb) tb=self.quoteHTML(tb)
return "\n<!--\n%s\n-->" % tb return "\n<!--\n%s\n-->" % tb
...@@ -507,7 +430,7 @@ class Response: ...@@ -507,7 +430,7 @@ class Response:
while tb.tb_next is not None: tb=tb.tb_next while tb.tb_next is not None: tb=tb.tb_next
el=str(tb.tb_lineno) el=str(tb.tb_lineno)
ef=str(tb.tb_frame.f_code.co_filename) ef=str(tb.tb_frame.f_code.co_filename)
if string.find(ev,'<html>') >= 0: ev='bobo exception' if find(ev,'<html>') >= 0: ev='bobo exception'
self.setHeader('bobo-exception-type',et) self.setHeader('bobo-exception-type',et)
self.setHeader('bobo-exception-value',ev) self.setHeader('bobo-exception-value',ev)
self.setHeader('bobo-exception-file',ef) self.setHeader('bobo-exception-file',ef)
...@@ -627,7 +550,7 @@ class Response: ...@@ -627,7 +550,7 @@ class Response:
headersl=headersl+self._cookie_list() headersl=headersl+self._cookie_list()
headersl[len(headersl):]=['',body] headersl[len(headersl):]=['',body]
return string.joinfields(headersl,'\n') return join(headersl,'\n')
def __repr__(self): def __repr__(self):
return 'CGIResponse(%s)' % `self.body` return 'CGIResponse(%s)' % `self.body`
...@@ -669,8 +592,8 @@ class Response: ...@@ -669,8 +592,8 @@ class Response:
def upcase(s): def upcase(s):
s=string.upper(s[:1])+s[1:] s=upper(s[:1])+s[1:]
l=string.find(s,'-') l=find(s,'-')
if l > 0: if l > 0:
l=l+1 l=l+1
return s[:l]+upcase(s[l:]) return s[:l]+upcase(s[l:])
...@@ -689,3 +612,94 @@ def main(): ...@@ -689,3 +612,94 @@ def main():
if __name__ == "__main__": main() if __name__ == "__main__": main()
############################################################################
#
# $Log: Response.py,v $
# Revision 1.21 1997/11/07 19:55:17 jim
# Added check for responses that look like bogus default object strings:
# <some damn instance as 123ab34c>
#
# Revision 1.20 1997/11/07 14:59:18 jim
# Fixed bug in printing tracebacks.
#
# Revision 1.19 1997/10/29 18:46:55 jim
# Fixed leak in exception handler.
#
# Revision 1.18 1997/10/22 14:48:26 jim
# Added simple repr method top support printing requests.
#
# Revision 1.17 1997/09/15 19:20:56 brian
# NS Server apparently chokes on multi-line headers, so bci exception info
# no longer uses ml.
#
# Revision 1.16 1997/04/29 18:29:00 jim
# Changed bobo-exception header code to use tb tail, rather than head
# for reporting errors.
#
# Revision 1.15 1997/04/18 19:46:19 jim
# Brian's changes to try and get file name and line no in exceptions.
#
# Revision 1.14 1997/04/12 17:17:32 jim
# Brian added loggic to set bobo-specific headers to transmit exception
# info.
#
# Revision 1.13 1997/04/11 23:13:23 jim
# Fixed cookies.
#
# Revision 1.12 1997/01/28 22:59:19 jim
# Fixed bug that caused html didling of non-html data
#
# Revision 1.11 1996/09/16 14:43:25 jim
# Changes to make shutdown methods work properly. Now shutdown methods
# can simply sys.exit(0).
#
# Added on-line documentation and debugging support to bobo.
#
# Revision 1.10 1996/09/13 22:52:10 jim
# *** empty log message ***
#
# Revision 1.9 1996/08/30 23:28:29 jfulton
# Added code to map 300 redirects to 302.
#
# Revision 1.8 1996/08/29 22:11:35 jfulton
# Bug fixes.
#
# Revision 1.7 1996/08/05 11:27:59 jfulton
# Added check for asHTML method.
# Added traceback comment quoting.
# Added code to add header of response doesn't contain one.
#
# Revision 1.6 1996/07/25 16:44:24 jfulton
# - Fixed bug in recognizing HTML exception values.
# - Added transaction support.
#
# Revision 1.5 1996/07/10 22:45:57 jfulton
# Made exception handling fussier about exception values.
# Now the value must contain white space to be considered an error
# message.
#
# Revision 1.4 1996/07/08 20:34:09 jfulton
# Many changes, including:
#
# - Butter realm management
# - Automatic type conversion
# - Improved documentation
# - ...
#
# Revision 1.3 1996/07/03 18:25:50 jfulton
# Added support for file upload via newcgi module.
#
# Revision 1.2 1996/07/01 11:51:54 jfulton
# Updated code to:
#
# - Provide a first cut authentication.authorization scheme
# - Fix several bugs
# - Provide better error messages
# - Provide automagic insertion of base
# - Support Fast CGI module publisher.
#
# Revision 1.1 1996/06/17 18:57:18 jfulton
# Almost initial version.
#
#
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