github.com/rigado/snapd@v2.42.5-go-mod+incompatible/httputil/client.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package httputil 21 22 import ( 23 "crypto/tls" 24 "net/http" 25 "net/url" 26 "time" 27 ) 28 29 type ClientOptions struct { 30 Timeout time.Duration 31 TLSConfig *tls.Config 32 MayLogBody bool 33 Proxy func(*http.Request) (*url.URL, error) 34 } 35 36 // NewHTTPCLient returns a new http.Client with a LoggedTransport, a 37 // Timeout and preservation of range requests across redirects 38 func NewHTTPClient(opts *ClientOptions) *http.Client { 39 if opts == nil { 40 opts = &ClientOptions{} 41 } 42 43 transport := newDefaultTransport() 44 transport.TLSClientConfig = opts.TLSConfig 45 if opts.Proxy != nil { 46 transport.Proxy = opts.Proxy 47 } 48 49 return &http.Client{ 50 Transport: &LoggedTransport{ 51 Transport: transport, 52 Key: "SNAPD_DEBUG_HTTP", 53 body: opts.MayLogBody, 54 }, 55 Timeout: opts.Timeout, 56 CheckRedirect: checkRedirect, 57 } 58 }