Issue 389: workaround where java urlconnection doesn't set content-length header when it should

This commit is contained in:
Adrian Cole 2010-10-26 18:36:32 -07:00
parent e2a8185c10
commit 6bb9d101d1
1 changed files with 11 additions and 9 deletions

View File

@ -85,11 +85,11 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
@Inject @Inject
public JavaUrlHttpCommandExecutorService(HttpUtils utils, public JavaUrlHttpCommandExecutorService(HttpUtils utils,
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor, @Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor,
DelegatingRetryHandler retryHandler, IOExceptionRetryHandler ioRetryHandler, DelegatingRetryHandler retryHandler, IOExceptionRetryHandler ioRetryHandler,
DelegatingErrorHandler errorHandler, HttpWire wire, @Named("untrusted") HostnameVerifier verifier, DelegatingErrorHandler errorHandler, HttpWire wire, @Named("untrusted") HostnameVerifier verifier,
@Named("untrusted") Supplier<SSLContext> untrustedSSLContextProvider) throws SecurityException, @Named("untrusted") Supplier<SSLContext> untrustedSSLContextProvider) throws SecurityException,
NoSuchFieldException { NoSuchFieldException {
super(utils, ioWorkerExecutor, retryHandler, ioRetryHandler, errorHandler, wire); super(utils, ioWorkerExecutor, retryHandler, ioRetryHandler, errorHandler, wire);
if (utils.getMaxConnections() > 0) if (utils.getMaxConnections() > 0)
System.setProperty("http.maxConnections", String.valueOf(checkNotNull(utils, "utils").getMaxConnections())); System.setProperty("http.maxConnections", String.valueOf(checkNotNull(utils, "utils").getMaxConnections()));
@ -218,21 +218,23 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
} }
// writeTo will close the output stream // writeTo will close the output stream
try { try {
request.getPayload().writeTo(connection.getOutputStream()); request.getPayload().writeTo(connection.getOutputStream());
} catch (IOException e){ } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
throw e; throw e;
} }
} else { } else {
connection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, "0"); connection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, "0");
// for some reason POST undoes the content length header above.
if (connection.getRequestMethod().equals("POST"))
connection.setChunkedStreamingMode(0);
} }
return connection; return connection;
} }
/** /**
* Only disconnect if there is no content, as disconnecting will throw away * Only disconnect if there is no content, as disconnecting will throw away unconsumed content.
* unconsumed content.
*/ */
@Override @Override
protected void cleanup(HttpURLConnection connection) { protected void cleanup(HttpURLConnection connection) {