forked from andybalholm/redwood
/
mime.go
44 lines (39 loc) · 1.1 KB
/
mime.go
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
package redwood
import (
"bytes"
"io"
"mime"
"net/http"
"strings"
)
// fixContentType checks resp's Content-Type header. If it is missing or too
// general, it sniffs the body content to find the content type, and updates
// the Content-Type header.
func fixContentType(resp *http.Response) {
ct, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type"))
if err != nil || !strings.Contains(ct, "/") {
ct = ""
}
switch ct {
case "unknown/unknown", "application/unknown", "*/*", "", "application/octet-stream":
// These types tend to be used for content whose type is unknown,
// so we should try to second-guess them.
preview := make([]byte, 512)
n, _ := resp.Body.Read(preview)
preview = preview[:n]
if n > 0 {
ct, _, _ = mime.ParseMediaType(http.DetectContentType(preview))
if ct != "application/octet-stream" {
resp.Header.Set("Content-Type", ct)
}
// Make the preview data available for re-reading.
var rc struct {
io.Reader
io.Closer
}
rc.Reader = io.MultiReader(bytes.NewBuffer(preview), resp.Body)
rc.Closer = resp.Body
resp.Body = rc
}
}
}