Browse Source

Allow overriding MIME types based on path regexes.

master
Solderpunk 1 year ago
parent
commit
5377c2941f
  1. 1
      README.md
  2. 1
      config.go
  3. 13
      handler.go

1
README.md

@ -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
all intermediate directories must exist, Molly Brown won't create
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").
* `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).

1
config.go

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

13
handler.go

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

Loading…
Cancel
Save