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.hc.core5.http.message;
29  
30  import java.io.Serializable;
31  
32  import org.apache.hc.core5.http.Chars;
33  import org.apache.hc.core5.http.FormattedHeader;
34  import org.apache.hc.core5.http.ParseException;
35  import org.apache.hc.core5.util.Args;
36  import org.apache.hc.core5.util.CharArrayBuffer;
37  import org.apache.hc.core5.util.Tokenizer;
38  
39  /**
40   * This class represents a raw HTTP header whose content is parsed 'on demand'
41   * only when the header value needs to be consumed.
42   *
43   * @since 4.0
44   */
45  public class BufferedHeader implements FormattedHeader, Serializable {
46  
47      private static final long serialVersionUID = -2768352615787625448L;
48  
49      /**
50       * Header name.
51       */
52      private final String name;
53  
54      /**
55       * The buffer containing the entire header line.
56       */
57      private final CharArrayBuffer buffer;
58  
59      /**
60       * The beginning of the header value in the buffer
61       */
62      private final int valuePos;
63  
64      private String value;
65  
66      /**
67       * @since 5.0
68       */
69      public static BufferedHeader create(final CharArrayBuffer buffer) {
70          try {
71              return new BufferedHeader(buffer);
72          } catch (final ParseException ex) {
73              throw new IllegalArgumentException(ex.getMessage());
74          }
75      }
76  
77      /**
78       * Creates a new header from a buffer.
79       * The name of the header will be parsed immediately,
80       * the value only if it is accessed.
81       *
82       * @param buffer    the buffer containing the header to represent
83       *
84       * @throws ParseException   in case of a parse error
85       */
86      public BufferedHeader(final CharArrayBuffer buffer) throws ParseException {
87          this(buffer, true);
88      }
89  
90      BufferedHeader(final CharArrayBuffer buffer, final boolean strict) throws ParseException {
91          super();
92          Args.notNull(buffer, "Char array buffer");
93          final int colon = buffer.indexOf(':');
94          if (colon <= 0) {
95              throw new ParseException("Invalid header", buffer, 0, buffer.length());
96          }
97          if (strict && Tokenizer.isWhitespace(buffer.charAt(colon - 1))) {
98              throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon - 1);
99          }
100         final String s = buffer.substringTrimmed(0, colon);
101         if (s.isEmpty()) {
102             throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon);
103         }
104         this.buffer = buffer;
105         this.name = s;
106         this.valuePos = colon + 1;
107     }
108 
109     @Override
110     public String getName() {
111         return this.name;
112     }
113 
114     @Override
115     public String getValue() {
116         if (value == null) {
117             int beginIdx = valuePos;
118             int endIdx = buffer.length();
119             while (beginIdx < buffer.length() && Tokenizer.isWhitespace(buffer.charAt(beginIdx))) {
120                 beginIdx++;
121             }
122             while (endIdx > beginIdx && Tokenizer.isWhitespace(buffer.charAt(endIdx - 1))) {
123                 endIdx--;
124             }
125             final StringBuilder buf = new StringBuilder(endIdx - beginIdx);
126             for (int i = beginIdx; i < endIdx; i++) {
127                 final char ch = buffer.charAt(i);
128                 if (ch == Chars.CR || ch == Chars.LF || ch == Chars.NULL) {
129                     buf.append((char) Chars.SP);
130                 } else {
131                     buf.append(ch);
132                 }
133             }
134             value = buf.toString();
135         }
136         return value;
137     }
138 
139     @Override
140     public boolean isSensitive() {
141         return false;
142     }
143 
144     @Override
145     public int getValuePos() {
146         return this.valuePos;
147     }
148 
149     @Override
150     public CharArrayBuffer getBuffer() {
151         return this.buffer;
152     }
153 
154     @Override
155     public String toString() {
156         return this.buffer.toString();
157     }
158 
159 }