SimpleClientHttpRequest uses fixed-length streaming mode (always sets content-length header); partial backport from 3.1 M2

This commit is contained in:
Juergen Hoeller
2011-07-13 23:03:35 +00:00
parent 24cb345b6c
commit 210c77ddb0
2 changed files with 14 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,7 +39,7 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest {
public final HttpHeaders getHeaders() {
return executed ? HttpHeaders.readOnlyHttpHeaders(headers) : this.headers;
return (this.executed ? HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers);
}
public final OutputStream getBody() throws IOException {
@@ -49,7 +49,11 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest {
public final ClientHttpResponse execute() throws IOException {
checkExecuted();
ClientHttpResponse result = executeInternal(this.headers, this.bufferedOutput.toByteArray());
byte[] bytes = this.bufferedOutput.toByteArray();
if (this.headers.getContentLength() == -1) {
this.headers.setContentLength(bytes.length);
}
ClientHttpResponse result = executeInternal(this.headers, bytes);
this.executed = true;
return result;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -66,10 +66,15 @@ final class SimpleClientHttpRequest extends AbstractClientHttpRequest {
this.connection.addRequestProperty(headerName, headerValue);
}
}
if (this.connection.getDoOutput()) {
this.connection.setFixedLengthStreamingMode(bufferedOutput.length);
}
this.connection.connect();
if (bufferedOutput.length > 0) {
if (this.connection.getDoOutput()) {
FileCopyUtils.copy(bufferedOutput, this.connection.getOutputStream());
}
return new SimpleClientHttpResponse(this.connection);
}