aboutsummaryrefslogtreecommitdiffhomepage
path: root/http/response/response.go
blob: f79262542637b818333e3114157817b25f018f04 (plain)
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
// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package response

import (
	"compress/flate"
	"compress/gzip"
	"net/http"
	"strings"
	"time"
)

// Redirect redirects the user to another location.
func Redirect(w http.ResponseWriter, r *http.Request, path string) {
	http.Redirect(w, r, path, http.StatusFound)
}

// NotModified sends a response with a 304 status code.
func NotModified(w http.ResponseWriter) {
	w.WriteHeader(http.StatusNotModified)
}

// Cache returns a response with caching headers.
func Cache(w http.ResponseWriter, r *http.Request, mimeType, etag string, data []byte, duration time.Duration) {
	w.Header().Set("Content-Type", mimeType)
	w.Header().Set("ETag", etag)
	w.Header().Set("Cache-Control", "public")
	w.Header().Set("Expires", time.Now().Add(duration).Format(time.RFC1123))

	if etag == r.Header.Get("If-None-Match") {
		w.WriteHeader(http.StatusNotModified)
		return
	}

	switch mimeType {
	case "text/javascript; charset=utf-8", "text/css; charset=utf-8":
		Compress(w, r, data)
	default:
		w.Write(data)
	}
}

// Compress the response sent to the browser.
func Compress(w http.ResponseWriter, r *http.Request, data []byte) {
	acceptEncoding := r.Header.Get("Accept-Encoding")

	switch {
	case strings.Contains(acceptEncoding, "gzip"):
		w.Header().Set("Content-Encoding", "gzip")
		gzipWriter := gzip.NewWriter(w)
		defer gzipWriter.Close()
		gzipWriter.Write(data)
	case strings.Contains(acceptEncoding, "deflate"):
		w.Header().Set("Content-Encoding", "deflate")
		flateWriter, _ := flate.NewWriter(w, -1)
		defer flateWriter.Close()
		flateWriter.Write(data)
	default:
		w.Write(data)
	}
}