You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ro...@apache.org on 2007/08/26 17:11:24 UTC
svn commit: r569825 - in
/jakarta/httpcomponents/httpcore/trunk/module-main/src:
main/java/org/apache/http/message/ test/java/org/apache/http/message/
Author: rolandw
Date: Sun Aug 26 08:11:23 2007
New Revision: 569825
URL: http://svn.apache.org/viewvc?rev=569825&view=rev
Log:
new interface HeaderValueFormatter, moved static formatting from NameValuePair to default implementation, adjusted test cases
Added:
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java (with props)
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueFormatter.java (with props)
jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java (with props)
Modified:
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicNameValuePair.java
jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java
jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestNameValuePair.java
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java?rev=569825&r1=569824&r2=569825&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java Sun Aug 26 08:11:23 2007
@@ -185,12 +185,13 @@
buffer.append(element.getName());
if (element.getValue() != null) {
buffer.append("=");
- buffer.append(element.getValue());
+ buffer.append(element.getValue()); //@@@ quoting?
}
NameValuePair[] params = element.getParameters();
for (int i = 0; i < params.length; i++) {
buffer.append("; ");
- BasicNameValuePair.format(buffer, params[i], false);
+ BasicHeaderValueFormatter.DEFAULT.formatNameValuePair
+ (buffer, params[i], false);
}
}
Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java?rev=569825&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java Sun Aug 26 08:11:23 2007
@@ -0,0 +1,285 @@
+/*
+ * $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.message;
+
+import org.apache.http.NameValuePair;
+import org.apache.http.util.CharArrayBuffer;
+
+
+/**
+ * Basic implementation for formatting header value elements.
+ * Instances of this class are stateless and thread-safe.
+ * Derived classes are expected to maintain these properties.
+ *
+ * @author <a href="mailto:oleg at ural.com">Oleg Kalnichevski</a>
+ * @author and others
+ *
+ *
+ * <!-- empty lines above to avoid 'svn diff' context problems -->
+ * @version $Revision$
+ *
+ * @since 4.0
+ */
+public class BasicHeaderValueFormatter implements HeaderValueFormatter {
+
+ /**
+ * A default instance of this class, for use as default or fallback.
+ * Note that {@link BasicHeaderValueFormatter} is not a singleton, there
+ * can be many instances of the class itself and of derived classes.
+ * The instance here provides non-customized, default behavior.
+ */
+ public final static
+ BasicHeaderValueFormatter DEFAULT = new BasicHeaderValueFormatter();
+
+
+ /**
+ * Special characters that can be used as separators in HTTP parameters.
+ * These special characters MUST be in a quoted string to be used within
+ * a parameter value .
+ */
+ public final static String SEPARATORS = " ;,:@()<>\\\"/[]?={}\t";
+
+
+ /**
+ * Unsafe special characters that must be escaped using the backslash
+ * character
+ */
+ public final static String UNSAFE_CHARS = "\"\\";
+
+
+
+ // public default constructor
+
+
+ // throw new UnsupportedOperationException("@@@");
+
+
+
+ /**
+ * Formats a set of parameters.
+ *
+ * @param nvps the parameters to format
+ * @param quote <code>true</code> to always format with quoted values,
+ * <code>false</code> to use quotes only when necessary
+ * @param formatter the formatter to use, or <code>null</code>
+ * for the {@link #DEFAULT default}
+ *
+ * @return the formatted parameters
+ */
+ public final static
+ String formatParameters(final NameValuePair[] nvps,
+ final boolean quote,
+ HeaderValueFormatter formatter) {
+ if (formatter == null)
+ formatter = BasicHeaderValueFormatter.DEFAULT;
+ return formatter.formatParameters(null, nvps, quote).toString();
+ }
+
+
+ // non-javadoc, see interface HeaderValueFormatter
+ public CharArrayBuffer formatParameters(CharArrayBuffer buffer,
+ NameValuePair[] nvps,
+ boolean quote) {
+ if (nvps == null) {
+ throw new IllegalArgumentException
+ ("Parameters must not be null.");
+ }
+
+ int len = estimateParametersLen(nvps);
+ if (buffer == null) {
+ buffer = new CharArrayBuffer(len);
+ } else {
+ buffer.ensureCapacity(len);
+ }
+
+ for (int i = 0; i < nvps.length; i++) {
+ if (i > 0) {
+ buffer.append("; ");
+ }
+ formatNameValuePair(buffer, nvps[i], quote);
+ }
+
+ return buffer;
+ }
+
+
+ /**
+ * Estimates the length of formatted parameters.
+ *
+ * @param nvps the parameters to format, or <code>null</code>
+ *
+ * @return a length estimate, in number of characters
+ */
+ protected int estimateParametersLen(NameValuePair[] nvps) {
+ if ((nvps == null) || (nvps.length < 1))
+ return 0;
+
+ int result = (nvps.length-1) * 2; // "; " between the parameters
+ for (int i=0; i<nvps.length; i++) {
+ result += estimateNameValuePairLen(nvps[i]);
+ }
+
+ return result;
+ }
+
+
+ /**
+ * Formats a name-value pair.
+ *
+ * @param nvp the name-value pair to format
+ * @param quote <code>true</code> to always format with a quoted value,
+ * <code>false</code> to use quotes only when necessary
+ * @param formatter the formatter to use, or <code>null</code>
+ * for the {@link #DEFAULT default}
+ *
+ * @return the formatted name-value pair
+ */
+ public final static
+ String formatNameValuePair(final NameValuePair nvp,
+ final boolean quote,
+ HeaderValueFormatter formatter) {
+ if (formatter == null)
+ formatter = BasicHeaderValueFormatter.DEFAULT;
+ return formatter.formatNameValuePair(null, nvp, quote).toString();
+ }
+
+
+ // non-javadoc, see interface HeaderValueFormatter
+ public CharArrayBuffer formatNameValuePair(CharArrayBuffer buffer,
+ NameValuePair nvp,
+ boolean quote) {
+ if (nvp == null) {
+ throw new IllegalArgumentException
+ ("NameValuePair must not be null.");
+ }
+
+ int len = estimateNameValuePairLen(nvp);
+ if (buffer == null) {
+ buffer = new CharArrayBuffer(len);
+ } else {
+ buffer.ensureCapacity(len);
+ }
+
+ buffer.append(nvp.getName());
+ final String value = nvp.getValue();
+ if (value != null) {
+ buffer.append('=');
+ doFormatValue(buffer, value, quote);
+ }
+
+ return buffer;
+ }
+
+
+ /**
+ * Estimates the length of a formatted name-value pair.
+ *
+ * @param nvp the name-value pair to format, or <code>null</code>
+ *
+ * @return a length estimate, in number of characters
+ */
+ protected int estimateNameValuePairLen(NameValuePair nvp) {
+ if (nvp == null)
+ return 0;
+
+ int result = nvp.getName().length(); // name
+ final String value = nvp.getValue();
+ if (value != null) {
+ // assume quotes, but no escaped characters
+ result += 3 + value.length(); // ="value"
+ }
+ return result;
+ }
+
+
+ /**
+ * Actually formats the value of a name-value pair.
+ * Called from {@link #formatNameValuePair formatNameValuePair}.
+ *
+ * @param buffer the buffer to append to, never <code>null</code>
+ * @param value the value to append, never <code>null</code>
+ * @param quote <code>true</code> to always format with quotes,
+ * <code>false</code> to use quotes only when necessary
+ */
+ protected void doFormatValue(final CharArrayBuffer buffer,
+ final String value,
+ boolean quote) {
+
+ if (!quote) {
+ for (int i = 0; (i < value.length()) && !quote; i++) {
+ quote = isSeparator(value.charAt(i));
+ }
+ }
+
+ if (quote) {
+ buffer.append('"');
+ }
+ for (int i = 0; i < value.length(); i++) {
+ char ch = value.charAt(i);
+ if (isUnsafe(ch)) {
+ buffer.append('\\');
+ }
+ buffer.append(ch);
+ }
+ if (quote) {
+ buffer.append('"');
+ }
+ }
+
+
+ /**
+ * Checks whether a character is a {@link #SEPARATORS separator}.
+ *
+ * @param ch the character to check
+ *
+ * @return <code>true</code> if the character is a separator,
+ * <code>false</code> otherwise
+ */
+ protected boolean isSeparator(char ch) {
+ return SEPARATORS.indexOf(ch) >= 0;
+ }
+
+
+ /**
+ * Checks whether a character is {@link #UNSAFE_CHARS unsafe}.
+ *
+ * @param ch the character to check
+ *
+ * @return <code>true</code> if the character is unsafe,
+ * <code>false</code> otherwise
+ */
+ protected boolean isUnsafe(char ch) {
+ return UNSAFE_CHARS.indexOf(ch) >= 0;
+ }
+
+
+} // class BasicHeaderValueFormatter
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java?rev=569825&r1=569824&r2=569825&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java Sun Aug 26 08:11:23 2007
@@ -56,7 +56,7 @@
*
*
* <!-- empty lines above to avoid 'svn diff' context problems -->
- * @version $Revision$ $Date$
+ * @version $Revision$
*
* @since 4.0
*/
@@ -64,8 +64,8 @@
/**
* A default instance of this class, for use as default or fallback.
- * Note that {@link BasicLineParser} is not a singleton, there can
- * be many instances of the class itself and of derived classes.
+ * Note that {@link BasicHeaderValueParser} is not a singleton, there
+ * can be many instances of the class itself and of derived classes.
* The instance here provides non-customized, default behavior.
*/
public final static
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java?rev=569825&r1=569824&r2=569825&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java Sun Aug 26 08:11:23 2007
@@ -54,6 +54,7 @@
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
* @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ * @author and others
*
*
* <!-- empty lines above to avoid 'svn diff' context problems -->
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicNameValuePair.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicNameValuePair.java?rev=569825&r1=569824&r2=569825&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicNameValuePair.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicNameValuePair.java Sun Aug 26 08:11:23 2007
@@ -145,157 +145,6 @@
return this.value;
}
-
- /**
- * Special characters that can be used as separators in HTTP parameters.
- * These special characters MUST be in a quoted string to be used within
- * a parameter value
- */
- private static final char[] SEPARATORS = {
- '(', ')', '<', '>', '@',
- ',', ';', ':', '\\', '"',
- '/', '[', ']', '?', '=',
- '{', '}', ' ', '\t'
- };
-
- /**
- * Unsafe special characters that must be escaped using the backslash
- * character
- */
- private static final char[] UNSAFE_CHARS = {
- '"', '\\'
- };
-
- private static boolean isOneOf(char[] chars, char ch) {
- for (int i = 0; i < chars.length; i++) {
- if (ch == chars[i]) {
- return true;
- }
- }
- return false;
- }
-
- private static boolean isUnsafeChar(char ch) {
- return isOneOf(UNSAFE_CHARS, ch);
- }
-
- private static boolean isSeparator(char ch) {
- return isOneOf(SEPARATORS, ch);
- }
-
- private static void format(
- final CharArrayBuffer buffer,
- final String value,
- boolean alwaysUseQuotes) {
- boolean unsafe = false;
- if (alwaysUseQuotes) {
- unsafe = true;
- } else {
- for (int i = 0; i < value.length(); i++) {
- if (isSeparator(value.charAt(i))) {
- unsafe = true;
- break;
- }
- }
- }
- if (unsafe) buffer.append('"');
- for (int i = 0; i < value.length(); i++) {
- char ch = value.charAt(i);
- if (isUnsafeChar(ch)) {
- buffer.append('\\');
- }
- buffer.append(ch);
- }
- if (unsafe) buffer.append('"');
- }
-
- /**
- * Produces textual representaion of the attribute/value pair using
- * formatting rules defined in RFC 2616
- *
- * @param buffer output buffer
- * @param param the parameter to be formatted
- * @param alwaysUseQuotes <tt>true</tt> if the parameter values must
- * always be enclosed in quotation marks, <tt>false</tt> otherwise
- */
- public static void format(
- final CharArrayBuffer buffer,
- final NameValuePair param,
- boolean alwaysUseQuotes) {
- if (buffer == null) {
- throw new IllegalArgumentException("String buffer may not be null");
- }
- if (param == null) {
- throw new IllegalArgumentException("Parameter may not be null");
- }
- buffer.append(param.getName());
- String value = param.getValue();
- if (value != null) {
- buffer.append("=");
- format(buffer, value, alwaysUseQuotes);
- }
- }
-
- /**
- * Produces textual representaion of the attribute/value pairs using
- * formatting rules defined in RFC 2616
- *
- * @param buffer output buffer
- * @param params the parameters to be formatted
- * @param alwaysUseQuotes <tt>true</tt> if the parameter values must
- * always be enclosed in quotation marks, <tt>false</tt> otherwise
- */
- public static void formatAll(
- final CharArrayBuffer buffer,
- final NameValuePair[] params,
- boolean alwaysUseQuotes) {
- if (buffer == null) {
- throw new IllegalArgumentException("String buffer may not be null");
- }
- if (params == null) {
- throw new IllegalArgumentException("Array of parameter may not be null");
- }
- for (int i = 0; i < params.length; i++) {
- if (i > 0) {
- buffer.append("; ");
- }
- format(buffer, params[i], alwaysUseQuotes);
- }
- }
-
- /**
- * Produces textual representaion of the attribute/value pair using
- * formatting rules defined in RFC 2616
- *
- * @param param the parameter to be formatted
- * @param alwaysUseQuotes <tt>true</tt> if the parameter values must
- * always be enclosed in quotation marks, <tt>false</tt> otherwise
- *
- * @return RFC 2616 conformant textual representaion of the
- * attribute/value pair
- */
- public static String format(final NameValuePair param, boolean alwaysUseQuotes) {
- CharArrayBuffer buffer = new CharArrayBuffer(16);
- format(buffer, param, alwaysUseQuotes);
- return buffer.toString();
- }
-
- /**
- * Produces textual representaion of the attribute/value pair using
- * formatting rules defined in RFC 2616
- *
- * @param params the parameters to be formatted
- * @param alwaysUseQuotes <tt>true</tt> if the parameter values must
- * always be enclosed in quotation marks, <tt>false</tt> otherwise
- *
- * @return RFC 2616 conformant textual representaion of the
- * attribute/value pair
- */
- public static String formatAll(final NameValuePair[] params, boolean alwaysUseQuotes) {
- CharArrayBuffer buffer = new CharArrayBuffer(16);
- formatAll(buffer, params, alwaysUseQuotes);
- return buffer.toString();
- }
/**
* Get a string representation of this pair.
@@ -303,11 +152,17 @@
* @return A string representation.
*/
public String toString() {
- CharArrayBuffer buffer = new CharArrayBuffer(16);
+ // don't call complex default formatting for a simple toString
+
+ int len = this.name.length();
+ if (this.value != null)
+ len += 1 + this.value.length();
+ CharArrayBuffer buffer = new CharArrayBuffer(len);
+
buffer.append(this.name);
if (this.value != null) {
buffer.append("=");
- format(buffer, this.value, false);
+ buffer.append(this.value);
}
return buffer.toString();
}
Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueFormatter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueFormatter.java?rev=569825&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueFormatter.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueFormatter.java Sun Aug 26 08:11:23 2007
@@ -0,0 +1,252 @@
+/*
+ * $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.message;
+
+
+import org.apache.http.HeaderElement;
+import org.apache.http.NameValuePair;
+import org.apache.http.ParseException;
+import org.apache.http.util.CharArrayBuffer;
+
+
+
+/**
+ * Interface for formatting elements of a header value.
+ * This is the complement to {@link HeaderValueParser}.
+ * Instances of this interface are expected to be stateless and thread-safe.
+ *
+ * <p>
+ * All formatting methods accept an optional buffer argument.
+ * If a buffer is passed in, the formatted element will be appended
+ * and the modified buffer is returned. If no buffer is passed in,
+ * a new buffer will be created and filled with the formatted element.
+ * In both cases, the caller is allowed to modify the returned buffer.
+ * </p>
+ *
+ *
+ * <!-- empty lines above to avoid 'svn diff' context problems -->
+ * @version $Revision$
+ *
+ * @since 4.0
+ */
+public interface HeaderValueFormatter {
+
+ /* *
+ * Parses a header value into elements.
+ * Parse errors are indicated as <code>RuntimeException</code>.
+ * <p>
+ * Some HTTP headers (such as the set-cookie header) have values that
+ * can be decomposed into multiple elements. In order to be processed
+ * by this parser, such headers must be in the following form:
+ * </p>
+ * <pre>
+ * header = [ element ] *( "," [ element ] )
+ * element = name [ "=" [ value ] ] *( ";" [ param ] )
+ * param = name [ "=" [ value ] ]
+ *
+ * name = token
+ * value = ( token | quoted-string )
+ *
+ * token = 1*<any char except "=", ",", ";", <"> and
+ * white space>
+ * quoted-string = <"> *( text | quoted-char ) <">
+ * text = any char except <">
+ * quoted-char = "\" char
+ * </pre>
+ * <p>
+ * Any amount of white space is allowed between any part of the
+ * header, element or param and is ignored. A missing value in any
+ * element or param will be stored as the empty {@link String};
+ * if the "=" is also missing <var>null</var> will be stored instead.
+ * </p>
+ *
+ * @param buffer buffer holding the header value to parse
+ *
+ * @return an array holding all elements of the header value
+ *
+ * @throws ParseException in case of a parse error
+ * /
+ HeaderElement[] parseElements(CharArrayBuffer buffer,
+ int indexFrom,
+ int indexTo)
+ throws ParseException
+ ;
+ */
+
+
+ /* *
+ * Parses a single header element.
+ * A header element consist of a semicolon-separate list
+ * of name=value definitions.
+ *
+ * @param buffer buffer holding the element to parse
+ *
+ * @return the parsed element
+ *
+ * @throws ParseException in case of a parse error
+ * /
+ HeaderElement parseHeaderElement(CharArrayBuffer buffer,
+ int indexFrom,
+ int indexTo)
+ throws ParseException
+ ;
+ */
+
+ /* *
+ * Parses a list of name-value pairs.
+ * These lists are used to specify parameters to a header element.
+ * Parse errors are indicated as <code>RuntimeException</code>.
+ * <p>
+ * This method comforms to the generic grammar and formatting rules
+ * outlined in the
+ * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2"
+ * >Section 2.2</a>
+ * and
+ * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6"
+ * >Section 3.6</a>
+ * of
+ * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.txt">RFC 2616</a>.
+ * </p>
+ * <h>2.2 Basic Rules</h>
+ * <p>
+ * The following rules are used throughout this specification to
+ * describe basic parsing constructs.
+ * The US-ASCII coded character set is defined by ANSI X3.4-1986.
+ * </p>
+ * <pre>
+ * OCTET = <any 8-bit sequence of data>
+ * CHAR = <any US-ASCII character (octets 0 - 127)>
+ * UPALPHA = <any US-ASCII uppercase letter "A".."Z">
+ * LOALPHA = <any US-ASCII lowercase letter "a".."z">
+ * ALPHA = UPALPHA | LOALPHA
+ * DIGIT = <any US-ASCII digit "0".."9">
+ * CTL = <any US-ASCII control character
+ * (octets 0 - 31) and DEL (127)>
+ * CR = <US-ASCII CR, carriage return (13)>
+ * LF = <US-ASCII LF, linefeed (10)>
+ * SP = <US-ASCII SP, space (32)>
+ * HT = <US-ASCII HT, horizontal-tab (9)>
+ * <"> = <US-ASCII double-quote mark (34)>
+ * </pre>
+ * <p>
+ * Many HTTP/1.1 header field values consist of words separated
+ * by LWS or special characters. These special characters MUST be
+ * in a quoted string to be used within
+ * a parameter value (as defined in section 3.6).
+ * <p>
+ * <pre>
+ * token = 1*<any CHAR except CTLs or separators>
+ * separators = "(" | ")" | "<" | ">" | "@"
+ * | "," | ";" | ":" | "\" | <">
+ * | "/" | "[" | "]" | "?" | "="
+ * | "{" | "}" | SP | HT
+ * </pre>
+ * <p>
+ * A string of text is parsed as a single word if it is quoted using
+ * double-quote marks.
+ * </p>
+ * <pre>
+ * quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
+ * qdtext = <any TEXT except <">>
+ * </pre>
+ * <p>
+ * The backslash character ("\") MAY be used as a single-character
+ * quoting mechanism only within quoted-string and comment constructs.
+ * </p>
+ * <pre>
+ * quoted-pair = "\" CHAR
+ * </pre>
+ * <h>3.6 Transfer Codings</h>
+ * <p>
+ * Parameters are in the form of attribute/value pairs.
+ * </p>
+ * <pre>
+ * parameter = attribute "=" value
+ * attribute = token
+ * value = token | quoted-string
+ * </pre>
+ *
+ * @param buffer buffer holding the name-value list to parse
+ *
+ * @return an array holding all items of the name-value list
+ *
+ * @throws ParseException in case of a parse error
+ * /
+ NameValuePair[] parseParameters(CharArrayBuffer buffer,
+ int indexFrom,
+ int indexTo)
+ throws ParseException
+ ;
+ */
+
+
+ /**
+ * Formats the parameters of a header element.
+ * That's a list of name-value pairs, to be separated by semicolons.
+ * This method will <i>not</i> generate a leading semicolon.
+ *
+ * @param buffer the buffer to append to, or
+ * <code>null</code> to create a new buffer
+ * @param nvps the parameters (name-value pairs) to format
+ * @param quote <code>true</code> to always format with quoted values,
+ * <code>false</code> to use quotes only when necessary
+ *
+ * @return a buffer with the formatted parameters.
+ * If the <code>buffer</code> argument was not <code>null</code>,
+ * that buffer will be returned.
+ */
+ CharArrayBuffer formatParameters(CharArrayBuffer buffer,
+ NameValuePair[] nvps,
+ boolean quote)
+ ;
+
+
+ /**
+ * Formats one name-value pair, where the value is optional.
+ *
+ * @param buffer the buffer to append to, or
+ * <code>null</code> to create a new buffer
+ * @param nvp the name-value pair to format
+ * @param quote <code>true</code> to always format with a quoted value,
+ * <code>false</code> to use quotes only when necessary
+ *
+ * @return a buffer with the formatted name-value pair.
+ * If the <code>buffer</code> argument was not <code>null</code>,
+ * that buffer will be returned.
+ */
+ CharArrayBuffer formatNameValuePair(CharArrayBuffer buffer,
+ NameValuePair nvp,
+ boolean quote)
+ ;
+
+}
+
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueFormatter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueFormatter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueFormatter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java?rev=569825&r1=569824&r2=569825&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java Sun Aug 26 08:11:23 2007
@@ -44,6 +44,7 @@
suite.addTest(TestHeader.suite());
suite.addTest(TestHeaderElement.suite());
suite.addTest(TestBasicHeaderValueParser.suite());
+ suite.addTest(TestBasicHeaderValueFormatter.suite());
suite.addTest(TestStatusLine.suite());
suite.addTest(TestRequestLine.suite());
suite.addTest(TestBasicLineParser.suite());
Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java?rev=569825&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java Sun Aug 26 08:11:23 2007
@@ -0,0 +1,160 @@
+/*
+ * $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/>.
+ *
+ * [Additional notices, if required by prior licensing conditions]
+ *
+ */
+
+package org.apache.http.message;
+
+import org.apache.http.HeaderElement;
+import org.apache.http.NameValuePair;
+import org.apache.http.util.CharArrayBuffer;
+
+import junit.framework.*;
+
+/**
+ * Tests for header value formatting.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ * @author and others
+ *
+ * @version $Revision$
+ */
+public class TestBasicHeaderValueFormatter extends TestCase {
+
+ // ------------------------------------------------------------ Constructor
+ public TestBasicHeaderValueFormatter(String testName) {
+ super(testName);
+ }
+
+ // ------------------------------------------------------------------- Main
+ public static void main(String args[]) {
+ String[] testCaseName = {
+ TestBasicHeaderValueFormatter.class.getName() };
+ junit.textui.TestRunner.main(testCaseName);
+ }
+
+ // ------------------------------------------------------- TestCase Methods
+
+ public static Test suite() {
+ return new TestSuite(TestBasicHeaderValueFormatter.class);
+ }
+
+
+
+ public void testNVPFormatting() throws Exception {
+ NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff");
+ NameValuePair param2 = new BasicNameValuePair("param", "this\\that");
+ NameValuePair param3 = new BasicNameValuePair("param", "this,that");
+ NameValuePair param4 = new BasicNameValuePair("param", "quote marks (\") must be escaped");
+ NameValuePair param5 = new BasicNameValuePair("param", "back slash (\\) must be escaped too");
+ NameValuePair param6 = new BasicNameValuePair("param", "values with\tblanks must always be quoted");
+ NameValuePair param7 = new BasicNameValuePair("param", null);
+
+
+ assertEquals("param=regular_stuff",
+ BasicHeaderValueFormatter.formatNameValuePair
+ (param1, false, null));
+ assertEquals("param=\"this\\\\that\"",
+ BasicHeaderValueFormatter.formatNameValuePair
+ (param2, false, null));
+ assertEquals("param=\"this,that\"",
+ BasicHeaderValueFormatter.formatNameValuePair
+ (param3, false, null));
+ assertEquals("param=\"quote marks (\\\") must be escaped\"",
+ BasicHeaderValueFormatter.formatNameValuePair
+ (param4, false, null));
+ assertEquals("param=\"back slash (\\\\) must be escaped too\"",
+ BasicHeaderValueFormatter.formatNameValuePair
+ (param5, false, null));
+ assertEquals("param=\"values with\tblanks must always be quoted\"",
+ BasicHeaderValueFormatter.formatNameValuePair
+ (param6, false, null));
+ assertEquals("param", BasicHeaderValueFormatter.formatNameValuePair
+ (param7, false, null));
+
+ assertEquals("param=\"regular_stuff\"",
+ BasicHeaderValueFormatter.formatNameValuePair
+ (param1, true, null));
+ assertEquals("param=\"this\\\\that\"",
+ BasicHeaderValueFormatter.formatNameValuePair
+ (param2, true, null));
+ assertEquals("param=\"this,that\"",
+ BasicHeaderValueFormatter.formatNameValuePair
+ (param3, true, null));
+ assertEquals("param=\"quote marks (\\\") must be escaped\"",
+ BasicHeaderValueFormatter.formatNameValuePair
+ (param4, true, null));
+ assertEquals("param=\"back slash (\\\\) must be escaped too\"",
+ BasicHeaderValueFormatter.formatNameValuePair
+ (param5, true, null));
+ assertEquals("param=\"values with\tblanks must always be quoted\"",
+ BasicHeaderValueFormatter.formatNameValuePair
+ (param6, true, null));
+ assertEquals("param",
+ BasicHeaderValueFormatter.formatNameValuePair
+ (param7, false, null));
+ }
+
+
+
+ public void testParamsFormatting() throws Exception {
+ NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff");
+ NameValuePair param2 = new BasicNameValuePair("param", "this\\that");
+ NameValuePair param3 = new BasicNameValuePair("param", "this,that");
+ NameValuePair[] params = new NameValuePair[] {param1, param2, param3};
+ assertEquals("param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\"",
+ BasicHeaderValueFormatter.formatParameters(params, false, null));
+ assertEquals("param=\"regular_stuff\"; param=\"this\\\\that\"; param=\"this,that\"",
+ BasicHeaderValueFormatter.formatParameters(params, true, null));
+ }
+
+
+ public void testInvalidNullArguments() throws Exception {
+
+ try {
+ BasicHeaderValueFormatter.formatNameValuePair
+ ((NameValuePair) null, true, null);
+ fail("IllegalArgumentException should habe been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+
+ try {
+ BasicHeaderValueFormatter.formatParameters
+ ((NameValuePair[]) null, true,
+ BasicHeaderValueFormatter.DEFAULT);
+ fail("IllegalArgumentException should habe been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java?rev=569825&r1=569824&r2=569825&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java Sun Aug 26 08:11:23 2007
@@ -1,5 +1,5 @@
/*
- * $Header$
+ * $HeadURL$
* $Revision$
* $Date$
* ====================================================================
@@ -44,7 +44,7 @@
* @author Rodney Waldhoff
* @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
* @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
- * @author <a href="mailto:oleg at ural.ru">oleg Kalnichevski</a>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
* @author and others
* @version $Id$
*/
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestNameValuePair.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestNameValuePair.java?rev=569825&r1=569824&r2=569825&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestNameValuePair.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestNameValuePair.java Sun Aug 26 08:11:23 2007
@@ -100,7 +100,7 @@
NameValuePair param2 = new BasicNameValuePair("name1", null);
assertEquals("name1", param2.toString());
}
-
+/*
public void testBasicFormatting() throws Exception {
NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff");
NameValuePair param2 = new BasicNameValuePair("param", "this\\that");
@@ -164,5 +164,5 @@
// expected
}
}
-
+*/
}