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 package com.foxinmy.weixin4j.http;
29
30 import com.foxinmy.weixin4j.util.CharArrayBuffer;
31 import com.foxinmy.weixin4j.util.NameValue;
32
33
34
35
36
37
38
39
40 public class HeaderValueFormatter {
41
42 public final static HeaderValueFormatter INSTANCE = new HeaderValueFormatter();
43
44
45
46
47
48
49 public final static String SEPARATORS = " ;,:@()<>\\\"/[]?={}\t";
50
51
52
53
54
55 public final static String UNSAFE_CHARS = "\"\\";
56
57 public HeaderValueFormatter() {
58 super();
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72 public static
73 String formatParameters(final NameValue[] nvps,
74 final boolean quote,
75 final HeaderValueFormatter formatter) {
76 return (formatter != null ? formatter : HeaderValueFormatter.INSTANCE)
77 .formatParameters(null, nvps, quote).toString();
78 }
79
80
81
82 public CharArrayBuffer formatParameters(final CharArrayBuffer charBuffer,
83 final NameValue[] nvps,
84 final boolean quote) {
85 final int len = estimateParametersLen(nvps);
86 CharArrayBuffer buffer = charBuffer;
87 if (buffer == null) {
88 buffer = new CharArrayBuffer(len);
89 } else {
90 buffer.ensureCapacity(len);
91 }
92
93 for (int i = 0; i < nvps.length; i++) {
94 if (i > 0) {
95 buffer.append("; ");
96 }
97 formatNameValuePair(buffer, nvps[i], quote);
98 }
99
100 return buffer;
101 }
102
103
104
105
106
107
108
109
110
111 protected int estimateParametersLen(final NameValue[] nvps) {
112 if ((nvps == null) || (nvps.length < 1)) {
113 return 0;
114 }
115
116 int result = (nvps.length-1) * 2;
117 for (final NameValue nvp : nvps) {
118 result += estimateNameValuePairLen(nvp);
119 }
120
121 return result;
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public static
137 String formatNameValuePair(final NameValue nvp,
138 final boolean quote,
139 final HeaderValueFormatter formatter) {
140 return (formatter != null ? formatter : HeaderValueFormatter.INSTANCE)
141 .formatNameValuePair(null, nvp, quote).toString();
142 }
143
144
145
146 public CharArrayBuffer formatNameValuePair(final CharArrayBuffer charBuffer,
147 final NameValue nvp,
148 final boolean quote) {
149 final int len = estimateNameValuePairLen(nvp);
150 CharArrayBuffer buffer = charBuffer;
151 if (buffer == null) {
152 buffer = new CharArrayBuffer(len);
153 } else {
154 buffer.ensureCapacity(len);
155 }
156
157 buffer.append(nvp.getName());
158 final String value = nvp.getValue();
159 if (value != null) {
160 buffer.append('=');
161 doFormatValue(buffer, value, quote);
162 }
163
164 return buffer;
165 }
166
167
168
169
170
171
172
173
174
175 protected int estimateNameValuePairLen(final NameValue nvp) {
176 if (nvp == null) {
177 return 0;
178 }
179
180 int result = nvp.getName().length();
181 final String value = nvp.getValue();
182 if (value != null) {
183
184 result += 3 + value.length();
185 }
186 return result;
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200 protected void doFormatValue(final CharArrayBuffer buffer,
201 final String value,
202 final boolean quote) {
203
204 boolean quoteFlag = quote;
205 if (!quoteFlag) {
206 for (int i = 0; (i < value.length()) && !quoteFlag; i++) {
207 quoteFlag = isSeparator(value.charAt(i));
208 }
209 }
210
211 if (quoteFlag) {
212 buffer.append('"');
213 }
214 for (int i = 0; i < value.length(); i++) {
215 final char ch = value.charAt(i);
216 if (isUnsafe(ch)) {
217 buffer.append('\\');
218 }
219 buffer.append(ch);
220 }
221 if (quoteFlag) {
222 buffer.append('"');
223 }
224 }
225
226
227
228
229
230
231
232
233
234
235 protected boolean isSeparator(final char ch) {
236 return SEPARATORS.indexOf(ch) >= 0;
237 }
238
239
240
241
242
243
244
245
246
247
248 protected boolean isUnsafe(final char ch) {
249 return UNSAFE_CHARS.indexOf(ch) >= 0;
250 }
251
252
253 }