/
https.go
94 lines (76 loc) · 2.21 KB
/
https.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
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 provider
import (
"context"
"encoding/base64"
"fmt"
"io"
"net/http"
"strings"
"github.com/minepkg/minepkg/internals/pkgid"
"github.com/minepkg/minepkg/pkg/manifest"
)
type HTTPSProvider struct {
Client *http.Client
}
type httpsResult struct {
cacheKey string
dependency *pkgid.ID
}
func NewHTTPSProvider() *HTTPSProvider {
return &HTTPSProvider{
Client: http.DefaultClient,
}
}
func (h *httpsResult) Lock() *manifest.DependencyLock {
lock := &manifest.DependencyLock{
Name: h.dependency.Name,
Provider: h.dependency.Provider,
Type: "mod",
URL: h.dependency.Version,
Version: h.cacheKey,
}
return lock
}
func (h *httpsResult) Dependencies() []*manifest.InterpretedDependency {
// there currently is no way to resolve these … so empty they stay
return []*manifest.InterpretedDependency{}
}
func (h *HTTPSProvider) Name() string { return "https" }
func (h *HTTPSProvider) Resolve(ctx context.Context, request *Request) (Result, error) {
if !strings.HasPrefix(request.Dependency.Version, "https://") {
return nil, fmt.Errorf("refusing to resolve non https url %s", request.Dependency.Version)
}
req, err := http.NewRequest("HEAD", request.Dependency.Version, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res, err := h.Client.Do(req)
if err != nil {
return nil, err
}
etag := res.Header.Get("etag")
lastModified := res.Header.Get("Last-Modified")
cacheKey := strings.TrimPrefix(strings.TrimSuffix(etag, `"`), `"`)
if etag == "" && lastModified != "" {
cacheKey = base64.StdEncoding.EncodeToString([]byte(lastModified))
}
if cacheKey == "" {
return nil, fmt.Errorf(
"the http server of %s does not set a \"ETag\" or \"Last-Modified\" header, which currently is a requirement",
request.Dependency.Name,
)
}
return &httpsResult{dependency: request.Dependency, cacheKey: cacheKey}, nil
}
func (h *HTTPSProvider) Fetch(ctx context.Context, toFetch Result) (io.Reader, int, error) {
req, err := http.NewRequestWithContext(ctx, "GET", toFetch.Lock().URL, nil)
if err != nil {
return nil, 0, err
}
fileRes, err := http.DefaultClient.Do(req)
if err != nil {
return nil, 0, err
}
return fileRes.Body, int(fileRes.ContentLength), nil
}