From 9e642d8820358349604990268bf26fcc2a1d36f2 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 29 Jun 2017 11:53:03 +0200 Subject: parse header op time duration on config load --- src/daq/s5proxy/src/s5proxy/config.go | 47 ++++++++++++++++++++++++++--------- src/daq/s5proxy/src/s5proxy/proxy.go | 18 +++----------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/daq/s5proxy/src/s5proxy/config.go b/src/daq/s5proxy/src/s5proxy/config.go index 10e1e7e..9bd5b3e 100644 --- a/src/daq/s5proxy/src/s5proxy/config.go +++ b/src/daq/s5proxy/src/s5proxy/config.go @@ -154,14 +154,25 @@ func (o *Operation) UnmarshalText(data []byte) (err error) { } type HeaderOperation struct { - Operation Operation `json:"op"` - Header string `json:"header"` - Value string `json:"value,omitempty"` + Operation Operation `json:"op"` + Header string `json:"header"` + Value string `json:"value,omitempty"` + ValueDuration time.Duration `json:"-"` } -type Duration int64 +func (h *HeaderOperation) Parse() (err error) { + switch h.Operation { + case OpTime: + if h.ValueDuration, err = time.ParseDuration(h.Value); err != nil { + return err + } + } + return nil +} + +type SFiveDuration int64 -func (d *Duration) UnmarshalText(data []byte) error { +func (d *SFiveDuration) UnmarshalText(data []byte) error { v, err := time.ParseDuration(string(data)) if err != nil { return err @@ -169,17 +180,17 @@ func (d *Duration) UnmarshalText(data []byte) error { if v < 1*time.Second { return errors.New("duration must be >= 1s") } - *d = Duration(v / time.Millisecond) + *d = SFiveDuration(v / time.Millisecond) return nil } type SFiveConf struct { - Sock string `json:"socket"` - Hostname string `json:"hostname"` - Tags []string `json:"tags"` - Duration Duration `json:"duration"` - Format string `json:"format"` - Stream []string `json:"stream"` + Sock string `json:"socket"` + Hostname string `json:"hostname"` + Tags []string `json:"tags"` + Duration SFiveDuration `json:"duration"` + Format string `json:"format"` + Stream []string `json:"stream"` } type Config struct { @@ -208,6 +219,18 @@ func readConfig(configfile string) (conf *Config, err error) { if err = json.NewDecoder(f).Decode(conf); err != nil { return } + + for idx, _ := range conf.RequestHeader { + if err = conf.RequestHeader[idx].Parse(); err != nil { + return + } + } + for idx, _ := range conf.ResponseHeader { + if err = conf.ResponseHeader[idx].Parse(); err != nil { + return + } + } + if conf.Protocol == HTTPAndHTTPS || conf.Protocol == HTTPSOnly { if conf.CertFile == "" || conf.KeyFile == "" { return nil, errors.New("HTTPs is enabled but no certificate and/or key file is supplied") diff --git a/src/daq/s5proxy/src/s5proxy/proxy.go b/src/daq/s5proxy/src/s5proxy/proxy.go index b36a66a..dec8271 100644 --- a/src/daq/s5proxy/src/s5proxy/proxy.go +++ b/src/daq/s5proxy/src/s5proxy/proxy.go @@ -45,13 +45,7 @@ import ( "github.com/soheilhy/cmux" ) -func generateTime(input string) string { - d, err := time.ParseDuration(input) - if err != nil { - s5l.Printf("PROXY: can't parse duration: %v", err) - return "" - } - +func generateTime(d time.Duration) string { return time.Now().UTC().Add(d).Format(time.RFC1123) } @@ -90,9 +84,7 @@ func (r s5proxyResponseWriter) WriteHeader(status int) { case OpSet: r.wrapped.Header().Set(h.Header, h.Value) case OpTime: - if ts := generateTime(h.Value); ts != "" { - r.wrapped.Header().Set(h.Header, ts) - } + r.wrapped.Header().Set(h.Header, generateTime(h.ValueDuration)) } } r.wrapped.WriteHeader(status) @@ -149,9 +141,7 @@ func NewProxy(conf *Config, stats *Stats) (p *Proxy, err error) { case OpSet: req.Header.Set(h.Header, h.Value) case OpTime: - if ts := generateTime(h.Value); ts != "" { - req.Header.Set(h.Header, ts) - } + req.Header.Set(h.Header, generateTime(h.ValueDuration)) } } } @@ -220,7 +210,7 @@ func (p *Proxy) Run() error { return err } - s5l.Printf("PROXY: listening on '%s' using protocol: %s", p.conf.ListenAddr, p.conf.Protocol) + s5l.Printf("PROXY: listening on '%s' using protocol: %s", l.Addr(), p.conf.Protocol) switch p.conf.Protocol { case HTTPAndHTTPS: m := cmux.New(l) -- cgit v1.2.3