forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
44 lines (35 loc) · 911 Bytes
/
transport.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
package cargo
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
)
type Transport struct{}
func NewTransport() Transport {
return Transport{}
}
func (t Transport) Drop(root, uri string) (io.ReadCloser, error) {
if strings.HasPrefix(uri, "file://") {
file, err := os.Open(filepath.Join(root, strings.TrimPrefix(uri, "file://")))
if err != nil {
return nil, fmt.Errorf("failed to open file: %s", err)
}
return file, nil
}
request, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, fmt.Errorf("failed to parse request uri: %s", err)
}
response, err := http.DefaultClient.Do(request)
if err != nil {
return nil, fmt.Errorf("failed to make request: %s", err)
}
if response.StatusCode >= 400 {
response.Body.Close()
return nil, fmt.Errorf("unexpected status code %d while fetching %q", response.StatusCode, uri)
}
return response.Body, nil
}