View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.message;
29  
30  import org.apache.http.HeaderElement;
31  import org.apache.http.NameValuePair;
32  import org.apache.http.ParseException;
33  import org.apache.http.util.CharArrayBuffer;
34  
35  /**
36   * Interface for parsing header values into elements.
37   * Instances of this interface are expected to be stateless and thread-safe.
38   *
39   * @since 4.0
40   */
41  public interface HeaderValueParser {
42  
43      /**
44       * Parses a header value into elements.
45       * Parse errors are indicated as <code>RuntimeException</code>.
46       * <p>
47       * Some HTTP headers (such as the set-cookie header) have values that
48       * can be decomposed into multiple elements. In order to be processed
49       * by this parser, such headers must be in the following form:
50       * </p>
51       * <pre>
52       * header  = [ element ] *( "," [ element ] )
53       * element = name [ "=" [ value ] ] *( ";" [ param ] )
54       * param   = name [ "=" [ value ] ]
55       *
56       * name    = token
57       * value   = ( token | quoted-string )
58       *
59       * token         = 1*&lt;any char except "=", ",", ";", &lt;"&gt; and
60       *                       white space&gt;
61       * quoted-string = &lt;"&gt; *( text | quoted-char ) &lt;"&gt;
62       * text          = any char except &lt;"&gt;
63       * quoted-char   = "\" char
64       * </pre>
65       * <p>
66       * Any amount of white space is allowed between any part of the
67       * header, element or param and is ignored. A missing value in any
68       * element or param will be stored as the empty {@link String};
69       * if the "=" is also missing <var>null</var> will be stored instead.
70       * </p>
71       *
72       * @param buffer    buffer holding the header value to parse
73       * @param cursor    the parser cursor containing the current position and
74       *                  the bounds within the buffer for the parsing operation
75       *
76       * @return  an array holding all elements of the header value
77       *
78       * @throws ParseException        in case of a parse error
79       */
80      HeaderElement[] parseElements(
81              CharArrayBuffer buffer,
82              ParserCursor cursor) throws ParseException;
83  
84      /**
85       * Parses a single header element.
86       * A header element consist of a semicolon-separate list
87       * of name=value definitions.
88       *
89       * @param buffer    buffer holding the element to parse
90       * @param cursor    the parser cursor containing the current position and
91       *                  the bounds within the buffer for the parsing operation
92       *
93       * @return  the parsed element
94       *
95       * @throws ParseException        in case of a parse error
96       */
97      HeaderElement parseHeaderElement(
98              CharArrayBuffer buffer,
99              ParserCursor cursor) throws ParseException;
100 
101     /**
102      * Parses a list of name-value pairs.
103      * These lists are used to specify parameters to a header element.
104      * Parse errors are indicated as <code>ParseException</code>.
105      *
106      * @param buffer    buffer holding the name-value list to parse
107      * @param cursor    the parser cursor containing the current position and
108      *                  the bounds within the buffer for the parsing operation
109      *
110      * @return  an array holding all items of the name-value list
111      *
112      * @throws ParseException        in case of a parse error
113      */
114     NameValuePair[] parseParameters(
115             CharArrayBuffer buffer,
116             ParserCursor cursor) throws ParseException;
117 
118 
119     /**
120      * Parses a name=value specification, where the = and value are optional.
121      *
122      * @param buffer    the buffer holding the name-value pair to parse
123      * @param cursor    the parser cursor containing the current position and
124      *                  the bounds within the buffer for the parsing operation
125      *
126      * @return  the name-value pair, where the value is <code>null</code>
127      *          if no value is specified
128      */
129     NameValuePair parseNameValuePair(
130             CharArrayBuffer buffer,
131             ParserCursor cursor) throws ParseException;
132 
133 }
134