Commit 049452c9 authored by Amos Latteier's avatar Amos Latteier

Added a debug logging facility to trace ZServer requests. Updated the HTTP and...

Added a debug logging facility to trace ZServer requests. Updated the HTTP and PCGI servers to work with the debug logger. Eventially this facility will be rolled into zLOG.
parent 24f2418d
##############################################################################
#
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
#
# Copyright (c) Digital Creations. All rights reserved.
#
# This license has been certified as Open Source(tm).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions in source code must retain the above copyright
# notice, this list of conditions, and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. Digital Creations requests that attribution be given to Zope
# in any manner possible. Zope includes a "Powered by Zope"
# button that is installed by default. While it is not a license
# violation to remove this button, it is requested that the
# attribution remain. A significant investment has been put
# into Zope, and this effort will continue if the Zope community
# continues to grow. This is one way to assure that growth.
#
# 4. All advertising materials and documentation mentioning
# features derived from or use of this software must display
# the following acknowledgement:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# In the event that the product being advertised includes an
# intact Zope distribution (with copyright and license included)
# then this clause is waived.
#
# 5. Names associated with Zope or Digital Creations must not be used to
# endorse or promote products derived from this software without
# prior written permission from Digital Creations.
#
# 6. Modified redistributions of any form whatsoever must retain
# the following acknowledgment:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# Intact (re-)distributions of any official Zope release do not
# require an external acknowledgement.
#
# 7. Modifications are encouraged but must be packaged separately as
# patches to official Zope releases. Distributions that do not
# clearly separate the patches from the original work must be clearly
# labeled as unofficial distributions. Modifications which do not
# carry the name Zope may be packaged in any form, as long as they
# conform to all of the clauses above.
#
#
# Disclaimer
#
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations. Specific
# attributions are listed in the accompanying credits file.
#
##############################################################################
import time, thread
class DebugLogger:
"""
Logs debugging information about how ZServer is handling requests
and responses. This log can be used to help locate troublesome requests.
The format is:
<code> <request id> <time> <data>
where:
'code' is B for begin, I for received input, A for received output,
E for sent output.
'request id' is a unique request id.
'time' is the time in GMT ISO format.
'data' is the HTTP method and the PATH INFO for B, the size of the input
for I, the HTTP status code and the size of the output for A, or
nothing for E.
Note: This facility will be probably be adapted to the zLOG framework.
"""
def __init__(self, filename):
self.file=open(filename, 'a+b')
l=thread.allocate_lock()
self._acquire=l.acquire
self._release=l.release
def log(self, code, request_id, data=''):
self._acquire()
try:
t=time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime(time.time()))
self.file.write(
'%s %s %s %s\n' % (code, request_id, t, data)
)
finally:
self._release()
def log(*args): pass
\ No newline at end of file
############################################################################## ##############################################################################
# #
# Zope Public License (ZPL) Version 1.0 # Zope Public License (ZPL) Version 1.0
# ------------------------------------- # -------------------------------------
# #
# Copyright (c) Digital Creations. All rights reserved. # Copyright (c) Digital Creations. All rights reserved.
# #
# This license has been certified as Open Source(tm). # This license has been certified as Open Source(tm).
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are # modification, are permitted provided that the following conditions are
# met: # met:
# #
# 1. Redistributions in source code must retain the above copyright # 1. Redistributions in source code must retain the above copyright
# notice, this list of conditions, and the following disclaimer. # notice, this list of conditions, and the following disclaimer.
# #
# 2. Redistributions in binary form must reproduce the above copyright # 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in # notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the # the documentation and/or other materials provided with the
# distribution. # distribution.
# #
# 3. Digital Creations requests that attribution be given to Zope # 3. Digital Creations requests that attribution be given to Zope
# in any manner possible. Zope includes a "Powered by Zope" # in any manner possible. Zope includes a "Powered by Zope"
# button that is installed by default. While it is not a license # button that is installed by default. While it is not a license
# violation to remove this button, it is requested that the # violation to remove this button, it is requested that the
# attribution remain. A significant investment has been put # attribution remain. A significant investment has been put
# into Zope, and this effort will continue if the Zope community # into Zope, and this effort will continue if the Zope community
# continues to grow. This is one way to assure that growth. # continues to grow. This is one way to assure that growth.
# #
# 4. All advertising materials and documentation mentioning # 4. All advertising materials and documentation mentioning
# features derived from or use of this software must display # features derived from or use of this software must display
# the following acknowledgement: # the following acknowledgement:
# #
# "This product includes software developed by Digital Creations # "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment # for use in the Z Object Publishing Environment
# (http://www.zope.org/)." # (http://www.zope.org/)."
# #
# In the event that the product being advertised includes an # In the event that the product being advertised includes an
# intact Zope distribution (with copyright and license included) # intact Zope distribution (with copyright and license included)
# then this clause is waived. # then this clause is waived.
# #
# 5. Names associated with Zope or Digital Creations must not be used to # 5. Names associated with Zope or Digital Creations must not be used to
# endorse or promote products derived from this software without # endorse or promote products derived from this software without
# prior written permission from Digital Creations. # prior written permission from Digital Creations.
# #
# 6. Modified redistributions of any form whatsoever must retain # 6. Modified redistributions of any form whatsoever must retain
# the following acknowledgment: # the following acknowledgment:
# #
# "This product includes software developed by Digital Creations # "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment # for use in the Z Object Publishing Environment
# (http://www.zope.org/)." # (http://www.zope.org/)."
# #
# Intact (re-)distributions of any official Zope release do not # Intact (re-)distributions of any official Zope release do not
# require an external acknowledgement. # require an external acknowledgement.
# #
# 7. Modifications are encouraged but must be packaged separately as # 7. Modifications are encouraged but must be packaged separately as
# patches to official Zope releases. Distributions that do not # patches to official Zope releases. Distributions that do not
# clearly separate the patches from the original work must be clearly # clearly separate the patches from the original work must be clearly
# labeled as unofficial distributions. Modifications which do not # labeled as unofficial distributions. Modifications which do not
# carry the name Zope may be packaged in any form, as long as they # carry the name Zope may be packaged in any form, as long as they
# conform to all of the clauses above. # conform to all of the clauses above.
# #
# #
# Disclaimer # Disclaimer
# #
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY # THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE. # SUCH DAMAGE.
# #
# #
# This software consists of contributions made by Digital Creations and # This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations. Specific # many individuals on behalf of Digital Creations. Specific
# attributions are listed in the accompanying credits file. # attributions are listed in the accompanying credits file.
# #
############################################################################## ##############################################################################
""" """
ZServer HTTPResponse ZServer HTTPResponse
The HTTPResponse class takes care of server headers, response munging The HTTPResponse class takes care of server headers, response munging
and logging duties. and logging duties.
""" """
import time, regex, string, sys, tempfile import time, regex, string, sys, tempfile
from cStringIO import StringIO from cStringIO import StringIO
from ZPublisher.HTTPResponse import HTTPResponse, end_of_header_search from ZPublisher.HTTPResponse import HTTPResponse, end_of_header_search
from medusa.http_date import build_http_date from medusa.http_date import build_http_date
from PubCore.ZEvent import Wakeup from PubCore.ZEvent import Wakeup
from medusa.producers import hooked_producer from medusa.producers import hooked_producer
from medusa import http_server, asyncore from medusa import http_server, asyncore
from Producers import ShutdownProducer, LoggingProducer, CallbackProducer from Producers import ShutdownProducer, LoggingProducer, CallbackProducer
import DebugLogger
class ZServerHTTPResponse(HTTPResponse):
"Used to push data into a channel's producer fifo" class ZServerHTTPResponse(HTTPResponse):
"Used to push data into a channel's producer fifo"
http_chunk=0
http_chunk_size=1024 http_chunk=0
http_chunk_size=1024
# defaults
_http_version='1.0' # defaults
_http_connection='close' _http_version='1.0'
_server_version='Zope/2.0 ZServer/2.0' _http_connection='close'
_server_version='Zope/2.0 ZServer/2.0'
def __str__(self,
html_search=regex.compile('<html>',regex.casefold).search, def __str__(self,
): html_search=regex.compile('<html>',regex.casefold).search,
if self._wrote: return '' # Streaming output was used. ):
if self._wrote: return '' # Streaming output was used.
headers=self.headers
body=self.body headers=self.headers
if body: body=self.body
isHTML=self.isHTML(body) if body:
if not headers.has_key('content-type'): isHTML=self.isHTML(body)
if isHTML: if not headers.has_key('content-type'):
c='text/html' if isHTML:
else: c='text/html'
c='text/plain' else:
self.setHeader('content-type',c) c='text/plain'
else: self.setHeader('content-type',c)
isHTML = headers['content-type']=='text/html' else:
if isHTML and end_of_header_search(self.body) < 0: isHTML = headers['content-type']=='text/html'
lhtml=html_search(body) if isHTML and end_of_header_search(self.body) < 0:
if lhtml >= 0: lhtml=html_search(body)
lhtml=lhtml+6 if lhtml >= 0:
body='%s<head></head>\n%s' % (body[:lhtml],body[lhtml:]) lhtml=lhtml+6
else: body='%s<head></head>\n%s' % (body[:lhtml],body[lhtml:])
body='<html><head></head>\n' + body else:
self.setBody(body) body='<html><head></head>\n' + body
body=self.body self.setBody(body)
body=self.body
# set 204 (no content) status if 200 and response is empty
if not headers.has_key('content-type') and \ # set 204 (no content) status if 200 and response is empty
not headers.has_key('content-length') and \ if not headers.has_key('content-type') and \
not headers.has_key('transfer-encoding') and \ not headers.has_key('content-length') and \
self.status == 200: not headers.has_key('transfer-encoding') and \
self.setStatus('nocontent') self.status == 200:
self.setStatus('nocontent')
# add content length if not transfer encoded
if not headers.has_key('content-length') and \ # add content length if not transfer encoded
not headers.has_key('transfer-encoding'): if not headers.has_key('content-length') and \
self.setHeader('content-length',len(body)) not headers.has_key('transfer-encoding'):
self.setHeader('content-length',len(body))
headersl=[]
append=headersl.append headersl=[]
append=headersl.append
status=headers.get('status', '200 OK')
status=headers.get('status', '200 OK')
# status header must come first.
append("HTTP/%s %s" % (self._http_version, status)) # status header must come first.
if headers.has_key('status'): append("HTTP/%s %s" % (self._http_version, status))
del headers['status'] if headers.has_key('status'):
del headers['status']
# add zserver headers
append('Server: %s' % self._server_version) # add zserver headers
append('Date: %s' % build_http_date(time.time())) append('Server: %s' % self._server_version)
chunk=0 append('Date: %s' % build_http_date(time.time()))
if self._http_version=='1.0': chunk=0
if self._http_connection=='keep alive': if self._http_version=='1.0':
if self.headers.has_key('content-length'): if self._http_connection=='keep alive':
self.setHeader('Connection','close') if self.headers.has_key('content-length'):
else: self.setHeader('Connection','close')
self.setHeader('Connection','Keep-Alive') else:
else: self.setHeader('Connection','Keep-Alive')
self.setHeader('Connection','close') else:
elif self._http_version=='1.1': self.setHeader('Connection','close')
if self._http_connection=='close': elif self._http_version=='1.1':
self.setHeader('Connection','close') if self._http_connection=='close':
elif not self.headers.has_key('content-length'): self.setHeader('Connection','close')
if self.headers.has_key('transfer-encoding'): elif not self.headers.has_key('content-length'):
if self.headers['transfer-encoding'] != 'chunked': if self.headers.has_key('transfer-encoding'):
self.setHeader('Connection','close') if self.headers['transfer-encoding'] != 'chunked':
else: self.setHeader('Connection','close')
chunk=1 else:
elif self.http_chunk: chunk=1
self.setHeader('Transfer-Encoding','chunked') elif self.http_chunk:
chunk=1 self.setHeader('Transfer-Encoding','chunked')
else: chunk=1
self.setHeader('Connection','close') else:
self.setHeader('Connection','close')
if chunk:
chunked_body='' if chunk:
while body: chunked_body=''
chunk=body[:self.http_chunk_size] while body:
body=body[self.http_chunk_size:] chunk=body[:self.http_chunk_size]
chunked_body='%s%x\r\n%s\r\n' % (chunked_body, len(chunk), chunk) body=body[self.http_chunk_size:]
chunked_body='%s0\r\n\r\n' % chunked_body chunked_body='%s%x\r\n%s\r\n' % (chunked_body, len(chunk), chunk)
body=chunked_body chunked_body='%s0\r\n\r\n' % chunked_body
body=chunked_body
for key, val in headers.items():
if string.lower(key)==key: for key, val in headers.items():
# only change non-literal header names if string.lower(key)==key:
key="%s%s" % (string.upper(key[:1]), key[1:]) # only change non-literal header names
start=0 key="%s%s" % (string.upper(key[:1]), key[1:])
l=string.find(key,'-',start) start=0
while l >= start: l=string.find(key,'-',start)
key="%s-%s%s" % (key[:l],string.upper(key[l+1:l+2]),key[l+2:]) while l >= start:
start=l+1 key="%s-%s%s" % (key[:l],string.upper(key[l+1:l+2]),key[l+2:])
l=string.find(key,'-',start) start=l+1
append("%s: %s" % (key, val)) l=string.find(key,'-',start)
if self.cookies: append("%s: %s" % (key, val))
headersl=headersl+self._cookie_list() if self.cookies:
headersl[len(headersl):]=[self.accumulated_headers, body] headersl=headersl+self._cookie_list()
return string.join(headersl,'\r\n') headersl[len(headersl):]=[self.accumulated_headers, body]
return string.join(headersl,'\r\n')
_tempfile=None
_tempstart=0 _tempfile=None
def write(self,data): _tempstart=0
"""\ def write(self,data):
Return data as a stream """\
Return data as a stream
HTML data may be returned using a stream-oriented interface.
This allows the browser to display partial results while HTML data may be returned using a stream-oriented interface.
computation of a response to proceed. This allows the browser to display partial results while
computation of a response to proceed.
The published object should first set any output headers or
cookies on the response object. The published object should first set any output headers or
cookies on the response object.
Note that published objects must not generate any errors
after beginning stream-oriented output. Note that published objects must not generate any errors
after beginning stream-oriented output.
"""
stdout=self.stdout """
stdout=self.stdout
if not self._wrote:
l=self.headers.get('content-length', None) if not self._wrote:
if l is not None: l=self.headers.get('content-length', None)
try: if l is not None:
if type(l) is type(''): l=string.atoi(l) try:
if l > 128000: if type(l) is type(''): l=string.atoi(l)
self._tempfile=tempfile.TemporaryFile() if l > 128000:
except: pass self._tempfile=tempfile.TemporaryFile()
except: pass
stdout.write(str(self))
self._wrote=1 stdout.write(str(self))
self._wrote=1
if not data: return
if not data: return
t=self._tempfile
if t is None: t=self._tempfile
stdout.write(data) if t is None:
else: stdout.write(data)
l=len(data) else:
b=self._tempstart l=len(data)
e=b+l b=self._tempstart
t.seek(b) e=b+l
t.write(data) t.seek(b)
self._tempstart=e t.write(data)
stdout.write(file_part_producer(t,b,e), l) self._tempstart=e
stdout.write(file_part_producer(t,b,e), l)
# XXX add server headers, etc to write()
# XXX add server headers, etc to write()
def _finish(self):
stdout=self.stdout def _finish(self):
stdout=self.stdout
t=self._tempfile
if t is not None: t=self._tempfile
stdout.write(file_close_producer(t), 0) if t is not None:
self._tempfile=None stdout.write(file_close_producer(t), 0)
self._tempfile=None
stdout.finish(self)
stdout.close() stdout.finish(self)
stdout.close()
self.stdout=None # need to break cycle?
self._request=None self.stdout=None # need to break cycle?
self._request=None
class ChannelPipe:
"""Experimental pipe from ZPublisher to a ZServer Channel. class ChannelPipe:
Should only be used by one thread at a time. Note also that """Experimental pipe from ZPublisher to a ZServer Channel.
the channel will be being handled by another thread, thus Should only be used by one thread at a time. Note also that
restrict access to channel to the push method only.""" the channel will be being handled by another thread, thus
restrict access to channel to the push method only."""
def __init__(self, request):
self._channel=request.channel def __init__(self, request):
self._request=request self._channel=request.channel
self._shutdown=0 self._request=request
self._close=0 self._shutdown=0
self._bytes=0 self._close=0
self._bytes=0
def write(self, text, l=None):
if l is None: l=len(text) def write(self, text, l=None):
self._bytes=self._bytes + l if l is None: l=len(text)
self._channel.push(text,0) self._bytes=self._bytes + l
Wakeup() self._channel.push(text,0)
Wakeup()
def close(self):
self._channel.push(LoggingProducer(self._request, self._bytes), 0) def close(self):
self._channel.push(CallbackProducer(self._channel.done), 0) DebugLogger.log('A', id(self._request),
if self._shutdown: '%s %s' % (self._request.reply_code, self._bytes))
try: r=self._shutdown[0] self._channel.push(LoggingProducer(self._request, self._bytes), 0)
except: r=0 self._channel.push(CallbackProducer(self._channel.done), 0)
sys.ZServerExitCode=r self._channel.push(CallbackProducer(lambda t=('E', id(self._request)): apply(DebugLogger.log, t)), 0)
self._channel.push(ShutdownProducer(), 0) if self._shutdown:
Wakeup(lambda: asyncore.close_all()) try: r=self._shutdown[0]
else: except: r=0
if self._close: self._channel.push(None, 0) sys.ZServerExitCode=r
Wakeup() self._channel.push(ShutdownProducer(), 0)
Wakeup(lambda: asyncore.close_all())
self._channel=None #need to break cycles? else:
self._request=None if self._close: self._channel.push(None, 0)
Wakeup()
def flush(self): pass # yeah, whatever
self._channel=None #need to break cycles?
def finish(self, response): self._request=None
if response.headers.get('bobo-exception-type', '') == \
'exceptions.SystemExit': def flush(self): pass # yeah, whatever
r=response.headers.get('bobo-exception-value','0') def finish(self, response):
try: r=string.atoi(r) if response.headers.get('bobo-exception-type', '') == \
except: r = r and 1 or 0 'exceptions.SystemExit':
self._shutdown=r,
if response.headers.get('connection','') == 'close' or \ r=response.headers.get('bobo-exception-value','0')
response.headers.get('Connection','') == 'close': try: r=string.atoi(r)
self._close=1 except: r = r and 1 or 0
self._request.reply_code=response.status self._shutdown=r,
if response.headers.get('connection','') == 'close' or \
def retry(self): response.headers.get('Connection','') == 'close':
"""Return a request object to be used in a retry attempt self._close=1
""" self._request.reply_code=response.status
# This implementation is a bit lame, because it assumes that def retry(self):
# only stdout stderr were passed to the constructor. OTOH, I """Return a request object to be used in a retry attempt
# think that that's all that is ever passed. """
response=self.__class__(stdout=self.stdout, stderr=self.stderr) # This implementation is a bit lame, because it assumes that
response._http_version=self._http_version # only stdout stderr were passed to the constructor. OTOH, I
response._http_connection=self._http_connection # think that that's all that is ever passed.
response._server_version=self._server_version
return response response=self.__class__(stdout=self.stdout, stderr=self.stderr)
response._http_version=self._http_version
response._http_connection=self._http_connection
response._server_version=self._server_version
def make_response(request, headers): return response
"Simple http response factory"
# should this be integrated into the HTTPResponse constructor?
response=ZServerHTTPResponse(stdout=ChannelPipe(request), stderr=StringIO()) def make_response(request, headers):
response._http_version=request.version "Simple http response factory"
response._http_connection=string.lower( # should this be integrated into the HTTPResponse constructor?
http_server.get_header(http_server.CONNECTION, request.header))
response._server_version=request.channel.server.SERVER_IDENT response=ZServerHTTPResponse(stdout=ChannelPipe(request), stderr=StringIO())
return response response._http_version=request.version
response._http_connection=string.lower(
http_server.get_header(http_server.CONNECTION, request.header))
response._server_version=request.channel.server.SERVER_IDENT
class file_part_producer: return response
"producer wrapper for part of a file[-like] objects"
# match http_channel's outgoing buffer size
out_buffer_size = 1<<16 class file_part_producer:
"producer wrapper for part of a file[-like] objects"
def __init__(self, file, start, end):
self.file=file # match http_channel's outgoing buffer size
self.start=start out_buffer_size = 1<<16
self.end=end
def __init__(self, file, start, end):
def more(self): self.file=file
end=self.end self.start=start
if not end: return '' self.end=end
start=self.start
if start >= end: return '' def more(self):
end=self.end
file=self.file if not end: return ''
file.seek(start) start=self.start
size=end-start if start >= end: return ''
bsize=self.out_buffer_size
if size > bsize: size=bsize file=self.file
file.seek(start)
data = file.read(size) size=end-start
if data: bsize=self.out_buffer_size
start=start+len(data) if size > bsize: size=bsize
if start < end:
self.start=start data = file.read(size)
return data if data:
start=start+len(data)
self.end=0 if start < end:
del self.file self.start=start
return data
return data
self.end=0
class file_close_producer: del self.file
def __init__(self, file): return data
self.file=file
class file_close_producer:
def more(self):
file=self.file def __init__(self, file):
if file is not None: self.file=file
file.close()
self.file=None def more(self):
return '' file=self.file
if file is not None:
file.close()
self.file=None
return ''
...@@ -123,7 +123,7 @@ from medusa.default_handler import split_path, unquote, get_header ...@@ -123,7 +123,7 @@ from medusa.default_handler import split_path, unquote, get_header
from ZServer import CONNECTION_LIMIT, ZOPE_VERSION, ZSERVER_VERSION from ZServer import CONNECTION_LIMIT, ZOPE_VERSION, ZSERVER_VERSION
from zLOG import LOG, register_subsystem, BLATHER, INFO, WARNING, ERROR from zLOG import LOG, register_subsystem, BLATHER, INFO, WARNING, ERROR
import DebugLogger
register_subsystem('ZServer HTTPServer') register_subsystem('ZServer HTTPServer')
...@@ -140,10 +140,7 @@ header2env={'content-length' : 'CONTENT_LENGTH', ...@@ -140,10 +140,7 @@ header2env={'content-length' : 'CONTENT_LENGTH',
class zhttp_handler: class zhttp_handler:
"A medusa style handler for zhttp_server" "A medusa style handler for zhttp_server"
# XXX add code to allow env overriding
def __init__ (self, module, uri_base=None, env=None): def __init__ (self, module, uri_base=None, env=None):
"""Creates a zope_handler """Creates a zope_handler
...@@ -182,6 +179,9 @@ class zhttp_handler: ...@@ -182,6 +179,9 @@ class zhttp_handler:
def handle_request(self,request): def handle_request(self,request):
self.hits.increment() self.hits.increment()
DebugLogger.log('B', id(request), '%s %s' % (string.upper(request.command), request.uri))
size=get_header(CONTENT_LENGTH, request.header) size=get_header(CONTENT_LENGTH, request.header)
if size and size != '0': if size and size != '0':
size=string.atoi(size) size=string.atoi(size)
...@@ -269,6 +269,14 @@ class zhttp_handler: ...@@ -269,6 +269,14 @@ class zhttp_handler:
def continue_request(self, sin, request): def continue_request(self, sin, request):
"continue handling request now that we have the stdin" "continue handling request now that we have the stdin"
s=get_header(CONTENT_LENGTH, request.header)
if s:
s=string.atoi(s)
else:
s=0
DebugLogger.log('I', id(request), s)
env=self.get_environment(request) env=self.get_environment(request)
zresponse=make_response(request,env) zresponse=make_response(request,env)
zrequest=HTTPRequest(sin, env, zresponse) zrequest=HTTPRequest(sin, env, zresponse)
......
...@@ -110,7 +110,8 @@ from PubCore import handle ...@@ -110,7 +110,8 @@ from PubCore import handle
from PubCore.ZEvent import Wakeup from PubCore.ZEvent import Wakeup
from ZPublisher.HTTPResponse import HTTPResponse from ZPublisher.HTTPResponse import HTTPResponse
from ZPublisher.HTTPRequest import HTTPRequest from ZPublisher.HTTPRequest import HTTPRequest
from Producers import ShutdownProducer, LoggingProducer from Producers import ShutdownProducer, LoggingProducer, CallbackProducer
import DebugLogger
from cStringIO import StringIO from cStringIO import StringIO
from tempfile import TemporaryFile from tempfile import TemporaryFile
...@@ -141,6 +142,9 @@ class PCGIChannel(asynchat.async_chat): ...@@ -141,6 +142,9 @@ class PCGIChannel(asynchat.async_chat):
self.size=string.atoi(self.data.read()) self.size=string.atoi(self.data.read())
self.set_terminator(self.size) self.set_terminator(self.size)
if self.size==0: if self.size==0:
DebugLogger.log('I', id(self), 0)
self.set_terminator('\r\n') self.set_terminator('\r\n')
self.data=StringIO() self.data=StringIO()
self.send_response() self.send_response()
...@@ -170,9 +174,16 @@ class PCGIChannel(asynchat.async_chat): ...@@ -170,9 +174,16 @@ class PCGIChannel(asynchat.async_chat):
string.strip(self.env['PATH_INFO']),'/')) string.strip(self.env['PATH_INFO']),'/'))
self.env['PATH_INFO'] = '/' + string.join(path[len(script):],'/') self.env['PATH_INFO'] = '/' + string.join(path[len(script):],'/')
self.data=StringIO() self.data=StringIO()
DebugLogger.log('B', id(self),
'%s %s' % (self.env['REQUEST_METHOD'], self.env['PATH_INFO']))
# now read the next size header # now read the next size header
self.set_terminator(10) self.set_terminator(10)
else: else:
DebugLogger.log('I', id(self), self.terminator)
# we're done, we've got both env and stdin # we're done, we've got both env and stdin
self.set_terminator('\r\n') self.set_terminator('\r\n')
self.data.seek(0) self.data.seek(0)
...@@ -195,9 +206,8 @@ class PCGIChannel(asynchat.async_chat): ...@@ -195,9 +206,8 @@ class PCGIChannel(asynchat.async_chat):
return 1 return 1
def log_request(self, bytes): def log_request(self, bytes):
# XXX need to add reply code logging
if self.env.has_key('PATH_INFO'): if self.env.has_key('PATH_INFO'):
path='%s%s' % (self.server.module, self.env['PATH_INFO']) path=self.env['PATH_INFO']
else: else:
path='%s/' % self.server.module path='%s/' % self.server.module
if self.env.has_key('REQUEST_METHOD'): if self.env.has_key('REQUEST_METHOD'):
...@@ -207,24 +217,24 @@ class PCGIChannel(asynchat.async_chat): ...@@ -207,24 +217,24 @@ class PCGIChannel(asynchat.async_chat):
if self.addr: if self.addr:
self.server.logger.log ( self.server.logger.log (
self.addr[0], self.addr[0],
'%d - - [%s] "%s %s" %d' % ( '%d - - [%s] "%s %s" %d %d' % (
self.addr[1], self.addr[1],
time.strftime ( time.strftime (
'%d/%b/%Y:%H:%M:%S ', '%d/%b/%Y:%H:%M:%S ',
time.gmtime(time.time()) time.gmtime(time.time())
) + tz_for_log, ) + tz_for_log,
method, path, bytes method, path, self.reply_code, bytes
) )
) )
else: else:
self.server.logger.log ( self.server.logger.log (
'127.0.0.1', '127.0.0.1',
'- - [%s] "%s %s" %d' % ( '- - [%s] "%s %s" %d %d' % (
time.strftime ( time.strftime (
'%d/%b/%Y:%H:%M:%S ', '%d/%b/%Y:%H:%M:%S ',
time.gmtime(time.time()) time.gmtime(time.time())
) + tz_for_log, ) + tz_for_log,
method, path, bytes method, path, self.reply_code, bytes
) )
) )
...@@ -399,8 +409,16 @@ class PCGIPipe: ...@@ -399,8 +409,16 @@ class PCGIPipe:
def close(self): def close(self):
data=self._data.getvalue() data=self._data.getvalue()
l=len(data) l=len(data)
DebugLogger.log('A', id(self._channel),
'%s %s' % (self._channel.reply_code, l))
self._channel.push('%010d%s%010d' % (l, data, 0), 0) self._channel.push('%010d%s%010d' % (l, data, 0), 0)
self._channel.push(LoggingProducer(self._channel, l, 'log_request'), 0) self._channel.push(LoggingProducer(self._channel, l, 'log_request'), 0)
self._channel.push(CallbackProducer(
lambda t=('E', id(self._channel)): apply(DebugLogger.log,t)))
if self._shutdown: if self._shutdown:
try: r=self._shutdown[0] try: r=self._shutdown[0]
except: r=0 except: r=0
...@@ -413,10 +431,11 @@ class PCGIPipe: ...@@ -413,10 +431,11 @@ class PCGIPipe:
self._data=None self._data=None
self._channel=None self._channel=None
def finish(self,request): def finish(self, response):
if request.headers.get('bobo-exception-type','') == \ if response.headers.get('bobo-exception-type','') == \
'exceptions.SystemExit': 'exceptions.SystemExit':
r=request.headers.get('bobo-exception-value','0') r=response.headers.get('bobo-exception-value','0')
try: r=string.atoi(r) try: r=string.atoi(r)
except: r = r and 1 or 0 except: r = r and 1 or 0
self._shutdown=r, self._shutdown=r,
self._channel.reply_code=response.status
##############################################################################
#
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
#
# Copyright (c) Digital Creations. All rights reserved.
#
# This license has been certified as Open Source(tm).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions in source code must retain the above copyright
# notice, this list of conditions, and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. Digital Creations requests that attribution be given to Zope
# in any manner possible. Zope includes a "Powered by Zope"
# button that is installed by default. While it is not a license
# violation to remove this button, it is requested that the
# attribution remain. A significant investment has been put
# into Zope, and this effort will continue if the Zope community
# continues to grow. This is one way to assure that growth.
#
# 4. All advertising materials and documentation mentioning
# features derived from or use of this software must display
# the following acknowledgement:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# In the event that the product being advertised includes an
# intact Zope distribution (with copyright and license included)
# then this clause is waived.
#
# 5. Names associated with Zope or Digital Creations must not be used to
# endorse or promote products derived from this software without
# prior written permission from Digital Creations.
#
# 6. Modified redistributions of any form whatsoever must retain
# the following acknowledgment:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# Intact (re-)distributions of any official Zope release do not
# require an external acknowledgement.
#
# 7. Modifications are encouraged but must be packaged separately as
# patches to official Zope releases. Distributions that do not
# clearly separate the patches from the original work must be clearly
# labeled as unofficial distributions. Modifications which do not
# carry the name Zope may be packaged in any form, as long as they
# conform to all of the clauses above.
#
#
# Disclaimer
#
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations. Specific
# attributions are listed in the accompanying credits file.
#
##############################################################################
import time, thread
class DebugLogger:
"""
Logs debugging information about how ZServer is handling requests
and responses. This log can be used to help locate troublesome requests.
The format is:
<code> <request id> <time> <data>
where:
'code' is B for begin, I for received input, A for received output,
E for sent output.
'request id' is a unique request id.
'time' is the time in GMT ISO format.
'data' is the HTTP method and the PATH INFO for B, the size of the input
for I, the HTTP status code and the size of the output for A, or
nothing for E.
Note: This facility will be probably be adapted to the zLOG framework.
"""
def __init__(self, filename):
self.file=open(filename, 'a+b')
l=thread.allocate_lock()
self._acquire=l.acquire
self._release=l.release
def log(self, code, request_id, data=''):
self._acquire()
try:
t=time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime(time.time()))
self.file.write(
'%s %s %s %s\n' % (code, request_id, t, data)
)
finally:
self._release()
def log(*args): pass
\ No newline at end of file
############################################################################## ##############################################################################
# #
# Zope Public License (ZPL) Version 1.0 # Zope Public License (ZPL) Version 1.0
# ------------------------------------- # -------------------------------------
# #
# Copyright (c) Digital Creations. All rights reserved. # Copyright (c) Digital Creations. All rights reserved.
# #
# This license has been certified as Open Source(tm). # This license has been certified as Open Source(tm).
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are # modification, are permitted provided that the following conditions are
# met: # met:
# #
# 1. Redistributions in source code must retain the above copyright # 1. Redistributions in source code must retain the above copyright
# notice, this list of conditions, and the following disclaimer. # notice, this list of conditions, and the following disclaimer.
# #
# 2. Redistributions in binary form must reproduce the above copyright # 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in # notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the # the documentation and/or other materials provided with the
# distribution. # distribution.
# #
# 3. Digital Creations requests that attribution be given to Zope # 3. Digital Creations requests that attribution be given to Zope
# in any manner possible. Zope includes a "Powered by Zope" # in any manner possible. Zope includes a "Powered by Zope"
# button that is installed by default. While it is not a license # button that is installed by default. While it is not a license
# violation to remove this button, it is requested that the # violation to remove this button, it is requested that the
# attribution remain. A significant investment has been put # attribution remain. A significant investment has been put
# into Zope, and this effort will continue if the Zope community # into Zope, and this effort will continue if the Zope community
# continues to grow. This is one way to assure that growth. # continues to grow. This is one way to assure that growth.
# #
# 4. All advertising materials and documentation mentioning # 4. All advertising materials and documentation mentioning
# features derived from or use of this software must display # features derived from or use of this software must display
# the following acknowledgement: # the following acknowledgement:
# #
# "This product includes software developed by Digital Creations # "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment # for use in the Z Object Publishing Environment
# (http://www.zope.org/)." # (http://www.zope.org/)."
# #
# In the event that the product being advertised includes an # In the event that the product being advertised includes an
# intact Zope distribution (with copyright and license included) # intact Zope distribution (with copyright and license included)
# then this clause is waived. # then this clause is waived.
# #
# 5. Names associated with Zope or Digital Creations must not be used to # 5. Names associated with Zope or Digital Creations must not be used to
# endorse or promote products derived from this software without # endorse or promote products derived from this software without
# prior written permission from Digital Creations. # prior written permission from Digital Creations.
# #
# 6. Modified redistributions of any form whatsoever must retain # 6. Modified redistributions of any form whatsoever must retain
# the following acknowledgment: # the following acknowledgment:
# #
# "This product includes software developed by Digital Creations # "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment # for use in the Z Object Publishing Environment
# (http://www.zope.org/)." # (http://www.zope.org/)."
# #
# Intact (re-)distributions of any official Zope release do not # Intact (re-)distributions of any official Zope release do not
# require an external acknowledgement. # require an external acknowledgement.
# #
# 7. Modifications are encouraged but must be packaged separately as # 7. Modifications are encouraged but must be packaged separately as
# patches to official Zope releases. Distributions that do not # patches to official Zope releases. Distributions that do not
# clearly separate the patches from the original work must be clearly # clearly separate the patches from the original work must be clearly
# labeled as unofficial distributions. Modifications which do not # labeled as unofficial distributions. Modifications which do not
# carry the name Zope may be packaged in any form, as long as they # carry the name Zope may be packaged in any form, as long as they
# conform to all of the clauses above. # conform to all of the clauses above.
# #
# #
# Disclaimer # Disclaimer
# #
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY # THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE. # SUCH DAMAGE.
# #
# #
# This software consists of contributions made by Digital Creations and # This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations. Specific # many individuals on behalf of Digital Creations. Specific
# attributions are listed in the accompanying credits file. # attributions are listed in the accompanying credits file.
# #
############################################################################## ##############################################################################
""" """
ZServer HTTPResponse ZServer HTTPResponse
The HTTPResponse class takes care of server headers, response munging The HTTPResponse class takes care of server headers, response munging
and logging duties. and logging duties.
""" """
import time, regex, string, sys, tempfile import time, regex, string, sys, tempfile
from cStringIO import StringIO from cStringIO import StringIO
from ZPublisher.HTTPResponse import HTTPResponse, end_of_header_search from ZPublisher.HTTPResponse import HTTPResponse, end_of_header_search
from medusa.http_date import build_http_date from medusa.http_date import build_http_date
from PubCore.ZEvent import Wakeup from PubCore.ZEvent import Wakeup
from medusa.producers import hooked_producer from medusa.producers import hooked_producer
from medusa import http_server, asyncore from medusa import http_server, asyncore
from Producers import ShutdownProducer, LoggingProducer, CallbackProducer from Producers import ShutdownProducer, LoggingProducer, CallbackProducer
import DebugLogger
class ZServerHTTPResponse(HTTPResponse):
"Used to push data into a channel's producer fifo" class ZServerHTTPResponse(HTTPResponse):
"Used to push data into a channel's producer fifo"
http_chunk=0
http_chunk_size=1024 http_chunk=0
http_chunk_size=1024
# defaults
_http_version='1.0' # defaults
_http_connection='close' _http_version='1.0'
_server_version='Zope/2.0 ZServer/2.0' _http_connection='close'
_server_version='Zope/2.0 ZServer/2.0'
def __str__(self,
html_search=regex.compile('<html>',regex.casefold).search, def __str__(self,
): html_search=regex.compile('<html>',regex.casefold).search,
if self._wrote: return '' # Streaming output was used. ):
if self._wrote: return '' # Streaming output was used.
headers=self.headers
body=self.body headers=self.headers
if body: body=self.body
isHTML=self.isHTML(body) if body:
if not headers.has_key('content-type'): isHTML=self.isHTML(body)
if isHTML: if not headers.has_key('content-type'):
c='text/html' if isHTML:
else: c='text/html'
c='text/plain' else:
self.setHeader('content-type',c) c='text/plain'
else: self.setHeader('content-type',c)
isHTML = headers['content-type']=='text/html' else:
if isHTML and end_of_header_search(self.body) < 0: isHTML = headers['content-type']=='text/html'
lhtml=html_search(body) if isHTML and end_of_header_search(self.body) < 0:
if lhtml >= 0: lhtml=html_search(body)
lhtml=lhtml+6 if lhtml >= 0:
body='%s<head></head>\n%s' % (body[:lhtml],body[lhtml:]) lhtml=lhtml+6
else: body='%s<head></head>\n%s' % (body[:lhtml],body[lhtml:])
body='<html><head></head>\n' + body else:
self.setBody(body) body='<html><head></head>\n' + body
body=self.body self.setBody(body)
body=self.body
# set 204 (no content) status if 200 and response is empty
if not headers.has_key('content-type') and \ # set 204 (no content) status if 200 and response is empty
not headers.has_key('content-length') and \ if not headers.has_key('content-type') and \
not headers.has_key('transfer-encoding') and \ not headers.has_key('content-length') and \
self.status == 200: not headers.has_key('transfer-encoding') and \
self.setStatus('nocontent') self.status == 200:
self.setStatus('nocontent')
# add content length if not transfer encoded
if not headers.has_key('content-length') and \ # add content length if not transfer encoded
not headers.has_key('transfer-encoding'): if not headers.has_key('content-length') and \
self.setHeader('content-length',len(body)) not headers.has_key('transfer-encoding'):
self.setHeader('content-length',len(body))
headersl=[]
append=headersl.append headersl=[]
append=headersl.append
status=headers.get('status', '200 OK')
status=headers.get('status', '200 OK')
# status header must come first.
append("HTTP/%s %s" % (self._http_version, status)) # status header must come first.
if headers.has_key('status'): append("HTTP/%s %s" % (self._http_version, status))
del headers['status'] if headers.has_key('status'):
del headers['status']
# add zserver headers
append('Server: %s' % self._server_version) # add zserver headers
append('Date: %s' % build_http_date(time.time())) append('Server: %s' % self._server_version)
chunk=0 append('Date: %s' % build_http_date(time.time()))
if self._http_version=='1.0': chunk=0
if self._http_connection=='keep alive': if self._http_version=='1.0':
if self.headers.has_key('content-length'): if self._http_connection=='keep alive':
self.setHeader('Connection','close') if self.headers.has_key('content-length'):
else: self.setHeader('Connection','close')
self.setHeader('Connection','Keep-Alive') else:
else: self.setHeader('Connection','Keep-Alive')
self.setHeader('Connection','close') else:
elif self._http_version=='1.1': self.setHeader('Connection','close')
if self._http_connection=='close': elif self._http_version=='1.1':
self.setHeader('Connection','close') if self._http_connection=='close':
elif not self.headers.has_key('content-length'): self.setHeader('Connection','close')
if self.headers.has_key('transfer-encoding'): elif not self.headers.has_key('content-length'):
if self.headers['transfer-encoding'] != 'chunked': if self.headers.has_key('transfer-encoding'):
self.setHeader('Connection','close') if self.headers['transfer-encoding'] != 'chunked':
else: self.setHeader('Connection','close')
chunk=1 else:
elif self.http_chunk: chunk=1
self.setHeader('Transfer-Encoding','chunked') elif self.http_chunk:
chunk=1 self.setHeader('Transfer-Encoding','chunked')
else: chunk=1
self.setHeader('Connection','close') else:
self.setHeader('Connection','close')
if chunk:
chunked_body='' if chunk:
while body: chunked_body=''
chunk=body[:self.http_chunk_size] while body:
body=body[self.http_chunk_size:] chunk=body[:self.http_chunk_size]
chunked_body='%s%x\r\n%s\r\n' % (chunked_body, len(chunk), chunk) body=body[self.http_chunk_size:]
chunked_body='%s0\r\n\r\n' % chunked_body chunked_body='%s%x\r\n%s\r\n' % (chunked_body, len(chunk), chunk)
body=chunked_body chunked_body='%s0\r\n\r\n' % chunked_body
body=chunked_body
for key, val in headers.items():
if string.lower(key)==key: for key, val in headers.items():
# only change non-literal header names if string.lower(key)==key:
key="%s%s" % (string.upper(key[:1]), key[1:]) # only change non-literal header names
start=0 key="%s%s" % (string.upper(key[:1]), key[1:])
l=string.find(key,'-',start) start=0
while l >= start: l=string.find(key,'-',start)
key="%s-%s%s" % (key[:l],string.upper(key[l+1:l+2]),key[l+2:]) while l >= start:
start=l+1 key="%s-%s%s" % (key[:l],string.upper(key[l+1:l+2]),key[l+2:])
l=string.find(key,'-',start) start=l+1
append("%s: %s" % (key, val)) l=string.find(key,'-',start)
if self.cookies: append("%s: %s" % (key, val))
headersl=headersl+self._cookie_list() if self.cookies:
headersl[len(headersl):]=[self.accumulated_headers, body] headersl=headersl+self._cookie_list()
return string.join(headersl,'\r\n') headersl[len(headersl):]=[self.accumulated_headers, body]
return string.join(headersl,'\r\n')
_tempfile=None
_tempstart=0 _tempfile=None
def write(self,data): _tempstart=0
"""\ def write(self,data):
Return data as a stream """\
Return data as a stream
HTML data may be returned using a stream-oriented interface.
This allows the browser to display partial results while HTML data may be returned using a stream-oriented interface.
computation of a response to proceed. This allows the browser to display partial results while
computation of a response to proceed.
The published object should first set any output headers or
cookies on the response object. The published object should first set any output headers or
cookies on the response object.
Note that published objects must not generate any errors
after beginning stream-oriented output. Note that published objects must not generate any errors
after beginning stream-oriented output.
"""
stdout=self.stdout """
stdout=self.stdout
if not self._wrote:
l=self.headers.get('content-length', None) if not self._wrote:
if l is not None: l=self.headers.get('content-length', None)
try: if l is not None:
if type(l) is type(''): l=string.atoi(l) try:
if l > 128000: if type(l) is type(''): l=string.atoi(l)
self._tempfile=tempfile.TemporaryFile() if l > 128000:
except: pass self._tempfile=tempfile.TemporaryFile()
except: pass
stdout.write(str(self))
self._wrote=1 stdout.write(str(self))
self._wrote=1
if not data: return
if not data: return
t=self._tempfile
if t is None: t=self._tempfile
stdout.write(data) if t is None:
else: stdout.write(data)
l=len(data) else:
b=self._tempstart l=len(data)
e=b+l b=self._tempstart
t.seek(b) e=b+l
t.write(data) t.seek(b)
self._tempstart=e t.write(data)
stdout.write(file_part_producer(t,b,e), l) self._tempstart=e
stdout.write(file_part_producer(t,b,e), l)
# XXX add server headers, etc to write()
# XXX add server headers, etc to write()
def _finish(self):
stdout=self.stdout def _finish(self):
stdout=self.stdout
t=self._tempfile
if t is not None: t=self._tempfile
stdout.write(file_close_producer(t), 0) if t is not None:
self._tempfile=None stdout.write(file_close_producer(t), 0)
self._tempfile=None
stdout.finish(self)
stdout.close() stdout.finish(self)
stdout.close()
self.stdout=None # need to break cycle?
self._request=None self.stdout=None # need to break cycle?
self._request=None
class ChannelPipe:
"""Experimental pipe from ZPublisher to a ZServer Channel. class ChannelPipe:
Should only be used by one thread at a time. Note also that """Experimental pipe from ZPublisher to a ZServer Channel.
the channel will be being handled by another thread, thus Should only be used by one thread at a time. Note also that
restrict access to channel to the push method only.""" the channel will be being handled by another thread, thus
restrict access to channel to the push method only."""
def __init__(self, request):
self._channel=request.channel def __init__(self, request):
self._request=request self._channel=request.channel
self._shutdown=0 self._request=request
self._close=0 self._shutdown=0
self._bytes=0 self._close=0
self._bytes=0
def write(self, text, l=None):
if l is None: l=len(text) def write(self, text, l=None):
self._bytes=self._bytes + l if l is None: l=len(text)
self._channel.push(text,0) self._bytes=self._bytes + l
Wakeup() self._channel.push(text,0)
Wakeup()
def close(self):
self._channel.push(LoggingProducer(self._request, self._bytes), 0) def close(self):
self._channel.push(CallbackProducer(self._channel.done), 0) DebugLogger.log('A', id(self._request),
if self._shutdown: '%s %s' % (self._request.reply_code, self._bytes))
try: r=self._shutdown[0] self._channel.push(LoggingProducer(self._request, self._bytes), 0)
except: r=0 self._channel.push(CallbackProducer(self._channel.done), 0)
sys.ZServerExitCode=r self._channel.push(CallbackProducer(lambda t=('E', id(self._request)): apply(DebugLogger.log, t)), 0)
self._channel.push(ShutdownProducer(), 0) if self._shutdown:
Wakeup(lambda: asyncore.close_all()) try: r=self._shutdown[0]
else: except: r=0
if self._close: self._channel.push(None, 0) sys.ZServerExitCode=r
Wakeup() self._channel.push(ShutdownProducer(), 0)
Wakeup(lambda: asyncore.close_all())
self._channel=None #need to break cycles? else:
self._request=None if self._close: self._channel.push(None, 0)
Wakeup()
def flush(self): pass # yeah, whatever
self._channel=None #need to break cycles?
def finish(self, response): self._request=None
if response.headers.get('bobo-exception-type', '') == \
'exceptions.SystemExit': def flush(self): pass # yeah, whatever
r=response.headers.get('bobo-exception-value','0') def finish(self, response):
try: r=string.atoi(r) if response.headers.get('bobo-exception-type', '') == \
except: r = r and 1 or 0 'exceptions.SystemExit':
self._shutdown=r,
if response.headers.get('connection','') == 'close' or \ r=response.headers.get('bobo-exception-value','0')
response.headers.get('Connection','') == 'close': try: r=string.atoi(r)
self._close=1 except: r = r and 1 or 0
self._request.reply_code=response.status self._shutdown=r,
if response.headers.get('connection','') == 'close' or \
def retry(self): response.headers.get('Connection','') == 'close':
"""Return a request object to be used in a retry attempt self._close=1
""" self._request.reply_code=response.status
# This implementation is a bit lame, because it assumes that def retry(self):
# only stdout stderr were passed to the constructor. OTOH, I """Return a request object to be used in a retry attempt
# think that that's all that is ever passed. """
response=self.__class__(stdout=self.stdout, stderr=self.stderr) # This implementation is a bit lame, because it assumes that
response._http_version=self._http_version # only stdout stderr were passed to the constructor. OTOH, I
response._http_connection=self._http_connection # think that that's all that is ever passed.
response._server_version=self._server_version
return response response=self.__class__(stdout=self.stdout, stderr=self.stderr)
response._http_version=self._http_version
response._http_connection=self._http_connection
response._server_version=self._server_version
def make_response(request, headers): return response
"Simple http response factory"
# should this be integrated into the HTTPResponse constructor?
response=ZServerHTTPResponse(stdout=ChannelPipe(request), stderr=StringIO()) def make_response(request, headers):
response._http_version=request.version "Simple http response factory"
response._http_connection=string.lower( # should this be integrated into the HTTPResponse constructor?
http_server.get_header(http_server.CONNECTION, request.header))
response._server_version=request.channel.server.SERVER_IDENT response=ZServerHTTPResponse(stdout=ChannelPipe(request), stderr=StringIO())
return response response._http_version=request.version
response._http_connection=string.lower(
http_server.get_header(http_server.CONNECTION, request.header))
response._server_version=request.channel.server.SERVER_IDENT
class file_part_producer: return response
"producer wrapper for part of a file[-like] objects"
# match http_channel's outgoing buffer size
out_buffer_size = 1<<16 class file_part_producer:
"producer wrapper for part of a file[-like] objects"
def __init__(self, file, start, end):
self.file=file # match http_channel's outgoing buffer size
self.start=start out_buffer_size = 1<<16
self.end=end
def __init__(self, file, start, end):
def more(self): self.file=file
end=self.end self.start=start
if not end: return '' self.end=end
start=self.start
if start >= end: return '' def more(self):
end=self.end
file=self.file if not end: return ''
file.seek(start) start=self.start
size=end-start if start >= end: return ''
bsize=self.out_buffer_size
if size > bsize: size=bsize file=self.file
file.seek(start)
data = file.read(size) size=end-start
if data: bsize=self.out_buffer_size
start=start+len(data) if size > bsize: size=bsize
if start < end:
self.start=start data = file.read(size)
return data if data:
start=start+len(data)
self.end=0 if start < end:
del self.file self.start=start
return data
return data
self.end=0
class file_close_producer: del self.file
def __init__(self, file): return data
self.file=file
class file_close_producer:
def more(self):
file=self.file def __init__(self, file):
if file is not None: self.file=file
file.close()
self.file=None def more(self):
return '' file=self.file
if file is not None:
file.close()
self.file=None
return ''
...@@ -123,7 +123,7 @@ from medusa.default_handler import split_path, unquote, get_header ...@@ -123,7 +123,7 @@ from medusa.default_handler import split_path, unquote, get_header
from ZServer import CONNECTION_LIMIT, ZOPE_VERSION, ZSERVER_VERSION from ZServer import CONNECTION_LIMIT, ZOPE_VERSION, ZSERVER_VERSION
from zLOG import LOG, register_subsystem, BLATHER, INFO, WARNING, ERROR from zLOG import LOG, register_subsystem, BLATHER, INFO, WARNING, ERROR
import DebugLogger
register_subsystem('ZServer HTTPServer') register_subsystem('ZServer HTTPServer')
...@@ -140,10 +140,7 @@ header2env={'content-length' : 'CONTENT_LENGTH', ...@@ -140,10 +140,7 @@ header2env={'content-length' : 'CONTENT_LENGTH',
class zhttp_handler: class zhttp_handler:
"A medusa style handler for zhttp_server" "A medusa style handler for zhttp_server"
# XXX add code to allow env overriding
def __init__ (self, module, uri_base=None, env=None): def __init__ (self, module, uri_base=None, env=None):
"""Creates a zope_handler """Creates a zope_handler
...@@ -182,6 +179,9 @@ class zhttp_handler: ...@@ -182,6 +179,9 @@ class zhttp_handler:
def handle_request(self,request): def handle_request(self,request):
self.hits.increment() self.hits.increment()
DebugLogger.log('B', id(request), '%s %s' % (string.upper(request.command), request.uri))
size=get_header(CONTENT_LENGTH, request.header) size=get_header(CONTENT_LENGTH, request.header)
if size and size != '0': if size and size != '0':
size=string.atoi(size) size=string.atoi(size)
...@@ -269,6 +269,14 @@ class zhttp_handler: ...@@ -269,6 +269,14 @@ class zhttp_handler:
def continue_request(self, sin, request): def continue_request(self, sin, request):
"continue handling request now that we have the stdin" "continue handling request now that we have the stdin"
s=get_header(CONTENT_LENGTH, request.header)
if s:
s=string.atoi(s)
else:
s=0
DebugLogger.log('I', id(request), s)
env=self.get_environment(request) env=self.get_environment(request)
zresponse=make_response(request,env) zresponse=make_response(request,env)
zrequest=HTTPRequest(sin, env, zresponse) zrequest=HTTPRequest(sin, env, zresponse)
......
...@@ -110,7 +110,8 @@ from PubCore import handle ...@@ -110,7 +110,8 @@ from PubCore import handle
from PubCore.ZEvent import Wakeup from PubCore.ZEvent import Wakeup
from ZPublisher.HTTPResponse import HTTPResponse from ZPublisher.HTTPResponse import HTTPResponse
from ZPublisher.HTTPRequest import HTTPRequest from ZPublisher.HTTPRequest import HTTPRequest
from Producers import ShutdownProducer, LoggingProducer from Producers import ShutdownProducer, LoggingProducer, CallbackProducer
import DebugLogger
from cStringIO import StringIO from cStringIO import StringIO
from tempfile import TemporaryFile from tempfile import TemporaryFile
...@@ -141,6 +142,9 @@ class PCGIChannel(asynchat.async_chat): ...@@ -141,6 +142,9 @@ class PCGIChannel(asynchat.async_chat):
self.size=string.atoi(self.data.read()) self.size=string.atoi(self.data.read())
self.set_terminator(self.size) self.set_terminator(self.size)
if self.size==0: if self.size==0:
DebugLogger.log('I', id(self), 0)
self.set_terminator('\r\n') self.set_terminator('\r\n')
self.data=StringIO() self.data=StringIO()
self.send_response() self.send_response()
...@@ -170,9 +174,16 @@ class PCGIChannel(asynchat.async_chat): ...@@ -170,9 +174,16 @@ class PCGIChannel(asynchat.async_chat):
string.strip(self.env['PATH_INFO']),'/')) string.strip(self.env['PATH_INFO']),'/'))
self.env['PATH_INFO'] = '/' + string.join(path[len(script):],'/') self.env['PATH_INFO'] = '/' + string.join(path[len(script):],'/')
self.data=StringIO() self.data=StringIO()
DebugLogger.log('B', id(self),
'%s %s' % (self.env['REQUEST_METHOD'], self.env['PATH_INFO']))
# now read the next size header # now read the next size header
self.set_terminator(10) self.set_terminator(10)
else: else:
DebugLogger.log('I', id(self), self.terminator)
# we're done, we've got both env and stdin # we're done, we've got both env and stdin
self.set_terminator('\r\n') self.set_terminator('\r\n')
self.data.seek(0) self.data.seek(0)
...@@ -195,9 +206,8 @@ class PCGIChannel(asynchat.async_chat): ...@@ -195,9 +206,8 @@ class PCGIChannel(asynchat.async_chat):
return 1 return 1
def log_request(self, bytes): def log_request(self, bytes):
# XXX need to add reply code logging
if self.env.has_key('PATH_INFO'): if self.env.has_key('PATH_INFO'):
path='%s%s' % (self.server.module, self.env['PATH_INFO']) path=self.env['PATH_INFO']
else: else:
path='%s/' % self.server.module path='%s/' % self.server.module
if self.env.has_key('REQUEST_METHOD'): if self.env.has_key('REQUEST_METHOD'):
...@@ -207,24 +217,24 @@ class PCGIChannel(asynchat.async_chat): ...@@ -207,24 +217,24 @@ class PCGIChannel(asynchat.async_chat):
if self.addr: if self.addr:
self.server.logger.log ( self.server.logger.log (
self.addr[0], self.addr[0],
'%d - - [%s] "%s %s" %d' % ( '%d - - [%s] "%s %s" %d %d' % (
self.addr[1], self.addr[1],
time.strftime ( time.strftime (
'%d/%b/%Y:%H:%M:%S ', '%d/%b/%Y:%H:%M:%S ',
time.gmtime(time.time()) time.gmtime(time.time())
) + tz_for_log, ) + tz_for_log,
method, path, bytes method, path, self.reply_code, bytes
) )
) )
else: else:
self.server.logger.log ( self.server.logger.log (
'127.0.0.1', '127.0.0.1',
'- - [%s] "%s %s" %d' % ( '- - [%s] "%s %s" %d %d' % (
time.strftime ( time.strftime (
'%d/%b/%Y:%H:%M:%S ', '%d/%b/%Y:%H:%M:%S ',
time.gmtime(time.time()) time.gmtime(time.time())
) + tz_for_log, ) + tz_for_log,
method, path, bytes method, path, self.reply_code, bytes
) )
) )
...@@ -399,8 +409,16 @@ class PCGIPipe: ...@@ -399,8 +409,16 @@ class PCGIPipe:
def close(self): def close(self):
data=self._data.getvalue() data=self._data.getvalue()
l=len(data) l=len(data)
DebugLogger.log('A', id(self._channel),
'%s %s' % (self._channel.reply_code, l))
self._channel.push('%010d%s%010d' % (l, data, 0), 0) self._channel.push('%010d%s%010d' % (l, data, 0), 0)
self._channel.push(LoggingProducer(self._channel, l, 'log_request'), 0) self._channel.push(LoggingProducer(self._channel, l, 'log_request'), 0)
self._channel.push(CallbackProducer(
lambda t=('E', id(self._channel)): apply(DebugLogger.log,t)))
if self._shutdown: if self._shutdown:
try: r=self._shutdown[0] try: r=self._shutdown[0]
except: r=0 except: r=0
...@@ -413,10 +431,11 @@ class PCGIPipe: ...@@ -413,10 +431,11 @@ class PCGIPipe:
self._data=None self._data=None
self._channel=None self._channel=None
def finish(self,request): def finish(self, response):
if request.headers.get('bobo-exception-type','') == \ if response.headers.get('bobo-exception-type','') == \
'exceptions.SystemExit': 'exceptions.SystemExit':
r=request.headers.get('bobo-exception-value','0') r=response.headers.get('bobo-exception-value','0')
try: r=string.atoi(r) try: r=string.atoi(r)
except: r = r and 1 or 0 except: r = r and 1 or 0
self._shutdown=r, self._shutdown=r,
self._channel.reply_code=response.status
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