def _serialize_headers( self, env, connection): keepalive = env.get('keepalive', None) response_headers =\ [ ( '-'.join([x.capitalize() for x in key.split('-')]), value ) for key, value in env[ env['mode'] ]['header'] ] response_headers_list = [x[0] for x in response_headers ] if 'Date' not in response_headers_list: response_headers.append(('Date', format_date_time(time.time()))) elif keepalive is False or env['request_version'] == 'HTTP/1.0': if 'Connection' not in response_headers_list: response_headers.append(('Connection', 'close')) keepalive = False elif ('Connection', 'close') in response_headers: keepalive = False if keepalive and 'Connection' not in response_headers_list: response_headers.append(('Connection','Keep-Alive')) status = env['status'] if isinstance( status, int ): code = '' elif ' ' in status: status, code = status.split(' ',1) status = int(status) else: code, status = '', int( status ) if env['status'] not in [204, 304]: # the reply will include message-body; make sure we have either Content-Length or chunked if 'Content-Length' not in response_headers_list: if env['request_version'] != 'HTTP/1.0': response_headers.append(('Transfer-Encoding', 'chunked')) if not 'Content-Type' in response_headers_list: response_headers.append(('Content-Type', 'text/html; charset=UTF-8') ) towrite = [ '%s %s %s\r\n' % (env['request_version'], status, code ) ] for header in response_headers: towrite.append('%s: %s\r\n' % header) if keepalive is not None: env['keepalive'] = keepalive return ''.join(towrite)
def _serialize_headers(self, env, connection): keepalive = env.get('keepalive', None) response_headers = \ [( '-'.join([x.capitalize() for x in key.split('-')]), value ) for key, value in env[env['mode']]['header']] response_headers_list = [x[0] for x in response_headers] if 'Date' not in response_headers_list: response_headers.append(('Date', format_date_time(time.time()))) elif keepalive is False or env['request_version'] == 'HTTP/1.0': if 'Connection' not in response_headers_list: response_headers.append(('Connection', 'close')) keepalive = False elif ('Connection', 'close') in response_headers: keepalive = False status = env['status'] if isinstance(status, int): code = '' elif ' ' in status: status, code = status.split(' ', 1) status = int(status) else: code, status = '', int(status) if env['status'] not in [204, 304]: # the reply will include message-body; make sure we have either Content-Length or chunked if 'Content-Length' not in response_headers_list: if env['request_version'] != 'HTTP/1.0': response_headers.append(('Transfer-Encoding', 'chunked')) if not 'Content-Type' in response_headers_list: response_headers.append( ('Content-Type', 'text/html; charset=UTF-8')) towrite = ['%s %s %s\r\n' % (env['request_version'], status, code)] for header in response_headers: towrite.append('%s: %s\r\n' % header) if keepalive is not None: env['keepalive'] = keepalive return ''.join(towrite)
def __call__(self, environ, start_response): # WebsocketHixie76 handshake (test_haproxy) if environ['wsgi.websocket_version'] == 'hixie-76': part1, part2, socket = environ['wsgi.hixie-keys'] towrite = [ 'HTTP/1.1 %s\r\n'%self.status, 'Date: %s\r\n'%format_date_time(time.time())] for header in self._abs_headerlist(environ): towrite.append("%s: %s\r\n" % header) towrite.append("\r\n") socket.sendall(''.join(towrite)) key3 = environ['wsgi.input'].read(8) if not key3: key3 = environ['wsgi.input'].rfile.read(8) socket.sendall( md5(struct.pack("!II", part1, part2) + key3).digest()) else: write = start_response( self.status, self._abs_headerlist(environ)) write(self.body) try: self.session.acquire(self.request) except: # should use specific exception self.websocket.send('o') self.websocket.send( close_frame(2010, "Another connection still open", '\n')) self.websocket.close() return StopStreaming() if self.open(): gevent.joinall((gevent.spawn(self.send), gevent.spawn(self.receive))) raise StopStreaming()
def __call__(self, environ, start_response): # WebsocketHixie76 handshake (test_haproxy) if environ['wsgi.websocket_version'] == 'hixie-76': part1, part2, socket = environ['wsgi.hixie-keys'] towrite = [ 'HTTP/1.1 %s\r\n' % self.status, 'Date: %s\r\n' % format_date_time(time.time()) ] for header in self._abs_headerlist(environ): towrite.append("%s: %s\r\n" % header) towrite.append("\r\n") socket.sendall(''.join(towrite)) key3 = environ['wsgi.input'].read(8) if not key3: key3 = environ['wsgi.input'].rfile.read(8) socket.sendall( md5(struct.pack("!II", part1, part2) + key3).digest()) else: write = start_response(self.status, self._abs_headerlist(environ)) write(self.body) try: self.session.acquire(self.request) except: # should use specific exception self.websocket.send('o') self.websocket.send( close_frame(2010, "Another connection still open", '\n')) self.websocket.close() return StopStreaming() if self.open(): gevent.joinall( (gevent.spawn(self.send), gevent.spawn(self.receive))) raise StopStreaming()