Skip to content

Commit 1dfe35e

Browse files
committed
fix: provide more accurate result for content type
1 parent 718f2b9 commit 1dfe35e

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@
2727
import jakarta.activation.MimetypesFileTypeMap;
2828

2929
import java.io.File;
30+
import java.io.IOException;
3031
import java.io.InputStream;
3132
import java.net.MalformedURLException;
3233
import java.net.URISyntaxException;
3334
import java.net.URL;
35+
import java.nio.file.Files;
36+
import java.nio.file.InvalidPathException;
37+
import java.nio.file.Paths;
3438
import java.util.*;
3539
import java.util.stream.Collectors;
3640

@@ -160,23 +164,33 @@ public int getEffectiveMinorVersion() {
160164

161165
@Override
162166
@SuppressFBWarnings("PATH_TRAVERSAL_IN") // suppressing because we are using the getValidFilePath
163-
public String getMimeType(String s) {
164-
if (s == null || !s.contains(".")) {
167+
public String getMimeType(String file) {
168+
if (file == null || !file.contains(".")) {
165169
return null;
166170
}
171+
String mimeType = null;
172+
try {
173+
mimeType = Files.probeContentType(Paths.get(file));
174+
} catch (IOException | InvalidPathException e) {
175+
log("unable to probe for content type, will use fallback", e);
176+
}
177+
178+
// MimetypesFileTypeMap is kept for backwards compatibility, remove in 2.0
167179
if (mimeTypes == null) {
168180
mimeTypes = new MimetypesFileTypeMap();
169181
}
170-
if (s.endsWith(".css")) {
171-
return "text/css";
182+
String backwardsCompatibleMimeType = mimeTypes.getContentType(file);
183+
// The getContentType method of the MimetypesFileTypeMap
184+
// returns MimetypesFileTypeMap.defaultType = application/octet-stream
185+
// instead of null when the type cannot be found.
186+
if (mimeType == null || (backwardsCompatibleMimeType != null && !backwardsCompatibleMimeType.equals(mimeType)
187+
&& !MediaType.APPLICATION_OCTET_STREAM.equals(backwardsCompatibleMimeType))) {
188+
log("using type " + backwardsCompatibleMimeType + " from MimetypesFileTypeMap for " + file
189+
+ " instead of " + mimeType + " for backwards compatibility");
190+
mimeType = backwardsCompatibleMimeType;
172191
}
173-
if (s.endsWith(".js")) {
174-
return "application/javascript";
175-
}
176-
// TODO: The getContentType method of the MimetypesFileTypeMap returns application/octet-stream
177-
// instead of null when the type cannot be found. We should replace with an implementation that
178-
// loads the System mime types ($JAVA_HOME/lib/mime.types
179-
return mimeTypes.getContentType(s);
192+
193+
return mimeType;
180194
}
181195

182196

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
@@ -70,7 +70,7 @@ void getMimeType_mimeTypeOfJavascript_expectApplicationJavascript() {
7070
String tmpFilePath = TMP_DIR + "some.js";
7171
AwsServletContext ctx = new AwsServletContext(null);
7272
String mimeType = ctx.getMimeType(tmpFilePath);
73-
assertEquals("application/javascript", mimeType);
73+
assertEquals("text/javascript", mimeType);
7474
}
7575

7676
@Test

0 commit comments

Comments
 (0)