HttpHeader.java
3.48 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Decompiled with CFR 0_118.
*/
package com.day.crx.packmgr.impl.support;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HttpHeader {
private final String name;
private final String header;
private Map<String, String> parts;
public HttpHeader(String headerline) {
int pos = headerline.indexOf(58);
if (pos < 0) {
throw new IllegalArgumentException("header invalid, no colon:" + headerline);
}
this.name = headerline.substring(0, pos).trim();
this.header = headerline.substring(pos + 1).trim();
this.parse();
}
public HttpHeader(String name, String value) {
this.name = name.trim();
this.header = value.trim();
this.parse();
}
public String getName() {
return this.name;
}
public String getPart(String name) {
return this.parts == null ? null : this.parts.get(name);
}
public String getPrimary() {
return this.parts == null ? this.header : this.parts.get("");
}
public String toString() {
return this.header;
}
protected String toString_test() {
String ret = this.getPrimary();
if (this.parts != null) {
for (String key : this.parts.keySet()) {
if ("".equals(key)) continue;
ret = ret + "; " + key + "=" + this.parts.get(key);
}
}
return ret;
}
private void parse() {
int pos = this.header.indexOf(59);
if (pos < 0) {
return;
}
this.parts = new HashMap<String, String>();
this.parts.put("", this.header.substring(0, pos++));
String parse = this.header + ";";
String name = "";
int start = pos;
int state = 0;
while (pos < parse.length()) {
char c = parse.charAt(pos);
switch (state) {
case 0: {
if (c == ';' || c == ' ' || c == '\r' || c == '\n' || c == '\t') break;
start = pos;
state = 1;
break;
}
case 1: {
if (c != '=') break;
name = this.header.substring(start, pos);
start = pos + 1;
state = 2;
break;
}
case 2: {
if (c == '\"') {
state = 3;
start = pos + 1;
break;
}
if (c == '\'') {
state = 4;
start = pos + 1;
break;
}
if (c != ' ' && c != ';') break;
this.parts.put(name.toLowerCase(), this.header.substring(start, pos));
state = 0;
name = "";
break;
}
case 3: {
if (c != '\"') break;
this.parts.put(name.toLowerCase(), this.header.substring(start, pos));
state = 0;
name = "";
break;
}
case 4: {
if (c != '\'') break;
this.parts.put(name.toLowerCase(), this.header.substring(start, pos));
state = 0;
name = "";
}
}
++pos;
}
}
}