summaryrefslogtreecommitdiff
path: root/fetch.go
blob: 3bcd480355fa7b5c5471350a7dc22a49eab15c78 (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
package main

import (
	"fmt"
	"io"
	"net/http"
	"net/url"
	"strings"
	"time"
)

// fetch makes a GET request to refURL, returning the HTML contents of
// that webpage. An error is also returned.
//
// A [url.URL] type is used for refURL to simplify recursive or else
// repeated use of this function when crawling webpages to, say, build
// a sitemap.
func fetch(refURL url.URL) ([]byte, error) {
	rawURL := refURL.String()

	// FIXME: make the timeout configurable.
	client := http.Client{
		Timeout: 2 * time.Second,
	}

	req, err := http.NewRequest(http.MethodGet, rawURL, nil)
	if err != nil {
		return nil, fmt.Errorf("can't create request: %w", err)
	}

	req.Header.Add("user-agent", "urls/1.0, GNU/Linux")

	resp, err := client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("client failed: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("status for %s for %s: %s", http.MethodGet, rawURL, resp.Status)
	}

	if contentType := resp.Header.Get("content-type"); !goodContentType(contentType) {
		return nil, fmt.Errorf("non-html content-type: %s", contentType)
	}

	htmlBytes, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("can't read reponse body into byte buffer")
	}

	return htmlBytes, nil
}

func goodContentType(contentType string) bool {
	what := strings.TrimSpace(strings.SplitN(contentType, ";", 2)[0])

	return what == "text/html"
}