aboutsummaryrefslogtreecommitdiffstats
path: root/configserver
diff options
context:
space:
mode:
authorValerij Fredriksen <freva@users.noreply.github.com>2020-09-17 10:48:50 +0200
committerGitHub <noreply@github.com>2020-09-17 10:48:50 +0200
commit237bebb307533ac824492cf00f6f97efa3bbd1cf (patch)
tree1d9ad01b3b30d07106e3d6f25c5118c76f1a42be /configserver
parent39a3723e3a122784f1d4be3e3d7a0c8dc26a5a06 (diff)
Revert "Revert "Fix content-types""
Diffstat (limited to 'configserver')
-rw-r--r--configserver/src/main/java/com/yahoo/vespa/config/server/http/SessionContentReadResponse.java34
-rw-r--r--configserver/src/main/resources/mime.types20
-rw-r--r--configserver/src/test/apps/content/foo/bar/file-without-extension1
-rw-r--r--configserver/src/test/apps/content/foo/bar/test.jar (renamed from configserver/src/test/apps/content/foo/bar/test.txt)0
-rw-r--r--configserver/src/test/apps/content/foo/test1.json (renamed from configserver/src/test/apps/content/foo/test1.txt)0
-rw-r--r--configserver/src/test/java/com/yahoo/vespa/config/server/http/ContentHandlerTestBase.java47
6 files changed, 81 insertions, 21 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/http/SessionContentReadResponse.java b/configserver/src/main/java/com/yahoo/vespa/config/server/http/SessionContentReadResponse.java
index a1a41cc0472..f122768672d 100644
--- a/configserver/src/main/java/com/yahoo/vespa/config/server/http/SessionContentReadResponse.java
+++ b/configserver/src/main/java/com/yahoo/vespa/config/server/http/SessionContentReadResponse.java
@@ -7,6 +7,12 @@ import com.yahoo.container.jdisc.HttpResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
import static com.yahoo.jdisc.http.HttpResponse.Status.OK;
@@ -17,6 +23,8 @@ import static com.yahoo.jdisc.http.HttpResponse.Status.OK;
*/
public class SessionContentReadResponse extends HttpResponse {
+ private static final Map<String, String> contentTypeByExtension = loadContentTypeByExtension();
+
private final ApplicationFile file;
public SessionContentReadResponse(ApplicationFile file) {
@@ -33,7 +41,31 @@ public class SessionContentReadResponse extends HttpResponse {
@Override
public String getContentType() {
- return HttpResponse.DEFAULT_MIME_TYPE;
+ String filename = file.getPath().getName();
+ int lastDotIndex = filename.lastIndexOf('.');
+ if (lastDotIndex >= 0) {
+ String contentType = contentTypeByExtension.get(filename.substring(lastDotIndex + 1));
+ if (contentType != null) return contentType;
+ }
+ return DEFAULT_MIME_TYPE;
+ }
+
+ private static Map<String, String> loadContentTypeByExtension() {
+ try {
+ Pattern whitespace = Pattern.compile("\\s");
+ Map<String, String> map = new HashMap<>();
+ for (String line : Files.readAllLines(Paths.get("src/main/resources/mime.types"))) {
+ if (line.isEmpty() || line.charAt(0) == '#') continue;
+
+ String[] parts = whitespace.split(line);
+ for (int i = 1; i < parts.length; i++)
+ map.putIfAbsent(parts[i], parts[0]);
+ }
+
+ return map;
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
}
}
diff --git a/configserver/src/main/resources/mime.types b/configserver/src/main/resources/mime.types
new file mode 100644
index 00000000000..f6672f7738d
--- /dev/null
+++ b/configserver/src/main/resources/mime.types
@@ -0,0 +1,20 @@
+application/xml xml
+application/java-archive jar
+application/javascript js
+application/json json
+
+text/html html
+text/css css
+
+font/collection ttc
+font/otf otf
+font/ttf ttf
+font/woff woff
+font/woff2 woff2
+
+image/x-icon ico
+image/gif gif
+image/jpeg jpg jpeg
+image/png png
+image/svg+xml svg
+image/tiff tiff tif
diff --git a/configserver/src/test/apps/content/foo/bar/file-without-extension b/configserver/src/test/apps/content/foo/bar/file-without-extension
new file mode 100644
index 00000000000..6b584e8ece5
--- /dev/null
+++ b/configserver/src/test/apps/content/foo/bar/file-without-extension
@@ -0,0 +1 @@
+content \ No newline at end of file
diff --git a/configserver/src/test/apps/content/foo/bar/test.txt b/configserver/src/test/apps/content/foo/bar/test.jar
index 401a9f6e542..401a9f6e542 100644
--- a/configserver/src/test/apps/content/foo/bar/test.txt
+++ b/configserver/src/test/apps/content/foo/bar/test.jar
diff --git a/configserver/src/test/apps/content/foo/test1.txt b/configserver/src/test/apps/content/foo/test1.json
index 5716ca5987c..5716ca5987c 100644
--- a/configserver/src/test/apps/content/foo/test1.txt
+++ b/configserver/src/test/apps/content/foo/test1.json
diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/ContentHandlerTestBase.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/ContentHandlerTestBase.java
index 20e52263350..d231c7ac60f 100644
--- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/ContentHandlerTestBase.java
+++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/ContentHandlerTestBase.java
@@ -1,6 +1,16 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http;
+import com.google.common.base.Joiner;
+import com.google.common.collect.Collections2;
+import com.yahoo.container.jdisc.HttpResponse;
+import com.yahoo.jdisc.http.HttpRequest;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+
import static com.yahoo.jdisc.Response.Status.BAD_REQUEST;
import static com.yahoo.jdisc.Response.Status.NOT_FOUND;
import static com.yahoo.jdisc.Response.Status.OK;
@@ -8,30 +18,21 @@ import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
-import java.io.IOException;
-import java.util.Arrays;
-import java.util.Collection;
-
-import org.junit.Test;
-import com.google.common.base.Joiner;
-import com.google.common.collect.Collections2;
-import com.yahoo.container.jdisc.HttpResponse;
-import com.yahoo.jdisc.http.HttpRequest;
-
public abstract class ContentHandlerTestBase extends SessionHandlerTest {
protected String baseUrl = "http://foo:1337/application/v2/tenant/default/session/1/content/";
@Test
public void require_that_content_can_be_retrieved() throws IOException {
assertContent("/test.txt", "foo\n");
- assertContent("/foo/", generateResultArray("foo/bar/", "foo/test1.txt", "foo/test2.txt"));
- assertContent("/foo", generateResultArray("foo/"));
- assertContent("/foo/test1.txt", "bar\n");
+ assertContent("/foo/", generateResultArray("foo/bar/", "foo/test1.json", "foo/test2.txt"), "application/json");
+ assertContent("/foo", generateResultArray("foo/"), "application/json");
+ assertContent("/foo/test1.json", "bar\n", "application/json");
assertContent("/foo/test2.txt", "baz\n");
- assertContent("/foo/bar/", generateResultArray("foo/bar/test.txt"));
- assertContent("/foo/bar", generateResultArray("foo/bar/"));
- assertContent("/foo/bar/test.txt", "bim\n");
- assertContent("/foo/?recursive=true", generateResultArray("foo/bar/", "foo/bar/test.txt", "foo/test1.txt", "foo/test2.txt"));
+ assertContent("/foo/bar/", generateResultArray("foo/bar/file-without-extension", "foo/bar/test.jar"), "application/json");
+ assertContent("/foo/bar", generateResultArray("foo/bar/"), "application/json");
+ assertContent("/foo/bar/file-without-extension", "content");
+ assertContent("/foo/bar/test.jar", "bim\n", "application/java-archive");
+ assertContent("/foo/?recursive=true", generateResultArray("foo/bar/", "foo/bar/file-without-extension", "foo/bar/test.jar", "foo/test1.json", "foo/test2.txt"), "application/json");
}
@Test
@@ -58,21 +59,27 @@ public abstract class ContentHandlerTestBase extends SessionHandlerTest {
"{\"status\":\"new\",\"md5\":\"d3b07384d113edec49eaa6238ad5ff00\",\"name\":\"" + baseUrl + "test.txt\"}");
assertStatus("/foo/?return=status",
"[{\"status\":\"new\",\"md5\":\"\",\"name\":\"" + baseUrl + "foo/bar\"}," +
- "{\"status\":\"new\",\"md5\":\"c157a79031e1c40f85931829bc5fc552\",\"name\":\"" + baseUrl + "foo/test1.txt\"}," +
+ "{\"status\":\"new\",\"md5\":\"c157a79031e1c40f85931829bc5fc552\",\"name\":\"" + baseUrl + "foo/test1.json\"}," +
"{\"status\":\"new\",\"md5\":\"258622b1688250cb619f3c9ccaefb7eb\",\"name\":\"" + baseUrl + "foo/test2.txt\"}]");
assertStatus("/foo/?return=status&recursive=true",
"[{\"status\":\"new\",\"md5\":\"\",\"name\":\"" + baseUrl + "foo/bar\"}," +
- "{\"status\":\"new\",\"md5\":\"579cae6111b269c0129af36a2243b873\",\"name\":\"" + baseUrl + "foo/bar/test.txt\"}," +
- "{\"status\":\"new\",\"md5\":\"c157a79031e1c40f85931829bc5fc552\",\"name\":\"" + baseUrl + "foo/test1.txt\"}," +
+ "{\"status\":\"new\",\"md5\":\"9a0364b9e99bb480dd25e1f0284c8555\",\"name\":\"" + baseUrl + "foo/bar/file-without-extension\"}," +
+ "{\"status\":\"new\",\"md5\":\"579cae6111b269c0129af36a2243b873\",\"name\":\"" + baseUrl + "foo/bar/test.jar\"}," +
+ "{\"status\":\"new\",\"md5\":\"c157a79031e1c40f85931829bc5fc552\",\"name\":\"" + baseUrl + "foo/test1.json\"}," +
"{\"status\":\"new\",\"md5\":\"258622b1688250cb619f3c9ccaefb7eb\",\"name\":\"" + baseUrl + "foo/test2.txt\"}]");
}
protected void assertContent(String path, String expectedContent) throws IOException {
+ assertContent(path, expectedContent, HttpResponse.DEFAULT_MIME_TYPE);
+ }
+
+ protected void assertContent(String path, String expectedContent, String expectedContentType) throws IOException {
HttpResponse response = doRequest(HttpRequest.Method.GET, path);
assertNotNull(response);
final String renderedString = SessionHandlerTest.getRenderedString(response);
assertThat(renderedString, response.getStatus(), is(OK));
assertThat(renderedString, is(expectedContent));
+ assertThat(response.getContentType(), is(expectedContentType));
}
protected void assertStatus(String path, String expectedContent) throws IOException {