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 ch.boye.httpclientandroidlib.impl.cookie; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 33 import ch.boye.httpclientandroidlib.HeaderElement; 34 import ch.boye.httpclientandroidlib.NameValuePair; 35 import ch.boye.httpclientandroidlib.ParseException; 36 import ch.boye.httpclientandroidlib.annotation.Immutable; 37 import ch.boye.httpclientandroidlib.message.BasicHeaderElement; 38 import ch.boye.httpclientandroidlib.message.BasicNameValuePair; 39 import ch.boye.httpclientandroidlib.message.ParserCursor; 40 import ch.boye.httpclientandroidlib.protocol.HTTP; 41 import ch.boye.httpclientandroidlib.util.Args; 42 import ch.boye.httpclientandroidlib.util.CharArrayBuffer; 43 44 /** 45 * 46 * @since 4.0 47 */ 48 @Immutable 49 public class NetscapeDraftHeaderParser { 50 51 public final static NetscapeDraftHeaderParser DEFAULT = new NetscapeDraftHeaderParser(); 52 NetscapeDraftHeaderParser()53 public NetscapeDraftHeaderParser() { 54 super(); 55 } 56 parseHeader( final CharArrayBuffer buffer, final ParserCursor cursor)57 public HeaderElement parseHeader( 58 final CharArrayBuffer buffer, 59 final ParserCursor cursor) throws ParseException { 60 Args.notNull(buffer, "Char array buffer"); 61 Args.notNull(cursor, "Parser cursor"); 62 final NameValuePair nvp = parseNameValuePair(buffer, cursor); 63 final List<NameValuePair> params = new ArrayList<NameValuePair>(); 64 while (!cursor.atEnd()) { 65 final NameValuePair param = parseNameValuePair(buffer, cursor); 66 params.add(param); 67 } 68 return new BasicHeaderElement( 69 nvp.getName(), 70 nvp.getValue(), params.toArray(new NameValuePair[params.size()])); 71 } 72 parseNameValuePair( final CharArrayBuffer buffer, final ParserCursor cursor)73 private NameValuePair parseNameValuePair( 74 final CharArrayBuffer buffer, final ParserCursor cursor) { 75 boolean terminated = false; 76 77 int pos = cursor.getPos(); 78 final int indexFrom = cursor.getPos(); 79 final int indexTo = cursor.getUpperBound(); 80 81 // Find name 82 String name = null; 83 while (pos < indexTo) { 84 final char ch = buffer.charAt(pos); 85 if (ch == '=') { 86 break; 87 } 88 if (ch == ';') { 89 terminated = true; 90 break; 91 } 92 pos++; 93 } 94 95 if (pos == indexTo) { 96 terminated = true; 97 name = buffer.substringTrimmed(indexFrom, indexTo); 98 } else { 99 name = buffer.substringTrimmed(indexFrom, pos); 100 pos++; 101 } 102 103 if (terminated) { 104 cursor.updatePos(pos); 105 return new BasicNameValuePair(name, null); 106 } 107 108 // Find value 109 String value = null; 110 int i1 = pos; 111 112 while (pos < indexTo) { 113 final char ch = buffer.charAt(pos); 114 if (ch == ';') { 115 terminated = true; 116 break; 117 } 118 pos++; 119 } 120 121 int i2 = pos; 122 // Trim leading white spaces 123 while (i1 < i2 && (HTTP.isWhitespace(buffer.charAt(i1)))) { 124 i1++; 125 } 126 // Trim trailing white spaces 127 while ((i2 > i1) && (HTTP.isWhitespace(buffer.charAt(i2 - 1)))) { 128 i2--; 129 } 130 value = buffer.substring(i1, i2); 131 if (terminated) { 132 pos++; 133 } 134 cursor.updatePos(pos); 135 return new BasicNameValuePair(name, value); 136 } 137 138 } 139