From a543fc4fff2d328cbbdf9aafc0324c46a5b90f2e Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sat, 17 Nov 2007 13:39:46 +0000 Subject: [PATCH] Re-synched HttpClient trunk with HttpCore trunk git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@595948 13f79535-47bb-0310-9956-ffa450edef68 --- module-client/pom.xml | 2 +- .../apache/http/impl/auth/RFC2617Scheme.java | 6 +- .../http/impl/conn/DefaultResponseParser.java | 7 +- .../http/impl/cookie/BrowserCompatSpec.java | 22 ++++- .../cookie/NetscapeDraftHeaderParser.java | 78 +++++++++++++++ .../http/impl/cookie/NetscapeDraftSpec.java | 23 ++++- .../http/impl/cookie/TestAllCookieImpl.java | 1 + .../cookie/TestNetscapeDraftHeaderParser.java | 96 +++++++++++++++++++ 8 files changed, 226 insertions(+), 9 deletions(-) create mode 100644 module-client/src/main/java/org/apache/http/impl/cookie/NetscapeDraftHeaderParser.java create mode 100644 module-client/src/test/java/org/apache/http/impl/cookie/TestNetscapeDraftHeaderParser.java diff --git a/module-client/pom.xml b/module-client/pom.xml index a3b715d6b..f04a6f746 100644 --- a/module-client/pom.xml +++ b/module-client/pom.xml @@ -63,7 +63,7 @@ org.apache.httpcomponents httpcore - 4.0-alpha6 + 4.0-alpha7-SNAPSHOT commons-logging diff --git a/module-client/src/main/java/org/apache/http/impl/auth/RFC2617Scheme.java b/module-client/src/main/java/org/apache/http/impl/auth/RFC2617Scheme.java index 227202e92..6c15d75fc 100644 --- a/module-client/src/main/java/org/apache/http/impl/auth/RFC2617Scheme.java +++ b/module-client/src/main/java/org/apache/http/impl/auth/RFC2617Scheme.java @@ -40,6 +40,8 @@ import org.apache.http.auth.AuthScheme; import org.apache.http.auth.AUTH; import org.apache.http.auth.MalformedChallengeException; import org.apache.http.message.BasicHeaderValueParser; +import org.apache.http.message.HeaderValueParser; +import org.apache.http.message.ParserCursor; import org.apache.http.protocol.HTTP; import org.apache.http.util.CharArrayBuffer; @@ -120,7 +122,9 @@ public abstract class RFC2617Scheme implements AuthScheme { if (!s.equalsIgnoreCase(getSchemeName())) { throw new MalformedChallengeException("Invalid scheme identifier: " + s); } - HeaderElement[] elements = BasicHeaderValueParser.DEFAULT.parseElements(buffer, pos, buffer.length()); + HeaderValueParser parser = BasicHeaderValueParser.DEFAULT; + ParserCursor cursor = new ParserCursor(pos, buffer.length()); + HeaderElement[] elements = parser.parseElements(buffer, cursor); if (elements.length == 0) { throw new MalformedChallengeException("Authentication challenge is empty"); } diff --git a/module-client/src/main/java/org/apache/http/impl/conn/DefaultResponseParser.java b/module-client/src/main/java/org/apache/http/impl/conn/DefaultResponseParser.java index 04515ab3e..dea640ab6 100644 --- a/module-client/src/main/java/org/apache/http/impl/conn/DefaultResponseParser.java +++ b/module-client/src/main/java/org/apache/http/impl/conn/DefaultResponseParser.java @@ -43,6 +43,7 @@ import org.apache.http.conn.params.ConnConnectionPNames; import org.apache.http.impl.io.AbstractMessageParser; import org.apache.http.io.SessionInputBuffer; import org.apache.http.message.LineParser; +import org.apache.http.message.ParserCursor; import org.apache.http.params.HttpParams; import org.apache.http.util.CharArrayBuffer; @@ -75,13 +76,15 @@ public class DefaultResponseParser extends AbstractMessageParser { this.lineBuf.clear(); //read out the HTTP status string int count = 0; + ParserCursor cursor = null; do { int i = sessionBuffer.readLine(this.lineBuf); if (i == -1 && count == 0) { // The server just dropped connection on us throw new NoHttpResponseException("The target server failed to respond"); } - if (lineParser.hasProtocolVersion(this.lineBuf, 0)) { + cursor = new ParserCursor(0, this.lineBuf.length()); + if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) { // Got one break; } else if (i == -1 || count >= this.maxGarbageLines) { @@ -92,7 +95,7 @@ public class DefaultResponseParser extends AbstractMessageParser { count++; } while(true); //create the status line from the status string - StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, 0, this.lineBuf.length()); + StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor); return this.responseFactory.newHttpResponse(statusline, null); } diff --git a/module-client/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java b/module-client/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java index 207b317ec..0297835c2 100644 --- a/module-client/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java +++ b/module-client/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java @@ -31,6 +31,7 @@ package org.apache.http.impl.cookie; +import org.apache.http.FormattedHeader; import org.apache.http.Header; import org.apache.http.HeaderElement; import org.apache.http.cookie.ClientCookie; @@ -38,8 +39,8 @@ import org.apache.http.cookie.Cookie; import org.apache.http.cookie.CookieOrigin; import org.apache.http.cookie.MalformedCookieException; import org.apache.http.cookie.SM; -import org.apache.http.message.BasicHeaderValueParser; import org.apache.http.message.BufferedHeader; +import org.apache.http.message.ParserCursor; import org.apache.http.util.CharArrayBuffer; /** @@ -121,7 +122,24 @@ public class BrowserCompatSpec extends CookieSpecBase { } HeaderElement[] elems = null; if (isNetscapeCookie) { - elems = new HeaderElement[] { BasicHeaderValueParser.parseHeaderElement(headervalue, null) }; + NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT; + CharArrayBuffer buffer; + ParserCursor cursor; + if (header instanceof FormattedHeader) { + buffer = ((FormattedHeader) header).getBuffer(); + cursor = new ParserCursor( + ((FormattedHeader) header).getValuePos(), + buffer.length()); + } else { + String s = header.getValue(); + if (s == null) { + throw new MalformedCookieException("Header value is null"); + } + buffer = new CharArrayBuffer(s.length()); + buffer.append(s); + cursor = new ParserCursor(0, buffer.length()); + } + elems = new HeaderElement[] { parser.parseHeader(buffer, cursor) }; } else { elems = header.getElements(); } diff --git a/module-client/src/main/java/org/apache/http/impl/cookie/NetscapeDraftHeaderParser.java b/module-client/src/main/java/org/apache/http/impl/cookie/NetscapeDraftHeaderParser.java new file mode 100644 index 000000000..fce91a567 --- /dev/null +++ b/module-client/src/main/java/org/apache/http/impl/cookie/NetscapeDraftHeaderParser.java @@ -0,0 +1,78 @@ +/* + * $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 + * . + * + */ + +package org.apache.http.impl.cookie; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.http.HeaderElement; +import org.apache.http.NameValuePair; +import org.apache.http.ParseException; +import org.apache.http.message.BasicHeaderElement; +import org.apache.http.message.BasicHeaderValueParser; +import org.apache.http.message.ParserCursor; +import org.apache.http.util.CharArrayBuffer; + +public class NetscapeDraftHeaderParser { + + public final static NetscapeDraftHeaderParser DEFAULT = new NetscapeDraftHeaderParser(); + + private final static char[] DELIMITERS = new char[] { ';' }; + + private final BasicHeaderValueParser nvpParser; + + public NetscapeDraftHeaderParser() { + super(); + this.nvpParser = BasicHeaderValueParser.DEFAULT; + } + + public HeaderElement parseHeader( + final CharArrayBuffer buffer, + final ParserCursor cursor) throws ParseException { + if (buffer == null) { + throw new IllegalArgumentException("Char array buffer may not be null"); + } + if (cursor == null) { + throw new IllegalArgumentException("Parser cursor may not be null"); + } + NameValuePair nvp = this.nvpParser.parseNameValuePair(buffer, cursor, DELIMITERS); + List params = new ArrayList(); + while (!cursor.atEnd()) { + NameValuePair param = this.nvpParser.parseNameValuePair(buffer, cursor, DELIMITERS); + params.add(param); + } + return new BasicHeaderElement( + nvp.getName(), + nvp.getValue(), (NameValuePair[]) params.toArray(new NameValuePair[params.size()])); + } + +} diff --git a/module-client/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java b/module-client/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java index efef018b6..e9f64b45c 100644 --- a/module-client/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java +++ b/module-client/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java @@ -31,6 +31,7 @@ package org.apache.http.impl.cookie; +import org.apache.http.FormattedHeader; import org.apache.http.Header; import org.apache.http.HeaderElement; import org.apache.http.cookie.ClientCookie; @@ -38,8 +39,8 @@ import org.apache.http.cookie.Cookie; import org.apache.http.cookie.CookieOrigin; import org.apache.http.cookie.MalformedCookieException; import org.apache.http.cookie.SM; -import org.apache.http.message.BasicHeaderValueParser; import org.apache.http.message.BufferedHeader; +import org.apache.http.message.ParserCursor; import org.apache.http.util.CharArrayBuffer; /** @@ -105,8 +106,24 @@ public class NetscapeDraftSpec extends CookieSpecBase { if (origin == null) { throw new IllegalArgumentException("Cookie origin may not be null"); } - String headervalue = header.getValue(); - return parse(new HeaderElement[] { BasicHeaderValueParser.parseHeaderElement(headervalue, null) }, origin); + NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT; + CharArrayBuffer buffer; + ParserCursor cursor; + if (header instanceof FormattedHeader) { + buffer = ((FormattedHeader) header).getBuffer(); + cursor = new ParserCursor( + ((FormattedHeader) header).getValuePos(), + buffer.length()); + } else { + String s = header.getValue(); + if (s == null) { + throw new MalformedCookieException("Header value is null"); + } + buffer = new CharArrayBuffer(s.length()); + buffer.append(s); + cursor = new ParserCursor(0, buffer.length()); + } + return parse(new HeaderElement[] { parser.parseHeader(buffer, cursor) }, origin); } public Header[] formatCookies(final Cookie[] cookies) { diff --git a/module-client/src/test/java/org/apache/http/impl/cookie/TestAllCookieImpl.java b/module-client/src/test/java/org/apache/http/impl/cookie/TestAllCookieImpl.java index 6058a8191..0d0ae10d2 100644 --- a/module-client/src/test/java/org/apache/http/impl/cookie/TestAllCookieImpl.java +++ b/module-client/src/test/java/org/apache/http/impl/cookie/TestAllCookieImpl.java @@ -47,6 +47,7 @@ public class TestAllCookieImpl extends TestCase { suite.addTest(TestBasicCookieAttribHandlers.suite()); suite.addTest(TestNetscapeCookieAttribHandlers.suite()); suite.addTest(TestRFC2109CookieAttribHandlers.suite()); + suite.addTest(TestNetscapeDraftHeaderParser.suite()); suite.addTest(TestBrowserCompatSpec.suite()); suite.addTest(TestCookieNetscapeDraft.suite()); suite.addTest(TestCookieRFC2109Spec.suite()); diff --git a/module-client/src/test/java/org/apache/http/impl/cookie/TestNetscapeDraftHeaderParser.java b/module-client/src/test/java/org/apache/http/impl/cookie/TestNetscapeDraftHeaderParser.java new file mode 100644 index 000000000..f8b491f14 --- /dev/null +++ b/module-client/src/test/java/org/apache/http/impl/cookie/TestNetscapeDraftHeaderParser.java @@ -0,0 +1,96 @@ +/* + * $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 + * . + * + */ + +package org.apache.http.impl.cookie; + +import org.apache.http.HeaderElement; +import org.apache.http.NameValuePair; +import org.apache.http.message.ParserCursor; +import org.apache.http.util.CharArrayBuffer; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit tests for {@link NetscapeDraftHeaderParser}. + * + * @author Oleg Kalnichevski + */ +public class TestNetscapeDraftHeaderParser extends TestCase { + + public TestNetscapeDraftHeaderParser(String testName) { + super(testName); + } + + public static void main(String args[]) { + String[] testCaseName = { TestNetscapeDraftHeaderParser.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + public static Test suite() { + return new TestSuite(TestNetscapeDraftHeaderParser.class); + } + + public void testNetscapeCookieParsing() throws Exception { + NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT; + + String s = + "name = value; test; test1 = stuff,with,commas ; test2 = \"stuff; stuff\"; test3=\"stuff"; + CharArrayBuffer buffer = new CharArrayBuffer(16); + buffer.append(s); + ParserCursor cursor = new ParserCursor(0, s.length()); + + HeaderElement he = parser.parseHeader(buffer, cursor); + assertEquals("name", he.getName()); + assertEquals("value", he.getValue()); + NameValuePair[] params = he.getParameters(); + assertEquals("test", params[0].getName()); + assertEquals(null, params[0].getValue()); + assertEquals("test1", params[1].getName()); + assertEquals("stuff,with,commas", params[1].getValue()); + assertEquals("test2", params[2].getName()); + assertEquals("stuff; stuff", params[2].getValue()); + assertEquals("test3", params[3].getName()); + assertEquals("\"stuff", params[3].getValue()); + assertEquals(s.length(), cursor.getPos()); + assertTrue(cursor.atEnd()); + + s = " "; + buffer = new CharArrayBuffer(16); + buffer.append(s); + cursor = new ParserCursor(0, s.length()); + he = parser.parseHeader(buffer, cursor); + assertEquals("", he.getName()); + assertEquals(null, he.getValue()); + } + +}