Browse Source

fix #2812 Refactor content type setting in API response headers for enhanced accuracy and coverage

Shinsuke Sugaya 1 year ago
parent
commit
054c7a0b81

+ 37 - 11
src/main/java/org/codelibs/fess/api/engine/SearchEngineApiManager.java

@@ -108,17 +108,6 @@ public class SearchEngineApiManager extends BaseApiManager {
     }
     }
 
 
     protected void processRequest(final HttpServletRequest request, final HttpServletResponse response, final String path) {
     protected void processRequest(final HttpServletRequest request, final HttpServletResponse response, final String path) {
-        if (StringUtil.isNotBlank(path)) {
-            final String lowerPath = path.toLowerCase(Locale.ROOT);
-            if (lowerPath.endsWith(".html")) {
-                response.setContentType("text/html;charset=utf-8");
-            } else if (lowerPath.endsWith(".txt")) {
-                response.setContentType("text/plain");
-            } else if (lowerPath.endsWith(".css")) {
-                response.setContentType("text/css");
-            }
-        }
-
         if ("/_plugin".equals(path) || path.startsWith("/_plugin/")) {
         if ("/_plugin".equals(path) || path.startsWith("/_plugin/")) {
             processPluginRequest(request, response, path.replaceFirst("^/_plugin", StringUtil.EMPTY));
             processPluginRequest(request, response, path.replaceFirst("^/_plugin", StringUtil.EMPTY));
             return;
             return;
@@ -153,6 +142,12 @@ public class SearchEngineApiManager extends BaseApiManager {
             try (ServletOutputStream out = response.getOutputStream(); InputStream in = curlResponse.getContentAsStream()) {
             try (ServletOutputStream out = response.getOutputStream(); InputStream in = curlResponse.getContentAsStream()) {
                 response.setStatus(curlResponse.getHttpStatusCode());
                 response.setStatus(curlResponse.getHttpStatusCode());
                 writeHeaders(response);
                 writeHeaders(response);
+                final String responseContentType = curlResponse.getHeaderValue("Content-Type");
+                if (StringUtil.isBlank(responseContentType)) {
+                    response.setHeader("Content-Type", "application/json");
+                } else {
+                    response.setHeader("Content-Type", responseContentType);
+                }
                 CopyUtil.copy(in, out);
                 CopyUtil.copy(in, out);
             } catch (final ClientAbortException e) {
             } catch (final ClientAbortException e) {
                 logger.debug("Client aborts this request.", e);
                 logger.debug("Client aborts this request.", e);
@@ -166,6 +161,37 @@ public class SearchEngineApiManager extends BaseApiManager {
     }
     }
 
 
     protected void processPluginRequest(final HttpServletRequest request, final HttpServletResponse response, final String path) {
     protected void processPluginRequest(final HttpServletRequest request, final HttpServletResponse response, final String path) {
+        if (StringUtil.isNotBlank(path)) {
+            final String lowerPath = path.toLowerCase(Locale.ROOT);
+            if (lowerPath.endsWith(".html")) {
+                response.setContentType("text/html;charset=utf-8");
+            } else if (lowerPath.endsWith(".css")) {
+                response.setContentType("text/css");
+            } else if (lowerPath.endsWith(".eot")) {
+                response.setContentType("application/vnd.ms-fontobject");
+            } else if (lowerPath.endsWith(".ico")) {
+                response.setContentType("image/vnd.microsoft.icon");
+            } else if (lowerPath.endsWith(".js")) {
+                response.setContentType("text/javascript");
+            } else if (lowerPath.endsWith(".json")) {
+                response.setContentType("application/json");
+            } else if (lowerPath.endsWith(".otf")) {
+                response.setContentType("font/otf");
+            } else if (lowerPath.endsWith(".svg")) {
+                response.setContentType("image/svg+xml");
+            } else if (lowerPath.endsWith(".ttf")) {
+                response.setContentType("font/ttf");
+            } else if (lowerPath.endsWith(".txt")) {
+                response.setContentType("text/plain");
+            } else if (lowerPath.endsWith(".woff")) {
+                response.setContentType("font/woff");
+            } else if (lowerPath.endsWith(".woff2")) {
+                response.setContentType("font/woff2");
+            } else if (lowerPath.endsWith("/")) {
+                response.setContentType("text/html;charset=utf-8");
+            }
+        }
+
         Path filePath = ResourceUtil.getSitePath(path.replaceAll("\\.\\.+", StringUtil.EMPTY).replaceAll("/+", "/").split("/"));
         Path filePath = ResourceUtil.getSitePath(path.replaceAll("\\.\\.+", StringUtil.EMPTY).replaceAll("/+", "/").split("/"));
         if (Files.isDirectory(filePath)) {
         if (Files.isDirectory(filePath)) {
             filePath = filePath.resolve("index.html");
             filePath = filePath.resolve("index.html");