@Override public Sink createRequestBody(Request request, long contentLength) throws IOException { if ("chunked".equalsIgnoreCase(request.header("Transfer-Encoding"))) { // Stream a request body of unknown length. return httpConnection.newChunkedSink(); } if (contentLength != -1) { // Stream a request body of a known length. return httpConnection.newFixedLengthSink(contentLength); } throw new IllegalStateException( "Cannot stream a request body without chunked encoding or a known content length!"); }
/** * Prepares the HTTP headers and sends them to the server. * * <p>For streaming requests with a body, headers must be prepared <strong>before</strong> the * output stream has been written to. Otherwise the body would need to be buffered! * * <p>For non-streaming requests with a body, headers must be prepared <strong>after</strong> the * output stream has been written to and closed. This ensures that the {@code Content-Length} * header field receives the proper value. */ public void writeRequestHeaders(Request request) throws IOException { httpEngine.writingRequestHeaders(); String requestLine = RequestLine.get(request, httpEngine.getConnection().getRoute().getProxy().type()); httpConnection.writeRequest(request.headers(), requestLine); }