/
FileTypeHandler.java
168 lines (148 loc) · 7.12 KB
/
FileTypeHandler.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
/*
* Copyright 2017-2020 original authors
*
* Licensed 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 io.micronaut.http.server.netty.types.files;
import io.micronaut.core.annotation.Internal;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MutableHttpHeaders;
import io.micronaut.http.MutableHttpResponse;
import io.micronaut.http.netty.NettyMutableHttpResponse;
import io.micronaut.http.server.netty.NettyHttpRequest;
import io.micronaut.http.server.netty.configuration.NettyHttpServerConfiguration;
import io.micronaut.http.server.netty.types.NettyCustomizableResponseTypeHandler;
import io.micronaut.http.server.netty.types.NettyFileCustomizableResponseType;
import io.micronaut.http.server.types.CustomizableResponseTypeException;
import io.micronaut.http.server.types.files.StreamedFile;
import io.micronaut.http.server.types.files.SystemFile;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpResponse;
import java.io.File;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
/**
* Responsible for writing files out to the response in Netty.
*
* @author James Kleeh
* @since 1.0
*/
@Internal
public class FileTypeHandler implements NettyCustomizableResponseTypeHandler<Object> {
// sorted array of entity headers
// https://tools.ietf.org/html/rfc2616#section-7.1
private static final String[] ENTITY_HEADERS = new String[] {HttpHeaders.ALLOW, HttpHeaders.CONTENT_ENCODING, HttpHeaders.CONTENT_LANGUAGE, HttpHeaders.CONTENT_LENGTH, HttpHeaders.CONTENT_LOCATION, HttpHeaders.CONTENT_MD5, HttpHeaders.CONTENT_RANGE, HttpHeaders.CONTENT_TYPE, HttpHeaders.EXPIRES, HttpHeaders.LAST_MODIFIED};
private static final Class<?>[] SUPPORTED_TYPES = new Class[]{File.class, StreamedFile.class, NettyFileCustomizableResponseType.class, SystemFile.class};
private final NettyHttpServerConfiguration.FileTypeHandlerConfiguration configuration;
/**
* @param configuration The file type handler configuration
*/
public FileTypeHandler(NettyHttpServerConfiguration.FileTypeHandlerConfiguration configuration) {
this.configuration = configuration;
}
@SuppressWarnings("MagicNumber")
@Override
public void handle(Object obj, HttpRequest<?> request, MutableHttpResponse<?> response, ChannelHandlerContext context) {
NettyFileCustomizableResponseType type;
if (obj instanceof File) {
type = new NettySystemFileCustomizableResponseType((File) obj);
} else if (obj instanceof NettyFileCustomizableResponseType) {
type = (NettyFileCustomizableResponseType) obj;
} else if (obj instanceof StreamedFile) {
type = new NettyStreamedFileCustomizableResponseType((StreamedFile) obj);
} else if (obj instanceof SystemFile) {
type = new NettySystemFileCustomizableResponseType((SystemFile) obj);
} else {
throw new CustomizableResponseTypeException("FileTypeHandler only supports File or FileCustomizableResponseType types");
}
long lastModified = type.getLastModified();
// Cache Validation
ZonedDateTime ifModifiedSince = request.getHeaders().getDate(HttpHeaders.IF_MODIFIED_SINCE);
if (ifModifiedSince != null) {
// Only compare up to the second because the datetime format we send to the client
// does not have milliseconds
long ifModifiedSinceDateSeconds = ifModifiedSince.toEpochSecond();
long fileLastModifiedSeconds = lastModified / 1000;
if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) {
FullHttpResponse nettyResponse = notModified(response);
if (request instanceof NettyHttpRequest) {
((NettyHttpRequest<?>) request).prepareHttp2ResponseIfNecessary(nettyResponse);
}
context.writeAndFlush(nettyResponse);
return;
}
}
if (!response.getHeaders().contains(HttpHeaders.CONTENT_TYPE)) {
response.header(HttpHeaders.CONTENT_TYPE, type.getMediaType().toString());
}
setDateAndCacheHeaders(response, lastModified);
type.process(response);
type.write(request, response, context);
context.read();
}
@Override
public boolean supports(Class<?> type) {
return Arrays.stream(SUPPORTED_TYPES)
.anyMatch((aClass -> aClass.isAssignableFrom(type)));
}
/**
* @param response The Http response
* @param lastModified The last modified
*/
protected void setDateAndCacheHeaders(MutableHttpResponse response, long lastModified) {
// Date header
MutableHttpHeaders headers = response.getHeaders();
LocalDateTime now = LocalDateTime.now();
headers.date(now);
// Add cache headers
LocalDateTime cacheSeconds = now.plus(configuration.getCacheSeconds(), ChronoUnit.SECONDS);
if (response.header(HttpHeaders.EXPIRES) == null) {
headers.expires(cacheSeconds);
}
if (response.header(HttpHeaders.CACHE_CONTROL) == null) {
NettyHttpServerConfiguration.FileTypeHandlerConfiguration.CacheControlConfiguration cacheConfig = configuration.getCacheControl();
StringBuilder header = new StringBuilder(cacheConfig.getPublic() ? "public" : "private")
.append(", max-age=")
.append(configuration.getCacheSeconds());
response.header(HttpHeaders.CACHE_CONTROL, header.toString());
}
if (response.header(HttpHeaders.LAST_MODIFIED) == null) {
headers.lastModified(lastModified);
}
}
/**
* @param response The Http response
*/
protected void setDateHeader(MutableHttpResponse response) {
MutableHttpHeaders headers = response.getHeaders();
LocalDateTime now = LocalDateTime.now();
headers.date(now);
}
private static void copyNonEntityHeaders(MutableHttpResponse<?> from, MutableHttpResponse to) {
from.getHeaders().forEachValue((header, value) -> {
if (Arrays.binarySearch(ENTITY_HEADERS, header) < 0) {
to.getHeaders().add(header, value);
}
});
}
private FullHttpResponse notModified(MutableHttpResponse<?> originalResponse) {
MutableHttpResponse response = HttpResponse.notModified();
copyNonEntityHeaders(originalResponse, response);
setDateHeader(response);
return ((NettyMutableHttpResponse) response).toFullHttpResponse();
}
}