1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/message/BasicHeaderValueParser.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,403 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.message; 1.32 + 1.33 +import java.util.List; 1.34 +import java.util.ArrayList; 1.35 + 1.36 +import ch.boye.httpclientandroidlib.HeaderElement; 1.37 +import ch.boye.httpclientandroidlib.NameValuePair; 1.38 +import ch.boye.httpclientandroidlib.ParseException; 1.39 +import ch.boye.httpclientandroidlib.protocol.HTTP; 1.40 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer; 1.41 + 1.42 +/** 1.43 + * Basic implementation for parsing header values into elements. 1.44 + * Instances of this class are stateless and thread-safe. 1.45 + * Derived classes are expected to maintain these properties. 1.46 + * 1.47 + * @since 4.0 1.48 + */ 1.49 +public class BasicHeaderValueParser implements HeaderValueParser { 1.50 + 1.51 + /** 1.52 + * A default instance of this class, for use as default or fallback. 1.53 + * Note that {@link BasicHeaderValueParser} is not a singleton, there 1.54 + * can be many instances of the class itself and of derived classes. 1.55 + * The instance here provides non-customized, default behavior. 1.56 + */ 1.57 + public final static 1.58 + BasicHeaderValueParser DEFAULT = new BasicHeaderValueParser(); 1.59 + 1.60 + private final static char PARAM_DELIMITER = ';'; 1.61 + private final static char ELEM_DELIMITER = ','; 1.62 + private final static char[] ALL_DELIMITERS = new char[] { 1.63 + PARAM_DELIMITER, 1.64 + ELEM_DELIMITER 1.65 + }; 1.66 + 1.67 + // public default constructor 1.68 + 1.69 + 1.70 + /** 1.71 + * Parses elements with the given parser. 1.72 + * 1.73 + * @param value the header value to parse 1.74 + * @param parser the parser to use, or <code>null</code> for default 1.75 + * 1.76 + * @return array holding the header elements, never <code>null</code> 1.77 + */ 1.78 + public final static 1.79 + HeaderElement[] parseElements(final String value, 1.80 + HeaderValueParser parser) 1.81 + throws ParseException { 1.82 + 1.83 + if (value == null) { 1.84 + throw new IllegalArgumentException 1.85 + ("Value to parse may not be null"); 1.86 + } 1.87 + 1.88 + if (parser == null) 1.89 + parser = BasicHeaderValueParser.DEFAULT; 1.90 + 1.91 + CharArrayBuffer buffer = new CharArrayBuffer(value.length()); 1.92 + buffer.append(value); 1.93 + ParserCursor cursor = new ParserCursor(0, value.length()); 1.94 + return parser.parseElements(buffer, cursor); 1.95 + } 1.96 + 1.97 + 1.98 + // non-javadoc, see interface HeaderValueParser 1.99 + public HeaderElement[] parseElements(final CharArrayBuffer buffer, 1.100 + final ParserCursor cursor) { 1.101 + 1.102 + if (buffer == null) { 1.103 + throw new IllegalArgumentException("Char array buffer may not be null"); 1.104 + } 1.105 + if (cursor == null) { 1.106 + throw new IllegalArgumentException("Parser cursor may not be null"); 1.107 + } 1.108 + 1.109 + List elements = new ArrayList(); 1.110 + while (!cursor.atEnd()) { 1.111 + HeaderElement element = parseHeaderElement(buffer, cursor); 1.112 + if (!(element.getName().length() == 0 && element.getValue() == null)) { 1.113 + elements.add(element); 1.114 + } 1.115 + } 1.116 + return (HeaderElement[]) 1.117 + elements.toArray(new HeaderElement[elements.size()]); 1.118 + } 1.119 + 1.120 + 1.121 + /** 1.122 + * Parses an element with the given parser. 1.123 + * 1.124 + * @param value the header element to parse 1.125 + * @param parser the parser to use, or <code>null</code> for default 1.126 + * 1.127 + * @return the parsed header element 1.128 + */ 1.129 + public final static 1.130 + HeaderElement parseHeaderElement(final String value, 1.131 + HeaderValueParser parser) 1.132 + throws ParseException { 1.133 + 1.134 + if (value == null) { 1.135 + throw new IllegalArgumentException 1.136 + ("Value to parse may not be null"); 1.137 + } 1.138 + 1.139 + if (parser == null) 1.140 + parser = BasicHeaderValueParser.DEFAULT; 1.141 + 1.142 + CharArrayBuffer buffer = new CharArrayBuffer(value.length()); 1.143 + buffer.append(value); 1.144 + ParserCursor cursor = new ParserCursor(0, value.length()); 1.145 + return parser.parseHeaderElement(buffer, cursor); 1.146 + } 1.147 + 1.148 + 1.149 + // non-javadoc, see interface HeaderValueParser 1.150 + public HeaderElement parseHeaderElement(final CharArrayBuffer buffer, 1.151 + final ParserCursor cursor) { 1.152 + 1.153 + if (buffer == null) { 1.154 + throw new IllegalArgumentException("Char array buffer may not be null"); 1.155 + } 1.156 + if (cursor == null) { 1.157 + throw new IllegalArgumentException("Parser cursor may not be null"); 1.158 + } 1.159 + 1.160 + NameValuePair nvp = parseNameValuePair(buffer, cursor); 1.161 + NameValuePair[] params = null; 1.162 + if (!cursor.atEnd()) { 1.163 + char ch = buffer.charAt(cursor.getPos() - 1); 1.164 + if (ch != ELEM_DELIMITER) { 1.165 + params = parseParameters(buffer, cursor); 1.166 + } 1.167 + } 1.168 + return createHeaderElement(nvp.getName(), nvp.getValue(), params); 1.169 + } 1.170 + 1.171 + 1.172 + /** 1.173 + * Creates a header element. 1.174 + * Called from {@link #parseHeaderElement}. 1.175 + * 1.176 + * @return a header element representing the argument 1.177 + */ 1.178 + protected HeaderElement createHeaderElement( 1.179 + final String name, 1.180 + final String value, 1.181 + final NameValuePair[] params) { 1.182 + return new BasicHeaderElement(name, value, params); 1.183 + } 1.184 + 1.185 + 1.186 + /** 1.187 + * Parses parameters with the given parser. 1.188 + * 1.189 + * @param value the parameter list to parse 1.190 + * @param parser the parser to use, or <code>null</code> for default 1.191 + * 1.192 + * @return array holding the parameters, never <code>null</code> 1.193 + */ 1.194 + public final static 1.195 + NameValuePair[] parseParameters(final String value, 1.196 + HeaderValueParser parser) 1.197 + throws ParseException { 1.198 + 1.199 + if (value == null) { 1.200 + throw new IllegalArgumentException 1.201 + ("Value to parse may not be null"); 1.202 + } 1.203 + 1.204 + if (parser == null) 1.205 + parser = BasicHeaderValueParser.DEFAULT; 1.206 + 1.207 + CharArrayBuffer buffer = new CharArrayBuffer(value.length()); 1.208 + buffer.append(value); 1.209 + ParserCursor cursor = new ParserCursor(0, value.length()); 1.210 + return parser.parseParameters(buffer, cursor); 1.211 + } 1.212 + 1.213 + 1.214 + 1.215 + // non-javadoc, see interface HeaderValueParser 1.216 + public NameValuePair[] parseParameters(final CharArrayBuffer buffer, 1.217 + final ParserCursor cursor) { 1.218 + 1.219 + if (buffer == null) { 1.220 + throw new IllegalArgumentException("Char array buffer may not be null"); 1.221 + } 1.222 + if (cursor == null) { 1.223 + throw new IllegalArgumentException("Parser cursor may not be null"); 1.224 + } 1.225 + 1.226 + int pos = cursor.getPos(); 1.227 + int indexTo = cursor.getUpperBound(); 1.228 + 1.229 + while (pos < indexTo) { 1.230 + char ch = buffer.charAt(pos); 1.231 + if (HTTP.isWhitespace(ch)) { 1.232 + pos++; 1.233 + } else { 1.234 + break; 1.235 + } 1.236 + } 1.237 + cursor.updatePos(pos); 1.238 + if (cursor.atEnd()) { 1.239 + return new NameValuePair[] {}; 1.240 + } 1.241 + 1.242 + List params = new ArrayList(); 1.243 + while (!cursor.atEnd()) { 1.244 + NameValuePair param = parseNameValuePair(buffer, cursor); 1.245 + params.add(param); 1.246 + char ch = buffer.charAt(cursor.getPos() - 1); 1.247 + if (ch == ELEM_DELIMITER) { 1.248 + break; 1.249 + } 1.250 + } 1.251 + 1.252 + return (NameValuePair[]) 1.253 + params.toArray(new NameValuePair[params.size()]); 1.254 + } 1.255 + 1.256 + /** 1.257 + * Parses a name-value-pair with the given parser. 1.258 + * 1.259 + * @param value the NVP to parse 1.260 + * @param parser the parser to use, or <code>null</code> for default 1.261 + * 1.262 + * @return the parsed name-value pair 1.263 + */ 1.264 + public final static 1.265 + NameValuePair parseNameValuePair(final String value, 1.266 + HeaderValueParser parser) 1.267 + throws ParseException { 1.268 + 1.269 + if (value == null) { 1.270 + throw new IllegalArgumentException 1.271 + ("Value to parse may not be null"); 1.272 + } 1.273 + 1.274 + if (parser == null) 1.275 + parser = BasicHeaderValueParser.DEFAULT; 1.276 + 1.277 + CharArrayBuffer buffer = new CharArrayBuffer(value.length()); 1.278 + buffer.append(value); 1.279 + ParserCursor cursor = new ParserCursor(0, value.length()); 1.280 + return parser.parseNameValuePair(buffer, cursor); 1.281 + } 1.282 + 1.283 + 1.284 + // non-javadoc, see interface HeaderValueParser 1.285 + public NameValuePair parseNameValuePair(final CharArrayBuffer buffer, 1.286 + final ParserCursor cursor) { 1.287 + return parseNameValuePair(buffer, cursor, ALL_DELIMITERS); 1.288 + } 1.289 + 1.290 + private static boolean isOneOf(final char ch, final char[] chs) { 1.291 + if (chs != null) { 1.292 + for (int i = 0; i < chs.length; i++) { 1.293 + if (ch == chs[i]) { 1.294 + return true; 1.295 + } 1.296 + } 1.297 + } 1.298 + return false; 1.299 + } 1.300 + 1.301 + public NameValuePair parseNameValuePair(final CharArrayBuffer buffer, 1.302 + final ParserCursor cursor, 1.303 + final char[] delimiters) { 1.304 + 1.305 + if (buffer == null) { 1.306 + throw new IllegalArgumentException("Char array buffer may not be null"); 1.307 + } 1.308 + if (cursor == null) { 1.309 + throw new IllegalArgumentException("Parser cursor may not be null"); 1.310 + } 1.311 + 1.312 + boolean terminated = false; 1.313 + 1.314 + int pos = cursor.getPos(); 1.315 + int indexFrom = cursor.getPos(); 1.316 + int indexTo = cursor.getUpperBound(); 1.317 + 1.318 + // Find name 1.319 + String name = null; 1.320 + while (pos < indexTo) { 1.321 + char ch = buffer.charAt(pos); 1.322 + if (ch == '=') { 1.323 + break; 1.324 + } 1.325 + if (isOneOf(ch, delimiters)) { 1.326 + terminated = true; 1.327 + break; 1.328 + } 1.329 + pos++; 1.330 + } 1.331 + 1.332 + if (pos == indexTo) { 1.333 + terminated = true; 1.334 + name = buffer.substringTrimmed(indexFrom, indexTo); 1.335 + } else { 1.336 + name = buffer.substringTrimmed(indexFrom, pos); 1.337 + pos++; 1.338 + } 1.339 + 1.340 + if (terminated) { 1.341 + cursor.updatePos(pos); 1.342 + return createNameValuePair(name, null); 1.343 + } 1.344 + 1.345 + // Find value 1.346 + String value = null; 1.347 + int i1 = pos; 1.348 + 1.349 + boolean qouted = false; 1.350 + boolean escaped = false; 1.351 + while (pos < indexTo) { 1.352 + char ch = buffer.charAt(pos); 1.353 + if (ch == '"' && !escaped) { 1.354 + qouted = !qouted; 1.355 + } 1.356 + if (!qouted && !escaped && isOneOf(ch, delimiters)) { 1.357 + terminated = true; 1.358 + break; 1.359 + } 1.360 + if (escaped) { 1.361 + escaped = false; 1.362 + } else { 1.363 + escaped = qouted && ch == '\\'; 1.364 + } 1.365 + pos++; 1.366 + } 1.367 + 1.368 + int i2 = pos; 1.369 + // Trim leading white spaces 1.370 + while (i1 < i2 && (HTTP.isWhitespace(buffer.charAt(i1)))) { 1.371 + i1++; 1.372 + } 1.373 + // Trim trailing white spaces 1.374 + while ((i2 > i1) && (HTTP.isWhitespace(buffer.charAt(i2 - 1)))) { 1.375 + i2--; 1.376 + } 1.377 + // Strip away quotes if necessary 1.378 + if (((i2 - i1) >= 2) 1.379 + && (buffer.charAt(i1) == '"') 1.380 + && (buffer.charAt(i2 - 1) == '"')) { 1.381 + i1++; 1.382 + i2--; 1.383 + } 1.384 + value = buffer.substring(i1, i2); 1.385 + if (terminated) { 1.386 + pos++; 1.387 + } 1.388 + cursor.updatePos(pos); 1.389 + return createNameValuePair(name, value); 1.390 + } 1.391 + 1.392 + /** 1.393 + * Creates a name-value pair. 1.394 + * Called from {@link #parseNameValuePair}. 1.395 + * 1.396 + * @param name the name 1.397 + * @param value the value, or <code>null</code> 1.398 + * 1.399 + * @return a name-value pair representing the arguments 1.400 + */ 1.401 + protected NameValuePair createNameValuePair(final String name, final String value) { 1.402 + return new BasicNameValuePair(name, value); 1.403 + } 1.404 + 1.405 +} 1.406 +