Changed custom headers naming to forward headers

This commit is contained in:
Ramiro Antonio 2019-04-04 10:36:42 -03:00
parent 559a119aa0
commit ea5c6efd92
5 changed files with 21 additions and 21 deletions

View File

@ -38,7 +38,7 @@ var (
aCertFile = flag.String("certfile", "", "TLS certificate file path")
aKeyFile = flag.String("keyfile", "", "TLS private key file path")
aAuthorization = flag.String("authorization", "", "Defines a constant Authorization header value passed to all the image source servers. -enable-url-source flag must be defined. This overwrites authorization headers forwarding behavior via X-Forward-Authorization")
aCustomHeaders = flag.String("forward-headers", "", "Forwards custom headers to the image source server. -enable-url-source flag must be defined.")
aForwardHeaders = flag.String("forward-headers", "", "Forwards custom headers to the image source server. -enable-url-source flag must be defined.")
aPlaceholder = flag.String("placeholder", "", "Image path to image custom placeholder to be used in case of error. Recommended minimum image size is: 1200x1200")
aDisableEndpoints = flag.String("disable-endpoints", "", "Comma separated endpoints to disable. E.g: form,crop,rotate,health")
aHTTPCacheTTL = flag.Int("http-cache-ttl", -1, "The TTL in seconds")
@ -146,7 +146,7 @@ func main() {
HTTPReadTimeout: *aReadTimeout,
HTTPWriteTimeout: *aWriteTimeout,
Authorization: *aAuthorization,
CustomHeaders: parseCustomHeaders(*aCustomHeaders),
ForwardHeaders: parseForwardHeaders(*aForwardHeaders),
AllowedOrigins: parseOrigins(*aAllowedOrigins),
MaxAllowedSize: *aMaxAllowedSize,
}
@ -268,14 +268,14 @@ func checkHTTPCacheTTL(ttl int) {
}
}
func parseCustomHeaders(customHeaders string) []string {
func parseForwardHeaders(forwardHeaders string) []string {
var headers []string
if customHeaders == "" {
if forwardHeaders == "" {
return headers
}
for _, header := range strings.Split(customHeaders, ",") {
for _, header := range strings.Split(forwardHeaders, ",") {
if header != "" {
headers = append(headers, header)
headers = append(headers, strings.TrimSpace(header))
}
}
return headers

View File

@ -33,7 +33,7 @@ type ServerOptions struct {
KeyFile string
Authorization string
Placeholder string
CustomHeaders []string
ForwardHeaders []string
PlaceholderImage []byte
Endpoints Endpoints
AllowedOrigins []*url.URL

View File

@ -13,7 +13,7 @@ type SourceConfig struct {
Authorization string
MountPath string
Type ImageSourceType
CustomHeaders []string
ForwardHeaders []string
AllowedOrigins []*url.URL
MaxAllowedSize int
}
@ -39,7 +39,7 @@ func LoadSources(o ServerOptions) {
Authorization: o.Authorization,
AllowedOrigins: o.AllowedOrigins,
MaxAllowedSize: o.MaxAllowedSize,
CustomHeaders: o.CustomHeaders,
ForwardHeaders: o.ForwardHeaders,
})
}
}

View File

@ -86,8 +86,8 @@ func (s *HTTPImageSource) setAuthorizationHeader(req *http.Request, ireq *http.R
}
}
func (s *HTTPImageSource) setCustomHeaders(req *http.Request, ireq *http.Request) {
headers := s.Config.CustomHeaders
func (s *HTTPImageSource) setForwardHeaders(req *http.Request, ireq *http.Request) {
headers := s.Config.ForwardHeaders
for _, header := range headers {
if header != "" && ireq.Header.Get(header) != "" && req.Header.Get(header) == "" {
req.Header.Set(header, ireq.Header.Get(header))
@ -104,8 +104,8 @@ func newHTTPRequest(s *HTTPImageSource, ireq *http.Request, method string, url *
req.Header.Set("User-Agent", "imaginary/"+Version)
req.URL = url
if len(s.Config.CustomHeaders) != 0 {
s.setCustomHeaders(req, ireq)
if len(s.Config.ForwardHeaders) != 0 {
s.setForwardHeaders(req, ireq)
}
// Forward auth header to the target server, if necessary

View File

@ -134,13 +134,13 @@ func TestHttpImageSourceForwardHeaders(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url=http://bar.com", nil)
r.Header.Set(header, "foobar")
source := &HTTPImageSource{&SourceConfig{CustomHeaders: cases}}
source := &HTTPImageSource{&SourceConfig{ForwardHeaders: cases}}
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
oreq := &http.Request{Header: make(http.Header)}
source.setCustomHeaders(oreq, r)
source.setForwardHeaders(oreq, r)
if oreq.Header.Get(header) != "foobar" {
t.Fatal("Missmatch custom header")
@ -159,7 +159,7 @@ func TestHttpImageSourceNotForwardHeaders(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url="+url.String(), nil)
r.Header.Set("Not-Forward", "foobar")
source := &HTTPImageSource{&SourceConfig{CustomHeaders: cases}}
source := &HTTPImageSource{&SourceConfig{ForwardHeaders: cases}}
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
@ -182,7 +182,7 @@ func TestHttpImageSourceForwardedHeadersNotOverride(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url="+url.String() , nil)
r.Header.Set("Authorization", "foobar")
source := &HTTPImageSource{&SourceConfig{Authorization: "ValidAPIKey", CustomHeaders: cases}}
source := &HTTPImageSource{&SourceConfig{Authorization: "ValidAPIKey", ForwardHeaders: cases}}
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
@ -205,7 +205,7 @@ func TestHttpImageSourceCaseSensitivityInForwardedHeaders(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url="+url.String(), nil)
r.Header.Set("x-custom", "foobar")
source := &HTTPImageSource{&SourceConfig{CustomHeaders: cases}}
source := &HTTPImageSource{&SourceConfig{ForwardHeaders: cases}}
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
@ -224,13 +224,13 @@ func TestHttpImageSourceEmptyForwardedHeaders(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?url="+url.String(), nil)
source := &HTTPImageSource{&SourceConfig{CustomHeaders: cases}}
source := &HTTPImageSource{&SourceConfig{ForwardHeaders: cases}}
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
if len(source.Config.CustomHeaders) != 0 {
t.Log(source.Config.CustomHeaders)
if len(source.Config.ForwardHeaders) != 0 {
t.Log(source.Config.ForwardHeaders)
t.Fatal("Setted empty custom header")
}