Exemple #1
0
 def finish(self):
     """Implements `.HTTPConnection.finish`."""
     if (self._expected_content_remaining is not None
             and self._expected_content_remaining != 0
             and not self.stream.closed()):
         self.stream.close()
         raise httputil.HTTPOutputError(
             "Tried to write %d bytes less than Content-Length" %
             self._expected_content_remaining)
     if self._chunking_output:
         if not self.stream.closed():
             self._pending_write = self.stream.write(b"0\r\n\r\n")
             self._pending_write.add_done_callback(self._on_write_complete)
     self._write_finished = True
     # If the app finished the request while we're still reading,
     # divert any remaining data away from the delegate and
     # close the connection when we're done sending our response.
     # Closing the connection is the only way to avoid reading the
     # whole input body.
     if not self._read_finished:
         self._disconnect_on_finish = True
     # No more data is coming, so instruct TCP to send any remaining
     # data immediately instead of waiting for a full packet or ack.
     self.stream.set_nodelay(True)
     if self._pending_write is None:
         self._finish_request(None)
     else:
         self._pending_write.add_done_callback(self._finish_request)
 def finish(self):
     """Implements `.HTTPConnection.finish`."""
     if (self._expected_content_remaining is not None and
             self._expected_content_remaining != 0 and
             not self.stream.closed()):
         self.stream.close()
         raise httputil.HTTPOutputError(
             "Tried to write %d bytes less than Content-Length" %
             self._expected_content_remaining)
     if self._chunking_output:
         if not self.stream.closed():
             # `Transfer-Encoding:chunked`: The terminating chunk is a
             # regular chunk, with the exception that its length is zero.
             self._pending_write = self.stream.write(b"0\r\n\r\n")
             self._pending_write.add_done_callback(self._on_write_complete)
     self._write_finished = True
     # If the app finished the request while we're still reading,
     # divert any remaining data away from the delegate and
     # close the connection when we're done sending our response.
     # Closing the connection is the only way to avoid reading the
     # whole input body.
     if not self._read_finished:
         self._disconnect_on_finish = True
     # No more data is coming, so instruct TCP to send any remaining
     # data immediately instead of waiting for a full packet or ack.
     # 关闭 Nagle 算法,效果相当于让 socket 立即 flush 数据到客户端,随后将在
     # `_finish_request` 中恢复 Nagle 算法。
     self.stream.set_nodelay(True)
     if self._pending_write is None:
         self._finish_request(None)
     else:
         # 最后一次挂起的写操作完成后回调 `_finish_request` 方法。
         self._pending_write.add_done_callback(self._finish_request)
Exemple #3
0
 def finish(self):
     if (self._expected_content_remaining is not None
             and self._expected_content_remaining != 0):
         self._error = httputil.HTTPOutputError(
             "Tried to write %d bytes less than Content-Length" %
             self._expected_content_remaining)
         raise self._error
     self._finished = True
Exemple #4
0
 def write(self, chunk, callback=None):
     if self._expected_content_remaining is not None:
         self._expected_content_remaining -= len(chunk)
         if self._expected_content_remaining < 0:
             self._error = httputil.HTTPOutputError(
                 "Tried to write more data than Content-Length")
             raise self._error
     self._write_buffer.append(chunk)
     if callback is not None:
         callback()
     return _dummy_future
Exemple #5
0
 def _format_chunk(self, chunk):
     if self._expected_content_remaining is not None:
         self._expected_content_remaining -= len(chunk)
         if self._expected_content_remaining < 0:
             # Close the stream now to stop further framing errors.
             self.stream.close()
             raise httputil.HTTPOutputError(
                 "Tried to write more data than Content-Length")
     if self._chunking_output and chunk:
         # Don't write out empty chunks because that means END-OF-STREAM
         # with chunked encoding
         return utf8("%x" % len(chunk)) + b"\r\n" + chunk + b"\r\n"
     else:
         return chunk
Exemple #6
0
 def finish(self):
     """Implements `.HTTPConnection.finish`."""
     if (self._expected_content_remaining is not None
             and self._expected_content_remaining != 0
             and not self.stream.closed()):
         self.stream.close()
         raise httputil.HTTPOutputError(
             "Tried to write %d bytes less than Content-Length" %
             self._expected_content_remaining)
     if self._chunking_output:
         if not self.stream.closed():
             self._pending_write = self.stream.write(b"0\r\n\r\n")
             self._pending_write.add_done_callback(self._on_write_complete)
     self._write_finished = True
     if not self._read_finished:
         self._disconnect_on_finish = True
     # No more data is coming, so instruct TCP to send any remaining
     # data immediately instead of waiting for a full packet or ack.
     self.stream.set_nodelay(True)
     if self._pending_write is None:
         self._finish_request(None)
     else:
         self._pending_write.add_done_callback(self._finish_request)