github.com/migueleliasweb/helm@v2.6.1+incompatible/pkg/getter/httpgetter.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 http://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 16 package getter 17 18 import ( 19 "bytes" 20 "fmt" 21 "io" 22 "net/http" 23 24 "k8s.io/helm/pkg/tlsutil" 25 "k8s.io/helm/pkg/urlutil" 26 ) 27 28 //httpGetter is the efault HTTP(/S) backend handler 29 type httpGetter struct { 30 client *http.Client 31 } 32 33 //Get performs a Get from repo.Getter and returns the body. 34 func (g *httpGetter) Get(href string) (*bytes.Buffer, error) { 35 buf := bytes.NewBuffer(nil) 36 37 resp, err := g.client.Get(href) 38 if err != nil { 39 return buf, err 40 } 41 if resp.StatusCode != 200 { 42 return buf, fmt.Errorf("Failed to fetch %s : %s", href, resp.Status) 43 } 44 45 _, err = io.Copy(buf, resp.Body) 46 resp.Body.Close() 47 return buf, err 48 } 49 50 // newHTTPGetter constructs a valid http/https client as Getter 51 func newHTTPGetter(URL, CertFile, KeyFile, CAFile string) (Getter, error) { 52 var client httpGetter 53 if CertFile != "" && KeyFile != "" { 54 tlsConf, err := tlsutil.NewClientTLS(CertFile, KeyFile, CAFile) 55 if err != nil { 56 return nil, fmt.Errorf("can't create TLS config for client: %s", err.Error()) 57 } 58 tlsConf.BuildNameToCertificate() 59 60 sni, err := urlutil.ExtractHostname(URL) 61 if err != nil { 62 return nil, err 63 } 64 tlsConf.ServerName = sni 65 66 client.client = &http.Client{ 67 Transport: &http.Transport{ 68 TLSClientConfig: tlsConf, 69 }, 70 } 71 } else { 72 client.client = http.DefaultClient 73 } 74 return &client, nil 75 }