Templates breaking mime type of other files in the path (even with forced mime)

(Renaud Guérin) #1

Enabling templates for .html (or just with default settings) breaks the mime types of JSON files in the paths it is enabled for. They’re all served as text/plain; charset=utf-8

Plot twist : I can’t reproduce this with Caddy 1.0.0 on my Mac, I only have this problem with the popular abiosoft/caddy-docker Docker image (so, I don’t know if it’s a Linux specific problem or due to the way it’s built)

Repro steps: very simple Caddyfile with

:2015
templates
mime .json application/json
  • curl -I localhost:2016/ (templated index.html) correctly returns Content-Type: text/html; charset=utf-8
  • curl -I localhost:2016/config.json (plain JSON file) wrongly returns Content-Type: text/plain; charset=utf-8

What I’ve tried:

  • Rebuilding the Abiosoft container without any plugins, to match the vanilla Caddy I’m using on Mac (they now both output the exact same version and plugins). Didn’t work.
  • Forcing/not forcing the type with the mime directive. Didn’t work.
  • Explicitly setting templates / .html and using the long form with path / ext / between. Didn’t work.
  • Restricting templates to a subpath like this : templates /subdir .html. This works : JSON files outside of that path are now served correctly. But I can’t use that because I need my index templated !

Any help greatly appreciated !

(Matthew Fay) #2

Confirmed this problem in abiosoft/caddy:1.0.0. Also confirmed working fine on my mac, too.

MacOS
:2015
templates
~/Projects/test
➜ caddy -version
Caddy v1.0.0 (h1:KI6RPGih2GFzWRPG8s9clKK28Ns4ZlVMKR/v7mxq6+c=)

~/Projects/test
➜ caddy
Activating privacy features... done.

Serving HTTP on port 2015
http://:2015
~/Projects/test
➜ curl -i localhost:2015/sample.json
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 151
Content-Type: application/json
Etag: "poyh2x47"
Last-Modified: Tue, 26 Mar 2019 04:13:45 GMT
Server: Caddy
Date: Wed, 08 May 2019 23:44:15 GMT

{
    "str": "I'm a string",
    "int_example": 5,
    "struct": {
        "foo": 1,
        "bar": "foobar"
    },
    "arr": ["foo", "bar", "etc"]
}
abiosoft/caddy:1.0.0
:2015
templates
whitestrake at cadmus in ~
❯ docker run --rm -it --name "testcaddy" -v (pwd)/Caddyfile:/etc/Caddyfile -v (pwd)/sample.json:/srv/sample.json -p 2015:2015 abiosoft/caddy:1.0.0
Activating privacy features... done.
2019/05/08 23:51:23 [INFO][cache:0xc000032140] Started certificate maintenance routine

Serving HTTP on port 2015
http://:2015

2019/05/08 23:51:23 [INFO] Serving http://:2015
whitestrake at cadmus in ~
❯ docker exec -it testcaddy caddy -version
Caddy v1.0.0 (h1:KI6RPGih2GFzWRPG8s9clKK28Ns4ZlVMKR/v7mxq6+c=)
whitestrake at cadmus in ~
❯ curl -i localhost:2015/sample.json
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 151
Content-Type: text/plain; charset=utf-8
Etag: "pr7m0e47"
Last-Modified: Wed, 08 May 2019 23:45:50 GMT
Server: Caddy
Date: Wed, 08 May 2019 23:51:38 GMT

{
    "str": "I'm a string",
    "int_example": 5,
    "struct": {
        "foo": 1,
        "bar": "foobar"
    },
    "arr": ["foo", "bar", "etc"]
}

You might want to open an issue at https://github.com/abiosoft/caddy-docker/issues so the author can have a look into it.

Oh, you’ve already opened an issue 6 hours ago… Guess I didn’t need to bother testing that…

1 Like