github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+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 "strings" 24 25 "k8s.io/helm/pkg/tlsutil" 26 "k8s.io/helm/pkg/urlutil" 27 "k8s.io/helm/pkg/version" 28 ) 29 30 //httpGetter is the efault HTTP(/S) backend handler 31 type httpGetter struct { 32 client *http.Client 33 } 34 35 //Get performs a Get from repo.Getter and returns the body. 36 func (g *httpGetter) Get(href string) (*bytes.Buffer, error) { 37 buf := bytes.NewBuffer(nil) 38 39 // Set a helm specific user agent so that a repo server and metrics can 40 // separate helm calls from other tools interacting with repos. 41 req, err := http.NewRequest("GET", href, nil) 42 if err != nil { 43 return buf, err 44 } 45 req.Header.Set("User-Agent", "Helm/"+strings.TrimPrefix(version.GetVersion(), "v")) 46 47 resp, err := g.client.Do(req) 48 if err != nil { 49 return buf, err 50 } 51 if resp.StatusCode != 200 { 52 return buf, fmt.Errorf("Failed to fetch %s : %s", href, resp.Status) 53 } 54 55 _, err = io.Copy(buf, resp.Body) 56 resp.Body.Close() 57 return buf, err 58 } 59 60 // newHTTPGetter constructs a valid http/https client as Getter 61 func newHTTPGetter(URL, CertFile, KeyFile, CAFile string) (Getter, error) { 62 var client httpGetter 63 if CertFile != "" && KeyFile != "" { 64 tlsConf, err := tlsutil.NewClientTLS(CertFile, KeyFile, CAFile) 65 if err != nil { 66 return nil, fmt.Errorf("can't create TLS config for client: %s", err.Error()) 67 } 68 tlsConf.BuildNameToCertificate() 69 70 sni, err := urlutil.ExtractHostname(URL) 71 if err != nil { 72 return nil, err 73 } 74 tlsConf.ServerName = sni 75 76 client.client = &http.Client{ 77 Transport: &http.Transport{ 78 TLSClientConfig: tlsConf, 79 }, 80 } 81 } else { 82 client.client = http.DefaultClient 83 } 84 return &client, nil 85 }