Skip to content

Commit 0675c1e

Browse files
committed
fix: provide more accurate result for content type
1 parent a8f8f7e commit 0675c1e

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsServletContext.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@
2323
import javax.activation.MimetypesFileTypeMap;
2424
import javax.servlet.*;
2525
import javax.servlet.descriptor.JspConfigDescriptor;
26+
import javax.ws.rs.core.MediaType;
2627

2728
import java.io.File;
29+
import java.io.IOException;
2830
import java.io.InputStream;
2931
import java.net.MalformedURLException;
3032
import java.net.URISyntaxException;
3133
import java.net.URL;
34+
import java.nio.file.Files;
35+
import java.nio.file.InvalidPathException;
36+
import java.nio.file.Paths;
3237
import java.util.*;
3338
import java.util.stream.Collectors;
3439

@@ -129,23 +134,33 @@ public int getEffectiveMinorVersion() {
129134

130135
@Override
131136
@SuppressFBWarnings("PATH_TRAVERSAL_IN") // suppressing because we are using the getValidFilePath
132-
public String getMimeType(String s) {
133-
if (s == null || !s.contains(".")) {
137+
public String getMimeType(String file) {
138+
if (file == null || !file.contains(".")) {
134139
return null;
135140
}
141+
String mimeType = null;
142+
try {
143+
mimeType = Files.probeContentType(Paths.get(file));
144+
} catch (IOException | InvalidPathException e) {
145+
log("unable to probe for content type, will use fallback", e);
146+
}
147+
148+
// MimetypesFileTypeMap is kept for backwards compatibility, remove in 2.0
136149
if (mimeTypes == null) {
137150
mimeTypes = new MimetypesFileTypeMap();
138151
}
139-
if (s.endsWith(".css")) {
140-
return "text/css";
152+
String backwardsCompatibleMimeType = mimeTypes.getContentType(file);
153+
// The getContentType method of the MimetypesFileTypeMap
154+
// returns MimetypesFileTypeMap.defaultType = application/octet-stream
155+
// instead of null when the type cannot be found.
156+
if (mimeType == null || (backwardsCompatibleMimeType != null && !backwardsCompatibleMimeType.equals(mimeType)
157+
&& !MediaType.APPLICATION_OCTET_STREAM.equals(backwardsCompatibleMimeType))) {
158+
log("using type " + backwardsCompatibleMimeType + " from MimetypesFileTypeMap for " + file
159+
+ " instead of " + mimeType + " for backwards compatibility");
160+
mimeType = backwardsCompatibleMimeType;
141161
}
142-
if (s.endsWith(".js")) {
143-
return "application/javascript";
144-
}
145-
// TODO: The getContentType method of the MimetypesFileTypeMap returns application/octet-stream
146-
// instead of null when the type cannot be found. We should replace with an implementation that
147-
// loads the System mime types ($JAVA_HOME/lib/mime.types
148-
return mimeTypes.getContentType(s);
162+
163+
return mimeType;
149164
}
150165

151166

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/internal/servlet/AwsServletContextTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void getMimeType_mimeTypeOfJavascript_expectApplicationJavascript() {
7575
String tmpFilePath = TMP_DIR + "some.js";
7676
AwsServletContext ctx = new AwsServletContext(null);
7777
String mimeType = ctx.getMimeType(tmpFilePath);
78-
assertEquals("application/javascript", mimeType);
78+
assertEquals("text/javascript", mimeType);
7979
}
8080

8181
@Test

0 commit comments

Comments
 (0)