You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2019/03/05 22:51:43 UTC

[GitHub] [trafficcontrol] rob05c commented on a change in pull request #3360: Cache Config Parser

rob05c commented on a change in pull request #3360: Cache Config Parser
URL: https://github.com/apache/trafficcontrol/pull/3360#discussion_r262679452
 
 

 ##########
 File path: traffic_ops/testing/api/v14/config/cache_config/cache_config.go
 ##########
 @@ -0,0 +1,265 @@
+/*
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+package cache_config
+
+import (
+	"regexp"
+	"strings"
+
+	. "github.com/apache/trafficcontrol/traffic_ops/testing/api/v14/config"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/test"
+	"github.com/go-ozzo/ozzo-validation/is"
+)
+
+// ParseCacheConfig takes a string presumed to be an ATS cache.config and validates
+// it is syntatically correct.
+//
+// The general format of a cache config is three types of labels separated by spaces:
+//
+//  primary_destination=value secondary_specifier=value action=value
+//
+// For a full description of how to format a cache config, refer to the ATS documentation
+// for the cache config:
+// https://docs.trafficserver.apache.org/en/latest/admin-guide/files/cache.config.en.html
+//
+func ParseCacheConfig(config string) test.Error {
+	lines := strings.Split(config, "\n")
+
+	if len(lines) == 1 {
+		return parseConfigRule(lines[0])
+	}
+
+	for i, ln := range lines {
+		err := parseConfigRule(ln)
+		if err != nil {
+			return err.Prepend("error on line %d: ", i+1)
+		}
+	}
+
+	return nil
+}
+
+func parsePrimaryDestinations(lhs string, rhs string) test.Error {
+
+	switch lhs {
+	case "dest_domain":
+		// dest_host is an alias for dest_domain
+		fallthrough
+	case "dest_host":
+		if err := is.Host.Validate(rhs); err != nil {
+			return ErrorContext.NewError(InvalidHost, `"%s" %v`, rhs, err)
+		}
+	case "dest_ip":
+		if err := is.IP.Validate(rhs); err != nil {
+			return ErrorContext.NewError(InvalidIP, `"%s" %v`, rhs, err)
+		}
+	case "host_regex":
+		fallthrough
+	case "url_regex":
+		// only makes sure the regex compiles, not that the regex generates anything valid
+		if _, err := regexp.Compile(rhs); err != nil {
+			return ErrorContext.NewError(InvalidRegex, "%v", err)
+		}
+	default:
+		return ErrorContext.NewError(InvalidLabel)
+	}
+
+	return nil
+}
+
+func parseSecondarySpecifiers(lhs string, rhs string) test.Error {
+
+	switch lhs {
+	case "port":
+		if err := is.Port.Validate(rhs); err != nil {
+			return ErrorContext.AddErrorCode(InvalidPort, err)
+		}
+	case "scheme":
+		if rhs != "http" && rhs != "https" {
 
 Review comment:
   The scheme is case-insensitive. I think this should lowercase it before comparing, `rhs = strings.ToLower(rhs)`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services