[Zope-Checkins] CVS: Zope3/lib/python/Zope/Server - HTTPResponse.py:1.1.2.1 HTTPServer.py:1.1.2.1
Shane Hathaway
shane@digicool.com
Fri, 16 Nov 2001 11:47:51 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/Server
In directory cvs.zope.org:/tmp/cvs-serv13948
Added Files:
Tag: Zope-3x-branch
HTTPResponse.py HTTPServer.py
Log Message:
Copied essential bits of ZServer
=== Added File Zope3/lib/python/Zope/Server/HTTPResponse.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""
ZServer HTTPResponse
The HTTPResponse class takes care of server headers, response munging
and logging duties.
"""
import time, re, sys, tempfile
from cStringIO import StringIO
import thread
from ZPublisher.HTTPResponse import HTTPResponse
from medusa.http_date import build_http_date
from PubCore.ZEvent import Wakeup
from medusa.producers import hooked_producer
from medusa import http_server
import asyncore
from Producers import ShutdownProducer, LoggingProducer, CallbackProducer, \
file_part_producer, file_close_producer
from types import LongType
import DebugLogger
class ZServerHTTPResponse(HTTPResponse):
"Used to push data into a channel's producer fifo"
# Set this value to 1 if streaming output in
# HTTP/1.1 should use chunked encoding
http_chunk=1
http_chunk_size=1024
# defaults
_http_version='1.0'
_http_connection='close'
_server_version='Zope/2.0 ZServer/2.0'
# using streaming response
_streaming=0
# using chunking transfer-encoding
_chunking=0
def __str__(self,
html_search=re.compile('<html>',re.I).search,
):
if self._wrote:
if self._chunking:
return '0\r\n\r\n'
else:
return ''
headers=self.headers
body=self.body
# set 204 (no content) status if 200 and response is empty
# and not streaming
if not headers.has_key('content-type') and \
not headers.has_key('content-length') and \
not self._streaming and \
self.status == 200:
self.setStatus('nocontent')
# add content length if not streaming
if not headers.has_key('content-length') and \
not self._streaming:
self.setHeader('content-length',len(body))
content_length= headers.get('content-length', None)
if content_length>0 :
self.setHeader('content-length', content_length)
headersl=[]
append=headersl.append
status=headers.get('status', '200 OK')
# status header must come first.
append("HTTP/%s %s" % (self._http_version or '1.0' , status))
if headers.has_key('status'):
del headers['status']
# add zserver headers
append('Server: %s' % self._server_version)
append('Date: %s' % build_http_date(time.time()))
if self._http_version=='1.0':
if self._http_connection=='keep-alive' and \
self.headers.has_key('content-length'):
self.setHeader('Connection','Keep-Alive')
else:
self.setHeader('Connection','close')
# Close the connection if we have been asked to.
# Use chunking if streaming output.
if self._http_version=='1.1':
if self._http_connection=='close':
self.setHeader('Connection','close')
elif not self.headers.has_key('content-length'):
if self.http_chunk and self._streaming:
self.setHeader('Transfer-Encoding','chunked')
self._chunking=1
else:
self.setHeader('Connection','close')
for key, val in headers.items():
if key.lower()==key:
# only change non-literal header names
key="%s%s" % (key[:1].upper(), key[1:])
start=0
l=key.find('-',start)
while l >= start:
key="%s-%s%s" % (key[:l],key[l+1:l+2].upper(),key[l+2:])
start=l+1
l=key.find('-',start)
append("%s: %s" % (key, val))
if self.cookies:
headersl=headersl+self._cookie_list()
headersl[len(headersl):]=[self.accumulated_headers, body]
return "\r\n".join(headersl)
_tempfile=None
_templock=None
_tempstart=0
def write(self,data):
"""\
Return data as a stream
HTML data may be returned using a stream-oriented interface.
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.
Note that published objects must not generate any errors
after beginning stream-oriented output.
"""
stdout=self.stdout
if not self._wrote:
l=self.headers.get('content-length', None)
if l is not None:
try:
if type(l) is type(''): l=int(l)
if l > 128000:
self._tempfile=tempfile.TemporaryFile()
self._templock=thread.allocate_lock()
except: pass
self._streaming=1
stdout.write(str(self))
self._wrote=1
if not data: return
if self._chunking:
data = '%x\r\n%s\r\n' % (len(data),data)
t=self._tempfile
if t is None:
stdout.write(data)
else:
l=len(data)
b=self._tempstart
e=b+l
self._templock.acquire()
try:
t.seek(b)
t.write(data)
finally:
self._templock.release()
self._tempstart=e
stdout.write(file_part_producer(t,self._templock,b,e), l)
_retried_response = None
def _finish(self):
if self._retried_response:
try:
self._retried_response._finish()
finally:
self._retried_response = None
return
stdout=self.stdout
t=self._tempfile
if t is not None:
stdout.write(file_close_producer(t), 0)
self._tempfile=None
stdout.finish(self)
stdout.close()
self.stdout=None # need to break cycle?
self._request=None
def retry(self):
"""Return a request object to be used in a retry attempt
"""
# This implementation is a bit lame, because it assumes that
# only stdout stderr were passed to the constructor. OTOH, I
# think that that's all that is ever passed.
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
self._retried_response = response
return response
class ChannelPipe:
"""Experimental pipe from ZPublisher to a ZServer Channel.
Should only be used by one thread at a time. Note also that
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
self._request=request
self._shutdown=0
self._close=0
self._bytes=0
def write(self, text, l=None):
if self._channel.closed:
return
if l is None: l=len(text)
self._bytes=self._bytes + l
self._channel.push(text,0)
Wakeup()
def close(self):
DebugLogger.log('A', id(self._request),
'%s %s' % (self._request.reply_code, self._bytes))
if not self._channel.closed:
self._channel.push(LoggingProducer(self._request, self._bytes), 0)
self._channel.push(CallbackProducer(self._channel.done), 0)
self._channel.push(CallbackProducer(
lambda t=('E', id(self._request)): apply(DebugLogger.log, t)), 0)
if self._shutdown:
try: r=self._shutdown[0]
except: r=0
sys.ZServerExitCode=r
self._channel.push(ShutdownProducer(), 0)
Wakeup()
else:
if self._close: self._channel.push(None, 0)
Wakeup()
else:
# channel closed too soon
self._request.log(self._bytes)
DebugLogger.log('E', id(self._request))
if self._shutdown:
try: r=self._shutdown[0]
except: r=0
sys.ZServerExitCode=r
Wakeup(lambda: asyncore.close_all())
else:
Wakeup()
self._channel=None #need to break cycles?
self._request=None
def flush(self): pass # yeah, whatever
def finish(self, response):
if response.headers.get('bobo-exception-type', '') == \
'exceptions.SystemExit':
r=response.headers.get('bobo-exception-value','0')
try: r=int(r)
except: r = r and 1 or 0
self._shutdown=r,
if response.headers.get('connection','') == 'close' or \
response.headers.get('Connection','') == 'close':
self._close=1
self._request.reply_code=response.status
def make_response(request, headers):
"Simple http response factory"
# should this be integrated into the HTTPResponse constructor?
response=ZServerHTTPResponse(stdout=ChannelPipe(request), stderr=StringIO())
response._http_version=request.version
response._http_connection=(
http_server.get_header(http_server.CONNECTION, request.header)).lower()
response._server_version=request.channel.server.SERVER_IDENT
return response
=== Added File Zope3/lib/python/Zope/Server/HTTPServer.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""
Medusa HTTP server for Zope
changes from Medusa's http_server
Request Threads -- Requests are processed by threads from a thread
pool.
Output Handling -- Output is pushed directly into the producer
fifo by the request-handling thread. The HTTP server does not do
any post-processing such as chunking.
Pipelineable -- This is needed for protocols such as HTTP/1.1 in
which mutiple requests come in on the same channel, before
responses are sent back. When requests are pipelined, the client
doesn't wait for the response before sending another request. The
server must ensure that responses are sent back in the same order
as requests are received.
"""
import sys
import re
import os
import types
import thread
import time
import socket
from cStringIO import StringIO
from PubCore import handle
from HTTPResponse import make_response
from ZPublisher.HTTPRequest import HTTPRequest
from medusa.http_server import http_server,get_header, http_channel, VERSION_STRING
import asyncore
from medusa import counter, producers
from medusa.test import max_sockets
from medusa.default_handler import unquote
from asyncore import compact_traceback, dispatcher
from ZServer import CONNECTION_LIMIT, ZOPE_VERSION, ZSERVER_VERSION
from ZServer import requestCloseOnExec
from zLOG import LOG, register_subsystem, BLATHER, INFO, WARNING, ERROR
import DebugLogger
from medusa import logger
register_subsystem('ZServer HTTPServer')
CONTENT_LENGTH = re.compile('Content-Length: ([0-9]+)',re.I)
CONNECTION = re.compile('Connection: (.*)', re.I)
# maps request some headers to environment variables.
# (those that don't start with 'HTTP_')
header2env={'content-length' : 'CONTENT_LENGTH',
'content-type' : 'CONTENT_TYPE',
'connection' : 'CONNECTION_TYPE',
}
class zhttp_collector:
def __init__(self, handler, request, size):
self.handler = handler
self.request = request
if size > 524288:
# write large upload data to a file
from tempfile import TemporaryFile
self.data = TemporaryFile('w+b')
else:
self.data = StringIO()
request.channel.set_terminator(size)
request.collector=self
# put and post collection methods
#
def collect_incoming_data (self, data):
self.data.write(data)
def found_terminator(self):
# reset collector
self.request.channel.set_terminator('\r\n\r\n')
self.request.collector=None
# finish request
self.data.seek(0)
r=self.request
d=self.data
del self.request
del self.data
self.handler.continue_request(d,r)
class zhttp_handler:
"A medusa style handler for zhttp_server"
def __init__ (self, module, uri_base=None, env=None):
"""Creates a zope_handler
module -- string, the name of the module to publish
uri_base -- string, the base uri of the published module
defaults to '/<module name>' if not given.
env -- dictionary, environment variables to be overridden.
Replaces standard variables with supplied ones.
"""
self.module_name=module
self.env_override=env or {}
self.hits = counter.counter()
# if uri_base is unspecified, assume it
# starts with the published module name
#
if uri_base is None:
uri_base='/%s' % module
elif uri_base == '':
uri_base='/'
else:
if uri_base[0] != '/':
uri_base='/'+uri_base
if uri_base[-1] == '/':
uri_base=uri_base[:-1]
self.uri_base=uri_base
uri_regex='%s.*' % self.uri_base
self.uri_regex = re.compile(uri_regex)
def match(self, request):
uri = request.uri
if self.uri_regex.match(uri):
return 1
else:
return 0
def handle_request(self,request):
self.hits.increment()
DebugLogger.log('B', id(request), '%s %s' % (request.command.upper(), request.uri))
size=get_header(CONTENT_LENGTH, request.header)
if size and size != '0':
size=int(size)
zhttp_collector(self, request, size)
else:
sin=StringIO()
self.continue_request(sin,request)
def get_environment(self, request,
# These are strictly performance hackery...
h2ehas=header2env.has_key,
h2eget=header2env.get,
workdir=os.getcwd(),
ospath=os.path,
):
(path, params, query, fragment) = request.split_uri()
if params: path = path + params # undo medusa bug!
while path and path[0] == '/':
path = path[1:]
if '%' in path:
path = unquote(path)
if query:
# ZPublisher doesn't want the leading '?'
query = query[1:]
server=request.channel.server
env = {}
env['REQUEST_METHOD']=request.command.upper()
env['SERVER_PORT']=str(server.port)
env['SERVER_NAME']=server.server_name
env['SERVER_SOFTWARE']=server.SERVER_IDENT
env['SERVER_PROTOCOL']="HTTP/"+request.version
env['channel.creation_time']=request.channel.creation_time
if self.uri_base=='/':
env['SCRIPT_NAME']=''
env['PATH_INFO']='/' + path
else:
env['SCRIPT_NAME'] = self.uri_base
try:
path_info=path.split(self.uri_base[1:],1)[1]
except:
path_info=''
env['PATH_INFO']=path_info
env['PATH_TRANSLATED']=ospath.normpath(ospath.join(
workdir, env['PATH_INFO']))
if query:
env['QUERY_STRING'] = query
env['GATEWAY_INTERFACE']='CGI/1.1'
env['REMOTE_ADDR']=request.channel.addr[0]
# If we're using a resolving logger, try to get the
# remote host from the resolver's cache.
if hasattr(server.logger, 'resolver'):
dns_cache=server.logger.resolver.cache
if dns_cache.has_key(env['REMOTE_ADDR']):
remote_host=dns_cache[env['REMOTE_ADDR']][2]
if remote_host is not None:
env['REMOTE_HOST']=remote_host
env_has=env.has_key
for header in request.header:
key,value=header.split(":",1)
key=key.lower()
value=value.strip()
if h2ehas(key) and value:
env[h2eget(key)]=value
else:
key='HTTP_%s' % ("_".join(key.split( "-"))).upper()
if value and not env_has(key):
env[key]=value
env.update(self.env_override)
return env
def continue_request(self, sin, request):
"continue handling request now that we have the stdin"
s=get_header(CONTENT_LENGTH, request.header)
if s:
s=int(s)
else:
s=0
DebugLogger.log('I', id(request), s)
env=self.get_environment(request)
zresponse=make_response(request,env)
zrequest=HTTPRequest(sin, env, zresponse)
request.channel.current_request=None
request.channel.queue.append((self.module_name, zrequest, zresponse))
request.channel.work()
def status(self):
return producers.simple_producer("""
<li>Zope Handler
<ul>
<li><b>Published Module:</b> %s
<li><b>Hits:</b> %s
</ul>""" %(self.module_name, self.hits)
)
class zhttp_channel(http_channel):
"http channel"
closed=0
zombie_timeout=100*60 # 100 minutes
def __init__(self, server, conn, addr):
http_channel.__init__(self, server, conn, addr)
requestCloseOnExec(conn)
self.queue=[]
self.working=0
def push(self, producer, send=1):
# this is thread-safe when send is false
# note, that strings are not wrapped in
# producers by default
if self.closed:
return
self.producer_fifo.push(producer)
if send: self.initiate_send()
push_with_producer=push
def work(self):
"try to handle a request"
if not self.working:
if self.queue:
self.working=1
try: module_name, request, response=self.queue.pop(0)
except: return
handle(module_name, request, response)
def close(self):
self.closed=1
while self.queue:
self.queue.pop()
if self.current_request is not None:
self.current_request.channel=None # break circ refs
self.current_request=None
while self.producer_fifo:
p=self.producer_fifo.first()
if p is not None and type(p) != types.StringType:
p.more() # free up resources held by producer
self.producer_fifo.pop()
dispatcher.close(self)
def done(self):
"Called when a publishing request is finished"
self.working=0
self.work()
def kill_zombies(self):
now = int (time.time())
for channel in asyncore.socket_map.values():
if channel.__class__ == self.__class__:
if (now - channel.creation_time) > channel.zombie_timeout:
channel.close()
class zhttp_server(http_server):
"http server"
SERVER_IDENT='Zope/%s ZServer/%s' % (ZOPE_VERSION,ZSERVER_VERSION)
channel_class = zhttp_channel
shutup=0
def __init__ (self, ip, port, resolver=None, logger_object=None):
self.shutup=1
http_server.__init__(self, ip, port, resolver, logger_object)
self.shutup=0
self.log_info('HTTP server started at %s\n'
'\tHostname: %s\n\tPort: %d' % (
time.ctime(time.time()),
self.server_name,
self.server_port
))
def log_info(self, message, type='info'):
if self.shutup: return
dispatcher.log_info(self, message, type)
def create_socket(self, family, type):
dispatcher.create_socket(self, family, type)
requestCloseOnExec(self.socket)
def readable(self):
return self.accepting and \
len(asyncore.socket_map) < CONNECTION_LIMIT
def listen(self, num):
# override asyncore limits for nt's listen queue size
self.accepting = 1
return self.socket.listen (num)