Skip to content

Commit 6194070

Browse files
committed
(catalogd) Don't write to header after checking for Preconditions
`checkPreconditions` already writes http.StatusNotModified to the writer. It also writes http.StatusPreconditionFailed in certain cases. Writing again could overwrite the status written by `checkPreconditions`. This PR fixes the issue. Refers [the source](https://cs.opensource.google/go/go/+/master:src/net/http/fs.go;l=271-274) for correct usage. Signed-off-by: Anik Bhattacharjee <[email protected]>
1 parent 38b4795 commit 6194070

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

catalogd/internal/storage/http_precoditions_check.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func checkIfModifiedSince(r *http.Request, w http.ResponseWriter, modtime time.T
5757
if ims == "" || isZeroTime(modtime) {
5858
return condTrue
5959
}
60-
t, err := ParseTime(ims)
60+
t, err := parseTime(ims)
6161
if err != nil {
6262
httpError(w, err)
6363
return condNone
@@ -76,7 +76,7 @@ func checkIfUnmodifiedSince(r *http.Request, modtime time.Time) condResult {
7676
if ius == "" || isZeroTime(modtime) {
7777
return condNone
7878
}
79-
t, err := ParseTime(ius)
79+
t, err := parseTime(ius)
8080
if err != nil {
8181
return condNone
8282
}
@@ -90,18 +90,18 @@ func checkIfUnmodifiedSince(r *http.Request, modtime time.Time) condResult {
9090
return condFalse
9191
}
9292

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

101101
var (
102102
unixEpochTime = time.Unix(0, 0)
103103
timeFormats = []string{
104-
TimeFormat,
104+
timeFormat,
105105
time.RFC850,
106106
time.ANSIC,
107107
}
@@ -155,11 +155,11 @@ func checkIfNoneMatch(r *http.Request) condResult {
155155
return condTrue
156156
}
157157

158-
// ParseTime parses a time header (such as the Date: header),
158+
// parseTime parses a time header (such as the Date: header),
159159
// trying each of the three formats allowed by HTTP/1.1:
160160
// [TimeFormat], [time.RFC850], and [time.ANSIC].
161161
// nolint:nonamedreturns
162-
func ParseTime(text string) (t time.Time, err error) {
162+
func parseTime(text string) (t time.Time, err error) {
163163
for _, layout := range timeFormats {
164164
t, err = time.Parse(layout, text)
165165
if err == nil {

catalogd/internal/storage/localdir.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ func (s *LocalDirV1) handleV1Query(w http.ResponseWriter, r *http.Request) {
231231
}
232232
defer catalogFile.Close()
233233

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

0 commit comments

Comments
 (0)