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

This commit is contained in:
Shinsuke Sugaya 2024-05-09 15:28:46 +09:00
parent 95277d5861
commit 054c7a0b81

View file

@ -108,17 +108,6 @@ public class SearchEngineApiManager extends BaseApiManager {
}
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/")) {
processPluginRequest(request, response, path.replaceFirst("^/_plugin", StringUtil.EMPTY));
return;
@ -153,6 +142,12 @@ public class SearchEngineApiManager extends BaseApiManager {
try (ServletOutputStream out = response.getOutputStream(); InputStream in = curlResponse.getContentAsStream()) {
response.setStatus(curlResponse.getHttpStatusCode());
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);
} catch (final ClientAbortException 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) {
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("/"));
if (Files.isDirectory(filePath)) {
filePath = filePath.resolve("index.html");