home

Exercise 3: Lens Calculator (bad mime-type)

Once again, these simple exercises are shaking out suprising issues. In Exercise 1, I found an error in golang template rendering would be interleaved with the error message. In Exercise 2, I was surprised that golang's panic output did not have a timestamp (my bad) and so added DJB's tai64 to logging. And in this exercise, I found that when I was trying to render the template contents as plain text (exercise3.template.txt), golang's ServeContent was returning the wrong mime type.

While I opened an Alpine Linux bug (12626) I'm not sure this is really an issue with Alpine. The root cause is that Alpine does not provide a mime type file in a location that go expects it, and the default golang mime types do not include the .txt extension.

mime/type.go:

    60  var builtinTypesLower = map[string]string{
    61  	".css":  "text/css; charset=utf-8",
    62  	".gif":  "image/gif",
    63  	".htm":  "text/html; charset=utf-8",
    64  	".html": "text/html; charset=utf-8",
    65  	".jpeg": "image/jpeg",
    66  	".jpg":  "image/jpeg",
    67  	".js":   "text/javascript; charset=utf-8",
    68  	".json": "application/json",
    69  	".mjs":  "text/javascript; charset=utf-8",
    70  	".pdf":  "application/pdf",
    71  	".png":  "image/png",
    72  	".svg":  "image/svg+xml",
    73  	".wasm": "application/wasm",
    74  	".webp": "image/webp",
    75  	".xml":  "text/xml; charset=utf-8",
    76  }

mime/type_unix.go

    19  var typeFiles = []string{
    20  	"/etc/mime.types",
    21  	"/etc/apache2/mime.types",
    22  	"/etc/apache/mime.types",
    23  	"/etc/httpd/conf/mime.types",
    24  }

I found that the Alpine package provides the file /etc/mime.types, and after adding that package and restarting the golang server go returned text/plain as the content type.

-- Mark Bucciarelli, April 22, 2021