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