示例#1
0
文件: serve.py 项目: dhain/webskewer
def handle_connection(sock, application, ssl=False):
    remote_addr, remote_port = sock.getpeername()[:2]
    local_addr, local_port = sock.getsockname()[:2]
    server_env = {
        'REMOTE_ADDR': remote_addr,
        'REMOTE_PORT': remote_port,
        'SERVER_ADDR': local_addr,
        'SERVER_PORT': local_port,
        'SERVER_NAME': 'localhost',
        'SCRIPT_NAME': '',
        'wsgi.version': (1,0),
        'wsgi.url_scheme': 'http' if not ssl else 'https',
        'wsgi.errors': sys.stderr,
        'wsgi.multithread': False,
        'wsgi.multiprocess': False,
        'wsgi.run_once': False,
    }
    if ssl:
        server_env['HTTPS'] = 'on'
    with closing(sock):
        try:
            for environ in recv_requests(sock):
                environ.update(server_env)
                handler = RequestHandler(sock, application, environ)
                try:
                    if handler.handle():
                        break
                except Exception:
                    print >> sys.stderr, log_exc(remote_addr)
                    break
                connection = environ.get('HTTP_CONNECTION', '').lower()
                if environ['webskewer.http_version'] < (1,1):
                    break
                if connection == 'close':
                    break
        except exceptions.Error, err:
            environ = {
                'wsgi.input': DummyFile(),
                'webskewer.http_version': (1,1),
                'webskewer.bad_request': True,
            }
            environ.update(server_env)
            handler = RequestHandler(sock, BadRequest(), environ)
            handler.handle()
            print >> sys.stderr, log_err(repr(err), remote_addr)
        except greennet.ConnectionLost:
            pass
示例#2
0
文件: serve.py 项目: dhain/webskewer
 def handle(self):
     resp = None
     try:
         resp = self.application(self.environ, self.start_response)
         for data in resp:
             if data:
                 self.write(data)
         if not self.headers_sent:
             self._write_headers()
     except Exception:
         print >> sys.stderr, log_exc(self.environ['REMOTE_ADDR'])
         if self.headers_sent:
             return True # signal caller to close connection
         self.application = ServerError()
         return self.handle()
     finally:
         if hasattr(resp, 'close'):
             resp.close()
     if self.chunked:
         greennet.sendall(self.sock, '0\r\n\r\n')
     if not 'webskewer.bad_request' in self.environ:
         print log_req(self.environ, self.status, self.headers, self.bytes_sent)
     for _ in self.environ['wsgi.input']:
         pass
示例#3
0
文件: serve.py 项目: dhain/webskewer
                if connection == 'close':
                    break
        except exceptions.Error, err:
            environ = {
                'wsgi.input': DummyFile(),
                'webskewer.http_version': (1,1),
                'webskewer.bad_request': True,
            }
            environ.update(server_env)
            handler = RequestHandler(sock, BadRequest(), environ)
            handler.handle()
            print >> sys.stderr, log_err(repr(err), remote_addr)
        except greennet.ConnectionLost:
            pass
        except Exception:
            print >> sys.stderr, log_exc(remote_addr)


def accept_connections(sock, application):
    with closing(sock):
        while True:
            client, addr = greennet.accept(sock)
            greennet.schedule(greenlet(handle_connection), client, application)


try:
    import greennet.ssl
except ImportError:
    pass
else:
    def accept_ssl(sock, application, cert, verify=None, timeout=None):