-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
GzipHandler.java
1170 lines (1078 loc) · 41.1 KB
/
GzipHandler.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 (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.server.handler.gzip;
import java.io.IOException;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.Deflater;
import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.CompressedContentFormat;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.http.PreEncodedHttpField;
import org.eclipse.jetty.http.pathmap.PathSpecSet;
import org.eclipse.jetty.server.HttpOutput;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.util.AsciiLowerCaseSet;
import org.eclipse.jetty.util.IncludeExclude;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.compression.CompressionPool;
import org.eclipse.jetty.util.compression.DeflaterPool;
import org.eclipse.jetty.util.compression.InflaterPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A Handler that can dynamically GZIP uncompress requests, and compress responses.
* <p>
* The GzipHandler can be applied to the entire server (a {@code gzip.mod} is included in
* the {@code jetty-home}) or it may be applied to individual contexts.
* </p>
* <p>
* Both Request uncompress and Response compress are gated by a configurable
* {@link DispatcherType} check on the GzipHandler.
* (This is similar in behavior to a {@link javax.servlet.Filter} configuration
* you would find in a Servlet Descriptor file ({@code WEB-INF/web.xml})
* <br>(Default: {@link DispatcherType#REQUEST}).
* </p>
* <p>
* Requests with a {@code Content-Encoding} header with the value {@code gzip} will
* be uncompressed by a {@link GzipHttpInputInterceptor} for any API that uses
* {@link HttpServletRequest#getInputStream()} or {@link HttpServletRequest#getReader()}.
* </p>
* <p>
* Response compression has a number of checks before GzipHandler will perform compression.
* </p>
* <ol>
* <li>
* Does the request contain a {@code Accept-Encoding} header that specifies
* {@code gzip} value?
* </li>
* <li>
* Is the {@link HttpServletRequest#getMethod()} allowed by the configured HTTP Method Filter.
* <br> (Default: {@code GET})
* </li>
* <li>
* Is the incoming Path allowed by the configured Path Specs filters?
* <br> (Default: all paths are allowed)
* </li>
* <li>
* Is the Request User-Agent allowed by the configured User-Agent filters?
* <br> (Default: MSIE 6 is excluded)
* </li>
* <li>
* Is the Response {@code Content-Length} header present, and does its
* value meet the minimum gzip size requirements (default 32 bytes)?
* </li>
* <li>
* Is the Request {@code Accept} header present and does it contain the
* required {@code gzip} value?
* </li>
* </ol>
* <p>
* When you encounter a configurable filter in the GzipHandler (method, paths, user-agent,
* mime-types, etc) that has both Included and Excluded values, note that the Included
* values always win over the Excluded values.
* </p>
* <p>
* <em>Important note about Default Values</em>:
* It is important to note that the GzipHandler will automatically configure itself from the
* MimeType present on the Server, System, and Contexts and the ultimate set of default values
* for the various filters (paths, methods, mime-types, etc) can be influenced by the
* available mime types to work with.
* </p>
* <p>
* ETag (or Entity Tag) information: any Request headers for {@code If-None-Match} or
* {@code If-Match} will be evaluated by the GzipHandler to determine if it was involved
* in compression of the response earlier. This is usually present as a {@code --gzip} suffix
* on the ETag that the Client User-Agent is tracking and handed to the Jetty server.
* The special {@code --gzip} suffix on the ETag is how GzipHandler knows that the content
* passed through itself, and this suffix will be stripped from the Request header values
* before the request is sent onwards to the specific webapp / servlet endpoint for
* handling.
* If a ETag is present in the Response headers, and GzipHandler is compressing the
* contents, it will add the {@code --gzip} suffix before the Response headers are committed
* and sent to the User Agent.
* Note that the suffix used is determined by {@link CompressedContentFormat#ETAG_SEPARATOR}
* </p>
* <p>
* This implementation relies on an Jetty internal {@link org.eclipse.jetty.server.HttpOutput.Interceptor}
* mechanism to allow for effective and efficient compression of the response on all Output API usages:
* </p>
* <ul>
* <li>
* {@link javax.servlet.ServletOutputStream} - Obtained from {@link HttpServletResponse#getOutputStream()}
* using the traditional Blocking I/O techniques
* </li>
* <li>
* {@link javax.servlet.WriteListener} - Provided to
* {@link javax.servlet.ServletOutputStream#setWriteListener(javax.servlet.WriteListener)}
* using the new (since Servlet 3.1) Async I/O techniques
* </li>
* <li>
* {@link java.io.PrintWriter} - Obtained from {@link HttpServletResponse#getWriter()}
* using Blocking I/O techniques
* </li>
* </ul>
* <p>
* Historically the compression of responses were accomplished via
* Servlet Filters (eg: {@code GzipFilter}) and usage of {@link javax.servlet.http.HttpServletResponseWrapper}.
* Since the introduction of Async I/O in Servlet 3.1, this older form of Gzip support
* in web applications has been problematic and bug ridden.
* </p>
*/
public class GzipHandler extends HandlerWrapper implements GzipFactory
{
public static final EnumSet<HttpHeader> ETAG_HEADERS = EnumSet.of(HttpHeader.IF_MATCH, HttpHeader.IF_NONE_MATCH);
public static final String GZIP_HANDLER_ETAGS = "o.e.j.s.h.gzip.GzipHandler.etag";
public static final String GZIP = "gzip";
public static final String DEFLATE = "deflate";
public static final int DEFAULT_MIN_GZIP_SIZE = 32;
public static final int BREAK_EVEN_GZIP_SIZE = 23;
private static final Logger LOG = LoggerFactory.getLogger(GzipHandler.class);
private static final HttpField X_CE_GZIP = new PreEncodedHttpField("X-Content-Encoding", "gzip");
private static final Pattern COMMA_GZIP = Pattern.compile(".*, *gzip");
private InflaterPool _inflaterPool;
private DeflaterPool _deflaterPool;
private int _minGzipSize = DEFAULT_MIN_GZIP_SIZE;
private boolean _syncFlush = false;
private int _inflateBufferSize = -1;
private EnumSet<DispatcherType> _dispatchers = EnumSet.of(DispatcherType.REQUEST);
// non-static, as other GzipHandler instances may have different configurations
private final IncludeExclude<String> _methods = new IncludeExclude<>();
private final IncludeExclude<String> _paths = new IncludeExclude<>(PathSpecSet.class);
private final IncludeExclude<String> _inflatePaths = new IncludeExclude<>(PathSpecSet.class);
private final IncludeExclude<String> _mimeTypes = new IncludeExclude<>(AsciiLowerCaseSet.class);
private HttpField _vary = GzipHttpOutputInterceptor.VARY_ACCEPT_ENCODING;
/**
* Instantiates a new GzipHandler.
*/
public GzipHandler()
{
_methods.include(HttpMethod.GET.asString());
_methods.include(HttpMethod.POST.asString());
for (String type : MimeTypes.getKnownMimeTypes())
{
if ("image/svg+xml".equals(type))
_paths.exclude("*.svgz");
else if (type.startsWith("image/") ||
type.startsWith("audio/") ||
type.startsWith("video/"))
_mimeTypes.exclude(type);
}
_mimeTypes.exclude("application/compress");
_mimeTypes.exclude("application/zip");
_mimeTypes.exclude("application/gzip");
_mimeTypes.exclude("application/bzip2");
_mimeTypes.exclude("application/brotli");
_mimeTypes.exclude("application/x-xz");
_mimeTypes.exclude("application/x-rar-compressed");
// It is possible to use SSE with GzipHandler but you will need to set _synFlush to true which will impact performance.
_mimeTypes.exclude("text/event-stream");
if (LOG.isDebugEnabled())
LOG.debug("{} mime types {}", this, _mimeTypes);
}
@Override
protected void doStart() throws Exception
{
Server server = getServer();
if (_inflaterPool == null)
{
_inflaterPool = InflaterPool.ensurePool(server);
addBean(_inflaterPool);
}
if (_deflaterPool == null)
{
_deflaterPool = DeflaterPool.ensurePool(server);
addBean(_deflaterPool);
}
super.doStart();
}
@Override
protected void doStop() throws Exception
{
super.doStop();
removeBean(_inflaterPool);
_inflaterPool = null;
removeBean(_deflaterPool);
_deflaterPool = null;
}
/**
* @return The VARY field to use.
*/
public HttpField getVary()
{
return _vary;
}
/**
* @param vary The VARY field to use. It if is not an instance of {@link PreEncodedHttpField},
* then it will be converted to one.
*/
public void setVary(HttpField vary)
{
if (isRunning())
throw new IllegalStateException(getState());
if (vary == null || (vary instanceof PreEncodedHttpField))
_vary = vary;
else
_vary = new PreEncodedHttpField(vary.getHeader(), vary.getName(), vary.getValue());
}
/**
* Add excluded to the HTTP methods filtering.
*
* @param methods The methods to exclude in compression
* @see #addIncludedMethods(String...)
*/
public void addExcludedMethods(String... methods)
{
for (String m : methods)
{
_methods.exclude(m);
}
}
/**
* Get the Set of {@link DispatcherType} that this Filter will operate on.
*
* @return the set of {@link DispatcherType} this filter will operate on
*/
public EnumSet<DispatcherType> getDispatcherTypes()
{
return _dispatchers;
}
/**
* Set of supported {@link DispatcherType} that this filter will operate on.
*
* @param dispatchers the set of {@link DispatcherType} that this filter will operate on
*/
public void setDispatcherTypes(EnumSet<DispatcherType> dispatchers)
{
_dispatchers = dispatchers;
}
/**
* Set the list of supported {@link DispatcherType} that this filter will operate on.
*
* @param dispatchers the list of {@link DispatcherType} that this filter will operate on
*/
public void setDispatcherTypes(DispatcherType... dispatchers)
{
_dispatchers = EnumSet.copyOf(Arrays.asList(dispatchers));
}
/**
* Adds excluded MIME types for response filtering.
*
* <p>
* <em>Deprecation Warning: </em>
* For backward compatibility the MIME types parameters may be comma separated strings,
* but this will not be supported in future versions of Jetty.
* </p>
*
* @param types The mime types to exclude (without charset or other parameters).
* @see #addIncludedMimeTypes(String...)
*/
public void addExcludedMimeTypes(String... types)
{
for (String t : types)
{
_mimeTypes.exclude(StringUtil.csvSplit(t));
}
}
/**
* Adds excluded Path Specs for request filtering.
*
* <p>
* There are 2 syntaxes supported, Servlet <code>url-pattern</code> based, and
* Regex based. This means that the initial characters on the path spec
* line are very strict, and determine the behavior of the path matching.
* <ul>
* <li>If the spec starts with <code>'^'</code> the spec is assumed to be
* a regex based path spec and will match with normal Java regex rules.</li>
* <li>If the spec starts with <code>'/'</code> then spec is assumed to be
* a Servlet url-pattern rules path spec for either an exact match
* or prefix based match.</li>
* <li>If the spec starts with <code>'*.'</code> then spec is assumed to be
* a Servlet url-pattern rules path spec for a suffix based match.</li>
* <li>All other syntaxes are unsupported</li>
* </ul>
* <p>
* Note: inclusion takes precedence over exclude.
*
* @param pathspecs Path specs (as per servlet spec) to exclude. If a
* ServletContext is available, the paths are relative to the context path,
* otherwise they are absolute.<br>
* For backward compatibility the pathspecs may be comma separated strings, but this
* will not be supported in future versions.
* @see #addIncludedPaths(String...)
*/
public void addExcludedPaths(String... pathspecs)
{
for (String p : pathspecs)
{
_paths.exclude(StringUtil.csvSplit(p));
}
}
/**
* Adds excluded Path Specs for request filtering on request inflation.
*
* <p>
* There are 2 syntaxes supported, Servlet <code>url-pattern</code> based, and
* Regex based. This means that the initial characters on the path spec
* line are very strict, and determine the behavior of the path matching.
* <ul>
* <li>If the spec starts with <code>'^'</code> the spec is assumed to be
* a regex based path spec and will match with normal Java regex rules.</li>
* <li>If the spec starts with <code>'/'</code> then spec is assumed to be
* a Servlet url-pattern rules path spec for either an exact match
* or prefix based match.</li>
* <li>If the spec starts with <code>'*.'</code> then spec is assumed to be
* a Servlet url-pattern rules path spec for a suffix based match.</li>
* <li>All other syntaxes are unsupported</li>
* </ul>
* <p>
* Note: inclusion takes precedence over exclude.
*
* @param pathspecs Path specs (as per servlet spec) to exclude. If a
* ServletContext is available, the paths are relative to the context path,
* otherwise they are absolute.<br>
* For backward compatibility the pathspecs may be comma separated strings, but this
* will not be supported in future versions.
* @see #addIncludedInflationPaths(String...)
*/
public void addExcludedInflationPaths(String... pathspecs)
{
for (String p : pathspecs)
{
_inflatePaths.exclude(StringUtil.csvSplit(p));
}
}
/**
* Adds included HTTP Methods (eg: POST, PATCH, DELETE) for filtering.
*
* @param methods The HTTP methods to include in compression.
* @see #addExcludedMethods(String...)
*/
public void addIncludedMethods(String... methods)
{
for (String m : methods)
{
_methods.include(m);
}
}
/**
* Is the {@link Deflater} running {@link Deflater#SYNC_FLUSH} or not.
*
* @return True if {@link Deflater#SYNC_FLUSH} is used, else {@link Deflater#NO_FLUSH}
* @see #setSyncFlush(boolean)
*/
public boolean isSyncFlush()
{
return _syncFlush;
}
/**
* Set the {@link Deflater} flush mode to use. {@link Deflater#SYNC_FLUSH}
* should be used if the application wishes to stream the data, but this may
* hurt compression performance.
*
* @param syncFlush True if {@link Deflater#SYNC_FLUSH} is used, else {@link Deflater#NO_FLUSH}
* @see #isSyncFlush()
*/
public void setSyncFlush(boolean syncFlush)
{
_syncFlush = syncFlush;
}
/**
* Add included MIME types for response filtering
*
* @param types The mime types to include (without charset or other parameters)
* For backward compatibility the mimetypes may be comma separated strings, but this
* will not be supported in future versions.
* @see #addExcludedMimeTypes(String...)
*/
public void addIncludedMimeTypes(String... types)
{
for (String t : types)
{
_mimeTypes.include(StringUtil.csvSplit(t));
}
}
/**
* Add included Path Specs for filtering.
*
* <p>
* There are 2 syntaxes supported, Servlet <code>url-pattern</code> based, and
* Regex based. This means that the initial characters on the path spec
* line are very strict, and determine the behavior of the path matching.
* <ul>
* <li>If the spec starts with <code>'^'</code> the spec is assumed to be
* a regex based path spec and will match with normal Java regex rules.</li>
* <li>If the spec starts with <code>'/'</code> then spec is assumed to be
* a Servlet url-pattern rules path spec for either an exact match
* or prefix based match.</li>
* <li>If the spec starts with <code>'*.'</code> then spec is assumed to be
* a Servlet url-pattern rules path spec for a suffix based match.</li>
* <li>All other syntaxes are unsupported</li>
* </ul>
* <p>
* Note: inclusion takes precedence over exclusion.
*
* @param pathspecs Path specs (as per servlet spec) to include. If a
* ServletContext is available, the paths are relative to the context path,
* otherwise they are absolute
*/
public void addIncludedPaths(String... pathspecs)
{
for (String p : pathspecs)
{
_paths.include(StringUtil.csvSplit(p));
}
}
/**
* Add included Path Specs for filtering on request inflation.
*
* <p>
* There are 2 syntaxes supported, Servlet <code>url-pattern</code> based, and
* Regex based. This means that the initial characters on the path spec
* line are very strict, and determine the behavior of the path matching.
* <ul>
* <li>If the spec starts with <code>'^'</code> the spec is assumed to be
* a regex based path spec and will match with normal Java regex rules.</li>
* <li>If the spec starts with <code>'/'</code> then spec is assumed to be
* a Servlet url-pattern rules path spec for either an exact match
* or prefix based match.</li>
* <li>If the spec starts with <code>'*.'</code> then spec is assumed to be
* a Servlet url-pattern rules path spec for a suffix based match.</li>
* <li>All other syntaxes are unsupported</li>
* </ul>
* <p>
* Note: inclusion takes precedence over exclusion.
*
* @param pathspecs Path specs (as per servlet spec) to include. If a
* ServletContext is available, the paths are relative to the context path,
* otherwise they are absolute
*/
public void addIncludedInflationPaths(String... pathspecs)
{
for (String p : pathspecs)
{
_inflatePaths.include(StringUtil.csvSplit(p));
}
}
@Override
public DeflaterPool.Entry getDeflaterEntry(Request request, long contentLength)
{
if (contentLength >= 0 && contentLength < _minGzipSize)
{
LOG.debug("{} excluded minGzipSize {}", this, request);
return null;
}
// check the accept encoding header
if (!request.getHttpFields().contains(HttpHeader.ACCEPT_ENCODING, "gzip"))
{
LOG.debug("{} excluded not gzip accept {}", this, request);
return null;
}
return _deflaterPool.acquire();
}
/**
* Get the current filter list of excluded HTTP methods
*
* @return the filter list of excluded HTTP methods
* @see #getIncludedMethods()
*/
public String[] getExcludedMethods()
{
Set<String> excluded = _methods.getExcluded();
return excluded.toArray(new String[0]);
}
/**
* Get the current filter list of excluded MIME types
*
* @return the filter list of excluded MIME types
* @see #getIncludedMimeTypes()
*/
public String[] getExcludedMimeTypes()
{
Set<String> excluded = _mimeTypes.getExcluded();
return excluded.toArray(new String[0]);
}
/**
* Get the current filter list of excluded Path Specs
*
* @return the filter list of excluded Path Specs
* @see #getIncludedPaths()
*/
public String[] getExcludedPaths()
{
Set<String> excluded = _paths.getExcluded();
return excluded.toArray(new String[0]);
}
/**
* Get the current filter list of excluded Path Specs for request inflation.
*
* @return the filter list of excluded Path Specs
* @see #getIncludedInflationPaths()
*/
public String[] getExcludedInflationPaths()
{
Set<String> excluded = _inflatePaths.getExcluded();
return excluded.toArray(new String[0]);
}
/**
* Get the current filter list of included HTTP Methods
*
* @return the filter list of included HTTP methods
* @see #getExcludedMethods()
*/
public String[] getIncludedMethods()
{
Set<String> includes = _methods.getIncluded();
return includes.toArray(new String[0]);
}
/**
* Get the current filter list of included MIME types
*
* @return the filter list of included MIME types
* @see #getExcludedMimeTypes()
*/
public String[] getIncludedMimeTypes()
{
Set<String> includes = _mimeTypes.getIncluded();
return includes.toArray(new String[0]);
}
/**
* Get the current filter list of included Path Specs
*
* @return the filter list of included Path Specs
* @see #getExcludedPaths()
*/
public String[] getIncludedPaths()
{
Set<String> includes = _paths.getIncluded();
return includes.toArray(new String[0]);
}
/**
* Get the current filter list of included Path Specs for request inflation.
*
* @return the filter list of included Path Specs
* @see #getExcludedInflationPaths()
*/
public String[] getIncludedInflationPaths()
{
Set<String> includes = _inflatePaths.getIncluded();
return includes.toArray(new String[0]);
}
/**
* Get the minimum size, in bytes, that a response {@code Content-Length} must be
* before compression will trigger.
*
* @return minimum response size (in bytes) that triggers compression
* @see #setMinGzipSize(int)
*/
public int getMinGzipSize()
{
return _minGzipSize;
}
protected HttpField getVaryField()
{
return _vary;
}
/**
* Get the size (in bytes) of the {@link java.util.zip.Inflater} buffer used to inflate
* compressed requests.
*
* @return size in bytes of the buffer, or 0 for no inflation.
*/
public int getInflateBufferSize()
{
return _inflateBufferSize;
}
/**
* Set the size (in bytes) of the {@link java.util.zip.Inflater} buffer used to inflate comrpessed requests.
*
* @param size size in bytes of the buffer, or 0 for no inflation.
*/
public void setInflateBufferSize(int size)
{
_inflateBufferSize = size;
}
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
if (baseRequest.isHandled())
{
super.handle(target, baseRequest, request, response);
return;
}
final ServletContext context = baseRequest.getServletContext();
final String path = baseRequest.getPathInContext();
LOG.debug("{} handle {} in {}", this, baseRequest, context);
if (!_dispatchers.contains(baseRequest.getDispatcherType()))
{
LOG.debug("{} excluded by dispatcherType {}", this, baseRequest.getDispatcherType());
super.handle(target, baseRequest, request, response);
return;
}
// Handle request inflation
HttpFields httpFields = baseRequest.getHttpFields();
boolean inflated = _inflateBufferSize > 0 && httpFields.contains(HttpHeader.CONTENT_ENCODING, "gzip") && isPathInflatable(path);
if (inflated)
{
if (LOG.isDebugEnabled())
LOG.debug("{} inflate {}", this, request);
GzipHttpInputInterceptor gzipHttpInputInterceptor =
new GzipHttpInputInterceptor(_inflaterPool, baseRequest.getHttpChannel().getByteBufferPool(),
_inflateBufferSize, baseRequest.getHttpChannel().isUseInputDirectByteBuffers());
baseRequest.getHttpInput().addInterceptor(gzipHttpInputInterceptor);
}
// From here on out, the response output gzip determination is made
// Don't attempt to modify the response output if it's already committed.
if (response.isCommitted())
{
super.handle(target, baseRequest, request, response);
return;
}
// Are we already being gzipped?
HttpOutput out = baseRequest.getResponse().getHttpOutput();
HttpOutput.Interceptor interceptor = out.getInterceptor();
boolean alreadyGzipped = false;
while (interceptor != null)
{
if (interceptor instanceof GzipHttpOutputInterceptor)
{
alreadyGzipped = true;
break;
}
interceptor = interceptor.getNextInterceptor();
}
// Update headers for etags and inflation
if (inflated || httpFields.contains(ETAG_HEADERS))
{
HttpFields.Mutable newFields = HttpFields.build(httpFields.size() + 1);
for (HttpField field : httpFields)
{
if (field.getHeader() == null)
{
newFields.add(field);
continue;
}
switch (field.getHeader())
{
case IF_MATCH:
case IF_NONE_MATCH:
{
String etags = field.getValue();
String etagsNoSuffix = CompressedContentFormat.GZIP.stripSuffixes(etags);
if (etagsNoSuffix.equals(etags))
newFields.add(field);
else
{
newFields.add(new HttpField(field.getHeader(), etagsNoSuffix));
baseRequest.setAttribute(GZIP_HANDLER_ETAGS, etags);
}
break;
}
case CONTENT_LENGTH:
newFields.add(inflated ? new HttpField("X-Content-Length", field.getValue()) : field);
break;
case CONTENT_ENCODING:
if (inflated)
{
if (field.getValue().equalsIgnoreCase("gzip"))
newFields.add(X_CE_GZIP);
else if (COMMA_GZIP.matcher(field.getValue()).matches())
{
String v = field.getValue();
v = v.substring(0, v.lastIndexOf(','));
newFields.add(X_CE_GZIP);
newFields.add(new HttpField(HttpHeader.CONTENT_ENCODING, v));
}
}
else
{
newFields.add(field);
}
break;
default:
newFields.add(field);
}
}
baseRequest.setHttpFields(newFields);
}
// Don't gzip if already gzipped;
if (alreadyGzipped)
{
LOG.debug("{} already intercepting {}", this, request);
super.handle(target, baseRequest, request, response);
return;
}
// If not a supported method - no Vary because no matter what client, this URI is always excluded
if (!_methods.test(baseRequest.getMethod()))
{
LOG.debug("{} excluded by method {}", this, request);
super.handle(target, baseRequest, request, response);
return;
}
// If not a supported URI- no Vary because no matter what client, this URI is always excluded
// Use pathInfo because this is be
if (!isPathGzipable(path))
{
LOG.debug("{} excluded by path {}", this, request);
super.handle(target, baseRequest, request, response);
return;
}
// Exclude non compressible mime-types known from URI extension. - no Vary because no matter what client, this URI is always excluded
String mimeType = context == null ? MimeTypes.getDefaultMimeByExtension(path) : context.getMimeType(path);
if (mimeType != null)
{
mimeType = HttpField.valueParameters(mimeType, null);
if (!isMimeTypeGzipable(mimeType))
{
LOG.debug("{} excluded by path suffix mime type {}", this, request);
// handle normally without setting vary header
super.handle(target, baseRequest, request, response);
return;
}
}
HttpOutput.Interceptor origInterceptor = out.getInterceptor();
try
{
// install interceptor and handle
out.setInterceptor(new GzipHttpOutputInterceptor(this, getVaryField(), baseRequest.getHttpChannel(), origInterceptor, isSyncFlush()));
super.handle(target, baseRequest, request, response);
}
finally
{
// reset interceptor if request not handled
if (!baseRequest.isHandled() && !baseRequest.isAsyncStarted())
out.setInterceptor(origInterceptor);
}
}
/**
* Test if the provided MIME type is allowed based on the MIME type filters.
*
* @param mimetype the MIME type to test
* @return true if allowed, false otherwise
*/
@Override
public boolean isMimeTypeGzipable(String mimetype)
{
return _mimeTypes.test(mimetype);
}
/**
* Test if the provided Request URI is allowed based on the Path Specs filters.
*
* @param requestURI the request uri
* @return whether compressing is allowed for the given the path
*/
protected boolean isPathGzipable(String requestURI)
{
if (requestURI == null)
return true;
return _paths.test(requestURI);
}
/**
* Test if the provided Request URI is allowed to be inflated based on the Path Specs filters.
*
* @param requestURI the request uri
* @return whether decompressing is allowed for the given the path.
*/
protected boolean isPathInflatable(String requestURI)
{
if (requestURI == null)
return true;
return _inflatePaths.test(requestURI);
}
/**
* Set the excluded filter list of HTTP methods (replacing any previously set)
*
* @param methods the HTTP methods to exclude
* @see #setIncludedMethods(String...)
*/
public void setExcludedMethods(String... methods)
{
_methods.getExcluded().clear();
_methods.exclude(methods);
}
/**
* Set the excluded filter list of MIME types (replacing any previously set)
*
* @param types The mime types to exclude (without charset or other parameters)
* @see #setIncludedMimeTypes(String...)
*/
public void setExcludedMimeTypes(String... types)
{
_mimeTypes.getExcluded().clear();
_mimeTypes.exclude(types);
}
/**
* Set the excluded filter list of MIME types (replacing any previously set)
*
* @param csvTypes The list of mime types to exclude (without charset or other parameters), CSV format
* @see #setIncludedMimeTypesList(String)
*/
public void setExcludedMimeTypesList(String csvTypes)
{
setExcludedMimeTypes(StringUtil.csvSplit(csvTypes));
}
/**
* Set the excluded filter list of Path specs (replacing any previously set)
*
* @param pathspecs Path specs (as per servlet spec) to exclude. If a
* ServletContext is available, the paths are relative to the context path,
* otherwise they are absolute.
* @see #setIncludedPaths(String...)
*/
public void setExcludedPaths(String... pathspecs)
{
_paths.getExcluded().clear();
_paths.exclude(pathspecs);
}
/**
* Set the excluded filter list of Path specs (replacing any previously set)
*
* @param pathspecs Path specs (as per servlet spec) to exclude from inflation. If a
* ServletContext is available, the paths are relative to the context path,
* otherwise they are absolute.
* @see #setIncludedInflatePaths(String...)
*/
public void setExcludedInflatePaths(String... pathspecs)
{
_inflatePaths.getExcluded().clear();
_inflatePaths.exclude(pathspecs);
}
/**
* Set of supported {@link DispatcherType} that this filter will operate on.
*
* @param dispatchers the set of {@link DispatcherType} that this filter will operate on
*/
public void setDispatcherTypes(String... dispatchers)
{
_dispatchers = EnumSet.copyOf(Stream.of(dispatchers)
.flatMap(s -> Stream.of(StringUtil.csvSplit(s)))
.map(DispatcherType::valueOf)
.collect(Collectors.toSet()));
}
/**
* Set the included filter list of HTTP methods (replacing any previously set)
*
* @param methods The methods to include in compression
* @see #setExcludedMethods(String...)
*/
public void setIncludedMethods(String... methods)
{
_methods.getIncluded().clear();
_methods.include(methods);
}
/**
* Set the included filter list of MIME types (replacing any previously set)
*
* @param types The mime types to include (without charset or other parameters)
* @see #setExcludedMimeTypes(String...)
*/
public void setIncludedMimeTypes(String... types)
{
_mimeTypes.getIncluded().clear();
_mimeTypes.include(types);
}
/**
* Set the included filter list of MIME types (replacing any previously set)
*
* @param csvTypes The list of mime types to include (without charset or other parameters), CSV format
* @see #setExcludedMimeTypesList(String)
*/
public void setIncludedMimeTypesList(String csvTypes)
{
setIncludedMimeTypes(StringUtil.csvSplit(csvTypes));
}
/**
* Set the included filter list of Path specs (replacing any previously set)
*
* @param pathspecs Path specs (as per servlet spec) to include. If a
* ServletContext is available, the paths are relative to the context path,
* otherwise they are absolute
* @see #setExcludedPaths(String...)
*/
public void setIncludedPaths(String... pathspecs)
{
_paths.getIncluded().clear();
_paths.include(pathspecs);
}
/**
* Set the included filter list of Path specs (replacing any previously set)
*
* @param pathspecs Path specs (as per servlet spec) to include for inflation. If a