1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.ByteBufUtil;
20 import io.netty.handler.codec.DateFormatter;
21 import io.netty.handler.codec.Headers;
22 import io.netty.handler.codec.HeadersUtils;
23 import io.netty.util.AsciiString;
24 import io.netty.util.CharsetUtil;
25
26 import java.text.ParseException;
27 import java.util.Calendar;
28 import java.util.Date;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import java.util.Set;
34
35 import static io.netty.util.AsciiString.contentEquals;
36 import static io.netty.util.AsciiString.contentEqualsIgnoreCase;
37 import static io.netty.util.AsciiString.trim;
38 import static io.netty.util.internal.ObjectUtil.checkNotNull;
39
40
41
42
43
44 public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>> {
45
46
47
48
49
50
51
52 @Deprecated
53 public static final HttpHeaders EMPTY_HEADERS = EmptyHttpHeaders.instance();
54
55
56
57
58
59
60 @Deprecated
61 public static final class Names {
62
63
64
65 public static final String ACCEPT = "Accept";
66
67
68
69 public static final String ACCEPT_CHARSET = "Accept-Charset";
70
71
72
73 public static final String ACCEPT_ENCODING = "Accept-Encoding";
74
75
76
77 public static final String ACCEPT_LANGUAGE = "Accept-Language";
78
79
80
81 public static final String ACCEPT_RANGES = "Accept-Ranges";
82
83
84
85 public static final String ACCEPT_PATCH = "Accept-Patch";
86
87
88
89 public static final String ACCESS_CONTROL_ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";
90
91
92
93 public static final String ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers";
94
95
96
97 public static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods";
98
99
100
101 public static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
102
103
104
105 public static final String ACCESS_CONTROL_EXPOSE_HEADERS = "Access-Control-Expose-Headers";
106
107
108
109 public static final String ACCESS_CONTROL_MAX_AGE = "Access-Control-Max-Age";
110
111
112
113 public static final String ACCESS_CONTROL_REQUEST_HEADERS = "Access-Control-Request-Headers";
114
115
116
117 public static final String ACCESS_CONTROL_REQUEST_METHOD = "Access-Control-Request-Method";
118
119
120
121 public static final String AGE = "Age";
122
123
124
125 public static final String ALLOW = "Allow";
126
127
128
129 public static final String AUTHORIZATION = "Authorization";
130
131
132
133 public static final String CACHE_CONTROL = "Cache-Control";
134
135
136
137 public static final String CONNECTION = "Connection";
138
139
140
141 public static final String CONTENT_BASE = "Content-Base";
142
143
144
145 public static final String CONTENT_ENCODING = "Content-Encoding";
146
147
148
149 public static final String CONTENT_LANGUAGE = "Content-Language";
150
151
152
153 public static final String CONTENT_LENGTH = "Content-Length";
154
155
156
157 public static final String CONTENT_LOCATION = "Content-Location";
158
159
160
161 public static final String CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding";
162
163
164
165 public static final String CONTENT_MD5 = "Content-MD5";
166
167
168
169 public static final String CONTENT_RANGE = "Content-Range";
170
171
172
173 public static final String CONTENT_TYPE = "Content-Type";
174
175
176
177 public static final String COOKIE = "Cookie";
178
179
180
181 public static final String DATE = "Date";
182
183
184
185 public static final String ETAG = "ETag";
186
187
188
189 public static final String EXPECT = "Expect";
190
191
192
193 public static final String EXPIRES = "Expires";
194
195
196
197 public static final String FROM = "From";
198
199
200
201 public static final String HOST = "Host";
202
203
204
205 public static final String IF_MATCH = "If-Match";
206
207
208
209 public static final String IF_MODIFIED_SINCE = "If-Modified-Since";
210
211
212
213 public static final String IF_NONE_MATCH = "If-None-Match";
214
215
216
217 public static final String IF_RANGE = "If-Range";
218
219
220
221 public static final String IF_UNMODIFIED_SINCE = "If-Unmodified-Since";
222
223
224
225 public static final String LAST_MODIFIED = "Last-Modified";
226
227
228
229 public static final String LOCATION = "Location";
230
231
232
233 public static final String MAX_FORWARDS = "Max-Forwards";
234
235
236
237 public static final String ORIGIN = "Origin";
238
239
240
241 public static final String PRAGMA = "Pragma";
242
243
244
245 public static final String PROXY_AUTHENTICATE = "Proxy-Authenticate";
246
247
248
249 public static final String PROXY_AUTHORIZATION = "Proxy-Authorization";
250
251
252
253 public static final String RANGE = "Range";
254
255
256
257 public static final String REFERER = "Referer";
258
259
260
261 public static final String RETRY_AFTER = "Retry-After";
262
263
264
265 public static final String SEC_WEBSOCKET_KEY1 = "Sec-WebSocket-Key1";
266
267
268
269 public static final String SEC_WEBSOCKET_KEY2 = "Sec-WebSocket-Key2";
270
271
272
273 public static final String SEC_WEBSOCKET_LOCATION = "Sec-WebSocket-Location";
274
275
276
277 public static final String SEC_WEBSOCKET_ORIGIN = "Sec-WebSocket-Origin";
278
279
280
281 public static final String SEC_WEBSOCKET_PROTOCOL = "Sec-WebSocket-Protocol";
282
283
284
285 public static final String SEC_WEBSOCKET_VERSION = "Sec-WebSocket-Version";
286
287
288
289 public static final String SEC_WEBSOCKET_KEY = "Sec-WebSocket-Key";
290
291
292
293 public static final String SEC_WEBSOCKET_ACCEPT = "Sec-WebSocket-Accept";
294
295
296
297 public static final String SERVER = "Server";
298
299
300
301 public static final String SET_COOKIE = "Set-Cookie";
302
303
304
305 public static final String SET_COOKIE2 = "Set-Cookie2";
306
307
308
309 public static final String TE = "TE";
310
311
312
313 public static final String TRAILER = "Trailer";
314
315
316
317 public static final String TRANSFER_ENCODING = "Transfer-Encoding";
318
319
320
321 public static final String UPGRADE = "Upgrade";
322
323
324
325 public static final String USER_AGENT = "User-Agent";
326
327
328
329 public static final String VARY = "Vary";
330
331
332
333 public static final String VIA = "Via";
334
335
336
337 public static final String WARNING = "Warning";
338
339
340
341 public static final String WEBSOCKET_LOCATION = "WebSocket-Location";
342
343
344
345 public static final String WEBSOCKET_ORIGIN = "WebSocket-Origin";
346
347
348
349 public static final String WEBSOCKET_PROTOCOL = "WebSocket-Protocol";
350
351
352
353 public static final String WWW_AUTHENTICATE = "WWW-Authenticate";
354
355 private Names() {
356 }
357 }
358
359
360
361
362
363
364 @Deprecated
365 public static final class Values {
366
367
368
369 public static final String APPLICATION_JSON = "application/json";
370
371
372
373 public static final String APPLICATION_X_WWW_FORM_URLENCODED =
374 "application/x-www-form-urlencoded";
375
376
377
378 public static final String BASE64 = "base64";
379
380
381
382 public static final String BINARY = "binary";
383
384
385
386 public static final String BOUNDARY = "boundary";
387
388
389
390 public static final String BYTES = "bytes";
391
392
393
394 public static final String CHARSET = "charset";
395
396
397
398 public static final String CHUNKED = "chunked";
399
400
401
402 public static final String CLOSE = "close";
403
404
405
406 public static final String COMPRESS = "compress";
407
408
409
410 public static final String CONTINUE = "100-continue";
411
412
413
414 public static final String DEFLATE = "deflate";
415
416
417
418 public static final String GZIP = "gzip";
419
420
421
422 public static final String GZIP_DEFLATE = "gzip,deflate";
423
424
425
426 public static final String IDENTITY = "identity";
427
428
429
430 public static final String KEEP_ALIVE = "keep-alive";
431
432
433
434 public static final String MAX_AGE = "max-age";
435
436
437
438 public static final String MAX_STALE = "max-stale";
439
440
441
442 public static final String MIN_FRESH = "min-fresh";
443
444
445
446 public static final String MULTIPART_FORM_DATA = "multipart/form-data";
447
448
449
450 public static final String MUST_REVALIDATE = "must-revalidate";
451
452
453
454 public static final String NO_CACHE = "no-cache";
455
456
457
458 public static final String NO_STORE = "no-store";
459
460
461
462 public static final String NO_TRANSFORM = "no-transform";
463
464
465
466 public static final String NONE = "none";
467
468
469
470 public static final String ONLY_IF_CACHED = "only-if-cached";
471
472
473
474 public static final String PRIVATE = "private";
475
476
477
478 public static final String PROXY_REVALIDATE = "proxy-revalidate";
479
480
481
482 public static final String PUBLIC = "public";
483
484
485
486 public static final String QUOTED_PRINTABLE = "quoted-printable";
487
488
489
490 public static final String S_MAXAGE = "s-maxage";
491
492
493
494 public static final String TRAILERS = "trailers";
495
496
497
498 public static final String UPGRADE = "Upgrade";
499
500
501
502 public static final String WEBSOCKET = "WebSocket";
503
504 private Values() {
505 }
506 }
507
508
509
510
511
512
513
514
515
516 @Deprecated
517 public static boolean isKeepAlive(HttpMessage message) {
518 return HttpUtil.isKeepAlive(message);
519 }
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542 @Deprecated
543 public static void setKeepAlive(HttpMessage message, boolean keepAlive) {
544 HttpUtil.setKeepAlive(message, keepAlive);
545 }
546
547
548
549
550 @Deprecated
551 public static String getHeader(HttpMessage message, String name) {
552 return message.headers().get(name);
553 }
554
555
556
557
558
559
560
561
562
563
564 @Deprecated
565 public static String getHeader(HttpMessage message, CharSequence name) {
566 return message.headers().get(name);
567 }
568
569
570
571
572
573
574 @Deprecated
575 public static String getHeader(HttpMessage message, String name, String defaultValue) {
576 return message.headers().get(name, defaultValue);
577 }
578
579
580
581
582
583
584
585
586
587
588
589 @Deprecated
590 public static String getHeader(HttpMessage message, CharSequence name, String defaultValue) {
591 return message.headers().get(name, defaultValue);
592 }
593
594
595
596
597
598
599 @Deprecated
600 public static void setHeader(HttpMessage message, String name, Object value) {
601 message.headers().set(name, value);
602 }
603
604
605
606
607
608
609
610
611
612
613
614 @Deprecated
615 public static void setHeader(HttpMessage message, CharSequence name, Object value) {
616 message.headers().set(name, value);
617 }
618
619
620
621
622
623
624 @Deprecated
625 public static void setHeader(HttpMessage message, String name, Iterable<?> values) {
626 message.headers().set(name, values);
627 }
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645 @Deprecated
646 public static void setHeader(HttpMessage message, CharSequence name, Iterable<?> values) {
647 message.headers().set(name, values);
648 }
649
650
651
652
653
654
655 @Deprecated
656 public static void addHeader(HttpMessage message, String name, Object value) {
657 message.headers().add(name, value);
658 }
659
660
661
662
663
664
665
666
667
668
669 @Deprecated
670 public static void addHeader(HttpMessage message, CharSequence name, Object value) {
671 message.headers().add(name, value);
672 }
673
674
675
676
677
678
679 @Deprecated
680 public static void removeHeader(HttpMessage message, String name) {
681 message.headers().remove(name);
682 }
683
684
685
686
687
688
689 @Deprecated
690 public static void removeHeader(HttpMessage message, CharSequence name) {
691 message.headers().remove(name);
692 }
693
694
695
696
697
698
699 @Deprecated
700 public static void clearHeaders(HttpMessage message) {
701 message.headers().clear();
702 }
703
704
705
706
707
708
709 @Deprecated
710 public static int getIntHeader(HttpMessage message, String name) {
711 return getIntHeader(message, (CharSequence) name);
712 }
713
714
715
716
717
718
719
720
721
722
723
724
725 @Deprecated
726 public static int getIntHeader(HttpMessage message, CharSequence name) {
727 String value = message.headers().get(name);
728 if (value == null) {
729 throw new NumberFormatException("header not found: " + name);
730 }
731 return Integer.parseInt(value);
732 }
733
734
735
736
737
738
739 @Deprecated
740 public static int getIntHeader(HttpMessage message, String name, int defaultValue) {
741 return message.headers().getInt(name, defaultValue);
742 }
743
744
745
746
747
748
749
750
751
752
753
754 @Deprecated
755 public static int getIntHeader(HttpMessage message, CharSequence name, int defaultValue) {
756 return message.headers().getInt(name, defaultValue);
757 }
758
759
760
761
762
763
764 @Deprecated
765 public static void setIntHeader(HttpMessage message, String name, int value) {
766 message.headers().setInt(name, value);
767 }
768
769
770
771
772
773
774
775 @Deprecated
776 public static void setIntHeader(HttpMessage message, CharSequence name, int value) {
777 message.headers().setInt(name, value);
778 }
779
780
781
782
783
784
785 @Deprecated
786 public static void setIntHeader(HttpMessage message, String name, Iterable<Integer> values) {
787 message.headers().set(name, values);
788 }
789
790
791
792
793
794
795
796 @Deprecated
797 public static void setIntHeader(HttpMessage message, CharSequence name, Iterable<Integer> values) {
798 message.headers().set(name, values);
799 }
800
801
802
803
804
805
806 @Deprecated
807 public static void addIntHeader(HttpMessage message, String name, int value) {
808 message.headers().add(name, value);
809 }
810
811
812
813
814
815
816 @Deprecated
817 public static void addIntHeader(HttpMessage message, CharSequence name, int value) {
818 message.headers().addInt(name, value);
819 }
820
821
822
823
824
825
826 @Deprecated
827 public static Date getDateHeader(HttpMessage message, String name) throws ParseException {
828 return getDateHeader(message, (CharSequence) name);
829 }
830
831
832
833
834
835
836
837
838
839
840
841
842 @Deprecated
843 public static Date getDateHeader(HttpMessage message, CharSequence name) throws ParseException {
844 String value = message.headers().get(name);
845 if (value == null) {
846 throw new ParseException("header not found: " + name, 0);
847 }
848 Date date = DateFormatter.parseHttpDate(value);
849 if (date == null) {
850 throw new ParseException("header can't be parsed into a Date: " + value, 0);
851 }
852 return date;
853 }
854
855
856
857
858
859
860 @Deprecated
861 public static Date getDateHeader(HttpMessage message, String name, Date defaultValue) {
862 return getDateHeader(message, (CharSequence) name, defaultValue);
863 }
864
865
866
867
868
869
870
871
872
873
874
875 @Deprecated
876 public static Date getDateHeader(HttpMessage message, CharSequence name, Date defaultValue) {
877 final String value = getHeader(message, name);
878 Date date = DateFormatter.parseHttpDate(value);
879 return date != null ? date : defaultValue;
880 }
881
882
883
884
885
886
887 @Deprecated
888 public static void setDateHeader(HttpMessage message, String name, Date value) {
889 setDateHeader(message, (CharSequence) name, value);
890 }
891
892
893
894
895
896
897
898
899
900 @Deprecated
901 public static void setDateHeader(HttpMessage message, CharSequence name, Date value) {
902 if (value != null) {
903 message.headers().set(name, DateFormatter.format(value));
904 } else {
905 message.headers().set(name, null);
906 }
907 }
908
909
910
911
912
913
914 @Deprecated
915 public static void setDateHeader(HttpMessage message, String name, Iterable<Date> values) {
916 message.headers().set(name, values);
917 }
918
919
920
921
922
923
924
925
926
927 @Deprecated
928 public static void setDateHeader(HttpMessage message, CharSequence name, Iterable<Date> values) {
929 message.headers().set(name, values);
930 }
931
932
933
934
935
936
937 @Deprecated
938 public static void addDateHeader(HttpMessage message, String name, Date value) {
939 message.headers().add(name, value);
940 }
941
942
943
944
945
946
947
948
949 @Deprecated
950 public static void addDateHeader(HttpMessage message, CharSequence name, Date value) {
951 message.headers().add(name, value);
952 }
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968 @Deprecated
969 public static long getContentLength(HttpMessage message) {
970 return HttpUtil.getContentLength(message);
971 }
972
973
974
975
976
977
978
979
980
981
982
983
984
985 @Deprecated
986 public static long getContentLength(HttpMessage message, long defaultValue) {
987 return HttpUtil.getContentLength(message, defaultValue);
988 }
989
990
991
992
993 @Deprecated
994 public static void setContentLength(HttpMessage message, long length) {
995 HttpUtil.setContentLength(message, length);
996 }
997
998
999
1000
1001
1002
1003 @Deprecated
1004 public static String getHost(HttpMessage message) {
1005 return message.headers().get(HttpHeaderNames.HOST);
1006 }
1007
1008
1009
1010
1011
1012
1013
1014 @Deprecated
1015 public static String getHost(HttpMessage message, String defaultValue) {
1016 return message.headers().get(HttpHeaderNames.HOST, defaultValue);
1017 }
1018
1019
1020
1021
1022
1023
1024 @Deprecated
1025 public static void setHost(HttpMessage message, String value) {
1026 message.headers().set(HttpHeaderNames.HOST, value);
1027 }
1028
1029
1030
1031
1032
1033
1034 @Deprecated
1035 public static void setHost(HttpMessage message, CharSequence value) {
1036 message.headers().set(HttpHeaderNames.HOST, value);
1037 }
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047 @Deprecated
1048 public static Date getDate(HttpMessage message) throws ParseException {
1049 return getDateHeader(message, HttpHeaderNames.DATE);
1050 }
1051
1052
1053
1054
1055
1056
1057
1058
1059 @Deprecated
1060 public static Date getDate(HttpMessage message, Date defaultValue) {
1061 return getDateHeader(message, HttpHeaderNames.DATE, defaultValue);
1062 }
1063
1064
1065
1066
1067
1068
1069 @Deprecated
1070 public static void setDate(HttpMessage message, Date value) {
1071 message.headers().set(HttpHeaderNames.DATE, value);
1072 }
1073
1074
1075
1076
1077
1078
1079
1080 @Deprecated
1081 public static boolean is100ContinueExpected(HttpMessage message) {
1082 return HttpUtil.is100ContinueExpected(message);
1083 }
1084
1085
1086
1087
1088
1089
1090
1091
1092 @Deprecated
1093 public static void set100ContinueExpected(HttpMessage message) {
1094 HttpUtil.set100ContinueExpected(message, true);
1095 }
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106 @Deprecated
1107 public static void set100ContinueExpected(HttpMessage message, boolean set) {
1108 HttpUtil.set100ContinueExpected(message, set);
1109 }
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119 @Deprecated
1120 public static boolean isTransferEncodingChunked(HttpMessage message) {
1121 return HttpUtil.isTransferEncodingChunked(message);
1122 }
1123
1124
1125
1126
1127 @Deprecated
1128 public static void removeTransferEncodingChunked(HttpMessage m) {
1129 HttpUtil.setTransferEncodingChunked(m, false);
1130 }
1131
1132
1133
1134
1135 @Deprecated
1136 public static void setTransferEncodingChunked(HttpMessage m) {
1137 HttpUtil.setTransferEncodingChunked(m, true);
1138 }
1139
1140
1141
1142
1143 @Deprecated
1144 public static boolean isContentLengthSet(HttpMessage m) {
1145 return HttpUtil.isContentLengthSet(m);
1146 }
1147
1148
1149
1150
1151 @Deprecated
1152 public static boolean equalsIgnoreCase(CharSequence name1, CharSequence name2) {
1153 return contentEqualsIgnoreCase(name1, name2);
1154 }
1155
1156 @Deprecated
1157 public static void encodeAscii(CharSequence seq, ByteBuf buf) {
1158 if (seq instanceof AsciiString) {
1159 ByteBufUtil.copy((AsciiString) seq, 0, buf, seq.length());
1160 } else {
1161 buf.writeCharSequence(seq, CharsetUtil.US_ASCII);
1162 }
1163 }
1164
1165
1166
1167
1168
1169
1170
1171 @Deprecated
1172 public static CharSequence newEntity(String name) {
1173 return new AsciiString(name);
1174 }
1175
1176 protected HttpHeaders() { }
1177
1178
1179
1180
1181 public abstract String get(String name);
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191 public String get(CharSequence name) {
1192 return get(name.toString());
1193 }
1194
1195
1196
1197
1198
1199
1200
1201
1202 public String get(CharSequence name, String defaultValue) {
1203 String value = get(name);
1204 if (value == null) {
1205 return defaultValue;
1206 }
1207 return value;
1208 }
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218 public abstract Integer getInt(CharSequence name);
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229 public abstract int getInt(CharSequence name, int defaultValue);
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239 public abstract Short getShort(CharSequence name);
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250 public abstract short getShort(CharSequence name, short defaultValue);
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260 public abstract Long getTimeMillis(CharSequence name);
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271 public abstract long getTimeMillis(CharSequence name, long defaultValue);
1272
1273
1274
1275
1276 public abstract List<String> getAll(String name);
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286 public List<String> getAll(CharSequence name) {
1287 return getAll(name.toString());
1288 }
1289
1290
1291
1292
1293
1294
1295
1296 public abstract List<Map.Entry<String, String>> entries();
1297
1298
1299
1300
1301 public abstract boolean contains(String name);
1302
1303
1304
1305
1306
1307 @Deprecated
1308 @Override
1309 public abstract Iterator<Entry<String, String>> iterator();
1310
1311
1312
1313
1314 public abstract Iterator<Entry<CharSequence, CharSequence>> iteratorCharSequence();
1315
1316
1317
1318
1319
1320
1321 public Iterator<String> valueStringIterator(CharSequence name) {
1322 return getAll(name).iterator();
1323 }
1324
1325
1326
1327
1328
1329
1330 public Iterator<? extends CharSequence> valueCharSequenceIterator(CharSequence name) {
1331 return valueStringIterator(name);
1332 }
1333
1334
1335
1336
1337
1338
1339
1340 public boolean contains(CharSequence name) {
1341 return contains(name.toString());
1342 }
1343
1344
1345
1346
1347 public abstract boolean isEmpty();
1348
1349
1350
1351
1352 public abstract int size();
1353
1354
1355
1356
1357
1358
1359 public abstract Set<String> names();
1360
1361
1362
1363
1364 public abstract HttpHeaders add(String name, Object value);
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379 public HttpHeaders add(CharSequence name, Object value) {
1380 return add(name.toString(), value);
1381 }
1382
1383
1384
1385
1386 public abstract HttpHeaders add(String name, Iterable<?> values);
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405 public HttpHeaders add(CharSequence name, Iterable<?> values) {
1406 return add(name.toString(), values);
1407 }
1408
1409
1410
1411
1412
1413
1414 public HttpHeaders add(HttpHeaders headers) {
1415 if (headers == null) {
1416 throw new NullPointerException("headers");
1417 }
1418 for (Map.Entry<String, String> e: headers) {
1419 add(e.getKey(), e.getValue());
1420 }
1421 return this;
1422 }
1423
1424
1425
1426
1427
1428
1429
1430 public abstract HttpHeaders addInt(CharSequence name, int value);
1431
1432
1433
1434
1435
1436
1437
1438 public abstract HttpHeaders addShort(CharSequence name, short value);
1439
1440
1441
1442
1443 public abstract HttpHeaders set(String name, Object value);
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458 public HttpHeaders set(CharSequence name, Object value) {
1459 return set(name.toString(), value);
1460 }
1461
1462
1463
1464
1465 public abstract HttpHeaders set(String name, Iterable<?> values);
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486 public HttpHeaders set(CharSequence name, Iterable<?> values) {
1487 return set(name.toString(), values);
1488 }
1489
1490
1491
1492
1493
1494
1495 public HttpHeaders set(HttpHeaders headers) {
1496 checkNotNull(headers, "headers");
1497
1498 clear();
1499
1500 if (headers.isEmpty()) {
1501 return this;
1502 }
1503
1504 for (Entry<String, String> entry : headers) {
1505 add(entry.getKey(), entry.getValue());
1506 }
1507 return this;
1508 }
1509
1510
1511
1512
1513
1514
1515
1516 public HttpHeaders setAll(HttpHeaders headers) {
1517 checkNotNull(headers, "headers");
1518
1519 if (headers.isEmpty()) {
1520 return this;
1521 }
1522
1523 for (Entry<String, String> entry : headers) {
1524 set(entry.getKey(), entry.getValue());
1525 }
1526 return this;
1527 }
1528
1529
1530
1531
1532
1533
1534
1535 public abstract HttpHeaders setInt(CharSequence name, int value);
1536
1537
1538
1539
1540
1541
1542
1543 public abstract HttpHeaders setShort(CharSequence name, short value);
1544
1545
1546
1547
1548 public abstract HttpHeaders remove(String name);
1549
1550
1551
1552
1553
1554
1555
1556 public HttpHeaders remove(CharSequence name) {
1557 return remove(name.toString());
1558 }
1559
1560
1561
1562
1563
1564
1565 public abstract HttpHeaders clear();
1566
1567
1568
1569
1570 public boolean contains(String name, String value, boolean ignoreCase) {
1571 Iterator<String> valueIterator = valueStringIterator(name);
1572 if (ignoreCase) {
1573 while (valueIterator.hasNext()) {
1574 if (valueIterator.next().equalsIgnoreCase(value)) {
1575 return true;
1576 }
1577 }
1578 } else {
1579 while (valueIterator.hasNext()) {
1580 if (valueIterator.next().equals(value)) {
1581 return true;
1582 }
1583 }
1584 }
1585 return false;
1586 }
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598 public boolean containsValue(CharSequence name, CharSequence value, boolean ignoreCase) {
1599 Iterator<? extends CharSequence> itr = valueCharSequenceIterator(name);
1600 while (itr.hasNext()) {
1601 if (containsCommaSeparatedTrimmed(itr.next(), value, ignoreCase)) {
1602 return true;
1603 }
1604 }
1605 return false;
1606 }
1607
1608 private static boolean containsCommaSeparatedTrimmed(CharSequence rawNext, CharSequence expected,
1609 boolean ignoreCase) {
1610 int begin = 0;
1611 int end;
1612 if (ignoreCase) {
1613 if ((end = AsciiString.indexOf(rawNext, ',', begin)) == -1) {
1614 if (contentEqualsIgnoreCase(trim(rawNext), expected)) {
1615 return true;
1616 }
1617 } else {
1618 do {
1619 if (contentEqualsIgnoreCase(trim(rawNext.subSequence(begin, end)), expected)) {
1620 return true;
1621 }
1622 begin = end + 1;
1623 } while ((end = AsciiString.indexOf(rawNext, ',', begin)) != -1);
1624
1625 if (begin < rawNext.length()) {
1626 if (contentEqualsIgnoreCase(trim(rawNext.subSequence(begin, rawNext.length())), expected)) {
1627 return true;
1628 }
1629 }
1630 }
1631 } else {
1632 if ((end = AsciiString.indexOf(rawNext, ',', begin)) == -1) {
1633 if (contentEquals(trim(rawNext), expected)) {
1634 return true;
1635 }
1636 } else {
1637 do {
1638 if (contentEquals(trim(rawNext.subSequence(begin, end)), expected)) {
1639 return true;
1640 }
1641 begin = end + 1;
1642 } while ((end = AsciiString.indexOf(rawNext, ',', begin)) != -1);
1643
1644 if (begin < rawNext.length()) {
1645 if (contentEquals(trim(rawNext.subSequence(begin, rawNext.length())), expected)) {
1646 return true;
1647 }
1648 }
1649 }
1650 }
1651 return false;
1652 }
1653
1654
1655
1656
1657
1658
1659 public final String getAsString(CharSequence name) {
1660 return get(name);
1661 }
1662
1663
1664
1665
1666
1667
1668 public final List<String> getAllAsString(CharSequence name) {
1669 return getAll(name);
1670 }
1671
1672
1673
1674
1675 public final Iterator<Entry<String, String>> iteratorAsString() {
1676 return iterator();
1677 }
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688 public boolean contains(CharSequence name, CharSequence value, boolean ignoreCase) {
1689 return contains(name.toString(), value.toString(), ignoreCase);
1690 }
1691
1692 @Override
1693 public String toString() {
1694 return HeadersUtils.toString(getClass(), iteratorCharSequence(), size());
1695 }
1696
1697
1698
1699
1700 public HttpHeaders copy() {
1701 return new DefaultHttpHeaders().set(this);
1702 }
1703 }