Welcome to mirror list, hosted at ThFree Co, Russian Federation.

resource.go « http_range « zip « vfs « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d35563b0fb0100448766d2e9f5e0fdbbad689239 (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
package http_range

import (
	"context"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"strconv"
	"strings"
)

type Resource struct {
	URL          string
	Etag         string
	LastModified string
	Size         int64
}

func NewResource(ctx context.Context, URL string) (*Resource, error) {
	// the `h.URL` is likely presigned only for GET
	req, err := http.NewRequest("GET", URL, nil)
	if err != nil {
		return nil, err
	}

	req = req.WithContext(ctx)

	// we fetch a single byte and ensure that range requests is additionally supported
	req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", 0, 0))
	res, err := httpClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer io.Copy(ioutil.Discard, res.Body)
	defer res.Body.Close()

	resource := &Resource{
		URL:          URL,
		Etag:         res.Header.Get("ETag"),
		LastModified: res.Header.Get("Last-Modified"),
	}

	switch res.StatusCode {
	case http.StatusOK:
		resource.Size = res.ContentLength
		println(resource.URL, resource.Etag, resource.LastModified, resource.Size)
		return resource, nil

	case http.StatusPartialContent:
		contentRange := res.Header.Get("Content-Range")
		ranges := strings.SplitN(contentRange, "/", 2)
		if len(ranges) != 2 {
			return nil, fmt.Errorf("invalid `Content-Range`: %q", contentRange)
		}

		resource.Size, err = strconv.ParseInt(ranges[1], 0, 64)
		if err != nil {
			return nil, err
		}

		return resource, nil

	case http.StatusRequestedRangeNotSatisfiable:
		return nil, ErrRangeRequestsNotSupported

	default:
		return nil, fmt.Errorf("failed with %d: %q", res.StatusCode, res.Status)
	}
}