428966 - Per-request cookie support.
This commit is contained in:
parent
660c05d399
commit
eb1900d10b
|
@ -18,6 +18,7 @@
|
|||
|
||||
package org.eclipse.jetty.client;
|
||||
|
||||
import java.net.CookieStore;
|
||||
import java.net.HttpCookie;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
|
@ -133,19 +134,14 @@ public abstract class HttpConnection implements Connection
|
|||
}
|
||||
|
||||
// Cookies
|
||||
List<HttpCookie> cookies = getHttpClient().getCookieStore().get(request.getURI());
|
||||
StringBuilder cookieString = null;
|
||||
for (int i = 0; i < cookies.size(); ++i)
|
||||
CookieStore cookieStore = getHttpClient().getCookieStore();
|
||||
if (cookieStore != null)
|
||||
{
|
||||
if (cookieString == null)
|
||||
cookieString = new StringBuilder();
|
||||
if (i > 0)
|
||||
cookieString.append("; ");
|
||||
HttpCookie cookie = cookies.get(i);
|
||||
cookieString.append(cookie.getName()).append("=").append(cookie.getValue());
|
||||
StringBuilder cookies = convertCookies(cookieStore.get(request.getURI()), null);
|
||||
cookies = convertCookies(request.getCookies(), cookies);
|
||||
if (cookies != null)
|
||||
request.header(HttpHeader.COOKIE.asString(), cookies.toString());
|
||||
}
|
||||
if (cookieString != null)
|
||||
request.header(HttpHeader.COOKIE.asString(), cookieString.toString());
|
||||
|
||||
// Authorization
|
||||
URI authenticationURI = proxy != null ? proxy.getURI() : request.getURI();
|
||||
|
@ -157,6 +153,20 @@ public abstract class HttpConnection implements Connection
|
|||
}
|
||||
}
|
||||
|
||||
private StringBuilder convertCookies(List<HttpCookie> cookies, StringBuilder builder)
|
||||
{
|
||||
for (int i = 0; i < cookies.size(); ++i)
|
||||
{
|
||||
if (builder == null)
|
||||
builder = new StringBuilder();
|
||||
if (builder.length() > 0)
|
||||
builder.append("; ");
|
||||
HttpCookie cookie = cookies.get(i);
|
||||
builder.append(cookie.getName()).append("=").append(cookie.getValue());
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
|
|
|
@ -20,6 +20,7 @@ package org.eclipse.jetty.client;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.HttpCookie;
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
|
@ -27,6 +28,7 @@ import java.nio.ByteBuffer;
|
|||
import java.nio.charset.UnsupportedCharsetException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
@ -75,6 +77,7 @@ public class HttpRequest implements Request
|
|||
private long timeout;
|
||||
private ContentProvider content;
|
||||
private boolean followRedirects;
|
||||
private List<HttpCookie> cookies;
|
||||
|
||||
protected HttpRequest(HttpClient client, HttpConversation conversation, URI uri)
|
||||
{
|
||||
|
@ -259,6 +262,21 @@ public class HttpRequest implements Request
|
|||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<HttpCookie> getCookies()
|
||||
{
|
||||
return cookies != null ? cookies : Collections.<HttpCookie>emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Request cookie(HttpCookie cookie)
|
||||
{
|
||||
if (cookies == null)
|
||||
cookies = new ArrayList<>();
|
||||
cookies.add(cookie);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Request attribute(String name, Object value)
|
||||
{
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
package org.eclipse.jetty.client.api;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpCookie;
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.ByteBuffer;
|
||||
|
@ -163,6 +164,17 @@ public interface Request
|
|||
*/
|
||||
Request header(HttpHeader header, String value);
|
||||
|
||||
/**
|
||||
* @return the cookies associated with this request
|
||||
*/
|
||||
List<HttpCookie> getCookies();
|
||||
|
||||
/**
|
||||
* @param cookie a cookie for this request
|
||||
* @return this request object
|
||||
*/
|
||||
Request cookie(HttpCookie cookie);
|
||||
|
||||
/**
|
||||
* @param name the name of the attribute
|
||||
* @param value the value of the attribute
|
||||
|
|
|
@ -22,7 +22,7 @@ import java.io.IOException;
|
|||
import java.net.HttpCookie;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
@ -83,12 +83,13 @@ public class HttpCookieTest extends AbstractHttpClientServerTest
|
|||
@Override
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
||||
{
|
||||
baseRequest.setHandled(true);
|
||||
Cookie[] cookies = request.getCookies();
|
||||
Assert.assertNotNull(cookies);
|
||||
Assert.assertEquals(1, cookies.length);
|
||||
Cookie cookie = cookies[0];
|
||||
Assert.assertEquals(name, cookie.getName());
|
||||
Assert.assertEquals(value, cookie.getValue());
|
||||
baseRequest.setHandled(true);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -122,4 +123,32 @@ public class HttpCookieTest extends AbstractHttpClientServerTest
|
|||
Assert.assertEquals(200, response.getStatus());
|
||||
Assert.assertTrue(client.getCookieStore().getCookies().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_PerRequestCookieIsSent() throws Exception
|
||||
{
|
||||
final String name = "foo";
|
||||
final String value = "bar";
|
||||
start(new AbstractHandler()
|
||||
{
|
||||
@Override
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
||||
{
|
||||
baseRequest.setHandled(true);
|
||||
Cookie[] cookies = request.getCookies();
|
||||
Assert.assertNotNull(cookies);
|
||||
Assert.assertEquals(1, cookies.length);
|
||||
Cookie cookie = cookies[0];
|
||||
Assert.assertEquals(name, cookie.getName());
|
||||
Assert.assertEquals(value, cookie.getValue());
|
||||
}
|
||||
});
|
||||
|
||||
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
|
||||
.scheme(scheme)
|
||||
.cookie(new HttpCookie(name, value))
|
||||
.timeout(5, TimeUnit.SECONDS)
|
||||
.send();
|
||||
Assert.assertEquals(200, response.getStatus());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue