Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Provide correct mime type for javascript and css #504

Merged
merged 4 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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