def prepareResponseHeaders(self): version = self.version # Figure out whether the connection should be closed. connection = self.request_data.headers.get('CONNECTION', '').lower() close_it = 0 response_headers = self.response_headers accumulated_headers = self.accumulated_headers if accumulated_headers is None: accumulated_headers = [] if version == '1.0': if connection == 'keep-alive': if 'Content-Length' not in response_headers: close_it = 1 else: response_headers['Connection'] = 'Keep-Alive' else: close_it = 1 elif version == '1.1': if 'connection: close' in (header.lower() for header in accumulated_headers): close_it = 1 if connection == 'close': close_it = 1 elif 'Transfer-Encoding' in response_headers: if response_headers['Transfer-Encoding'] != 'chunked': close_it = 1 elif self.status == '304': # Replying with headers only. pass elif 'Content-Length' not in response_headers: # accumulated_headers is a simple list, we need to cut off # the value of content-length manually if 'content-length' not in (header[:14].lower() for header in accumulated_headers): close_it = 1 # under HTTP 1.1 keep-alive is default, no need to set the header else: # Close if unrecognized HTTP version. close_it = 1 self.close_on_finish = close_it if close_it: self.response_headers['Connection'] = 'close' # Set the Server and Date field, if not yet specified. This is needed # if the server is used as a proxy. if 'server' not in (header[:6].lower() for header in accumulated_headers): self.response_headers['Server'] = self.channel.server.SERVER_IDENT else: self.response_headers['Via'] = self.channel.server.SERVER_IDENT if 'date' not in (header[:4].lower() for header in accumulated_headers): self.response_headers['Date'] = build_http_date(self.start_time)
def prepareResponseHeaders(self): version = self.version # Figure out whether the connection should be closed. connection = self.request_data.headers.get('CONNECTION', '').lower() close_it = 0 response_headers = self.response_headers accumulated_headers = self.accumulated_headers if accumulated_headers is None: accumulated_headers = [] if version == '1.0': if connection == 'keep-alive': if not ('Content-Length' in response_headers): close_it = 1 else: response_headers['Connection'] = 'Keep-Alive' else: close_it = 1 elif version == '1.1': if 'connection: close' in (header.lower() for header in accumulated_headers): close_it = 1 if connection == 'close': close_it = 1 elif 'Transfer-Encoding' in response_headers: if not response_headers['Transfer-Encoding'] == 'chunked': close_it = 1 elif self.status == '304': # Replying with headers only. pass elif not ('Content-Length' in response_headers): # accumulated_headers is a simple list, we need to cut off # the value of content-length manually if 'content-length' not in (header[:14].lower() for header in accumulated_headers): close_it = 1 # under HTTP 1.1 keep-alive is default, no need to set the header else: # Close if unrecognized HTTP version. close_it = 1 self.close_on_finish = close_it if close_it: self.response_headers['Connection'] = 'close' # Set the Server and Date field, if not yet specified. This is needed # if the server is used as a proxy. if 'server' not in (header[:6].lower() for header in accumulated_headers): self.response_headers['Server'] = self.channel.server.SERVER_IDENT else: self.response_headers['Via'] = self.channel.server.SERVER_IDENT if 'date' not in (header[:4].lower() for header in accumulated_headers): self.response_headers['Date'] = build_http_date(self.start_time)
def start(self): now = time.time() self.start_time = now self.response_headers['Date'] = build_http_date (now)
def testDateRoundTrip(self): from time import time t = int(time()) self.assertEquals(t, parse_http_date(build_http_date(t)))
def testDateRoundTrip(self): from time import time t = int(time()) self.assertEqual( t, http_date.parse_http_date(http_date.build_http_date(t)))