Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.impl.cookie; |
michael@0 | 29 | |
michael@0 | 30 | import java.util.ArrayList; |
michael@0 | 31 | import java.util.List; |
michael@0 | 32 | |
michael@0 | 33 | import ch.boye.httpclientandroidlib.annotation.Immutable; |
michael@0 | 34 | |
michael@0 | 35 | import ch.boye.httpclientandroidlib.HeaderElement; |
michael@0 | 36 | import ch.boye.httpclientandroidlib.NameValuePair; |
michael@0 | 37 | import ch.boye.httpclientandroidlib.ParseException; |
michael@0 | 38 | import ch.boye.httpclientandroidlib.message.BasicHeaderElement; |
michael@0 | 39 | import ch.boye.httpclientandroidlib.message.BasicNameValuePair; |
michael@0 | 40 | import ch.boye.httpclientandroidlib.message.ParserCursor; |
michael@0 | 41 | import ch.boye.httpclientandroidlib.protocol.HTTP; |
michael@0 | 42 | import ch.boye.httpclientandroidlib.util.CharArrayBuffer; |
michael@0 | 43 | |
michael@0 | 44 | /** |
michael@0 | 45 | * |
michael@0 | 46 | * @since 4.0 |
michael@0 | 47 | */ |
michael@0 | 48 | @Immutable |
michael@0 | 49 | public class NetscapeDraftHeaderParser { |
michael@0 | 50 | |
michael@0 | 51 | public final static NetscapeDraftHeaderParser DEFAULT = new NetscapeDraftHeaderParser(); |
michael@0 | 52 | |
michael@0 | 53 | public NetscapeDraftHeaderParser() { |
michael@0 | 54 | super(); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | public HeaderElement parseHeader( |
michael@0 | 58 | final CharArrayBuffer buffer, |
michael@0 | 59 | final ParserCursor cursor) throws ParseException { |
michael@0 | 60 | if (buffer == null) { |
michael@0 | 61 | throw new IllegalArgumentException("Char array buffer may not be null"); |
michael@0 | 62 | } |
michael@0 | 63 | if (cursor == null) { |
michael@0 | 64 | throw new IllegalArgumentException("Parser cursor may not be null"); |
michael@0 | 65 | } |
michael@0 | 66 | NameValuePair nvp = parseNameValuePair(buffer, cursor); |
michael@0 | 67 | List<NameValuePair> params = new ArrayList<NameValuePair>(); |
michael@0 | 68 | while (!cursor.atEnd()) { |
michael@0 | 69 | NameValuePair param = parseNameValuePair(buffer, cursor); |
michael@0 | 70 | params.add(param); |
michael@0 | 71 | } |
michael@0 | 72 | return new BasicHeaderElement( |
michael@0 | 73 | nvp.getName(), |
michael@0 | 74 | nvp.getValue(), params.toArray(new NameValuePair[params.size()])); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | private NameValuePair parseNameValuePair( |
michael@0 | 78 | final CharArrayBuffer buffer, final ParserCursor cursor) { |
michael@0 | 79 | boolean terminated = false; |
michael@0 | 80 | |
michael@0 | 81 | int pos = cursor.getPos(); |
michael@0 | 82 | int indexFrom = cursor.getPos(); |
michael@0 | 83 | int indexTo = cursor.getUpperBound(); |
michael@0 | 84 | |
michael@0 | 85 | // Find name |
michael@0 | 86 | String name = null; |
michael@0 | 87 | while (pos < indexTo) { |
michael@0 | 88 | char ch = buffer.charAt(pos); |
michael@0 | 89 | if (ch == '=') { |
michael@0 | 90 | break; |
michael@0 | 91 | } |
michael@0 | 92 | if (ch == ';') { |
michael@0 | 93 | terminated = true; |
michael@0 | 94 | break; |
michael@0 | 95 | } |
michael@0 | 96 | pos++; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | if (pos == indexTo) { |
michael@0 | 100 | terminated = true; |
michael@0 | 101 | name = buffer.substringTrimmed(indexFrom, indexTo); |
michael@0 | 102 | } else { |
michael@0 | 103 | name = buffer.substringTrimmed(indexFrom, pos); |
michael@0 | 104 | pos++; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | if (terminated) { |
michael@0 | 108 | cursor.updatePos(pos); |
michael@0 | 109 | return new BasicNameValuePair(name, null); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | // Find value |
michael@0 | 113 | String value = null; |
michael@0 | 114 | int i1 = pos; |
michael@0 | 115 | |
michael@0 | 116 | while (pos < indexTo) { |
michael@0 | 117 | char ch = buffer.charAt(pos); |
michael@0 | 118 | if (ch == ';') { |
michael@0 | 119 | terminated = true; |
michael@0 | 120 | break; |
michael@0 | 121 | } |
michael@0 | 122 | pos++; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | int i2 = pos; |
michael@0 | 126 | // Trim leading white spaces |
michael@0 | 127 | while (i1 < i2 && (HTTP.isWhitespace(buffer.charAt(i1)))) { |
michael@0 | 128 | i1++; |
michael@0 | 129 | } |
michael@0 | 130 | // Trim trailing white spaces |
michael@0 | 131 | while ((i2 > i1) && (HTTP.isWhitespace(buffer.charAt(i2 - 1)))) { |
michael@0 | 132 | i2--; |
michael@0 | 133 | } |
michael@0 | 134 | value = buffer.substring(i1, i2); |
michael@0 | 135 | if (terminated) { |
michael@0 | 136 | pos++; |
michael@0 | 137 | } |
michael@0 | 138 | cursor.updatePos(pos); |
michael@0 | 139 | return new BasicNameValuePair(name, value); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | } |