From 93097b74e6838faa403893aebc2ca0a34490baef Mon Sep 17 00:00:00 2001 From: Benoit Chesneau Date: Wed, 2 Dec 2009 19:43:56 +0100 Subject: [PATCH] return response now --- gunicorn/httprequest.py | 18 +++++++++++--- gunicorn/httpserver.py | 9 +++++-- gunicorn/socketserver.py | 54 ---------------------------------------- test.py | 10 +++++++- 4 files changed, 30 insertions(+), 61 deletions(-) delete mode 100644 gunicorn/socketserver.py diff --git a/gunicorn/httprequest.py b/gunicorn/httprequest.py index c6c8cca5..166d65be 100644 --- a/gunicorn/httprequest.py +++ b/gunicorn/httprequest.py @@ -41,6 +41,9 @@ class HTTPRequest(object): self.method = None self.path = None self.headers = {} + self.response_status = None + self.response_headers = {} + self._version = 11 self.fp = socket.makefile("rw", self.CHUNK_SIZE) @@ -93,8 +96,6 @@ class HTTPRequest(object): key = 'HTTP_' + key.replace('-', '_') if key not in ('HTTP_CONTENT_TYPE', 'HTTP_CONTENT_LENGTH'): environ[key] = value - - print environ return environ def read_headers(self): @@ -152,6 +153,16 @@ class HTTPRequest(object): data.seek(0) return data, str(length) or "" + def start_response(self, status, response_headers): + resp_head = [] + self.response_status = status + self.response_headers = {} + resp_head.append("%s %s" % (self.version, status)) + for name, value in response_headers: + resp_head.append("%s: %s" % (name, value)) + self.response_headers[name.lower()] = value + self.fp.write("%s\r\n\r\n" % "\r\n".join(resp_head)) + def write(self, data): self.fp.write(data) @@ -161,7 +172,7 @@ class HTTPRequest(object): def first_line(self, line): method, path, version = line.split(" ") - self.version = version + self.version = version.strip() self.method = method.upper() self.path = path @@ -202,7 +213,6 @@ class FileInput(object): self.close() return s - def readline(self, size=None): if self.fp is None or self.eof: return '' diff --git a/gunicorn/httpserver.py b/gunicorn/httpserver.py index b4a5b2f0..6cc1f0dd 100644 --- a/gunicorn/httpserver.py +++ b/gunicorn/httpserver.py @@ -25,6 +25,7 @@ import sys import time from gunicorn.httprequest import HTTPRequest +from gunicorn.httpresponse import HTTPResponse from gunicorn import socketserver from gunicorn.util import NullHandler @@ -149,8 +150,12 @@ class HTTPServer(object): def process_client(self, listener, conn, addr): """ do nothing just echo message""" req = HTTPRequest(conn, addr, listener.getsockname()) - environ = req.read() - req.write(str(environ)) + try: + result = self.app(req.read(), req.start_response) + response = HTTPResponse(req, result) + response.send() + except Exception, e: + print >>sys.stderr, str(e) req.close() def worker_loop(self, worker_pid, worker): diff --git a/gunicorn/socketserver.py b/gunicorn/socketserver.py deleted file mode 100644 index 7c0f955c..00000000 --- a/gunicorn/socketserver.py +++ /dev/null @@ -1,54 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2008,2009 Benoit Chesneau -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at# -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import socket - -class Socket(socket.socket): - def accept_nonblock(self): - self.setblocking(0) - return self.accept() - - -class TCPServer(Socket): - """class for server-side TCP sockets. - This is wrapper around socket.socket class""" - - def __init__(self, address, **opts): - self.address = address - self.backlog = opts.get('backlog', 1024) - self.timeout = opts.get('timeout', 300) - self.reuseaddr = opts.get('reuseaddr', True) - self.nodelay = opts.get('nodelay', True) - self.recbuf = opts.get('recbuf', 8192) - - socket.socket.__init__(self, socket.AF_INET, socket.SOCK_STREAM) - - if self.reuseaddr: - self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - - if self.nodelay: - self.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) - - if self.recbuf: - self.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, - self.recbuf) - - self.settimeout(self.timeout) - self.bind(address) - self.listen() - - def listen(self): - super(TCPServer, self).listen(self.backlog) \ No newline at end of file diff --git a/test.py b/test.py index 7ab67bcb..492b1367 100644 --- a/test.py +++ b/test.py @@ -1,5 +1,13 @@ from gunicorn.httpserver import HTTPServer + +def simple_app(environ, start_response): + """Simplest possible application object""" + status = '200 OK' + response_headers = [('Content-type','text/plain')] + start_response(status, response_headers) + return ['Hello world!\n'] + if __name__ == '__main__': - server = HTTPServer(None, 4) + server = HTTPServer(simple_app, 4) server.join() \ No newline at end of file