Skip to content

Commit 6ff3b66

Browse files
committed
changed the behavior of the getMimeType method to always try to load the mime type from the file name, without probing the file system. Updated unit tests to match (#254)
1 parent 1201296 commit 6ff3b66

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.slf4j.Logger;
2121
import org.slf4j.LoggerFactory;
2222

23+
import javax.activation.MimetypesFileTypeMap;
2324
import javax.servlet.Filter;
2425
import javax.servlet.FilterRegistration;
2526
import javax.servlet.RequestDispatcher;
@@ -72,6 +73,7 @@ public class AwsServletContext
7273
private Map<String, String> initParameters;
7374
private AwsLambdaServletContainerHandler containerHandler;
7475
private Logger log = LoggerFactory.getLogger(AwsServletContext.class);
76+
private MimetypesFileTypeMap mimeTypes; // lazily loaded in the getMimeType method
7577

7678

7779
//-------------------------------------------------------------
@@ -142,13 +144,16 @@ public int getEffectiveMinorVersion() {
142144
@Override
143145
@SuppressFBWarnings("PATH_TRAVERSAL_IN") // suppressing because we are using the getValidFilePath
144146
public String getMimeType(String s) {
145-
try {
146-
String validatedPath = SecurityUtils.getValidFilePath(s);
147-
return Files.probeContentType(Paths.get(validatedPath));
148-
} catch (IOException e) {
149-
log.warn("Could not find content type for file: " + SecurityUtils.encode(s), e);
147+
if (!s.contains(".")) {
150148
return null;
151149
}
150+
if (mimeTypes == null) {
151+
mimeTypes = new MimetypesFileTypeMap();
152+
}
153+
// TODO: The getContentType method of the MimetypesFileTypeMap returns application/octet-stream
154+
// instead of null when the type cannot be found. We should replace with an implementation that
155+
// loads the System mime types ($JAVA_HOME/lib/mime.types
156+
return mimeTypes.getContentType(s);
152157
}
153158

154159

@@ -232,7 +237,7 @@ public String getRealPath(String s) {
232237
try {
233238
absPath = new File(fileUrl.toURI()).getAbsolutePath();
234239
} catch (URISyntaxException e) {
235-
log.error("Error while looking for real path: {}", SecurityUtils.encode(s), SecurityUtils.encode(e.getMessage()));
240+
log.error("Error while looking for real path {}: {}", SecurityUtils.encode(s), SecurityUtils.encode(e.getMessage()));
236241
}
237242
}
238243
return absPath;

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.amazonaws.serverless.proxy.internal.servlet.filters.UrlPathValidator;
55

66
import org.junit.BeforeClass;
7+
import org.junit.Ignore;
78
import org.junit.Test;
89

910
import javax.servlet.Filter;
@@ -43,7 +44,7 @@ public static void setUp() {
4344
LambdaContainerHandler.getContainerConfig().addValidFilePath("C:\\MyTestFolder");
4445
}
4546

46-
@Test
47+
@Test @Ignore
4748
public void getMimeType_disabledPath_expectException() {
4849
AwsServletContext ctx = new AwsServletContext(null);
4950
try {
@@ -64,7 +65,7 @@ public void getMimeType_nonExistentFileInTaskPath_expectNull() {
6465

6566
@Test
6667
public void getMimeType_mimeTypeOfCorrectFile_expectMime() {
67-
String tmpFilePath = TMP_DIR + "/test_text.txt";
68+
String tmpFilePath = TMP_DIR + "test_text.txt";
6869
try {
6970
System.out.println("Writing to tmp file " + tmpFilePath);
7071
PrintWriter tmpWriter = new PrintWriter(tmpFilePath, "UTF-8");
@@ -73,23 +74,27 @@ public void getMimeType_mimeTypeOfCorrectFile_expectMime() {
7374

7475
AwsServletContext ctx = new AwsServletContext(null);
7576
String mimeType = ctx.getMimeType(tmpFilePath);
76-
String deducedMimeType = Files.probeContentType(Paths.get(tmpFilePath));
77-
assertEquals(deducedMimeType, mimeType);
77+
assertEquals("text/plain", mimeType);
7878

7979
mimeType = ctx.getMimeType("file://" + tmpFilePath);
80-
assertEquals(deducedMimeType, mimeType);
80+
assertEquals("text/plain", mimeType);
8181
} catch (FileNotFoundException e) {
8282
fail("tmp file not found");
8383
e.printStackTrace();
8484
} catch (UnsupportedEncodingException e) {
8585
fail("Unsupported encoding");
8686
e.printStackTrace();
87-
} catch (IOException e) {
88-
fail("Failed to prove tmp file content type");
89-
e.printStackTrace();
9087
}
9188
}
9289

90+
@Test
91+
public void getMimeType_unknownExtension_expectAppOctetStream() {
92+
AwsServletContext ctx = new AwsServletContext(null);
93+
String mimeType = ctx.getMimeType("myfile.unkext");
94+
assertEquals("application/octet-stream", mimeType);
95+
}
96+
97+
9398
@Test
9499
public void addFilter_nonExistentFilterClass_expectException() {
95100
AwsServletContext ctx = new AwsServletContext(null);

0 commit comments

Comments
 (0)