1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.metricshub.wbem.sblim.cimclient.internal.http;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 import java.util.Vector;
50
51
52
53
54
55 public class HttpHeaderParser {
56 String iRaw;
57
58 Vector<String[]> iNameValuePair;
59
60
61
62
63
64
65
66 public HttpHeaderParser(String pHeader) {
67 this.iRaw = pHeader;
68 this.iNameValuePair = new Vector<String[]>();
69 if (this.iRaw != null) {
70 this.iRaw = this.iRaw.trim();
71 char charArray[] = this.iRaw.toCharArray();
72 int startPosValue = 0;
73 int currentPos = 0;
74 boolean isName = true;
75 boolean withinQuote = false;
76 int len = charArray.length;
77 String nvp[] = new String[2];
78 do {
79 char c = charArray[currentPos];
80 if (c == '=') {
81 nvp[0] = (new String(charArray, startPosValue, currentPos - startPosValue)).toLowerCase();
82 isName = false;
83 startPosValue = ++currentPos;
84 } else if (c == '"') {
85 if (withinQuote) {
86 nvp[1] = new String(charArray, startPosValue, currentPos - startPosValue);
87 this.iNameValuePair.add(nvp);
88 nvp = new String[2];
89 withinQuote = false;
90 while (
91 ++currentPos < len &&
92 (charArray[currentPos] == ' ' || charArray[currentPos] == ',' || charArray[currentPos] == ';')
93 ) {
94
95 }
96 isName = true;
97 startPosValue = currentPos;
98 } else {
99 withinQuote = true;
100 startPosValue = ++currentPos;
101 }
102 } else if (c == ' ' || c == ',' || c == ';') {
103 if (withinQuote) {
104 currentPos++;
105 } else {
106 if (isName) {
107 nvp[0] = (new String(charArray, startPosValue, currentPos - startPosValue)).toLowerCase();
108 this.iNameValuePair.add(nvp);
109 nvp = new String[2];
110 } else {
111 nvp[1] = new String(charArray, startPosValue, currentPos - startPosValue);
112 this.iNameValuePair.add(nvp);
113 nvp = new String[2];
114 }
115
116 while (
117 ++currentPos < len &&
118 (charArray[currentPos] == ' ' || charArray[currentPos] == ',' || charArray[currentPos] == ';')
119 ) {
120
121 }
122 isName = true;
123 startPosValue = currentPos;
124 }
125 } else {
126 currentPos++;
127 }
128 } while (currentPos < len);
129
130 if (--currentPos > startPosValue) {
131 if (!isName) {
132 if (charArray[currentPos] == '"') {
133 nvp[1] = new String(charArray, startPosValue, currentPos - startPosValue);
134 this.iNameValuePair.add(nvp);
135 nvp = new String[2];
136 } else {
137 nvp[1] = new String(charArray, startPosValue, (currentPos - startPosValue) + 1);
138 this.iNameValuePair.add(nvp);
139 nvp = new String[2];
140 }
141 } else {
142 nvp[0] = (new String(charArray, startPosValue, (currentPos - startPosValue) + 1)).toLowerCase();
143 }
144 this.iNameValuePair.add(nvp);
145 } else if (currentPos == startPosValue) {
146 if (!isName) {
147 if (charArray[currentPos] == '"') {
148 nvp[1] = String.valueOf(charArray[currentPos - 1]);
149 this.iNameValuePair.add(nvp);
150 nvp = new String[2];
151 } else {
152 nvp[1] = String.valueOf(charArray[currentPos]);
153 this.iNameValuePair.add(nvp);
154 nvp = new String[2];
155 }
156 } else {
157 nvp[0] = String.valueOf(charArray[currentPos]).toLowerCase();
158 }
159 this.iNameValuePair.add(nvp);
160 }
161 }
162 }
163
164
165
166
167
168
169
170
171 public String getField(int pIndex) {
172 if (pIndex < 0 || pIndex > this.iNameValuePair.size()) return null;
173 return this.iNameValuePair.elementAt(pIndex)[0];
174 }
175
176
177
178
179
180
181
182 public String getValue(int pIndex) {
183 if (pIndex < 0 || pIndex > this.iNameValuePair.size()) return null;
184 return this.iNameValuePair.elementAt(pIndex)[1];
185 }
186
187
188
189
190
191
192
193
194 public String getValue(String pName) {
195 return getValue(pName, null);
196 }
197
198
199
200
201
202
203
204
205
206
207
208 public String getValue(String pName, String pDefault) {
209 if (pName == null) return pDefault;
210 pName = pName.toLowerCase();
211 for (int i = 0; i < this.iNameValuePair.size(); i++) {
212 if (this.iNameValuePair.elementAt(i)[0] == null) return pDefault;
213 if (pName.equals(this.iNameValuePair.elementAt(i)[0])) return this.iNameValuePair.elementAt(i)[1];
214 }
215 return pDefault;
216 }
217
218
219
220
221
222
223
224
225
226
227
228 public int getIntValue(String pName, int pDefault) {
229 try {
230 return Integer.parseInt(getValue(pName, String.valueOf(pDefault)));
231 } catch (Exception e) {
232 return pDefault;
233 }
234 }
235
236 @Override
237 public String toString() {
238 return "raw:" + this.iRaw;
239 }
240 }