ocket8888 commented on a change in pull request #4942:
URL: https://github.com/apache/trafficcontrol/pull/4942#discussion_r485977886



##########
File path: lib/go-rfc/cachecontrol.go
##########
@@ -99,18 +75,40 @@ func ParseETag(e string) (time.Time, error) {
        return t, nil
 }
 
-// ParseEtagsList parses a list of etags and returns the time equivalent 
string for each of the etags.
-func ParseEtagsList(eTags []string) []string {
-       tagTimes := make([]string, 0, len(eTags))
+// GetUnmodifiedTime gets the latest time out of the Etags (if present), or 
the If-Unmodified-Since (if present)
+func GetUnmodifiedTime(h http.Header) (time.Time, bool) {
+       if h == nil {
+               return time.Time{}, false
+       }
+       if im := h.Get(IfMatch); im != "" {
+               if et, ok := ParseETags(strings.Split(im, ",")); ok {
+                       return et, true
+               }
+       }
+       if ius := h.Get(IfUnmodifiedSince); ius != "" {
+               if tm, ok := ParseHTTPDate(ius); ok {
+                       return tm, true
+               }
+       }
+       return time.Time{}, false
+}
+
+// ParseETags the latest time of any valid ETag, and whether a valid ETag was 
found.
+func ParseETags(eTags []string) (time.Time, bool) {
+       latestTime := time.Time{}
        for _, tag := range eTags {
                tag = strings.TrimSpace(tag)
-               t, err := ParseETag(`"` + tag + `"`)
+               et, err := ParseETag(tag)
                // errors are recoverable, keep going through the list of etags
                if err != nil {
                        continue
                }
-               tagTime := t.Format("2006-01-02 15:04:05.000000-07")
-               tagTimes = append(tagTimes, `"`+tagTime+`"`)
+               if et.After(latestTime) {
+                       latestTime = et
+               }
        }
-       return tagTimes
-}
+       if latestTime == (time.Time{}) {
+               return time.Time{}, false
+       }
+       return latestTime, true
+}

Review comment:
       Same as above RE: terminating newline

##########
File path: lib/go-rfc/cachecontrol_test.go
##########
@@ -41,4 +41,4 @@ func TestETag(t *testing.T) {
        if ans.UTC().String() != "2020-08-06 18:11:22.278418 +0000 UTC" {
                t.Errorf("Expected time %v, actual %v", "2020-08-06 
18:11:22.278418 +0000 UTC", ans.UTC().String())
        }
-}
+}

Review comment:
       removing this terminating newline makes this no longer a text file per 
POSIX

##########
File path: traffic_ops/traffic_ops_golang/api/api.go
##########
@@ -994,3 +994,57 @@ func CreateDeprecationAlerts(alternative *string) 
tc.Alerts {
                return tc.CreateAlerts(tc.WarnLevel, "This endpoint is 
deprecated, and will be removed in the future")
        }
 }
+
+// GetLastUpdated checks for the resource in the database, and returns its 
last_updated timestamp, if available.
+func GetLastUpdated(tx *sqlx.Tx, ID int, tableName string) (*time.Time, bool, 
error) {
+       lastUpdated := time.Time{}
+       found := false
+       rows, err := tx.Query(`select last_updated from `+tableName+` where 
id=$1`, ID)
+       if err != nil {
+               return nil, found, errors.New("querying last_updated: " + 
err.Error())
+       }
+       defer rows.Close()
+       if !rows.Next() {
+               return nil, found, nil
+       }
+       found = true
+       if err := rows.Scan(&lastUpdated); err != nil {
+               return nil, found, errors.New("scanning last_updated: " + 
err.Error())
+       }
+       return &lastUpdated, found, nil
+}
+
+// IsUnmodified returns a boolean, saying whether or not the resource in 
question was modified since the time specified in the headers.
+//func IsUnmodified(h http.Header, existingLastUpdated *time.Time) bool {
+//     _, iumsTime := rfc.GetETagOrIfUnmodifiedSinceTime(h)
+//     existingEtag := rfc.ETag(*existingLastUpdated)
+//
+//     if h != nil {
+//             if h.Get(rfc.IfMatch) != "" && 
!strings.Contains(h.Get(rfc.IfMatch), existingEtag) {
+//                     return false
+//             }
+//             if iumsTime != nil && 
existingLastUpdated.UTC().After(iumsTime.UTC()) {
+//                     return false
+//             }
+//     }
+//     return true
+//}

Review comment:
       you wanna remove this now-unused code?

##########
File path: traffic_ops/traffic_ops_golang/api/shared_handlers.go
##########
@@ -25,15 +25,13 @@ import (
        "encoding/json"
        "errors"
        "fmt"
-       "github.com/apache/trafficcontrol/lib/go-rfc"
+       "github.com/apache/trafficcontrol/lib/go-log"
+       "github.com/apache/trafficcontrol/lib/go-tc"

Review comment:
       Shouldn't move these up into the standard imports, they should stay 
beneath in a separate set of lines.




----------------------------------------------------------------
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:
[email protected]


Reply via email to