* Merged CookieVersionSupport methods into the CookieSpec interface

* Added support for Set-Cookie2 and Cookie2 headers 

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@582479 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2007-10-06 11:11:09 +00:00
parent 4f153b3ed3
commit e6d76b2750
10 changed files with 84 additions and 58 deletions

View File

@ -167,6 +167,24 @@ public class RequestAddCookies implements HttpRequestInterceptor {
}
}
int ver = cookieSpec.getVersion();
if (ver > 0) {
boolean needVersionHeader = false;
for (int i = 0; i < cookies.length; i++) {
if (ver != cookies[i].getVersion()) {
needVersionHeader = true;
}
}
if (needVersionHeader) {
Header header = cookieSpec.getVersionHeader();
if (header != null) {
// Advertise cookie version support
request.addHeader(header);
}
}
}
// Stick the CookieSpec and CookieOrigin instances to the HTTP context
// so they could be obtained by the response interceptor
context.setAttribute(ClientContext.COOKIE_SPEC, cookieSpec);

View File

@ -36,6 +36,7 @@ import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Header;
import org.apache.http.HeaderIterator;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
@ -95,17 +96,25 @@ public class ResponseProcessCookies implements HttpResponseInterceptor {
LOG.info("CookieOrigin not available in HTTP context");
return;
}
Header[] headers = response.getHeaders(SM.SET_COOKIE);
processCookies(headers, cookieSpec, cookieOrigin, cookieStore);
HeaderIterator it = response.headerIterator(SM.SET_COOKIE);
processCookies(it, cookieSpec, cookieOrigin, cookieStore);
// see if the cookie spec supports cookie versioning.
if (cookieSpec.getVersion() > 0) {
// process set-cookie2 headers.
// Cookie2 will replace equivalent Cookie instances
it = response.headerIterator(SM.SET_COOKIE2);
processCookies(it, cookieSpec, cookieOrigin, cookieStore);
}
}
private static void processCookies(
final Header[] headers,
final HeaderIterator iterator,
final CookieSpec cookieSpec,
final CookieOrigin cookieOrigin,
final CookieStore cookieStore) {
for (int i = 0; i < headers.length; i++) {
Header header = headers[i];
while (iterator.hasNext()) {
Header header = iterator.nextHeader();
try {
Cookie[] cookies = cookieSpec.parse(header, cookieOrigin);
for (int c = 0; c < cookies.length; c++) {

View File

@ -50,6 +50,14 @@ import org.apache.http.Header;
*/
public interface CookieSpec {
/**
* Returns version of the state management this cookie specification
* conforms to.
*
* @return version of the state management specification
*/
int getVersion();
/**
* Parse the <tt>"Set-Cookie"</tt> Header into an array of Cookies.
*
@ -95,4 +103,11 @@ public interface CookieSpec {
*/
Header[] formatCookies(Cookie[] cookies);
/**
* Returns a request header identifying what version of the state management
* specification is understood. May be <code>null</code> if the cookie
* specification does not support <tt>Cookie2</tt> header.
*/
Header getVersionHeader();
}

View File

@ -1,48 +0,0 @@
/*
* $HeadURL$
* $Revision$
* $Date$
*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.cookie;
import org.apache.http.Header;
/**
* Defines cookie specification specific capabilities
*
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @since 3.1
*/
public interface CookieVersionSupport {
int getVersion();
Header getVersionHeader();
}

View File

@ -40,8 +40,9 @@ package org.apache.http.cookie;
*/
public interface SM {
public static final String COOKIE = "Cookie";
public static final String COOKIE_2 = "Cookie2";
public static final String SET_COOKIE = "Set-Cookie";
public static final String COOKIE = "Cookie";
public static final String COOKIE_2 = "Cookie2";
public static final String SET_COOKIE = "Set-Cookie";
public static final String SET_COOKIE2 = "Set-Cookie2";
}

View File

@ -152,5 +152,13 @@ public class BrowserCompatSpec extends CookieSpecBase {
}
return new Header[] { new BufferedHeader(buffer) };
}
public int getVersion() {
return 0;
}
public Header getVersionHeader() {
return null;
}
}

View File

@ -134,4 +134,12 @@ public class NetscapeDraftSpec extends CookieSpecBase {
return new Header[] { new BufferedHeader(buffer) };
}
public int getVersion() {
return 0;
}
public Header getVersionHeader() {
return null;
}
}

View File

@ -226,5 +226,13 @@ public class RFC2109Spec extends CookieSpecBase {
}
}
}
public int getVersion() {
return 1;
}
public Header getVersionHeader() {
return null;
}
}

View File

@ -41,7 +41,6 @@ import org.apache.http.cookie.ClientCookie;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieAttributeHandler;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.CookieVersionSupport;
import org.apache.http.cookie.MalformedCookieException;
import org.apache.http.cookie.SM;
import org.apache.http.message.BufferedHeader;
@ -55,7 +54,7 @@ import org.apache.http.util.CharArrayBuffer;
*
* @since 3.1
*/
public class RFC2965Spec extends RFC2109Spec implements CookieVersionSupport {
public class RFC2965Spec extends RFC2109Spec {
/**
* Default constructor

View File

@ -77,6 +77,14 @@ public class TestAbstractCookieSpec extends TestCase {
public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException {
}
public int getVersion() {
return 0;
}
public Header getVersionHeader() {
return null;
}
}
private static class DummyCookieAttribHandler implements CookieAttributeHandler {