-
Notifications
You must be signed in to change notification settings - Fork 896
/
ArmeriaHttpUtil.java
1380 lines (1226 loc) · 59.4 KB
/
ArmeriaHttpUtil.java
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright 2014 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.linecorp.armeria.internal.common;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static io.netty.util.AsciiString.EMPTY_STRING;
import static io.netty.util.ByteProcessor.FIND_COMMA;
import static io.netty.util.internal.StringUtil.decodeHexNibble;
import static java.util.Objects.requireNonNull;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringJoiner;
import java.util.function.BiConsumer;
import java.util.regex.Pattern;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Ascii;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.linecorp.armeria.common.Flags;
import com.linecorp.armeria.common.Http1HeaderNaming;
import com.linecorp.armeria.common.HttpData;
import com.linecorp.armeria.common.HttpHeaderNames;
import com.linecorp.armeria.common.HttpHeaders;
import com.linecorp.armeria.common.HttpHeadersBuilder;
import com.linecorp.armeria.common.HttpMethod;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.common.RequestHeaders;
import com.linecorp.armeria.common.RequestHeadersBuilder;
import com.linecorp.armeria.common.RequestTarget;
import com.linecorp.armeria.common.ResponseHeaders;
import com.linecorp.armeria.common.ResponseHeadersBuilder;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.util.Version;
import com.linecorp.armeria.internal.common.util.TemporaryThreadLocals;
import com.linecorp.armeria.server.ServerConfig;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.unix.DomainSocketAddress;
import io.netty.handler.codec.DefaultHeaders;
import io.netty.handler.codec.UnsupportedValueConverter;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.EmptyHttpHeaders;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http2.DefaultHttp2Headers;
import io.netty.handler.codec.http2.Http2Headers;
import io.netty.handler.codec.http2.HttpConversionUtil;
import io.netty.handler.codec.http2.HttpConversionUtil.ExtensionHeaderNames;
import io.netty.util.AsciiString;
import io.netty.util.HashingStrategy;
import io.netty.util.internal.StringUtil;
/**
* Provides various utility functions for internal use related with HTTP.
*
* <p>The conversion between HTTP/1 and HTTP/2 has been forked from Netty's {@link HttpConversionUtil}.
*/
public final class ArmeriaHttpUtil {
// Forked from Netty 4.1.34 at 4921f62c8ab8205fd222439dcd1811760b05daf1
/**
* The default case-insensitive {@link AsciiString} hasher and comparator for HTTP/2 headers.
*/
private static final HashingStrategy<AsciiString> HTTP2_HEADER_NAME_HASHER =
new HashingStrategy<AsciiString>() {
@Override
public int hashCode(AsciiString o) {
return o.hashCode();
}
@Override
public boolean equals(AsciiString a, AsciiString b) {
return a.contentEqualsIgnoreCase(b);
}
};
/**
* The default HTTP content-type charset.
*
* <p>Note that we use {@link StandardCharsets#UTF_8} as default because it is common practice even though
* it's not the HTTP standard.
*/
public static final Charset HTTP_DEFAULT_CONTENT_CHARSET = StandardCharsets.UTF_8;
/**
* The old {@code "proxy-connection"} header which has been superceded by {@code "connection"}.
*/
public static final AsciiString HEADER_NAME_PROXY_CONNECTION = AsciiString.cached("proxy-connection");
/**
* The set of headers that should not be directly copied when converting headers from HTTP/1 to HTTP/2.
*/
private static final CaseInsensitiveMap HTTP_TO_HTTP2_HEADER_DISALLOWED_LIST = new CaseInsensitiveMap();
/**
* The set of headers that should not be directly copied when converting headers from HTTP/2 to HTTP/1.
*/
private static final CaseInsensitiveMap HTTP2_TO_HTTP_HEADER_DISALLOWED_LIST = new CaseInsensitiveMap();
/**
* The set of headers that must not be directly copied when converting trailers.
*/
private static final CaseInsensitiveMap HTTP_TRAILER_DISALLOWED_LIST = new CaseInsensitiveMap();
static {
HTTP_TO_HTTP2_HEADER_DISALLOWED_LIST.add(HttpHeaderNames.CONNECTION, EMPTY_STRING);
HTTP_TO_HTTP2_HEADER_DISALLOWED_LIST.add(HttpHeaderNames.KEEP_ALIVE, EMPTY_STRING);
HTTP_TO_HTTP2_HEADER_DISALLOWED_LIST.add(HEADER_NAME_PROXY_CONNECTION, EMPTY_STRING);
HTTP_TO_HTTP2_HEADER_DISALLOWED_LIST.add(HttpHeaderNames.TRANSFER_ENCODING, EMPTY_STRING);
HTTP_TO_HTTP2_HEADER_DISALLOWED_LIST.add(HttpHeaderNames.UPGRADE, EMPTY_STRING);
HTTP_TO_HTTP2_HEADER_DISALLOWED_LIST.add(ExtensionHeaderNames.STREAM_ID.text(), EMPTY_STRING);
HTTP_TO_HTTP2_HEADER_DISALLOWED_LIST.add(ExtensionHeaderNames.SCHEME.text(), EMPTY_STRING);
HTTP_TO_HTTP2_HEADER_DISALLOWED_LIST.add(ExtensionHeaderNames.PATH.text(), EMPTY_STRING);
// https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.3
HTTP2_TO_HTTP_HEADER_DISALLOWED_LIST.add(HttpHeaderNames.AUTHORITY, EMPTY_STRING);
HTTP2_TO_HTTP_HEADER_DISALLOWED_LIST.add(HttpHeaderNames.METHOD, EMPTY_STRING);
HTTP2_TO_HTTP_HEADER_DISALLOWED_LIST.add(HttpHeaderNames.PATH, EMPTY_STRING);
HTTP2_TO_HTTP_HEADER_DISALLOWED_LIST.add(HttpHeaderNames.SCHEME, EMPTY_STRING);
HTTP2_TO_HTTP_HEADER_DISALLOWED_LIST.add(HttpHeaderNames.STATUS, EMPTY_STRING);
// https://datatracker.ietf.org/doc/html/rfc7540#section-8.1
// The "chunked" transfer encoding defined in Section 4.1 of [RFC7230] MUST NOT be used in HTTP/2.
HTTP2_TO_HTTP_HEADER_DISALLOWED_LIST.add(HttpHeaderNames.TRANSFER_ENCODING, EMPTY_STRING);
HTTP2_TO_HTTP_HEADER_DISALLOWED_LIST.add(ExtensionHeaderNames.STREAM_ID.text(), EMPTY_STRING);
HTTP2_TO_HTTP_HEADER_DISALLOWED_LIST.add(ExtensionHeaderNames.SCHEME.text(), EMPTY_STRING);
HTTP2_TO_HTTP_HEADER_DISALLOWED_LIST.add(ExtensionHeaderNames.PATH.text(), EMPTY_STRING);
// https://datatracker.ietf.org/doc/html/rfc7230#section-4.1.2
// https://datatracker.ietf.org/doc/html/rfc7540#section-8.1
// A sender MUST NOT generate a trailer that contains a field necessary for message framing:
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.TRANSFER_ENCODING, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.CONTENT_LENGTH, EMPTY_STRING);
// for request modifiers:
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.CACHE_CONTROL, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.EXPECT, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.HOST, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.MAX_FORWARDS, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.PRAGMA, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.RANGE, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.TE, EMPTY_STRING);
// for authentication:
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.WWW_AUTHENTICATE, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.AUTHORIZATION, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.PROXY_AUTHENTICATE, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.PROXY_AUTHORIZATION, EMPTY_STRING);
// for response control data:
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.DATE, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.LOCATION, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.RETRY_AFTER, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.VARY, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.WARNING, EMPTY_STRING);
// or for determining how to process the payload:
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.CONTENT_ENCODING, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.CONTENT_TYPE, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.CONTENT_RANGE, EMPTY_STRING);
HTTP_TRAILER_DISALLOWED_LIST.add(HttpHeaderNames.TRAILER, EMPTY_STRING);
}
static final Set<AsciiString> ADDITIONAL_REQUEST_HEADER_DISALLOWED_LIST = ImmutableSet.of(
HttpHeaderNames.SCHEME, HttpHeaderNames.STATUS, HttpHeaderNames.METHOD);
private static final Set<AsciiString> REQUEST_PSEUDO_HEADERS = ImmutableSet.of(
HttpHeaderNames.METHOD, HttpHeaderNames.SCHEME, HttpHeaderNames.AUTHORITY,
HttpHeaderNames.PATH, HttpHeaderNames.PROTOCOL);
private static final Set<AsciiString> PSEUDO_HEADERS = ImmutableSet.<AsciiString>builder()
.addAll(REQUEST_PSEUDO_HEADERS)
.add(HttpHeaderNames.STATUS)
.build();
public static final String SERVER_HEADER =
"Armeria/" + Version.get("armeria", ArmeriaHttpUtil.class.getClassLoader())
.artifactVersion();
/**
* Translations from HTTP/2 header name to the HTTP/1.x equivalent. Currently, we expect these headers to
* only allow a single value in the request. If adding headers that can potentially have multiple values,
* please check the usage in code accordingly.
*/
private static final CaseInsensitiveMap REQUEST_HEADER_TRANSLATIONS = new CaseInsensitiveMap();
static {
REQUEST_HEADER_TRANSLATIONS.add(Http2Headers.PseudoHeaderName.AUTHORITY.value(),
HttpHeaderNames.HOST);
}
private static final Splitter COOKIE_SPLITTER = Splitter.on(';').trimResults().omitEmptyStrings();
private static final String COOKIE_SEPARATOR = "; ";
private static final Joiner COOKIE_JOINER = Joiner.on(COOKIE_SEPARATOR);
@Nullable
private static final LoadingCache<AsciiString, String> HEADER_VALUE_CACHE =
Flags.headerValueCacheSpec() != null ? buildCache(Flags.headerValueCacheSpec()) : null;
private static final Set<AsciiString> CACHED_HEADERS = Flags.cachedHeaders().stream().map(AsciiString::of)
.collect(toImmutableSet());
/**
* Validator for the scheme part of the URI, as defined in
* <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.1">the section 3.1 of RFC3986</a>.
*/
private static final Pattern SCHEME_PATTERN = Pattern.compile("^([a-zA-Z][a-zA-Z0-9+\\-.]*)");
private static LoadingCache<AsciiString, String> buildCache(String spec) {
return Caffeine.from(spec).build(AsciiString::toString);
}
/**
* Returns normalized scheme.
*
* @throws IllegalArgumentException if the provided {@code scheme} does not conform to the criteria
* specified in RFC 3986.
*/
public static String schemeValidateAndNormalize(String scheme) {
final boolean isValidScheme = SCHEME_PATTERN.matcher(scheme).matches();
if (isValidScheme) {
return Ascii.toLowerCase(scheme);
} else {
throw new IllegalArgumentException("scheme: " + scheme + " (expected: a valid scheme)");
}
}
/**
* Returns the index of the authority part if the specified {@code reqTarget} is an absolute URI.
* Returns {@code -1} otherwise.
*/
public static int findAuthority(String reqTarget) {
final int firstColonIdx = reqTarget.indexOf(':');
if (firstColonIdx <= 0 || reqTarget.length() <= firstColonIdx + 3) {
return -1;
}
final int firstSlashIdx = reqTarget.indexOf('/');
if (firstSlashIdx <= 0 || firstSlashIdx < firstColonIdx) {
return -1;
}
if (reqTarget.charAt(firstColonIdx + 1) == '/' && reqTarget.charAt(firstColonIdx + 2) == '/') {
return firstColonIdx + 3;
}
return -1;
}
/**
* Concatenates the specified {@code prefix} and {@code path} into an absolute path.
*
* @throws IllegalArgumentException if {@code prefix} is not an absolute path prefix
*/
public static String concatPaths(String prefix, @Nullable String path) {
requireNonNull(prefix, "prefix");
checkArgument(!prefix.isEmpty() && prefix.charAt(0) == '/',
"prefix: %s (expected: an absolute path starting with '/')", prefix);
path = firstNonNull(path, "");
if (path.isEmpty()) {
return prefix;
}
if (prefix.length() == 1) { // means "/".equals(prefix)
if (path.charAt(0) == '/') {
return path; // Most requests will land here.
}
return simpleConcat("/", path);
}
return slowConcatPaths(prefix, path);
}
private static String slowConcatPaths(String prefix, String path) {
if (prefix.charAt(prefix.length() - 1) == '/') {
if (path.charAt(0) == '/') {
// `prefix` ends with '/' and `path` starts with '/'.
// Avoid double-slash by stripping the first slash of `path`.
try (TemporaryThreadLocals tmp = TemporaryThreadLocals.acquire()) {
return tmp.stringBuilder()
.append(prefix)
.append(path, 1, path.length())
.toString();
}
}
// `prefix` ends with '/' and `path` does not start with '/'.
// Simple concatenation would suffice.
return simpleConcat(prefix, path);
}
if (path.charAt(0) == '/' || path.charAt(0) == '?') {
// `prefix` does not end with '/' and `path` starts with '/' or '?'
// Simple concatenation would suffice.
return simpleConcat(prefix, path);
}
// `prefix` does not end with '/' and `path` does not start with '/' or '?'.
// Need to insert '/' in-between.
try (TemporaryThreadLocals tmp = TemporaryThreadLocals.acquire()) {
return tmp.stringBuilder()
.append(prefix)
.append('/')
.append(path)
.toString();
}
}
private static String simpleConcat(String prefix, String path) {
try (TemporaryThreadLocals tmp = TemporaryThreadLocals.acquire()) {
return tmp.stringBuilder()
.append(prefix)
.append(path)
.toString();
}
}
/**
* Decodes a percent-encoded path string.
*/
public static String decodePath(String path) {
if (path.indexOf('%') < 0) {
// No need to decode because it's not percent-encoded
return path;
}
// Decode percent-encoded characters, but don't decode %2F into /, so that a user can choose
// to use it as a non-separator.
//
// For example, for the path pattern `/orgs/{org_name}/agents/{agent_name}`:
// - orgs/mi6/agents/ethan-hunt
// - org_name: mi6
// - agent_name: ethan-hunt
// - orgs/mi%2F6/agents/ethan-hunt
// - org_name: mi/6
// - agent_name: ethan-hunt
return slowDecodePath(path, false);
}
/**
* Decodes a single percent-encoded path parameter.
*/
public static String decodePathParam(String pathParam) {
if (pathParam.indexOf('%') < 0) {
// No need to decode because it's not percent-encoded
return pathParam;
}
// Decode percent-encoded characters.
return slowDecodePath(pathParam, true);
}
private static String slowDecodePath(String path, boolean decodeSlash) {
// An invalid character is replaced with 0xFF, which will be replaced into '�' by UTF-8 decoder.
final int len = path.length();
try (TemporaryThreadLocals tempThreadLocals = TemporaryThreadLocals.acquire()) {
final byte[] buf = tempThreadLocals.byteArray(len);
int dstLen = 0;
for (int i = 0; i < len; i++) {
final char ch = path.charAt(i);
if (ch != '%') {
buf[dstLen++] = (byte) ((ch & 0xFF80) == 0 ? ch : 0xFF);
continue;
}
// Decode a percent-encoded character.
final int hexEnd = i + 3;
if (hexEnd > len) {
// '%' or '%x' (must be followed by two hexadigits)
buf[dstLen++] = (byte) 0xFF;
break;
}
final int digit1 = decodeHexNibble(path.charAt(++i));
final int digit2 = decodeHexNibble(path.charAt(++i));
if (digit1 < 0 || digit2 < 0) {
// The first or second digit is not hexadecimal.
buf[dstLen++] = (byte) 0xFF;
} else {
final byte decoded = (byte) ((digit1 << 4) | digit2);
if (decodeSlash || decoded != 0x2F) {
buf[dstLen++] = decoded;
} else {
buf[dstLen++] = '%';
buf[dstLen++] = '2';
buf[dstLen++] = (byte) path.charAt(i); // f or F - preserve the case.
}
}
}
return new String(buf, 0, dstLen, StandardCharsets.UTF_8);
}
}
/**
* Returns {@code true} if the specified {@code path} is an absolute {@code URI}.
*/
public static boolean isAbsoluteUri(@Nullable String maybeUri) {
if (maybeUri == null) {
return false;
}
final int firstColonIdx = maybeUri.indexOf(':');
if (firstColonIdx <= 0 || firstColonIdx + 3 >= maybeUri.length()) {
return false;
}
final int firstSlashIdx = maybeUri.indexOf('/');
if (firstSlashIdx <= 0 || firstSlashIdx < firstColonIdx) {
return false;
}
return maybeUri.charAt(firstColonIdx + 1) == '/' && maybeUri.charAt(firstColonIdx + 2) == '/';
}
/**
* Returns {@code true} if the specified HTTP status string represents an informational status.
*/
public static boolean isInformational(@Nullable String statusText) {
return statusText != null && !statusText.isEmpty() && statusText.charAt(0) == '1';
}
/**
* Returns {@code true} if the content of the response with the given {@link HttpStatus} is one of
* {@link HttpStatus#NO_CONTENT}, {@link HttpStatus#RESET_CONTENT} and {@link HttpStatus#NOT_MODIFIED}.
*
* @throws IllegalArgumentException if the specified {@code content} is not empty when the specified
* {@link HttpStatus} is one of {@link HttpStatus#NO_CONTENT},
* {@link HttpStatus#RESET_CONTENT} and {@link HttpStatus#NOT_MODIFIED}.
*/
public static boolean isContentAlwaysEmptyWithValidation(HttpStatus status, HttpData content) {
if (!status.isContentAlwaysEmpty()) {
return false;
}
if (!content.isEmpty()) {
throw new IllegalArgumentException(
"A " + status + " response must have empty content: " + content.length() + " byte(s)");
}
return true;
}
/**
* Returns {@code true} if the specified {@code headers} is a CORS preflight request.
*/
public static boolean isCorsPreflightRequest(RequestHeaders headers) {
requireNonNull(headers, "headers");
return headers.method() == HttpMethod.OPTIONS &&
headers.contains(HttpHeaderNames.ORIGIN) &&
headers.contains(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD);
}
/**
* Returns the disallowed response headers.
*/
@VisibleForTesting
static Set<AsciiString> disallowedResponseHeaderNames() {
// Request Pseudo-Headers are not allowed for response headers.
// https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.3
return REQUEST_PSEUDO_HEADERS;
}
/**
* Parses the specified HTTP header directives and invokes the specified {@code callback}
* with the directive names and values.
*/
public static void parseDirectives(String directives, BiConsumer<String, String> callback) {
final int len = directives.length();
for (int i = 0; i < len;) {
final int nameStart = i;
final String name;
final String value;
// Find the name.
for (; i < len; i++) {
final char ch = directives.charAt(i);
if (ch == ',' || ch == '=') {
break;
}
}
name = directives.substring(nameStart, i).trim();
// Find the value.
if (i == len || directives.charAt(i) == ',') {
// Skip comma or go beyond 'len' to break the loop.
i++;
value = null;
} else {
// Skip '='.
i++;
// Skip whitespaces.
for (; i < len; i++) {
final char ch = directives.charAt(i);
if (ch != ' ' && ch != '\t') {
break;
}
}
if (i < len && directives.charAt(i) == '\"') {
// Handle quoted string.
// Skip the opening quote.
i++;
final int valueStart = i;
// Find the closing quote.
for (; i < len; i++) {
if (directives.charAt(i) == '\"') {
break;
}
}
value = directives.substring(valueStart, i);
// Skip the closing quote.
i++;
// Find the comma and skip it.
for (; i < len; i++) {
if (directives.charAt(i) == ',') {
i++;
break;
}
}
} else {
// Handle unquoted string.
final int valueStart = i;
// Find the comma.
for (; i < len; i++) {
if (directives.charAt(i) == ',') {
break;
}
}
value = directives.substring(valueStart, i).trim();
// Skip the comma.
i++;
}
}
if (!name.isEmpty()) {
callback.accept(Ascii.toLowerCase(name), Strings.emptyToNull(value));
}
}
}
/**
* Converts the specified HTTP header directive value into a long integer.
*
* @return the converted value if {@code value} is equal to or greater than {@code 0}.
* {@code -1} otherwise, i.e. if a negative integer or not a number.
*/
public static long parseDirectiveValueAsSeconds(@Nullable String value) {
if (value == null) {
return -1;
}
try {
final long converted = Long.parseLong(value);
return converted >= 0 ? converted : -1;
} catch (NumberFormatException e) {
return -1;
}
}
/**
* Converts the specified Netty HTTP/2 into Armeria HTTP/2 {@link RequestHeaders}.
*/
public static RequestHeaders toArmeriaRequestHeaders(ChannelHandlerContext ctx, Http2Headers headers,
boolean endOfStream, String scheme,
ServerConfig cfg,
RequestTarget reqTarget) {
assert headers instanceof ArmeriaHttp2Headers;
final HttpHeadersBuilder builder = ((ArmeriaHttp2Headers) headers).delegate();
builder.endOfStream(endOfStream);
if (!builder.contains(HttpHeaderNames.CONTENT_LENGTH)) {
// `isContentLengthUnknown` is set to true so as not to automatically fill the content-length when
// the HTTP objects are aggregated.
builder.contentLengthUnknown();
}
// A CONNECT request might not have ":scheme". See https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.3
if (!builder.contains(HttpHeaderNames.SCHEME)) {
builder.add(HttpHeaderNames.SCHEME, scheme);
}
if (builder.get(HttpHeaderNames.AUTHORITY) == null && builder.get(HttpHeaderNames.HOST) == null) {
builder.add(HttpHeaderNames.AUTHORITY, defaultAuthority(ctx, cfg));
}
builder.set(HttpHeaderNames.PATH, reqTarget.toString());
final List<String> cookies = builder.getAll(HttpHeaderNames.COOKIE);
if (cookies.size() > 1) {
// Cookies must be concatenated into a single octet string.
// https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.5
builder.set(HttpHeaderNames.COOKIE, COOKIE_JOINER.join(cookies));
}
return RequestHeaders.of(builder.build());
}
private static String defaultAuthority(ChannelHandlerContext ctx, ServerConfig cfg) {
// The client violates the spec that the request headers must contain a Host header.
// But we just add Host header to allow the request.
// https://datatracker.ietf.org/doc/html/rfc7230#section-5.4
final String defaultHostname = cfg.defaultVirtualHost().defaultHostname();
final SocketAddress localAddr = ctx.channel().localAddress();
if (localAddr instanceof InetSocketAddress) {
return defaultHostname + ':' + ((InetSocketAddress) localAddr).getPort();
} else {
assert localAddr instanceof DomainSocketAddress : localAddr;
return defaultHostname;
}
}
/**
* Converts the specified Netty HTTP/2 into Armeria HTTP/2 headers.
*/
public static HttpHeaders toArmeria(Http2Headers http2Headers, boolean request, boolean endOfStream) {
assert http2Headers instanceof ArmeriaHttp2Headers;
final HttpHeadersBuilder delegate = ((ArmeriaHttp2Headers) http2Headers).delegate();
delegate.endOfStream(endOfStream);
maybeSetContentLengthUnknown(delegate.contains(HttpHeaderNames.CONTENT_LENGTH), delegate);
HttpHeaders headers = delegate.build();
if (request) {
if (headers.contains(HttpHeaderNames.METHOD)) {
headers = RequestHeaders.of(headers);
}
// http2Headers should be a trailers
} else {
if (headers.contains(HttpHeaderNames.STATUS)) {
headers = ResponseHeaders.of(headers);
}
}
return headers;
}
/**
* Converts the headers of the given Netty HTTP/1.x request into Armeria HTTP/2 headers.
* The following headers are only used if they can not be found in the {@code HOST} header or the
* {@code Request-Line} as defined by <a href="https://datatracker.ietf.org/doc/rfc7230/">rfc7230</a>
* <ul>
* <li>{@link ExtensionHeaderNames#SCHEME}</li>
* </ul>
* {@link ExtensionHeaderNames#PATH} is ignored and instead extracted from the {@code Request-Line}.
*/
public static RequestHeaders toArmeria(
ChannelHandlerContext ctx, HttpRequest in, RequestHeadersBuilder out,
ServerConfig cfg, String scheme, RequestTarget reqTarget) throws URISyntaxException {
final io.netty.handler.codec.http.HttpHeaders inHeaders = in.headers();
out.method(firstNonNull(HttpMethod.tryParse(in.method().name()), HttpMethod.UNKNOWN))
.scheme(scheme)
.path(reqTarget.toString());
if (!out.contains(HttpHeaderNames.HOST)) {
out.add(HttpHeaderNames.HOST, defaultAuthority(ctx, cfg));
}
purgeHttp1OnlyHeaders(inHeaders, out);
return out.build();
}
/**
* Converts the headers of the given Netty HTTP/1.x response into Armeria HTTP/2 headers.
*/
public static ResponseHeaders toArmeria(HttpResponse in) {
final io.netty.handler.codec.http.HttpHeaders inHeaders = in.headers();
final ResponseHeadersBuilder out = ResponseHeaders.builder();
out.sizeHint(inHeaders.size());
out.status(HttpStatus.valueOf(in.status().code()));
// Add the HTTP headers which have not been consumed above
toArmeria(inHeaders, out);
return out.build();
}
/**
* Converts the specified Netty HTTP/1 headers into Armeria HTTP/2 headers.
*/
public static HttpHeaders toArmeria(io.netty.handler.codec.http.HttpHeaders inHeaders) {
if (inHeaders.isEmpty()) {
return HttpHeaders.of();
}
final HttpHeadersBuilder out = HttpHeaders.builder();
out.sizeHint(inHeaders.size());
toArmeria(inHeaders, out);
return out.build();
}
/**
* Converts the specified Netty HTTP/1 headers into Armeria HTTP/2 headers.
* Functionally, this method is expected to behavior in the same way as
* {@link #purgeHttp1OnlyHeaders(io.netty.handler.codec.http.HttpHeaders, HttpHeadersBuilder)}.
*/
public static void toArmeria(io.netty.handler.codec.http.HttpHeaders inHeaders, HttpHeadersBuilder out) {
final Iterator<Entry<CharSequence, CharSequence>> iter = inHeaders.iteratorCharSequence();
// Choose 8 as a default size because it is unlikely we will see more than 4 Connection headers values,
// but still allowing for "enough" space in the map to reduce the chance of hash code collision.
final CaseInsensitiveMap connectionDisallowedList =
toLowercaseMap(inHeaders.valueCharSequenceIterator(HttpHeaderNames.CONNECTION), 8);
StringJoiner cookieJoiner = null;
while (iter.hasNext()) {
final Entry<CharSequence, CharSequence> entry = iter.next();
final AsciiString aName = HttpHeaderNames.of(entry.getKey()).toLowerCase();
if (HTTP_TO_HTTP2_HEADER_DISALLOWED_LIST.contains(aName) ||
connectionDisallowedList.contains(aName)) {
final CharSequence value = entry.getValue();
if (!maybeWebSocketUpgrade(aName, value)) {
continue;
}
}
// https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.2 makes a special exception for TE
if (aName.equals(HttpHeaderNames.TE)) {
toHttp2HeadersFilterTE(entry, out);
continue;
}
// Cookies must be concatenated into a single octet string.
// https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.5
final CharSequence value = entry.getValue();
if (aName.equals(HttpHeaderNames.COOKIE)) {
if (cookieJoiner == null) {
cookieJoiner = new StringJoiner(COOKIE_SEPARATOR);
}
COOKIE_SPLITTER.split(value).forEach(cookieJoiner::add);
} else {
out.add(aName, convertHeaderValue(aName, value));
}
}
if (cookieJoiner != null && cookieJoiner.length() != 0) {
out.add(HttpHeaderNames.COOKIE, cookieJoiner.toString());
}
maybeSetContentLengthUnknown(inHeaders.contains(HttpHeaderNames.CONTENT_LENGTH), out);
}
/**
* Removes HTTP/1 specified headers from a mutable headers map.
* Functionally this method is expected to behave the same as
* {@link #toArmeria(io.netty.handler.codec.http.HttpHeaders, HttpHeadersBuilder)}.
* This method should be preferred going forward as we continue implementing zero-copy
* for HTTP1 en/decoders.
*/
private static void purgeHttp1OnlyHeaders(io.netty.handler.codec.http.HttpHeaders inHeaders,
HttpHeadersBuilder out) {
//TODO(minwoox): dedup the logic between these method and toArmeria
maybeSetTeHeader(inHeaders, out);
maybeRemoveConnectionHeaders(inHeaders, out);
maybeSetCookie(inHeaders, out);
maybeSetContentLengthUnknown(inHeaders.contains(HttpHeaderNames.CONTENT_LENGTH), out);
}
private static void maybeRemoveConnectionHeaders(io.netty.handler.codec.http.HttpHeaders inHeaders,
HttpHeadersBuilder out) {
final CaseInsensitiveMap connectionDisallowedList =
toLowercaseMap(inHeaders.valueCharSequenceIterator(HttpHeaderNames.CONNECTION), 8);
final boolean isWebSocketUpgrade = isWebSocketUpgrade(inHeaders);
connectionDisallowedList.forEach(entry -> out.remove(entry.getKey()));
HTTP_TO_HTTP2_HEADER_DISALLOWED_LIST.forEach(entry -> out.remove(entry.getKey()));
if (isWebSocketUpgrade) {
out.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE.toString());
out.set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET.toString());
}
}
private static void maybeSetCookie(io.netty.handler.codec.http.HttpHeaders inHeaders,
HttpHeadersBuilder out) {
// Cookies must be concatenated into a single octet string.
// https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.5
if (out.contains(HttpHeaderNames.COOKIE)) {
final StringJoiner cookieJoiner = new StringJoiner(COOKIE_SEPARATOR);
inHeaders.getAll(HttpHeaderNames.COOKIE).forEach(
value -> COOKIE_SPLITTER.split(value).forEach(cookieJoiner::add));
out.set(HttpHeaderNames.COOKIE, cookieJoiner.toString());
}
}
private static void maybeSetTeHeader(io.netty.handler.codec.http.HttpHeaders inHeaders,
HttpHeadersBuilder out) {
if (!inHeaders.contains(HttpHeaderNames.TE)) {
return;
}
// https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.2 makes a special exception for TE
final boolean hasTrailersTe = findDelimitedIgnoreCase(HttpHeaderNames.TE, HttpHeaderValues.TRAILERS,
inHeaders);
if (hasTrailersTe) {
out.set(HttpHeaderNames.TE, HttpHeaderValues.TRAILERS.toString());
} else {
out.remove(HttpHeaderNames.TE);
}
}
private static boolean isWebSocketUpgrade(io.netty.handler.codec.http.HttpHeaders inHeaders) {
final boolean isUpgrade = findDelimitedIgnoreCase(HttpHeaderNames.CONNECTION,
HttpHeaderValues.UPGRADE, inHeaders);
final boolean isWebsocket = findDelimitedIgnoreCase(HttpHeaderNames.UPGRADE,
HttpHeaderValues.WEBSOCKET, inHeaders);
return isUpgrade && isWebsocket;
}
private static boolean findDelimitedIgnoreCase(AsciiString targetName, AsciiString targetValue,
io.netty.handler.codec.http.HttpHeaders httpHeaders) {
final List<String> allValues = httpHeaders.getAll(targetName);
if (allValues.isEmpty()) {
return false;
}
for (String value: allValues) {
if (targetValue.contentEqualsIgnoreCase(value)) {
return true;
}
final List<CharSequence> values = StringUtil.unescapeCsvFields(value);
for (CharSequence field : values) {
if (targetValue.contentEqualsIgnoreCase(AsciiString.trim(field))) {
return true;
}
}
}
return false;
}
private static void maybeSetContentLengthUnknown(boolean hasContentLength, HttpHeadersBuilder out) {
if (hasContentLength) {
return;
}
HttpMethod method = null;
if (out instanceof RequestHeadersBuilder) {
method = ((RequestHeadersBuilder) out).method();
}
final boolean isContentAlwaysEmpty;
if (method != null) {
isContentAlwaysEmpty = isContentAlwaysEmpty(method);
} else {
isContentAlwaysEmpty = false;
}
if (!isContentAlwaysEmpty) {
// Set isContentLengthUnknown to true not to override the content-length when the HTTP objects are
// aggregated.
out.contentLengthUnknown();
}
}
private static boolean isContentAlwaysEmpty(HttpMethod method) {
switch (method) {
case CONNECT:
case GET:
case HEAD:
case OPTIONS:
case TRACE:
return true;
default:
return false;
}
}
private static boolean maybeWebSocketUpgrade(AsciiString header, CharSequence value) {
if (HttpHeaderNames.CONNECTION.contentEqualsIgnoreCase(header) &&
HttpHeaderValues.UPGRADE.contentEqualsIgnoreCase(value)) {
return true;
}
return HttpHeaderNames.UPGRADE.contentEqualsIgnoreCase(header) &&
HttpHeaderValues.WEBSOCKET.contentEqualsIgnoreCase(value);
}
private static CaseInsensitiveMap toLowercaseMap(Iterator<? extends CharSequence> valuesIter,
int arraySizeHint) {
final CaseInsensitiveMap result = new CaseInsensitiveMap(arraySizeHint);
while (valuesIter.hasNext()) {
final AsciiString lowerCased = AsciiString.of(valuesIter.next()).toLowerCase();
try {
int index = lowerCased.forEachByte(FIND_COMMA);
if (index != -1) {
int start = 0;
do {
result.add(lowerCased.subSequence(start, index, false).trim(), EMPTY_STRING);
start = index + 1;
} while (start < lowerCased.length() &&
(index = lowerCased.forEachByte(start,
lowerCased.length() - start, FIND_COMMA)) != -1);
result.add(lowerCased.subSequence(start, lowerCased.length(), false).trim(), EMPTY_STRING);
} else {
result.add(lowerCased.trim(), EMPTY_STRING);
}
} catch (Exception e) {
// This is not expect to happen because FIND_COMMA never throws but must be caught
// because of the ByteProcessor interface.
throw new IllegalStateException(e);
}
}
return result;
}
/**
* Filter the {@link HttpHeaderNames#TE} header according to the
* <a href="https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.2">special rules in the HTTP/2 RFC</a>.
*
* @param entry the entry whose name is {@link HttpHeaderNames#TE}.
* @param out the resulting HTTP/2 headers.
*/
private static void toHttp2HeadersFilterTE(Entry<CharSequence, CharSequence> entry,
HttpHeadersBuilder out) {
if (AsciiString.indexOf(entry.getValue(), ',', 0) == -1) {
if (AsciiString.contentEqualsIgnoreCase(AsciiString.trim(entry.getValue()),
HttpHeaderValues.TRAILERS)) {
out.add(HttpHeaderNames.TE, HttpHeaderValues.TRAILERS.toString());
}
} else {
final List<CharSequence> teValues = StringUtil.unescapeCsvFields(entry.getValue());
for (CharSequence teValue : teValues) {
if (AsciiString.contentEqualsIgnoreCase(AsciiString.trim(teValue),
HttpHeaderValues.TRAILERS)) {
out.add(HttpHeaderNames.TE, HttpHeaderValues.TRAILERS.toString());
break;
}
}
}
}
/**
* Converts the specified Armeria HTTP/2 {@link ResponseHeaders} into Netty HTTP/2 headers.
*
* @param inputHeaders the HTTP/2 response headers to convert.
*/
public static Http2Headers toNettyHttp2ServerHeaders(HttpHeadersBuilder inputHeaders) {
for (Entry<AsciiString, AsciiString> disallowed : HTTP_TO_HTTP2_HEADER_DISALLOWED_LIST) {
inputHeaders.remove(disallowed.getKey());
}
// TODO(ikhoon): Implement HttpHeadersBuilder.remove(Predicate<AsciiString>) to remove values
// with a predicate.
for (AsciiString disallowed : disallowedResponseHeaderNames()) {
inputHeaders.remove(disallowed);
}
return new ArmeriaHttp2Headers(inputHeaders);
}
/**
* Converts the specified Armeria HTTP/2 response headers into Netty HTTP/2 headers.
*
* @param inputHeaders the HTTP/2 response headers to convert.
*/
public static Http2Headers toNettyHttp2ServerTrailers(HttpHeaders inputHeaders) {
final HttpHeadersBuilder builder = inputHeaders.toBuilder();
for (Entry<AsciiString, AsciiString> disallowed : HTTP_TO_HTTP2_HEADER_DISALLOWED_LIST) {
builder.remove(disallowed.getKey());
}
for (AsciiString disallowed : PSEUDO_HEADERS) {
builder.remove(disallowed);