alficles commented on a change in pull request #3899: Add TO Go server cache 
configs
URL: https://github.com/apache/trafficcontrol/pull/3899#discussion_r321566988
 
 

 ##########
 File path: lib/go-atscfg/servercachedotconfig.go
 ##########
 @@ -0,0 +1,91 @@
+package atscfg
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import (
+       "strconv"
+       "strings"
+
+       "github.com/apache/trafficcontrol/lib/go-tc"
+)
+
+type ServerCacheConfigDS struct {
+       OrgServerFQDN string
+       Type          tc.DSType
+}
+
+func MakeServerCacheDotConfig(
+       serverName tc.CacheName,
+       toToolName string, // tm.toolname global parameter (TODO: cache itself?)
+       toURL string, // tm.url global parameter (TODO: cache itself?)
+       dses map[tc.DeliveryServiceName]ServerCacheConfigDS,
+) string {
+       text := GenericHeaderComment(string(serverName), toToolName, toURL)
+
+       seenOrigins := map[string]struct{}{}
+       for _, ds := range dses {
+               if ds.Type != tc.DSTypeHTTPNoCache {
+                       continue
+               }
+               if _, ok := seenOrigins[ds.OrgServerFQDN]; ok {
+                       continue
+               }
+               seenOrigins[ds.OrgServerFQDN] = struct{}{}
+
+               originFQDN, originPort := GetOriginFQDNAndPort(ds.OrgServerFQDN)
+               if originPort != nil {
+                       text += `dest_domain=` + originFQDN + ` port=` + 
strconv.Itoa(*originPort) + ` scheme=http action=never-cache` + "\n"
+               } else {
+                       text += `dest_domain=` + originFQDN + ` scheme=http 
action=never-cache` + "\n"
+               }
+       }
+       return text
+}
+
+// TODO unit test
+func GetOriginFQDNAndPort(origin string) (string, *int) {
+       origin = strings.TrimSpace(origin)
+       origin = strings.Replace(origin, `https://`, ``, -1)
+       origin = strings.Replace(origin, `http://`, ``, -1)
+
+       // if the origin includes a path, strip it
+
+       slashI := strings.Index(origin, `/`)
+       if slashI != -1 {
+               origin = origin[:slashI]
+       }
+
+       hostName := origin
+
+       colonI := strings.Index(origin, ":")
+       if colonI == -1 {
+               return hostName, nil // no :, the origin must not include a port
+       }
+       portStr := origin[colonI+1:]
+       port, err := strconv.Atoi(portStr)
+       if err != nil {
+               // either the port isn't an integer, or the : we found was 
something else.
+               // Return the origin, as if it didn't contain a port.
+               return hostName, nil
+       }
+
+       hostName = origin[:colonI]
+       return hostName, &port
 
 Review comment:
   I can't help but wonder if the go standard library would do a better job of 
parsing the URI here. This will probably be fine, and it's probably just like 
what the perl did. But it seems that we're re-inventing a wheel that can be 
surprisingly hard to get right at times.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to