'Implements `.HTTPConnection.finish`.'

def finish(self):

    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('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
    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)
