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