fix http parser, support Accept: 100-continue, fixes in worker. Update

license headers
This commit is contained in:
Benoit Chesneau 2009-12-27 12:23:55 +01:00
parent cb79c8514d
commit 6cda6a868e
11 changed files with 263 additions and 137 deletions

29
LICENSE
View File

@ -1,14 +1,23 @@
2009 (c) Benoît Chesneau <benoitc@e-engura.org> 2009 (c) Benoît Chesneau <benoitc@e-engura.org>
2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com> 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com>
Permission to use, copy, modify, and distribute this software for any Permission is hereby granted, free of charge, to any person
purpose with or without fee is hereby granted, provided that the above obtaining a copy of this software and associated documentation
copyright notice and this permission notice appear in all copies. files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES The above copyright notice and this permission notice shall be
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF included in all copies or substantial portions of the Software.
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

View File

@ -4,17 +4,26 @@
# 2009 (c) Benoit Chesneau <benoitc@e-engura.com> # 2009 (c) Benoit Chesneau <benoitc@e-engura.com>
# 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com> # 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com>
# #
# Permission to use, copy, modify, and distribute this software for any # Permission is hereby granted, free of charge, to any person
# purpose with or without fee is hereby granted, provided that the above # obtaining a copy of this software and associated documentation
# copyright notice and this permission notice appear in all copies. # files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # The above copyright notice and this permission notice shall be
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # included in all copies or substantial portions of the Software.
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
import optparse as op import optparse as op
import os import os

View File

@ -3,17 +3,26 @@
# 2009 (c) Benoit Chesneau <benoitc@e-engura.com> # 2009 (c) Benoit Chesneau <benoitc@e-engura.com>
# 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com> # 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com>
# #
# Permission to use, copy, modify, and distribute this software for any # Permission is hereby granted, free of charge, to any person
# purpose with or without fee is hereby granted, provided that the above # obtaining a copy of this software and associated documentation
# copyright notice and this permission notice appear in all copies. # files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # The above copyright notice and this permission notice shall be
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # included in all copies or substantial portions of the Software.
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
__version__ = "0.1" __version__ = "0.1"

View File

@ -3,17 +3,26 @@
# 2009 (c) Benoit Chesneau <benoitc@e-engura.com> # 2009 (c) Benoit Chesneau <benoitc@e-engura.com>
# 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com> # 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com>
# #
# Permission to use, copy, modify, and distribute this software for any # Permission is hereby granted, free of charge, to any person
# purpose with or without fee is hereby granted, provided that the above # obtaining a copy of this software and associated documentation
# copyright notice and this permission notice appear in all copies. # files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # The above copyright notice and this permission notice shall be
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # included in all copies or substantial portions of the Software.
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
import errno import errno
import fcntl import fcntl

View File

@ -3,17 +3,26 @@
# 2009 (c) Benoit Chesneau <benoitc@e-engura.com> # 2009 (c) Benoit Chesneau <benoitc@e-engura.com>
# 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com> # 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com>
# #
# Permission to use, copy, modify, and distribute this software for any # Permission is hereby granted, free of charge, to any person
# purpose with or without fee is hereby granted, provided that the above # obtaining a copy of this software and associated documentation
# copyright notice and this permission notice appear in all copies. # files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # The above copyright notice and this permission notice shall be
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # included in all copies or substantial portions of the Software.
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
from request import HTTPRequest from request import HTTPRequest
from response import HTTPResponse from response import HTTPResponse

View File

@ -3,17 +3,26 @@
# 2009 (c) Benoit Chesneau <benoitc@e-engura.com> # 2009 (c) Benoit Chesneau <benoitc@e-engura.com>
# 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com> # 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com>
# #
# Permission to use, copy, modify, and distribute this software for any # Permission is hereby granted, free of charge, to any person
# purpose with or without fee is hereby granted, provided that the above # obtaining a copy of this software and associated documentation
# copyright notice and this permission notice appear in all copies. # files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # The above copyright notice and this permission notice shall be
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # included in all copies or substantial portions of the Software.
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
@ -29,13 +38,25 @@ class IOStream(object):
chunk_size = 4096 chunk_size = 4096
def __init__(self, sock): def __init__(self, sock):
self.sock = sock.dup() self.sock = sock
self.sock.setblocking(0)
self.buf = "" self.buf = ""
def recv(self, buffer_size): def recv(self, buffer_size):
buffer_size = buffer_size or 0
if self.buf:
l = len(self.buf)
if buffer_size > l:
buffer_size -= l
else:
s = self.buf[:buffer_size]
self.buf = self.buf[buffer_size:]
return s
try: try:
return self.sock.recv(buffer_size) data = self.sock.recv(buffer_size)
s = self.buf + data
self.buf = ''
return s
except socket.error, e: except socket.error, e:
if e[0] == EWOULDBLOCK: if e[0] == EWOULDBLOCK:
return None return None

View File

@ -3,17 +3,26 @@
# 2009 (c) Benoit Chesneau <benoitc@e-engura.com> # 2009 (c) Benoit Chesneau <benoitc@e-engura.com>
# 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com> # 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com>
# #
# Permission to use, copy, modify, and distribute this software for any # Permission is hereby granted, free of charge, to any person
# purpose with or without fee is hereby granted, provided that the above # obtaining a copy of this software and associated documentation
# copyright notice and this permission notice appear in all copies. # files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # The above copyright notice and this permission notice shall be
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # included in all copies or substantial portions of the Software.
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
import re import re
import StringIO import StringIO
@ -61,6 +70,10 @@ class HTTPRequest(object):
def read(self): def read(self):
# read headers # read headers
self.read_headers(first_line=True) self.read_headers(first_line=True)
if self.headers.get('ACCEPT', '').lower() == "100-continue":
self.io.send("100 Continue\n")
if "?" in self.path: if "?" in self.path:
path_info, query = self.path.split('?', 1) path_info, query = self.path.split('?', 1)
else: else:
@ -90,7 +103,7 @@ class HTTPRequest(object):
"PATH_INFO": unquote(path_info), "PATH_INFO": unquote(path_info),
"QUERY_STRING": query, "QUERY_STRING": query,
"RAW_URI": self.path, "RAW_URI": self.path,
"CONTENT_TYPE": self.headers.get('content-type', ''), "CONTENT_TYPE": self.headers.get('CONTENT-TYPE', ''),
"CONTENT_LENGTH": length, "CONTENT_LENGTH": length,
"REMOTE_ADDR": self.client_address[0], "REMOTE_ADDR": self.client_address[0],
"REMOTE_PORT": self.client_address[1], "REMOTE_PORT": self.client_address[1],
@ -165,15 +178,13 @@ class HTTPRequest(object):
return data, str(length) or "" return data, str(length) or ""
def start_response(self, status, response_headers): def start_response(self, status, response_headers):
self.response_status = status
self.response_status = int(status.split(" ")[0])
for name, value in response_headers: for name, value in response_headers:
name = _normalize_name(name) name = _normalize_name(name)
if not isinstance(value, basestring):
value = str(value)
self.response_headers[name] = value.strip() self.response_headers[name] = value.strip()
self.start_response_called = True self.start_response_called = True
print "response called"
def write(self, data): def write(self, data):
self.io.write(send) self.io.write(send)
@ -198,27 +209,37 @@ class FileInput(object):
stream_size = 4096 stream_size = 4096
def __init__(self, req): def __init__(self, req):
self.length = req.body_length() self.req = req
self.length = int(req.body_length() or 0)
self.io = req.io self.io = req.io
self._rbuf = "" self._rbuf = ""
self.size = 0
def close(self): def close(self):
self.eof = False self.eof = False
def read(self, amt=None): def read(self, amt=None):
if self._rbuf and not amt is None: if self.length and self.size >= self.length:
return ''
if self._rbuf and amt is not None:
L = len(self._rbuf) L = len(self._rbuf)
print L
if amt > L: if amt > L:
amt -= L amt -= L
else: else:
s = self._rbuf[:amt] s = self._rbuf[:amt]
self._rbuf = self._rbuf[amt:] self._rbuf = self._rbuf[amt:]
self.size += len(s)
return s return s
data = self.io.recv(amt) if amt is None:
amt = min(self. stream_size, self.length or 0)
data = self.req.io.recv(amt)
s = self._rbuf + data s = self._rbuf + data
self._rbuf = '' self._rbuf = ''
print "return %s" % s self.size += len(s)
return s return s
def readline(self, amt=-1): def readline(self, amt=-1):

View File

@ -3,17 +3,26 @@
# 2009 (c) Benoit Chesneau <benoitc@e-engura.com> # 2009 (c) Benoit Chesneau <benoitc@e-engura.com>
# 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com> # 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com>
# #
# Permission to use, copy, modify, and distribute this software for any # Permission is hereby granted, free of charge, to any person
# purpose with or without fee is hereby granted, provided that the above # obtaining a copy of this software and associated documentation
# copyright notice and this permission notice appear in all copies. # files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # The above copyright notice and this permission notice shall be
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # included in all copies or substantial portions of the Software.
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
from gunicorn.util import http_date from gunicorn.util import http_date
@ -41,7 +50,6 @@ class HTTPResponse(object):
resp_head.append("%s: %s\r\n" % (name, value)) resp_head.append("%s: %s\r\n" % (name, value))
self.io.send("%s\r\n" % "".join(resp_head)) self.io.send("%s\r\n" % "".join(resp_head))
for chunk in self.data: for chunk in self.data:
self.write(chunk) self.write(chunk)
self.req.close() self.req.close()

View File

@ -3,17 +3,26 @@
# 2009 (c) Benoit Chesneau <benoitc@e-engura.com> # 2009 (c) Benoit Chesneau <benoitc@e-engura.com>
# 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com> # 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com>
# #
# Permission to use, copy, modify, and distribute this software for any # Permission is hereby granted, free of charge, to any person
# purpose with or without fee is hereby granted, provided that the above # obtaining a copy of this software and associated documentation
# copyright notice and this permission notice appear in all copies. # files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # The above copyright notice and this permission notice shall be
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # included in all copies or substantial portions of the Software.
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
import time import time

View File

@ -3,17 +3,26 @@
# 2009 (c) Benoit Chesneau <benoitc@e-engura.com> # 2009 (c) Benoit Chesneau <benoitc@e-engura.com>
# 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com> # 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com>
# #
# Permission to use, copy, modify, and distribute this software for any # Permission is hereby granted, free of charge, to any person
# purpose with or without fee is hereby granted, provided that the above # obtaining a copy of this software and associated documentation
# copyright notice and this permission notice appear in all copies. # files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # The above copyright notice and this permission notice shall be
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # included in all copies or substantial portions of the Software.
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
import errno import errno
import fcntl import fcntl
@ -37,13 +46,14 @@ class Worker(object):
) )
def __init__(self, workerid, ppid, socket, module): def __init__(self, workerid, ppid, socket, module):
self.alive = True
self.id = workerid self.id = workerid
self.ppid = ppid self.ppid = ppid
self.socket = socket self.socket = socket
self.address = socket.getsockname() self.address = socket.getsockname()
self.tmp = os.tmpfile() self.tmp = os.tmpfile()
self.app = util.import_app(module) self.app = util.import_app(module)
self.alive = self.tmp.fileno()
def init_signals(self): def init_signals(self):
map(lambda s: signal.signal(s, signal.SIG_DFL), self.SIGNALS) map(lambda s: signal.signal(s, signal.SIG_DFL), self.SIGNALS)
@ -61,8 +71,10 @@ class Worker(object):
self.init_signals() self.init_signals()
spinner = 0 spinner = 0
while self.alive: while self.alive:
# Wait for a request to handle. spinner = (spinner+1) % 2
while self.alive: os.fchmod(self.alive, spinner)
while True:
try: try:
ret = select.select([self.socket], [], [], 2.0) ret = select.select([self.socket], [], [], 2.0)
except select.error, e: except select.error, e:
@ -75,27 +87,27 @@ class Worker(object):
# processing clients that more clients are waiting. When # processing clients that more clients are waiting. When
# there's no more clients waiting we go back to the select() # there's no more clients waiting we go back to the select()
# loop and wait for some lovin. # loop and wait for some lovin.
while self.alive: while True:
try: try:
(conn, addr) = self.socket.accept() (conn, addr) = self.socket.accept()
except socket.error, e: except socket.error, e:
if e[0] in [errno.EAGAIN, errno.EINTR]: if e[0] in (errno.EAGAIN, errno.EINTR,
errno.ECONNABORTED):
break # Jump back to select break # Jump back to select
raise # Uh oh! raise # Uh oh!
conn.setblocking(0) conn.setblocking(1)
try: try:
self.handle(conn, addr) self.handle(conn, addr)
except: except Exception, e:
log.exception("Error processing request.") log.exception("Error processing request. [%s]" % str(e))
# Update the fd mtime on each client completion # Update the fd mtime on each client completion
# to signal that this worker process is alive. # to signal that this worker process is alive.
spinner = (spinner+1) % 2 spinner = (spinner+1) % 2
os.fchmod(self.tmp.fileno(), spinner) os.fchmod(self.alive, spinner)
def handle(self, conn, client): def handle(self, conn, client):
fcntl.fcntl(conn.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)
req = http.HTTPRequest(conn, client, self.address) req = http.HTTPRequest(conn, client, self.address)
result = self.app(req.read(), req.start_response) result = self.app(req.read(), req.start_response)
response = http.HTTPResponse(req, result) response = http.HTTPResponse(req, result)

View File

@ -1,19 +1,29 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 - # -*- coding: utf-8 -
# #
# Copyright (c) 2009 Benoit Chesneau <benoitc@e-engura.com> # 2009 (c) Benoit Chesneau <benoitc@e-engura.com>
# 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com>
# #
# Permission to use, copy, modify, and distribute this software for any # Permission is hereby granted, free of charge, to any person
# purpose with or without fee is hereby granted, provided that the above # obtaining a copy of this software and associated documentation
# copyright notice and this permission notice appear in all copies. # files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # The above copyright notice and this permission notice shall be
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # included in all copies or substantial portions of the Software.
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
import os import os