java.lang.Object
net.i2p.i2ptunnel.MimeTypeDetector

public class MimeTypeDetectorextends Object
Utility class for MIME type detection based on URL file extensions.

This class provides static methods to determine the MIME type of a resource based on its file extension. It is used by I2PTunnelHTTPServer to set appropriate Content-Type headers for responses and to determine which security headers and cache policies should be applied.

The MIME type detection follows the Common MIME types specification as documented at MDN: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types

  • Field Details

    • CUSTOM_WHITELIST_SET

      private static final Set<String> CUSTOM_WHITELIST_SET
      MIME types that require security headers (Referrer-Policy, Allow).
    • IMMUTABLE_CACHE_WHITELIST_SET

      private static final Set<String> IMMUTABLE_CACHE_WHITELIST_SET
      MIME types that should use immutable cache control.
  • Constructor Details

    • MimeTypeDetector

      public MimeTypeDetector()
      Every lookup table and every method here is static, so an instance holds no state of its own; the class is meant to be used through its static methods.
  • Method Details

    • detectMimeType

      public static String detectMimeType(String url)
      Detects the MIME type based on a URL's file extension.

      If the URL contains a query string, it is stripped before checking the extension. If no matching extension is found or the URL is null, returns "application/octet-stream" as a safe default.

      Parameters:
      url - the URL or file path to detect the MIME type for; may be null
      Returns:
      the detected MIME type, or "application/octet-stream" if unknown
    • isCustomWhitelist

      public static boolean isCustomWhitelist(String mimeType)
      Checks if a MIME type requires custom security headers.

      MIME types in this whitelist (text/html, application/xhtml+xml, etc.) will receive additional security headers such as Referrer-Policy and Allow headers.

      Parameters:
      mimeType - the MIME type to check; may be null
      Returns:
      true if the MIME type requires security headers, false otherwise
    • isImmutableCache

      public static boolean isImmutableCache(String mimeType)
      Checks if a MIME type should use immutable cache control headers.

      MIME types that match known immutable content patterns (images, fonts, video, audio, etc.) will receive "Cache-Control: private, max-age=31536000, immutable" to enable long-term caching.

      Parameters:
      mimeType - the MIME type to check; may be null
      Returns:
      true if the MIME type should use immutable cache control, false otherwise