be consistent with what Nginx supports

This commit is contained in:
Elvin Efendi 2018-12-02 22:10:36 +04:00
parent ccd7b890fd
commit 4eabd535f9
3 changed files with 32 additions and 20 deletions

View file

@ -727,11 +727,15 @@ func buildNextUpstream(i, r interface{}) string {
return strings.Join(nextUpstreamCodes, " ") return strings.Join(nextUpstreamCodes, " ")
} }
var sizeUnitRegex = regexp.MustCompile("^[0-9]+[kKmMgG]{0,1}$") // refer to http://nginx.org/en/docs/syntax.html
// Nginx differentiates between size and offset
// offset directives support gigabytes in addition
var nginxSizeRegex = regexp.MustCompile("^[0-9]+[kKmM]{0,1}$")
var nginxOffsetRegex = regexp.MustCompile("^[0-9]+[kKmMgG]{0,1}$")
// isValidByteSize validates size units valid in nginx // isValidByteSize validates size units valid in nginx
// http://nginx.org/en/docs/syntax.html // http://nginx.org/en/docs/syntax.html
func isValidByteSize(input interface{}) bool { func isValidByteSize(input interface{}, isOffset bool) bool {
s, ok := input.(string) s, ok := input.(string)
if !ok { if !ok {
glog.Errorf("expected an 'string' type but %T was returned", input) glog.Errorf("expected an 'string' type but %T was returned", input)
@ -744,7 +748,11 @@ func isValidByteSize(input interface{}) bool {
return false return false
} }
return sizeUnitRegex.MatchString(s) if isOffset {
return nginxOffsetRegex.MatchString(s)
}
return nginxSizeRegex.MatchString(s)
} }
type ingressInformation struct { type ingressInformation struct {

View file

@ -559,24 +559,28 @@ func TestBuildDenyVariable(t *testing.T) {
func TestBuildByteSize(t *testing.T) { func TestBuildByteSize(t *testing.T) {
cases := []struct { cases := []struct {
input interface{} value interface{}
isOffset bool
expected bool expected bool
}{ }{
{"1000", true}, {"1000", false, true},
{"1000k", true}, {"1000k", false, true},
{"1m", true}, {"1m", false, true},
{"10g", true}, {"10g", false, false},
{" 1m ", true}, {" 1m ", false, true},
{"1000kk", false}, {"1000kk", false, false},
{"1000km", false}, {"1000km", false, false},
{"1mm", false}, {"1mm", false, false},
{nil, false}, {nil, false, false},
{"", false}, {"", false, false},
{" ", false}, {" ", false, false},
{"1G", true, true},
{"1000kk", true, false},
{"", true, false},
} }
for _, tc := range cases { for _, tc := range cases {
val := isValidByteSize(tc.input) val := isValidByteSize(tc.value, tc.isOffset)
if tc.expected != val { if tc.expected != val {
t.Errorf("Expected '%v' but returned '%v'", tc.expected, val) t.Errorf("Expected '%v' but returned '%v'", tc.expected, val)
} }

View file

@ -980,10 +980,10 @@ stream {
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_ssl_server_name on; proxy_ssl_server_name on;
proxy_pass_request_headers on; proxy_pass_request_headers on;
{{ if isValidByteSize $location.Proxy.BodySize }} {{ if isValidByteSize $location.Proxy.BodySize true }}
client_max_body_size {{ $location.Proxy.BodySize }}; client_max_body_size {{ $location.Proxy.BodySize }};
{{ end }} {{ end }}
{{ if isValidByteSize $location.ClientBodyBufferSize }} {{ if isValidByteSize $location.ClientBodyBufferSize false }}
client_body_buffer_size {{ $location.ClientBodyBufferSize }}; client_body_buffer_size {{ $location.ClientBodyBufferSize }};
{{ end }} {{ end }}
@ -1197,10 +1197,10 @@ stream {
} }
{{ end }} {{ end }}
{{ if isValidByteSize $location.Proxy.BodySize }} {{ if isValidByteSize $location.Proxy.BodySize true }}
client_max_body_size {{ $location.Proxy.BodySize }}; client_max_body_size {{ $location.Proxy.BodySize }};
{{ end }} {{ end }}
{{ if isValidByteSize $location.ClientBodyBufferSize }} {{ if isValidByteSize $location.ClientBodyBufferSize false }}
client_body_buffer_size {{ $location.ClientBodyBufferSize }}; client_body_buffer_size {{ $location.ClientBodyBufferSize }};
{{ end }} {{ end }}