diff --git a/gunicorn/arbiter.py b/gunicorn/arbiter.py index bce46f2a..2ad74c80 100644 --- a/gunicorn/arbiter.py +++ b/gunicorn/arbiter.py @@ -139,7 +139,6 @@ class Arbiter(object): self.manage_workers() while True: try: - sig = self.SIG_QUEUE.pop(0) if len(self.SIG_QUEUE) else None if sig is None: self.sleep() diff --git a/gunicorn/http/http_parser.py b/gunicorn/http/http_parser.py index c04bffa4..4043cf07 100644 --- a/gunicorn/http/http_parser.py +++ b/gunicorn/http/http_parser.py @@ -148,10 +148,10 @@ class HttpParser(object): if chunk_size <= 0: self._chunk_eof = True # we put data - return None, data[:end_offset] + return '', data[:end_offset] self.chunk_size = 0 return buf[chunk_size:], data[:end_offset] - return None, data + return '', data def trailing_header(self, data): i = data.find("\r\n\r\n") @@ -164,17 +164,17 @@ class HttpParser(object): """ dlen = len(data) - chunk = None + chunk = '' if self.is_chunked: chunk, data = self.read_chunk(data) if not chunk: - return None, data + return '', data else: if self._content_len > 0: nr = min(dlen, self._content_len) chunk = data[:nr] self._content_len -= nr - data = None + data = '' self.start_offset = 0 - return (chunk, data) \ No newline at end of file + return (chunk, data) diff --git a/gunicorn/http/request.py b/gunicorn/http/request.py index 7f88a4c0..4a13d3e9 100644 --- a/gunicorn/http/request.py +++ b/gunicorn/http/request.py @@ -54,6 +54,19 @@ class RequestError(Exception): class HTTPRequest(object): SERVER_VERSION = "gunicorn/%s" % __version__ + + DEFAULTS = { + "wsgi.url_scheme": 'http', + "wsgi.input": StringIO.StringIO(), + "wsgi.errors": sys.stderr, + "wsgi.version": (1, 0), + "wsgi.multithread": False, + "wsgi.multiprocess": True, + "wsgi.run_once": False, + "SCRIPT_NAME": "", + "SERVER_SOFTWARE": "gunicorn/%s" % __version__ + } + def __init__(self, socket, client_address, server_address): self.socket = socket @@ -68,6 +81,7 @@ class HTTPRequest(object): self.log = logging.getLogger(__name__) def read(self): + environ = {} headers = {} remain = CHUNK_SIZE buf = "" @@ -82,7 +96,8 @@ class HTTPRequest(object): if i != -1: break if not headers: - return {} + environ.update(self.DEFAULTS) + return environ buf = buf[i:] @@ -139,4 +154,4 @@ class HTTPRequest(object): if not isinstance(value, basestring): value = str(value) self.response_headers[name] = value.strip() - self.start_response_called = True \ No newline at end of file + self.start_response_called = True diff --git a/gunicorn/http/response.py b/gunicorn/http/response.py index 8464599d..50c943a4 100644 --- a/gunicorn/http/response.py +++ b/gunicorn/http/response.py @@ -58,11 +58,12 @@ class HTTPResponse(object): write(self.sock, "%s\r\n" % "".join(resp_head)) - for chunk in self.data: + + + for chunk in list(self.data): write(self.sock, chunk) - close(self.sock) if hasattr(self.data, "close"): - self.data.close() \ No newline at end of file + self.data.close() diff --git a/gunicorn/http/tee.py b/gunicorn/http/tee.py index 40aacb63..e3e72817 100644 --- a/gunicorn/http/tee.py +++ b/gunicorn/http/tee.py @@ -35,7 +35,7 @@ import os import StringIO import tempfile -from gunicorn.util import MAX_BODY, CHUNK_SIZE +from gunicorn.util import MAX_BODY, CHUNK_SIZE, read_partial class TeeInput(object): @@ -51,7 +51,6 @@ class TeeInput(object): if len(buf) > 0: chunk, self.buf = parser.filter_body(buf) - print chunk if chunk: self.tmp.write(chunk) self.tmp.seek(0) @@ -61,7 +60,7 @@ class TeeInput(object): def len(self): if self._len: return self._len if self.socket: - pos = self.tmp.tell() + pos = self.tmp.tell() while True: if not self._tee(CHUNK_SIZE): break @@ -79,10 +78,12 @@ class TeeInput(object): if length is None: r = self.tmp.read() or "" + print "avant %s" % str(len(r)) while True: chunk = self._tee(CHUNK_SIZE) if not chunk: break r += chunk + print "apres %s" % str(len(r)) return r else: diff = self._tmp_size() - self.tmp.tell() @@ -139,7 +140,9 @@ class TeeInput(object): data = read_partial(self.socket, length) self.buf += data chunk, self.buf = self.parser.filter_body(self.buf) + print self.buf if chunk: + print chunk self.tmp.write(chunk) self.tmp.seek(0, os.SEEK_END) return chunk @@ -165,9 +168,9 @@ class TeeInput(object): else: return int(os.fstat(self.tmp.fileno())[6]) - def _ensure_length(buf, length): + def _ensure_length(self, buf, length): if not buf or not self._len: return buf - while len(buf) < length and self.len != self.tmp.pos(): + while len(buf) < length and self.len != self.tmp.tell(): buf += self._tee(length - len(buf)) - return buf \ No newline at end of file + return buf diff --git a/gunicorn/util.py b/gunicorn/util.py index bd8c5250..2572b475 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -70,16 +70,19 @@ def read_partial(sock, length): def write(sock, data): buf = "" buf += data + dlen = len(data) while buf: try: bytes = sock.send(buf) - buf = buf[bytes:] - return bytes + if bytes < dlen: + buf = buf[bytes:] + continue + return dlen except socket.error, e: if e[0] in (errno.EWOULDBLOCK, errno.EAGAIN): - break + raise elif e[0] in (errno.EPIPE,): - continue + break raise def write_nonblock(sock, data): diff --git a/gunicorn/worker.py b/gunicorn/worker.py index 367d29c9..a0e533ed 100644 --- a/gunicorn/worker.py +++ b/gunicorn/worker.py @@ -116,7 +116,7 @@ class Worker(object): while self.alive: try: client, addr = self.socket.accept() - + # handle connection self.handle(client, addr) @@ -139,7 +139,6 @@ class Worker(object): except Exception, e: self.log.exception("Error processing request. [%s]" % str(e)) msg = "HTTP/1.0 500 Internal Server Error\r\n\r\n" - #util.write_nonblock(client, msg) util.close(client) del client