Exemplo n.º 1
0
 def _handle_error(self, err, detail=None):
     """
     Handle a problem with the request by generating an appropriate
     response.
     """
     assert self._input_state == WAITING
     if self._read_timeout_ev:
         self._read_timeout_ev.delete()
     if self._tcp_conn:
         self._tcp_conn.close()
         self._tcp_conn = None
     if detail:
         err['detail'] = detail
     status_code, status_phrase = err.get('status',
                                          ('504', 'Gateway Timeout'))
     hdrs = [
         ('Content-Type', 'text/plain'),
         ('Connection', 'close'),
     ]
     body = err['desc']
     if err.has_key('detail'):
         body += " (%s)" % err['detail']
     res_body_cb, res_done_cb = self.res_start_cb("1.1", status_code,
                                                  status_phrase, hdrs,
                                                  dummy)
     res_body_cb(str(body))
     push_tcp.schedule(0, res_done_cb, err)
Exemplo n.º 2
0
 def _handle_error(self, err, detail=None):
     """
     Handle a problem with the request by generating an appropriate
     response.
     """
     assert self._input_state == WAITING
     if self._read_timeout_ev:
         self._read_timeout_ev.delete()
     if self._tcp_conn:
         self._tcp_conn.close()
         self._tcp_conn = None
     if detail:
         err['detail'] = detail
     status_code, status_phrase = err.get('status', 
         ('504', 'Gateway Timeout')
     )
     hdrs = [
         ('Content-Type', 'text/plain'),
         ('Connection', 'close'),
     ]
     body = err['desc']
     if err.has_key('detail'):
         body += " (%s)" % err['detail']
     res_body_cb, res_done_cb = self.res_start_cb(
           "1.1", status_code, status_phrase, hdrs, dummy)
     res_body_cb(str(body))
     push_tcp.schedule(0, res_done_cb, err)
Exemplo n.º 3
0
 def _input_start(self, top_line, hdr_tuples, conn_tokens, transfer_codes, content_length):
     """
     Take the top set of headers from the input stream, parse them
     and queue the request to be processed by the application.
     """
     if self.read_timeout:
         self._timeout_ev.delete()
     try: 
         res_version, status_txt = top_line.split(None, 1)
         res_version = float(res_version.rsplit('/', 1)[1])
         # TODO: check that the protocol is HTTP
     except (ValueError, IndexError):
         self._handle_error(ERR_HTTP_VERSION, top_line)
         raise ValueError
     try:
         res_code, res_phrase = status_txt.split(None, 1)
     except ValueError:
         res_code = status_txt.rstrip()
         res_phrase = ""
     if 'close' not in conn_tokens:
         if (res_version == 1.0 and 'keep-alive' in conn_tokens) or \
             res_version > 1.0:
             self._conn_reusable = True
     if self.read_timeout:
         self._timeout_ev = push_tcp.schedule(
              self.read_timeout, self._input_error, ERR_READ_TIMEOUT, 'start')
     self.res_body_cb, self.res_done_cb = self.res_start_cb(
         res_version, res_code, res_phrase, hdr_tuples, self.res_body_pause)
     allows_body = (res_code not in no_body_status) or (self.method == "HEAD")
     return allows_body 
Exemplo n.º 4
0
 def _input_start(self, top_line, hdr_tuples, conn_tokens, transfer_codes, content_length):
     """
     Take the top set of headers from the input stream, parse them
     and queue the request to be processed by the application.
     """
     if self.read_timeout:
         self._timeout_ev.delete()
     try: 
         res_version, status_txt = top_line.split(None, 1)
         res_version = float(res_version.rsplit('/', 1)[1])
         # TODO: check that the protocol is HTTP
     except (ValueError, IndexError):
         self._handle_error(ERR_HTTP_VERSION, top_line)
         raise ValueError
     try:
         res_code, res_phrase = status_txt.split(None, 1)
     except ValueError:
         res_code = status_txt.rstrip()
         res_phrase = ""
     if 'close' not in conn_tokens:
         if (res_version == 1.0 and 'keep-alive' in conn_tokens) or \
             res_version > 1.0:
             self._conn_reusable = True
     if self.read_timeout:
         self._timeout_ev = push_tcp.schedule(
              self.read_timeout, self._input_error, ERR_READ_TIMEOUT, 'start')
     self.res_body_cb, self.res_done_cb = self.res_start_cb(
         res_version, res_code, res_phrase, hdr_tuples, self.res_body_pause)
     allows_body = (res_code not in no_body_status) or (self.method == "HEAD")
     return allows_body 
Exemplo n.º 5
0
 def _input_body(self, chunk):
     "Process a response body chunk from the wire."
     if self.read_timeout:
         self._timeout_ev.delete()
     self.res_body_cb(chunk)
     if self.read_timeout:
         self._timeout_ev = push_tcp.schedule(
              self.read_timeout, self._input_error, ERR_READ_TIMEOUT, 'body')
Exemplo n.º 6
0
 def _handle_connect(self, tcp_conn):
     "The connection has succeeded."
     self._tcp_conn = tcp_conn
     self._output("") # kick the output buffer
     if self.read_timeout:
         self._timeout_ev = push_tcp.schedule(
             self.read_timeout, self._handle_error, ERR_READ_TIMEOUT, 'connect')
     return self._handle_input, self._conn_closed, self._req_body_pause
Exemplo n.º 7
0
 def _input_body(self, chunk):
     "Process a response body chunk from the wire."
     if self.read_timeout:
         self._timeout_ev.delete()
     self.res_body_cb(chunk)
     if self.read_timeout:
         self._timeout_ev = push_tcp.schedule(
              self.read_timeout, self._input_error, ERR_READ_TIMEOUT, 'body')
Exemplo n.º 8
0
 def _handle_connect(self, tcp_conn):
     "The connection has succeeded."
     self._tcp_conn = tcp_conn
     self._output("") # kick the output buffer
     if self.read_timeout:
         self._timeout_ev = push_tcp.schedule(
             self.read_timeout, self._handle_error, ERR_READ_TIMEOUT, 'connect')
     return self._handle_input, self._conn_closed, self._req_body_pause
Exemplo n.º 9
0
 def _schedule_write(self):
     # We only need one write scheduled at a time.
     if not self.write_pending:
         push_tcp.schedule(0, self._write_frame_callback)
         self.write_pending = True
Exemplo n.º 10
0
 def _schedule_write(self):
     # We only need one write scheduled at a time.
     if not self.write_pending:
         push_tcp.schedule(0, self._write_frame_callback)
         self.write_pending = True