You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by ja...@apache.org on 2021/11/18 11:21:40 UTC

[solr] branch main updated: SOLR-15804 Allow content-types with ; charset in ShowFileRequestHandler (#418)

This is an automated email from the ASF dual-hosted git repository.

janhoy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/main by this push:
     new 46d2ad2  SOLR-15804 Allow content-types with ;charset in ShowFileRequestHandler (#418)
46d2ad2 is described below

commit 46d2ad2ca9f200628d905aeab2378a1399ae25bb
Author: Jan Høydahl <ja...@users.noreply.github.com>
AuthorDate: Thu Nov 18 12:21:31 2021 +0100

    SOLR-15804 Allow content-types with ;charset in ShowFileRequestHandler (#418)
    
    * Allow content-types with charset in ShowFileRequestHandler
    * Request .html as text/xml, .css as text/plain and .js as application/javascript
    * Highlight css as txt (crashed browser)
    * Set extension to 'xml' for managed-schema instead of 'text/xml'
---
 solr/CHANGES.txt                                               |  2 ++
 .../org/apache/solr/handler/admin/ShowFileRequestHandler.java  | 10 ++++++++--
 .../apache/solr/handler/admin/ShowFileRequestHandlerTest.java  |  7 +++++++
 solr/webapp/web/js/angular/controllers/files.js                |  6 +++---
 4 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index c16fff9..c96a00f 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -440,6 +440,8 @@ Bug Fixes
   
 * SOLR-15795: Fix REPLACENODE to not use source node when choosing a target node for new replicas (Houston Putman)
 
+* SOLR-15804: Admin UI once again can show files in the Core/Collection -> Files screen. Fixed regression from 8.11.0 (Karl Stoney, janhoy)
+
 ==================  8.11.0 ==================
 
 Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/ShowFileRequestHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/ShowFileRequestHandler.java
index bcd3479..b657d97 100644
--- a/solr/core/src/java/org/apache/solr/handler/admin/ShowFileRequestHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/admin/ShowFileRequestHandler.java
@@ -96,11 +96,16 @@ public class ShowFileRequestHandler extends RequestHandlerBase implements Permis
 {
   public static final String HIDDEN = "hidden";
   public static final String USE_CONTENT_TYPE = "contentType";
+  private static final Set<String> KNOWN_MIME_TYPES;
 
   protected Set<String> hiddenFiles;
 
   private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
+  static {
+    KNOWN_MIME_TYPES = new HashSet<>(MimeTypes.getKnownMimeTypes());
+    KNOWN_MIME_TYPES.add("text/xml");
+  }
 
   public ShowFileRequestHandler()
   {
@@ -265,10 +270,11 @@ public class ShowFileRequestHandler extends RequestHandlerBase implements Permis
       log.debug("No contentType specified");
       return null;
     }
-    if (!MimeTypes.getKnownMimeTypes().contains(contentType)) {
+    String rawContentType = contentType.split(";")[0].trim().toLowerCase(Locale.ROOT); // Strip away charset part
+    if (!KNOWN_MIME_TYPES.contains(rawContentType)) {
       throw new SolrException(ErrorCode.BAD_REQUEST, "Requested content type '" + contentType + "' is not supported.");
     }
-    if (contentType.toLowerCase(Locale.ROOT).contains("html")) {
+    if (rawContentType.contains("html")) {
       log.info("Using text/plain instead of {}", contentType);
       return "text/plain";
     }
diff --git a/solr/core/src/test/org/apache/solr/handler/admin/ShowFileRequestHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/ShowFileRequestHandlerTest.java
index 0e78329..ccdb465 100644
--- a/solr/core/src/test/org/apache/solr/handler/admin/ShowFileRequestHandlerTest.java
+++ b/solr/core/src/test/org/apache/solr/handler/admin/ShowFileRequestHandlerTest.java
@@ -171,6 +171,13 @@ public class ShowFileRequestHandlerTest extends SolrJettyTestBase {
     assertEquals("text/plain", ShowFileRequestHandler.getSafeContentType("text/html"));
     assertEquals("text/plain", ShowFileRequestHandler.getSafeContentType("application/xhtml+xml"));
 
+    // Content-type with charset
+    assertEquals("text/csv ; charset=utf-8", ShowFileRequestHandler.getSafeContentType("text/csv ; charset=utf-8"));
+    assertEquals("text/xml;charset=utf-8", ShowFileRequestHandler.getSafeContentType("text/xml;charset=utf-8"));
+
+    // Null
+    assertNull(ShowFileRequestHandler.getSafeContentType(null));
+
     // Non-known content types are rejected with 400 error
     expectThrows(SolrException.class, () -> ShowFileRequestHandler.getSafeContentType("foo/bar"));
   }
diff --git a/solr/webapp/web/js/angular/controllers/files.js b/solr/webapp/web/js/angular/controllers/files.js
index 76f7dd3..9275466 100644
--- a/solr/webapp/web/js/angular/controllers/files.js
+++ b/solr/webapp/web/js/angular/controllers/files.js
@@ -15,8 +15,8 @@
  limitations under the License.
 */
 
-var contentTypeMap = { xml : 'text/xml', html : 'text/html', js : 'text/javascript', json : 'application/json', 'css' : 'text/css' };
-var languages = {js: "javascript", xml:"xml", xsl:"xml", vm: "xml", html: "xml", json: "json", css: "css"};
+var contentTypeMap = { xml : 'application/xml', html : 'application/xml', js : 'application/javascript', json : 'application/json', css : 'text/plain' };
+var languages = {js: "javascript", xml:"xml", xsl:"xml", vm: "xml", html: "xml", json: "json"};
 
 solrAdminApp.controller('FilesController',
     function($scope, $rootScope, $routeParams, $location, Files, Constants) {
@@ -71,7 +71,7 @@ solrAdminApp.controller('FilesController',
             if ($scope.file && $scope.file !== '' && $scope.file.split('').pop()!=='/') {
                 var extension;
                 if ($scope.file === "managed-schema") {
-                  extension = contentTypeMap['xml'];
+                  extension = 'xml';
                 } else {
                   extension = $scope.file.match( /\.(\w+)$/)[1] || '';
                 }