private void connected(ClientConnection conn) { conn.setCurrentRequest(this); this.conn = conn; this.metric = client .httpClientMetrics() .requestBegin(conn.metric(), conn.localAddress(), conn.remoteAddress(), this); // If anything was written or the request ended before we got the connection, then // we need to write it now if (pendingMaxSize != -1) { conn.doSetWriteQueueMaxSize(pendingMaxSize); } if (pendingChunks != null) { ByteBuf pending = pendingChunks; pendingChunks = null; if (completed) { // we also need to write the head so optimize this and write all out in once writeHeadWithContent(pending, true); conn.reportBytesWritten(written); if (respHandler != null) { conn.endRequest(); } } else { writeHeadWithContent(pending, false); } } else { if (completed) { // we also need to write the head so optimize this and write all out in once writeHeadWithContent(Unpooled.EMPTY_BUFFER, true); conn.reportBytesWritten(written); if (respHandler != null) { conn.endRequest(); } } else { if (writeHead) { writeHead(); } } } }
private void write(ByteBuf buff, boolean end) { int readableBytes = buff.readableBytes(); if (readableBytes == 0 && !end) { // nothing to write to the connection just return return; } if (end) { completed = true; } if (!end && !chunked && !contentLengthSet()) { throw new IllegalStateException( "You must set the Content-Length header to be the total size of the message " + "body BEFORE sending any data if you are not using HTTP chunked encoding."); } written += buff.readableBytes(); if (conn == null) { if (pendingChunks == null) { pendingChunks = buff; } else { CompositeByteBuf pending; if (pendingChunks instanceof CompositeByteBuf) { pending = (CompositeByteBuf) pendingChunks; } else { pending = Unpooled.compositeBuffer(); pending.addComponent(pendingChunks).writerIndex(pendingChunks.writerIndex()); pendingChunks = pending; } pending.addComponent(buff).writerIndex(pending.writerIndex() + buff.writerIndex()); } connect(); } else { if (!headWritten) { writeHeadWithContent(buff, end); } else { if (end) { if (buff.isReadable()) { conn.writeToChannel(new DefaultLastHttpContent(buff, false)); } else { conn.writeToChannel(LastHttpContent.EMPTY_LAST_CONTENT); } } else { conn.writeToChannel(new DefaultHttpContent(buff)); } } if (end) { conn.reportBytesWritten(written); if (respHandler != null) { conn.endRequest(); } } } }