Skip to content

Commit

Permalink
Merge pull request #504 from Artur-/mime-types
Browse files Browse the repository at this point in the history
fix: Provide correct mime type for javascript and css
  • Loading branch information
deki committed May 19, 2023
2 parents 0601ce7 + 0c0650e commit f5cf12d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
5 changes: 0 additions & 5 deletions aws-serverless-java-container-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@
<artifactId>commons-fileupload2</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
<version>2.0.1</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@
import com.amazonaws.serverless.proxy.internal.SecurityUtils;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.activation.spi.MimeTypeRegistryProvider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.servlet.*;
import jakarta.servlet.ServletContext;
import jakarta.servlet.descriptor.JspConfigDescriptor;
import jakarta.activation.MimetypesFileTypeMap;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;


/**
Expand Down Expand Up @@ -60,7 +63,6 @@ public class AwsServletContext
private Map<String, String> initParameters;
private AwsLambdaServletContainerHandler containerHandler;
private Logger log = LoggerFactory.getLogger(AwsServletContext.class);
private MimetypesFileTypeMap mimeTypes; // lazily loaded in the getMimeType method


//-------------------------------------------------------------
Expand Down Expand Up @@ -160,18 +162,32 @@ public int getEffectiveMinorVersion() {

@Override
@SuppressFBWarnings("PATH_TRAVERSAL_IN") // suppressing because we are using the getValidFilePath
public String getMimeType(String s) {
if (s == null || !s.contains(".")) {
public String getMimeType(String file) {
if (file == null || !file.contains(".")) {
return null;
}
if (mimeTypes == null) {
mimeTypes = new MimetypesFileTypeMap();

String mimeType = null;

// may not work on Lambda until mailcap package is present https://github.com/awslabs/aws-serverless-java-container/pull/504
try {
mimeType = Files.probeContentType(Paths.get(file));
} catch (IOException | InvalidPathException e) {
log("unable to probe for content type, will use fallback", e);
}

if (mimeType == null) {
try {
String mimeTypeGuess = URLConnection.guessContentTypeFromName(new File(file).getName());
if (mimeTypeGuess !=null) {
mimeType = mimeTypeGuess;
}
} catch (Exception e) {
log("couldn't find a better contentType than " + mimeType + " for file " + file, e);
}
}

// TODO: The getContentType method of the MimetypesFileTypeMap returns application/octet-stream
// instead of null when the type cannot be found. We should replace with an implementation that
// loads the System mime types ($JAVA_HOME/lib/mime.types
return mimeTypes.getContentType(s);
return mimeType;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,19 @@ void getMimeType_mimeTypeOfCorrectFile_expectMime() {
mimeType = ctx.getMimeType("file://" + tmpFilePath);
assertEquals("text/plain", mimeType);
}
@Test
void getMimeType_mimeTypeOfJavascript_expectApplicationJavascript() {
String tmpFilePath = TMP_DIR + "some.js";
AwsServletContext ctx = new AwsServletContext(null);
String mimeType = ctx.getMimeType(tmpFilePath);
assertEquals("text/javascript", mimeType);
}

@Test
void getMimeType_unknownExtension_expectAppOctetStream() {
void getMimeType_unknownExtension_expectNull() {
AwsServletContext ctx = new AwsServletContext(null);
String mimeType = ctx.getMimeType("myfile.unkext");
assertEquals("application/octet-stream", mimeType);
assertNull(mimeType);
}


Expand Down

0 comments on commit f5cf12d

Please sign in to comment.