Client request implementations enforce RFC 6265 (cookies in a single header)

Issue: SPR-12196
This commit is contained in:
Juergen Hoeller 2014-09-16 15:02:21 +02:00
parent 7387475deb
commit 26a93b6a33
5 changed files with 43 additions and 48 deletions

View File

@ -32,6 +32,7 @@ import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.util.StringUtils;
/**
* {@link org.springframework.http.client.ClientHttpRequest} implementation that uses
@ -41,6 +42,7 @@ import org.springframework.http.HttpMethod;
*
* @author Oleg Kalnichevski
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 3.1
* @see HttpComponentsClientHttpRequestFactory#createRequest(URI, HttpMethod)
*/
@ -97,8 +99,12 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR
static void addHeaders(HttpUriRequest httpRequest, HttpHeaders headers) {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
if (!headerName.equalsIgnoreCase(HTTP.CONTENT_LEN) &&
!headerName.equalsIgnoreCase(HTTP.TRANSFER_ENCODING)) {
if (HttpHeaders.COOKIE.equalsIgnoreCase(headerName)) { // RFC 6265
String headerValue = StringUtils.collectionToDelimitedString(entry.getValue(), "; ");
httpRequest.addHeader(headerName, headerValue);
}
else if (!HTTP.CONTENT_LEN.equalsIgnoreCase(headerName) &&
!HTTP.TRANSFER_ENCODING.equalsIgnoreCase(headerName)) {
for (String headerValue : entry.getValue()) {
httpRequest.addHeader(headerName, headerValue);
}

View File

@ -20,8 +20,6 @@ import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import org.springframework.core.task.AsyncListenableTaskExecutor;
@ -32,7 +30,7 @@ import org.springframework.util.concurrent.ListenableFuture;
/**
* {@link org.springframework.http.client.ClientHttpRequest} implementation that uses
* standard J2SE facilities to execute buffered requests. Created via the
* standard JDK facilities to execute buffered requests. Created via the
* {@link org.springframework.http.client.SimpleClientHttpRequestFactory}.
*
* @author Arjen Poutsma
@ -79,12 +77,7 @@ final class SimpleBufferingAsyncClientHttpRequest extends AbstractBufferingAsync
return this.taskExecutor.submitListenable(new Callable<ClientHttpResponse>() {
@Override
public ClientHttpResponse call() throws Exception {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
for (String headerValue : entry.getValue()) {
connection.addRequestProperty(headerName, headerValue);
}
}
SimpleBufferingClientHttpRequest.addHeaders(connection, headers);
if (connection.getDoOutput() && outputStreaming) {
connection.setFixedLengthStreamingMode(bufferedOutput.length);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@ -26,12 +26,14 @@ import java.util.Map;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
/**
* {@link ClientHttpRequest} implementation that uses standard J2SE facilities to execute buffered requests.
* Created via the {@link SimpleClientHttpRequestFactory}.
* {@link ClientHttpRequest} implementation that uses standard JDK facilities to
* execute buffered requests. Created via the {@link SimpleClientHttpRequestFactory}.
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 3.0
* @see SimpleClientHttpRequestFactory#createRequest(java.net.URI, HttpMethod)
*/
@ -65,12 +67,7 @@ final class SimpleBufferingClientHttpRequest extends AbstractBufferingClientHttp
@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
for (String headerValue : entry.getValue()) {
this.connection.addRequestProperty(headerName, headerValue);
}
}
addHeaders(this.connection, headers);
if (this.connection.getDoOutput() && this.outputStreaming) {
this.connection.setFixedLengthStreamingMode(bufferedOutput.length);
@ -83,4 +80,25 @@ final class SimpleBufferingClientHttpRequest extends AbstractBufferingClientHttp
return new SimpleClientHttpResponse(this.connection);
}
/**
* Add the given headers to the given HTTP connection.
* @param connection the connection to add the headers to
* @param headers the headers to add
*/
static void addHeaders(HttpURLConnection connection, HttpHeaders headers) {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
if (HttpHeaders.COOKIE.equalsIgnoreCase(headerName)) { // RFC 6265
String headerValue = StringUtils.collectionToDelimitedString(entry.getValue(), "; ");
connection.setRequestProperty(headerName, headerValue);
}
else {
for (String headerValue : entry.getValue()) {
connection.addRequestProperty(headerName, headerValue);
}
}
}
}
}

View File

@ -21,8 +21,6 @@ import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import org.springframework.core.task.AsyncListenableTaskExecutor;
@ -91,22 +89,13 @@ final class SimpleStreamingAsyncClientHttpRequest extends AbstractAsyncClientHtt
this.connection.setChunkedStreamingMode(this.chunkSize);
}
}
writeHeaders(headers);
SimpleBufferingClientHttpRequest.addHeaders(this.connection, headers);
this.connection.connect();
this.body = this.connection.getOutputStream();
}
return StreamUtils.nonClosing(this.body);
}
private void writeHeaders(HttpHeaders headers) {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
for (String headerValue : entry.getValue()) {
this.connection.addRequestProperty(headerName, headerValue);
}
}
}
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(final HttpHeaders headers) throws IOException {
return this.taskExecutor.submitListenable(new Callable<ClientHttpResponse>() {
@ -117,7 +106,7 @@ final class SimpleStreamingAsyncClientHttpRequest extends AbstractAsyncClientHtt
body.close();
}
else {
writeHeaders(headers);
SimpleBufferingClientHttpRequest.addHeaders(connection, headers);
connection.connect();
}
}

View File

@ -21,16 +21,14 @@ import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.util.StreamUtils;
/**
* {@link ClientHttpRequest} implementation that uses standard J2SE facilities to execute streaming requests.
* Created via the {@link SimpleClientHttpRequestFactory}.
* {@link ClientHttpRequest} implementation that uses standard JDK facilities to
* execute streaming requests. Created via the {@link SimpleClientHttpRequestFactory}.
*
* @author Arjen Poutsma
* @since 3.0
@ -80,22 +78,13 @@ final class SimpleStreamingClientHttpRequest extends AbstractClientHttpRequest {
this.connection.setChunkedStreamingMode(this.chunkSize);
}
}
writeHeaders(headers);
SimpleBufferingClientHttpRequest.addHeaders(this.connection, headers);
this.connection.connect();
this.body = this.connection.getOutputStream();
}
return StreamUtils.nonClosing(this.body);
}
private void writeHeaders(HttpHeaders headers) {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
for (String headerValue : entry.getValue()) {
this.connection.addRequestProperty(headerName, headerValue);
}
}
}
@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
try {
@ -103,7 +92,7 @@ final class SimpleStreamingClientHttpRequest extends AbstractClientHttpRequest {
this.body.close();
}
else {
writeHeaders(headers);
SimpleBufferingClientHttpRequest.addHeaders(this.connection, headers);
this.connection.connect();
}
}