Better handling of missing content-type header in OPTIONS with entity

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.6.x@1787697 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2017-03-20 09:39:31 +00:00
parent ce237ae735
commit 90fd3936da
2 changed files with 15 additions and 5 deletions

View File

@ -32,7 +32,9 @@ import java.util.List;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HeaderElement; import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
@ -43,8 +45,8 @@ import org.apache.http.annotation.ThreadingBehavior;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.cache.HeaderConstants; import org.apache.http.client.cache.HeaderConstants;
import org.apache.http.client.methods.HttpRequestWrapper; import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.HttpEntityWrapper;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpResponse; import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine; import org.apache.http.message.BasicStatusLine;
@ -193,9 +195,17 @@ class RequestProtocolCompliance {
} }
private void addContentTypeHeaderIfMissing(final HttpEntityEnclosingRequest request) { private void addContentTypeHeaderIfMissing(final HttpEntityEnclosingRequest request) {
if (request.getEntity().getContentType() == null) { final HttpEntity entity = request.getEntity();
((AbstractHttpEntity) request.getEntity()).setContentType( if (entity != null && entity.getContentType() == null) {
ContentType.APPLICATION_OCTET_STREAM.getMimeType()); final HttpEntityWrapper entityWrapper = new HttpEntityWrapper(entity) {
@Override
public Header getContentType() {
return new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_OCTET_STREAM.getMimeType());
}
};
request.setEntity(entityWrapper);
} }
} }

View File

@ -42,9 +42,9 @@ import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpExecutionAware; import org.apache.http.client.methods.HttpExecutionAware;
import org.apache.http.client.methods.HttpRequestWrapper; import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.DateUtils;
import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.client.utils.DateUtils;
import org.apache.http.impl.execchain.ClientExecChain; import org.apache.http.impl.execchain.ClientExecChain;
import org.apache.http.message.BasicHttpEntityEnclosingRequest; import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.message.BasicHttpRequest; import org.apache.http.message.BasicHttpRequest;