github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/clone/internal/download/http.go (about) 1 // Copyright 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package download provides a fetcher to get data via http(s). 16 package download 17 18 import ( 19 "fmt" 20 "io" 21 "net/http" 22 "net/url" 23 24 "github.com/golang/glog" 25 ) 26 27 // NewHTTPFetcher returns an HTTPFetcher that gets paths appended to the given prefix. 28 func NewHTTPFetcher(prefix *url.URL) HTTPFetcher { 29 return HTTPFetcher{ 30 baseURL: prefix, 31 } 32 } 33 34 // HTTPFetcher gets the data over HTTP(S). 35 // This is thread safe. 36 type HTTPFetcher struct { 37 baseURL *url.URL 38 } 39 40 // GetData gets the data at the given path. 41 func (f HTTPFetcher) GetData(path string) ([]byte, error) { 42 u, err := f.baseURL.Parse(path) 43 if err != nil { 44 return nil, fmt.Errorf("url (%s) failed to parse(%s): %w", f.baseURL, path, err) 45 } 46 target := u.String() 47 resp, err := http.Get(target) 48 if err != nil { 49 return nil, fmt.Errorf("http.Get: %w", err) 50 } 51 defer func() { 52 if err := resp.Body.Close(); err != nil { 53 glog.Errorf("resp.Body.Close(): %v", err) 54 } 55 }() 56 if resp.StatusCode != 200 { 57 // Could be worth returning a special error when we've been explicitly told to back off. 58 return nil, fmt.Errorf("GET %v: %v", target, resp.Status) 59 } 60 // TODO(mhutchinson): Consider using io.LimitReader and making it configurable. 61 data, err := io.ReadAll(resp.Body) 62 if err != nil { 63 return nil, fmt.Errorf("io.ReadAll: %w", err) 64 } 65 return data, nil 66 }