1
18
19 package io.undertow.server.handlers.encoding;
20
21 import java.util.List;
22
23 import io.undertow.server.ConduitWrapper;
24 import io.undertow.server.HttpServerExchange;
25 import io.undertow.util.AttachmentKey;
26 import io.undertow.util.ConduitFactory;
27 import io.undertow.util.Headers;
28 import io.undertow.util.Methods;
29 import io.undertow.util.StatusCodes;
30 import org.xnio.conduits.StreamSinkConduit;
31
32
37 public class AllowedContentEncodings implements ConduitWrapper<StreamSinkConduit> {
38
39 public static final AttachmentKey<AllowedContentEncodings> ATTACHMENT_KEY = AttachmentKey.create(AllowedContentEncodings.class);
40
41 private final HttpServerExchange exchange;
42 private final List<EncodingMapping> encodings;
43
44
45 public AllowedContentEncodings(final HttpServerExchange exchange, final List<EncodingMapping> encodings) {
46 this.exchange = exchange;
47 this.encodings = encodings;
48 }
49
50
53 public String getCurrentContentEncoding() {
54 for (EncodingMapping encoding : encodings) {
55 if (encoding.getAllowed() == null || encoding.getAllowed().resolve(exchange)) {
56 return encoding.getName();
57 }
58 }
59 return Headers.IDENTITY.toString();
60 }
61
62 public EncodingMapping getEncoding() {
63 for (EncodingMapping encoding : encodings) {
64 if (encoding.getAllowed() == null || encoding.getAllowed().resolve(exchange)) {
65 return encoding;
66 }
67 }
68 return null;
69 }
70
71 public boolean isIdentity() {
72 return getCurrentContentEncoding().equals(Headers.IDENTITY.toString());
73 }
74
75
79 public boolean isNoEncodingsAllowed() {
80 return encodings.isEmpty();
81 }
82
83 @Override
84 public StreamSinkConduit wrap(final ConduitFactory<StreamSinkConduit> factory, final HttpServerExchange exchange) {
85 if (exchange.getResponseHeaders().contains(Headers.CONTENT_ENCODING)) {
86
87 return factory.create();
88 }
89
90 if (exchange.getResponseContentLength() != 0
91 && exchange.getStatusCode() != StatusCodes.NO_CONTENT
92 && exchange.getStatusCode() != StatusCodes.NOT_MODIFIED) {
93 EncodingMapping encoding = getEncoding();
94 if (encoding != null) {
95 exchange.getResponseHeaders().put(Headers.CONTENT_ENCODING, encoding.getName());
96 if (exchange.getRequestMethod().equals(Methods.HEAD)) {
97
98 return factory.create();
99 } else {
100 return encoding.getEncoding().getResponseWrapper().wrap(factory, exchange);
101 }
102 }
103 }
104 return factory.create();
105 }
106 }
107