Skip to content

🐛 (catalogd) Don't write to header after checking for Preconditions #1710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions catalogd/internal/storage/http_precoditions_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func checkIfModifiedSince(r *http.Request, w http.ResponseWriter, modtime time.T
if ims == "" || isZeroTime(modtime) {
return condTrue
}
t, err := ParseTime(ims)
t, err := parseTime(ims)
if err != nil {
httpError(w, err)
return condNone
Expand All @@ -76,7 +76,7 @@ func checkIfUnmodifiedSince(r *http.Request, modtime time.Time) condResult {
if ius == "" || isZeroTime(modtime) {
return condNone
}
t, err := ParseTime(ius)
t, err := parseTime(ius)
if err != nil {
return condNone
}
Expand All @@ -90,18 +90,18 @@ func checkIfUnmodifiedSince(r *http.Request, modtime time.Time) condResult {
return condFalse
}

// TimeFormat is the time format to use when generating times in HTTP
// timeFormat is the time format to use when generating times in HTTP
// headers. It is like [time.RFC1123] but hard-codes GMT as the time
// zone. The time being formatted must be in UTC for Format to
// generate the correct format.
//
// For parsing this time format, see [ParseTime].
const TimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
const timeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"

var (
unixEpochTime = time.Unix(0, 0)
timeFormats = []string{
TimeFormat,
timeFormat,
time.RFC850,
time.ANSIC,
}
Expand Down Expand Up @@ -155,11 +155,11 @@ func checkIfNoneMatch(r *http.Request) condResult {
return condTrue
}

// ParseTime parses a time header (such as the Date: header),
// parseTime parses a time header (such as the Date: header),
// trying each of the three formats allowed by HTTP/1.1:
// [TimeFormat], [time.RFC850], and [time.ANSIC].
// nolint:nonamedreturns
func ParseTime(text string) (t time.Time, err error) {
func parseTime(text string) (t time.Time, err error) {
for _, layout := range timeFormats {
t, err = time.Parse(layout, text)
if err == nil {
Expand Down
6 changes: 3 additions & 3 deletions catalogd/internal/storage/localdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ func (s *LocalDirV1) handleV1Query(w http.ResponseWriter, r *http.Request) {
}
defer catalogFile.Close()

w.Header().Set("Last-Modified", catalogStat.ModTime().UTC().Format(TimeFormat))
if checkPreconditions(w, r, catalogStat.ModTime()) {
w.WriteHeader(http.StatusNotModified)
w.Header().Set("Last-Modified", catalogStat.ModTime().UTC().Format(timeFormat))
done := checkPreconditions(w, r, catalogStat.ModTime())
if done {
return
}

Expand Down
Loading