-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileResponse.java
More file actions
154 lines (133 loc) · 5.07 KB
/
Copy pathFileResponse.java
File metadata and controls
154 lines (133 loc) · 5.07 KB
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
package httpserver;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.Date;
public class FileResponse extends HttpResponse {
private Path file;
private static String hexByte(byte value) {
String hex = Integer.toHexString(value);
// toHexString produces no leading 0s and
// 8 digits for for negative values.
if (value >= 16) {
return hex;
} else if (value >= 0) {
return "0" + hex;
} else {
return hex.substring(6);
}
}
private static String getSha1Digest(String... inputs) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-1");
for (String input: inputs) {
md.update(input.getBytes("UTF-8"));
}
} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
// Not happening.
}
StringBuffer hexDigest = new StringBuffer();
for (byte hashDigit: md.digest()) {
hexDigest.append(hexByte(hashDigit));
}
return hexDigest.toString();
}
private boolean isEtagInHeader(String header, String etag) {
if (header.equals("*")) {
return true;
}
for (String headerEtag: header.split(", *")) {
if (headerEtag.equals("\"" + etag + "\"")) {
return true;
}
}
return false;
}
public FileResponse(HttpRequest httpRequest, Path filePath) throws HttpError {
super(httpRequest);
file = filePath;
int fileSize;
// Close connections because keep-alive is not working. :-(
persistent = false;
try {
setHeader("Content-Size", String.format("%1d", Files.size(file)));
} catch (IOException ioe) {
// Just leave out the header.
}
setHeader(
"Content-Disposition",
String.format("attachment; filename=\"%1s\"", file.getFileName()));
setHeader("Content-Type", "application/octet-stream");
Date lastModified;
try {
lastModified = new Date(Files.getLastModifiedTime(file).toMillis());
setHeader("Last-Modified", rfc1123Format.format(lastModified));
} catch (IOException ioe) {
// Just leave out the header.
lastModified = null;
}
if (lastModified != null) {
// The following only make sense if we know the modification time.
String ifModifiedSinceHeader = request.getHeader("If-Modified-Since");
Date ifModifiedSince = null;
boolean isModified = true;
if (ifModifiedSinceHeader != null) {
try {
ifModifiedSince = rfc1123Format.parse(ifModifiedSinceHeader);
} catch (ParseException pe) {
ifModifiedSince = null;
}
if (ifModifiedSince != null) {
Date now = new Date(System.currentTimeMillis());
if (ifModifiedSince.after(now)) {
// Future dates are invalid.
ifModifiedSince = null;
} else if (!lastModified.after(ifModifiedSince)) {
isModified = false;
}
}
}
String etag = getSha1Digest(
rfc1123Format.format(lastModified),
file.toString()
);
setHeader("Etag", etag );
String ifMatch = request.getHeader("If-Match");
if (ifMatch != null && !isEtagInHeader(ifMatch, etag)) {
// Do not serve this request!
throw new HttpError(HttpStatus.PreconditionFailed(), "If-Match");
}
String ifNoneMatch = request.getHeader("If-None-Match");
if (ifNoneMatch != null ) {
if (isEtagInHeader(ifNoneMatch, etag)) {
if (request.getMethod() == HttpRequest.Method.GET ||
request.getMethod() == HttpRequest.Method.HEAD)
{
// Do not overwrite If-Modified-Since
isModified = ifModifiedSinceHeader == null ? false : isModified;
} else {
// Do not serve this request!
throw new HttpError(HttpStatus.PreconditionFailed(), "If-None-Match");
}
} else {
// Ignore If-Modified-Since.
isModified = true;
}
}
if (!isModified) {
// Do not serve the file content.
status = HttpStatus.NotModified();
}
}
}
public void writeBody(OutputStream out) throws IOException {
Files.copy(file, out);
out.flush();
}
}