1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/NetscapeDraftHeaderParser.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,142 @@ 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.impl.cookie; 1.32 + 1.33 +import java.util.ArrayList; 1.34 +import java.util.List; 1.35 + 1.36 +import ch.boye.httpclientandroidlib.annotation.Immutable; 1.37 + 1.38 +import ch.boye.httpclientandroidlib.HeaderElement; 1.39 +import ch.boye.httpclientandroidlib.NameValuePair; 1.40 +import ch.boye.httpclientandroidlib.ParseException; 1.41 +import ch.boye.httpclientandroidlib.message.BasicHeaderElement; 1.42 +import ch.boye.httpclientandroidlib.message.BasicNameValuePair; 1.43 +import ch.boye.httpclientandroidlib.message.ParserCursor; 1.44 +import ch.boye.httpclientandroidlib.protocol.HTTP; 1.45 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer; 1.46 + 1.47 +/** 1.48 + * 1.49 + * @since 4.0 1.50 + */ 1.51 +@Immutable 1.52 +public class NetscapeDraftHeaderParser { 1.53 + 1.54 + public final static NetscapeDraftHeaderParser DEFAULT = new NetscapeDraftHeaderParser(); 1.55 + 1.56 + public NetscapeDraftHeaderParser() { 1.57 + super(); 1.58 + } 1.59 + 1.60 + public HeaderElement parseHeader( 1.61 + final CharArrayBuffer buffer, 1.62 + final ParserCursor cursor) throws ParseException { 1.63 + if (buffer == null) { 1.64 + throw new IllegalArgumentException("Char array buffer may not be null"); 1.65 + } 1.66 + if (cursor == null) { 1.67 + throw new IllegalArgumentException("Parser cursor may not be null"); 1.68 + } 1.69 + NameValuePair nvp = parseNameValuePair(buffer, cursor); 1.70 + List<NameValuePair> params = new ArrayList<NameValuePair>(); 1.71 + while (!cursor.atEnd()) { 1.72 + NameValuePair param = parseNameValuePair(buffer, cursor); 1.73 + params.add(param); 1.74 + } 1.75 + return new BasicHeaderElement( 1.76 + nvp.getName(), 1.77 + nvp.getValue(), params.toArray(new NameValuePair[params.size()])); 1.78 + } 1.79 + 1.80 + private NameValuePair parseNameValuePair( 1.81 + final CharArrayBuffer buffer, final ParserCursor cursor) { 1.82 + boolean terminated = false; 1.83 + 1.84 + int pos = cursor.getPos(); 1.85 + int indexFrom = cursor.getPos(); 1.86 + int indexTo = cursor.getUpperBound(); 1.87 + 1.88 + // Find name 1.89 + String name = null; 1.90 + while (pos < indexTo) { 1.91 + char ch = buffer.charAt(pos); 1.92 + if (ch == '=') { 1.93 + break; 1.94 + } 1.95 + if (ch == ';') { 1.96 + terminated = true; 1.97 + break; 1.98 + } 1.99 + pos++; 1.100 + } 1.101 + 1.102 + if (pos == indexTo) { 1.103 + terminated = true; 1.104 + name = buffer.substringTrimmed(indexFrom, indexTo); 1.105 + } else { 1.106 + name = buffer.substringTrimmed(indexFrom, pos); 1.107 + pos++; 1.108 + } 1.109 + 1.110 + if (terminated) { 1.111 + cursor.updatePos(pos); 1.112 + return new BasicNameValuePair(name, null); 1.113 + } 1.114 + 1.115 + // Find value 1.116 + String value = null; 1.117 + int i1 = pos; 1.118 + 1.119 + while (pos < indexTo) { 1.120 + char ch = buffer.charAt(pos); 1.121 + if (ch == ';') { 1.122 + terminated = true; 1.123 + break; 1.124 + } 1.125 + pos++; 1.126 + } 1.127 + 1.128 + int i2 = pos; 1.129 + // Trim leading white spaces 1.130 + while (i1 < i2 && (HTTP.isWhitespace(buffer.charAt(i1)))) { 1.131 + i1++; 1.132 + } 1.133 + // Trim trailing white spaces 1.134 + while ((i2 > i1) && (HTTP.isWhitespace(buffer.charAt(i2 - 1)))) { 1.135 + i2--; 1.136 + } 1.137 + value = buffer.substring(i1, i2); 1.138 + if (terminated) { 1.139 + pos++; 1.140 + } 1.141 + cursor.updatePos(pos); 1.142 + return new BasicNameValuePair(name, value); 1.143 + } 1.144 + 1.145 +}