Allow overriding MIME types based on path regexes.

This commit is contained in:
Solderpunk 2020-06-28 00:12:32 +02:00
parent a07645dd2e
commit 5377c2941f
3 changed files with 12 additions and 3 deletions

View File

@ -92,6 +92,7 @@ The following options can be set in `/etc/molly.conf`:
* `LogPath`: Path to log file (default value `molly.log`). Note that * `LogPath`: Path to log file (default value `molly.log`). Note that
all intermediate directories must exist, Molly Brown won't create all intermediate directories must exist, Molly Brown won't create
them for you. them for you.
* `MimeOverrides`: A map from path regexs to MIME types. If the path of a file which is about to be served matches the regex, the specified MIME type will be used instead of one inferred from the filename extension.
* `DirectorySort`: A string specifying how to sort files in automatically generated directory listings. Must be one of "Name", "Size" or "Time" (default value "Name"). * `DirectorySort`: A string specifying how to sort files in automatically generated directory listings. Must be one of "Name", "Size" or "Time" (default value "Name").
* `DirectoryReverse` (boolean): if true, automatically generated directory listings will list files in descending order of whatever `DirectorySort` is set to (default value false). * `DirectoryReverse` (boolean): if true, automatically generated directory listings will list files in descending order of whatever `DirectorySort` is set to (default value false).
* `DirectoryTitles` (boolean): if true, automatically generated directory listings will use the first top-level heading (i.e. line beginning with "# ") in files with an extension of `GeminiExt` instead of the filename (default value false). * `DirectoryTitles` (boolean): if true, automatically generated directory listings will use the first top-level heading (i.e. line beginning with "# ") in files with an extension of `GeminiExt` instead of the filename (default value false).

View File

@ -17,6 +17,7 @@ type Config struct {
LogPath string LogPath string
TempRedirects map[string]string TempRedirects map[string]string
PermRedirects map[string]string PermRedirects map[string]string
MimeOverrides map[string]string
CGIPaths []string CGIPaths []string
SCGIPaths map[string]string SCGIPaths map[string]string
DirectorySort string DirectorySort string

View File

@ -380,14 +380,21 @@ func serveFile(path string, log *LogEntry, conn net.Conn, config Config) {
} else { } else {
mimeType = mime.TypeByExtension(ext) mimeType = mime.TypeByExtension(ext)
} }
// Add lang parameter // Override extension-based MIME type
if mimeType == "text/gemini" && config.DefaultLang != "" { for pathRegex, newType := range config.MimeOverrides {
mimeType += "; lang=" + config.DefaultLang overridden, err := regexp.Match(pathRegex, []byte(path))
if err == nil && overridden {
mimeType = newType
}
} }
// Set a generic MIME type if the extension wasn't recognised // Set a generic MIME type if the extension wasn't recognised
if mimeType == "" { if mimeType == "" {
mimeType = "application/octet-stream" mimeType = "application/octet-stream"
} }
// Add lang parameter
if mimeType == "text/gemini" && config.DefaultLang != "" {
mimeType += "; lang=" + config.DefaultLang
}
contents, err := ioutil.ReadFile(path) contents, err := ioutil.ReadFile(path)
if err != nil { if err != nil {