about summary refs log tree commit diff stats
path: root/internal/fetcher/http.go
blob: c5ec8fc3f0be121ca59a28c2c36c8e242ab68671 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package fetcher

import (
	"context"
	"fmt"
	"io"
	"net/http"
	"strings"
	"time"

	"go.alanpearce.eu/searchix/internal/config"

	"github.com/andybalholm/brotli"
	"github.com/pkg/errors"
	"go.alanpearce.eu/x/log"
)

type brotliReadCloser struct {
	src io.ReadCloser
	*brotli.Reader
}

func newBrotliReader(src io.ReadCloser) *brotliReadCloser {
	return &brotliReadCloser{
		src:    src,
		Reader: brotli.NewReader(src),
	}
}

func (r *brotliReadCloser) Close() error {
	return errors.Wrap(r.src.Close(), "failed to call close on underlying reader")
}

func fetchFileIfNeeded(
	ctx context.Context,
	log *log.Logger,
	mtime time.Time,
	url string,
) (body io.ReadCloser, newMtime time.Time, err error) {
	var ifModifiedSince string
	if !mtime.IsZero() {
		ifModifiedSince = strings.Replace(mtime.UTC().Format(time.RFC1123), "UTC", "GMT", 1)
	}

	req, err := http.NewRequestWithContext(ctx, "GET", url, http.NoBody)
	if err != nil {
		err = errors.WithMessagef(err, "could not create HTTP request for %s", url)

		return
	}

	req.Header.Set("User-Agent", fmt.Sprintf("Searchix %s", config.Version))

	if ifModifiedSince != "" {
		req.Header.Set("If-Modified-Since", ifModifiedSince)
	}
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		err = errors.WithMessagef(err, "could not make HTTP request to %s", url)

		return
	}

	switch res.StatusCode {
	case http.StatusNotModified:
		newMtime = mtime

		return
	case http.StatusOK:
		newMtime, err = time.Parse(time.RFC1123, res.Header.Get("Last-Modified"))
		if err != nil {
			log.Warn(
				"could not parse Last-Modified header from response",
				"value",
				res.Header.Get("Last-Modified"),
			)
			newMtime = time.Now()
		}

		switch ce := res.Header.Get("Content-Encoding"); ce {
		case "br":
			log.Debug("using brotli encoding")
			body = newBrotliReader(res.Body)
		case "", "identity", "gzip":
			body = res.Body
		default:
			err = fmt.Errorf("cannot handle a body with content-encoding %s", ce)
		}
	default:
		err = fmt.Errorf("got response code %d, don't know what to do", res.StatusCode)
	}

	return
}