13 changed files with 1698 additions and 30 deletions
@ -0,0 +1,122 @@
@@ -0,0 +1,122 @@
|
||||
/* |
||||
* Copyright 2002-2015 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.http; |
||||
|
||||
/** |
||||
* Representation for an HTTP Cookie. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @see <a href="https://tools.ietf.org/html/rfc6265">RFC 6265</a> |
||||
*/ |
||||
public class HttpCookie { |
||||
|
||||
private final String name; |
||||
|
||||
private final String value; |
||||
|
||||
private String domain; |
||||
|
||||
private String path; |
||||
|
||||
private long maxAge = Long.MIN_VALUE; |
||||
|
||||
private boolean secure; |
||||
|
||||
private boolean httpOnly; |
||||
|
||||
|
||||
public HttpCookie(String name, String value) { |
||||
this.name = name; |
||||
this.value = value; |
||||
} |
||||
|
||||
/** |
||||
* Return the cookie name. |
||||
*/ |
||||
public String getName() { |
||||
return this.name; |
||||
} |
||||
|
||||
/** |
||||
* Return the cookie value. |
||||
*/ |
||||
public String getValue() { |
||||
return this.value; |
||||
} |
||||
|
||||
public HttpCookie setPath(String path) { |
||||
this.path = path; |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Return the domain attribute of the cookie. |
||||
*/ |
||||
public String getDomain() { |
||||
return this.domain; |
||||
} |
||||
|
||||
public HttpCookie setDomain(String domain) { |
||||
this.domain = domain; |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Return the path attribute of the cookie. |
||||
*/ |
||||
public String getPath() { |
||||
return this.path; |
||||
} |
||||
|
||||
public HttpCookie setMaxAge(long maxAge) { |
||||
this.maxAge = maxAge; |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Return the maximum age attribute of the cookie in seconds or |
||||
* {@link Long#MIN_VALUE} if not set. |
||||
*/ |
||||
public long getMaxAge() { |
||||
return this.maxAge; |
||||
} |
||||
|
||||
public HttpCookie setSecure(boolean secure) { |
||||
this.secure = secure; |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Return true if the "Secure" attribute of the cookie is present. |
||||
*/ |
||||
public boolean isSecure() { |
||||
return this.secure; |
||||
} |
||||
|
||||
public HttpCookie setHttpOnly(boolean httpOnly) { |
||||
this.httpOnly = httpOnly; |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Return true if the "HttpOnly" attribute of the cookie is present. |
||||
* @see <a href="http://www.owasp.org/index.php/HTTPOnly">http://www.owasp.org/index.php/HTTPOnly</a>
|
||||
*/ |
||||
public boolean isHttpOnly() { |
||||
return this.httpOnly; |
||||
} |
||||
|
||||
} |
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,160 @@
@@ -0,0 +1,160 @@
|
||||
/* |
||||
* Copyright 2002-2015 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.http.server.reactive; |
||||
|
||||
import java.net.URI; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.After; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.junit.runners.Parameterized; |
||||
import reactor.Mono; |
||||
|
||||
import org.springframework.http.HttpCookie; |
||||
import org.springframework.http.RequestEntity; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.http.server.reactive.boot.HttpServer; |
||||
import org.springframework.http.server.reactive.boot.JettyHttpServer; |
||||
import org.springframework.http.server.reactive.boot.RxNettyHttpServer; |
||||
import org.springframework.http.server.reactive.boot.TomcatHttpServer; |
||||
import org.springframework.http.server.reactive.boot.UndertowHttpServer; |
||||
import org.springframework.util.SocketUtils; |
||||
import org.springframework.web.client.RestTemplate; |
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo; |
||||
import static org.hamcrest.Matchers.containsInAnyOrder; |
||||
import static org.hamcrest.Matchers.equalToIgnoringCase; |
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertThat; |
||||
|
||||
/** |
||||
* Temporarily does not extend AbstractHttpHandlerIntegrationTests in order to |
||||
* exclude Reactor Net due to https://github.com/reactor/reactor/issues/614.
|
||||
* |
||||
* @author Rossen Stoyanchev |
||||
*/ |
||||
@RunWith(Parameterized.class) |
||||
public class CookieIntegrationTests { |
||||
|
||||
protected int port; |
||||
|
||||
@Parameterized.Parameter(0) |
||||
public HttpServer server; |
||||
|
||||
private CookieHandler cookieHandler; |
||||
|
||||
|
||||
@Parameterized.Parameters(name = "server [{0}]") |
||||
public static Object[][] arguments() { |
||||
return new Object[][] { |
||||
{new JettyHttpServer()}, |
||||
{new RxNettyHttpServer()}, |
||||
// {new ReactorHttpServer()},
|
||||
{new TomcatHttpServer()}, |
||||
{new UndertowHttpServer()} |
||||
}; |
||||
} |
||||
|
||||
|
||||
@Before |
||||
public void setup() throws Exception { |
||||
this.port = SocketUtils.findAvailableTcpPort(); |
||||
this.server.setPort(this.port); |
||||
this.server.setHandler(createHttpHandler()); |
||||
this.server.afterPropertiesSet(); |
||||
this.server.start(); |
||||
} |
||||
|
||||
protected HttpHandler createHttpHandler() { |
||||
this.cookieHandler = new CookieHandler(); |
||||
return this.cookieHandler; |
||||
} |
||||
|
||||
@After |
||||
public void tearDown() throws Exception { |
||||
this.server.stop(); |
||||
} |
||||
|
||||
|
||||
@SuppressWarnings("unchecked") |
||||
@Test |
||||
public void basicTest() throws Exception { |
||||
URI url = new URI("http://localhost:" + port); |
||||
String header = "SID=31d4d96e407aad42; lang=en-US"; |
||||
ResponseEntity<Void> response = new RestTemplate().exchange( |
||||
RequestEntity.get(url).header("Cookie", header).build(), Void.class); |
||||
|
||||
Map<String, Set<HttpCookie>> requestCookies = this.cookieHandler.requestCookies; |
||||
assertEquals(2, requestCookies.size()); |
||||
|
||||
Set<HttpCookie> set = requestCookies.get("SID"); |
||||
assertEquals(1, set.size()); |
||||
assertEquals("31d4d96e407aad42", set.iterator().next().getValue()); |
||||
|
||||
set = requestCookies.get("lang"); |
||||
assertEquals(1, set.size()); |
||||
assertEquals("en-US", set.iterator().next().getValue()); |
||||
|
||||
List<String> headerValues = response.getHeaders().get("Set-Cookie"); |
||||
assertEquals(2, headerValues.size()); |
||||
|
||||
List<String> parts = splitCookieHeader(headerValues.get(0)); |
||||
assertThat(parts, containsInAnyOrder(equalTo("SID=31d4d96e407aad42"), |
||||
equalToIgnoringCase("Path=/"), equalToIgnoringCase("Secure"), |
||||
equalToIgnoringCase("HttpOnly"))); |
||||
|
||||
parts = splitCookieHeader(headerValues.get(1)); |
||||
assertThat(parts, containsInAnyOrder(equalTo("lang=en-US"), |
||||
equalToIgnoringCase("Path=/"), equalToIgnoringCase("Domain=example.com"))); |
||||
} |
||||
|
||||
// No client side HttpCookie support yet
|
||||
private List<String> splitCookieHeader(String value) { |
||||
List<String> list = new ArrayList<>(); |
||||
for (String s : value.split(";")){ |
||||
list.add(s.trim()); |
||||
} |
||||
return list; |
||||
} |
||||
|
||||
|
||||
private class CookieHandler implements HttpHandler { |
||||
|
||||
private Map<String, Set<HttpCookie>> requestCookies; |
||||
|
||||
|
||||
@Override |
||||
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { |
||||
|
||||
this.requestCookies = request.getHeaders().getCookies(); |
||||
this.requestCookies.size(); // Cause lazy loading
|
||||
|
||||
response.getHeaders().addCookie(new HttpCookie("SID", "31d4d96e407aad42") |
||||
.setPath("/").setHttpOnly(true).setSecure(true)); |
||||
response.getHeaders().addCookie(new HttpCookie("lang", "en-US") |
||||
.setDomain("example.com").setPath("/")); |
||||
response.writeHeaders(); |
||||
|
||||
return Mono.empty(); |
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue