summaryrefslogtreecommitdiffstats
path: root/content/posts/openbsd-httpd-mime-types/index.org
blob: 70d7d51bab4c41e098fc3baa3283835bdbdce483 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#+TITLE: Fixing OpenBSD 6.7 httpd MIME Types
#+DATE: 2020-08-15T01:49:22-04:00
#+DRAFT: false
#+DESCRIPTION: How to get OpenBSD httpd to return the proper MIME types for files
#+TAGS[]: openbsd httpd
#+KEYWORDS[]: openbsd httpd
#+SLUG:
#+SUMMARY:

#+ATTR_HTML: :title OpenBSD httpd MIME type recognition
#+ATTR_HTML: :alt OpenBSD httpd MIME type recognition
[[file:cover.png]]

On OpenBSD's httpd, there are only a select few MIME types that are
recognized by default. According to [[https://man.openbsd.org/httpd.conf.5][=httpd.conf(5)=]], those types are:
=ext/css=, =text/html=, =text/plain=, =image/gif=, =image/png=,
=image/jpeg=, =image/svg+xml=, and
=application/javascript=. Everything else is said to be of type
=application/octet-stream= by default.

This is OK for most static hosting situations, but can be challenging
for some common attachment types. For example, I recently made a blog
post that had an attached PDF. Normally web browsers open PDF files in
the browser's PDF viewer, but since it was being returned with the
MIME type =application/octet-stream=, the browser decided to download
it instead of opening it directly. This isn't a tragedy, but it can
be annoying and interrupt the flow of the user's experience.

The solution to this is to include the =application/pdf= MIME type in
a [[https://man.openbsd.org/httpd.conf.5#TYPES][=TYPES=]] block in your =httpd.conf=. There are two ways to go about
this. Either by manually defining the type you require, or including
the system's built-in MIME type database.

Using the first method you would include a types block that defines
all of the MIME types you require. This types block can go anywhere
outside of a server declaration.

The types are defined in the format of =type/subtype name [name ...]=.

#+begin_src
types {
    text/css                css
    text/html               html htm
    text/plain              txt
    image/gif               gif
    image/jpeg              jpeg jpg
    image/png               png
    application/javascript  js
    application/pdf         pdf
}
#+end_src

The second method, including the system's built-in MIME types
database, is simpler if you need to support proper MIME types for a
larger number of files, and don't want to go through the process of
writing them all in by hand. This is done by defining a similar types
block, but instead of writing in type data, you use the [[https://man.openbsd.org/httpd.conf.5#include][=include=]]
directive. OpenBSD's MIME type definitions are stored in
=/usr/share/misc/mime.types=.

#+BEGIN_SRC
types {
    include "/usr/share/misc/mime.types"
}
#+END_SRC

After editing the configuration file, all you have to do is reload
=httpd=, and it should begin recognizing file types properly.

#+BEGIN_SRC shell
doas rcctl reload httpd
#+END_SRC