github.com/yandex/pandora@v0.5.32/components/guns/http/http.go (about) 1 package phttp 2 3 import ( 4 "github.com/pkg/errors" 5 "go.uber.org/zap" 6 ) 7 8 type GunConfig struct { 9 Client ClientConfig `config:",squash"` 10 Target string `validate:"endpoint,required"` 11 TargetResolved string `config:"-"` 12 SSL bool 13 14 AutoTag AutoTagConfig `config:"auto-tag"` 15 AnswLog AnswLogConfig `config:"answlog"` 16 HTTPTrace HTTPTraceConfig `config:"httptrace"` 17 SharedClient struct { 18 ClientNumber int `config:"client-number,omitempty"` 19 Enabled bool `config:"enabled"` 20 } `config:"shared-client,omitempty"` 21 } 22 23 func NewHTTP1Gun(cfg GunConfig, answLog *zap.Logger) *BaseGun { 24 return NewBaseGun(HTTP1ClientConstructor, cfg, answLog) 25 } 26 27 func HTTP1ClientConstructor(clientConfig ClientConfig, target string) Client { 28 transport := NewTransport(clientConfig.Transport, NewDialer(clientConfig.Dialer).DialContext, target) 29 client := NewRedirectingClient(transport, clientConfig.Redirect) 30 return client 31 } 32 33 var _ ClientConstructor = HTTP1ClientConstructor 34 35 // NewHTTP2Gun return simple HTTP/2 gun that can shoot sequentially through one connection. 36 func NewHTTP2Gun(cfg GunConfig, answLog *zap.Logger) (*BaseGun, error) { 37 if !cfg.SSL { 38 // Open issue on github if you really need this feature. 39 return nil, errors.New("HTTP/2.0 over TCP is not supported. Please leave SSL option true by default.") 40 } 41 return NewBaseGun(HTTP2ClientConstructor, cfg, answLog), nil 42 } 43 44 func HTTP2ClientConstructor(clientConfig ClientConfig, target string) Client { 45 transport := NewHTTP2Transport(clientConfig.Transport, NewDialer(clientConfig.Dialer).DialContext, target) 46 client := NewRedirectingClient(transport, clientConfig.Redirect) 47 // Will panic and cancel shooting whet target doesn't support HTTP/2. 48 return &panicOnHTTP1Client{Client: client} 49 } 50 51 var _ ClientConstructor = HTTP2ClientConstructor 52 53 func DefaultHTTPGunConfig() GunConfig { 54 return GunConfig{ 55 SSL: false, 56 Client: DefaultClientConfig(), 57 AutoTag: AutoTagConfig{ 58 Enabled: false, 59 URIElements: 2, 60 NoTagOnly: true, 61 }, 62 AnswLog: AnswLogConfig{ 63 Enabled: false, 64 Path: "answ.log", 65 Filter: "error", 66 }, 67 HTTPTrace: HTTPTraceConfig{ 68 DumpEnabled: false, 69 TraceEnabled: false, 70 }, 71 } 72 } 73 74 func DefaultHTTP2GunConfig() GunConfig { 75 return GunConfig{ 76 Client: DefaultClientConfig(), 77 AutoTag: AutoTagConfig{ 78 Enabled: false, 79 URIElements: 2, 80 NoTagOnly: true, 81 }, 82 AnswLog: AnswLogConfig{ 83 Enabled: false, 84 Path: "answ.log", 85 Filter: "error", 86 }, 87 HTTPTrace: HTTPTraceConfig{ 88 DumpEnabled: false, 89 TraceEnabled: false, 90 }, 91 SSL: true, 92 } 93 }