コード例 #1
0
ファイル: server.py プロジェクト: JaapSuter/thor
 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.
     """
     try:
         method, _req_line = top_line.split(None, 1)
         uri, req_version = _req_line.rsplit(None, 1)
         req_version = req_version.rsplit('/', 1)[1]
     except (ValueError, IndexError):
         self.input_error(HttpVersionError(top_line))
         # TODO: more fine-grained
         raise ValueError
     if 'host' not in header_names(hdr_tuples):
         self.input_error(HostRequiredError())
         raise ValueError
     for code in transfer_codes:
         # we only support 'identity' and chunked' codes in requests
         if code not in ['identity', 'chunked']:
             self.input_error(TransferCodeError(code))
             raise ValueError
     exchange = HttpServerExchange(
         self, method, uri, hdr_tuples, req_version
     )
     self.ex_queue.append(exchange)
     self.server.emit('exchange', exchange)
     if not self.output_paused:
         # we only start new requests if we have some output buffer 
         # available. 
         exchange.request_start()
     allows_body = (content_length) or (transfer_codes != [])
     return allows_body
コード例 #2
0
ファイル: server.py プロジェクト: mnot/thor
 def input_start(self, top_line: bytes, hdr_tuples: RawHeaderListType,
                 conn_tokens: List[bytes], transfer_codes: List[bytes],
                 content_length: int) -> Tuple[bool, bool]:
     """
     Take the top set of headers from the input stream, parse them
     and queue the request to be processed by the application.
     """
     if self._idler:
         self._idler.delete()
         self._idler = None
     try:
         method, req_line = top_line.split(None, 1)
         uri, req_version = req_line.rsplit(None, 1)
         req_version = req_version.rsplit(b'/', 1)[1]
     except (ValueError, IndexError):
         self.input_error(HttpVersionError(top_line.decode('utf-8', 'replace')))
         # TODO: more fine-grained
         raise ValueError
     if b'host' not in header_names(hdr_tuples):
         self.input_error(HostRequiredError())
         raise ValueError
     for code in transfer_codes:
         # we only support 'identity' and chunked' codes in requests
         if code not in [b'identity', b'chunked']:
             self.input_error(TransferCodeError(code.decode('utf-8', 'replace')))
             raise ValueError
     exchange = HttpServerExchange(self, method, uri, hdr_tuples, req_version)
     self.ex_queue.append(exchange)
     self.server.emit('exchange', exchange)
     if not self.output_paused:
         # we only start new requests if we have some output buffer
         # available.
         exchange.request_start()
     allows_body = bool(content_length and content_length > 0) or (transfer_codes != [])
     return allows_body, True
コード例 #3
0
ファイル: client.py プロジェクト: nethi/thor
 def _req_start(self):
     """
     Actually queue the request headers for sending.
     """
     self._req_started = True
     req_hdrs = [
         i for i in self.req_hdrs if not i[0].lower() in req_rm_hdrs
     ]
     req_hdrs.append(("Host", self.authority))
     req_hdrs.append(("Connection", "keep-alive"))
     if "content-length" in header_names(req_hdrs):
         delimit = COUNTED
     elif self._req_body:
         req_hdrs.append(("Transfer-Encoding", "chunked"))
         delimit = CHUNKED
     else:
         delimit = NOBODY
     self.output_start("%s %s HTTP/1.1" % (self.method, self.req_target),
         req_hdrs, delimit
     )
コード例 #4
0
 def _req_start(self) -> None:
     """
     Queue the request headers for sending.
     """
     self._req_started = True
     req_hdrs = [i for i in self.req_hdrs if not i[0].lower() in req_rm_hdrs]
     req_hdrs.append((b"Host", self.authority))
     if self.client.idle_timeout > 0:
         req_hdrs.append((b"Connection", b"keep-alive"))
     else:
         req_hdrs.append((b"Connection", b"close"))
     if b"content-length" in header_names(req_hdrs):
         delimit = Delimiters.COUNTED
     elif self._req_body:
         req_hdrs.append((b"Transfer-Encoding", b"chunked"))
         delimit = Delimiters.CHUNKED
     else:
         delimit = Delimiters.NOBODY
     self._input_state = States.WAITING
     self.output_start(b"%s %s HTTP/1.1" % (self.method, self.req_target), req_hdrs, delimit)
コード例 #5
0
ファイル: test_http_utils.py プロジェクト: mnot/thor
 def test_header_names(self):
     hdrs_n = header_names(self.hdrs)
     self.assertEqual(hdrs_n, set(["a", "b", "c", "d"]))
コード例 #6
0
ファイル: test_http_utils.py プロジェクト: JaapSuter/thor
 def test_header_names(self):
     hdrs_n = header_names(hdrs)
     self.assertEqual(hdrs_n, set(['a', 'b', 'c', 'd']))