Compare commits

..

1 Commits

Author SHA1 Message Date
tomas 907e3ccec9 Protect changing in parallel using HTTP 412 precondition failed 2025-12-12 04:42:34 +02:00
-36
View File
@@ -116,12 +116,6 @@ func main() {
Priority: 999, // execute as latest as possible to allow users to provide their own route Priority: 999, // execute as latest as possible to allow users to provide their own route
}) })
app.OnRecordViewRequest().BindFunc(addLastModified)
app.OnRecordUpdateRequest().BindFunc(ifUnmodifiedSince)
app.OnRecordDeleteRequest().BindFunc(ifUnmodifiedSince)
if err := app.Start(); err != nil { if err := app.Start(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
@@ -135,33 +129,3 @@ func defaultPublicDir() string {
return filepath.Join(os.Args[0], "../pb_public") return filepath.Join(os.Args[0], "../pb_public")
} }
func addLastModified(e *core.RecordRequestEvent) error {
updated := e.Record.GetString("updated")
if updated != "" {
e.Response.Header().Add("Last-Modified", updated)
}
return e.Next()
}
func ifUnmodifiedSince(e *core.RecordRequestEvent) error {
updated := e.Record.GetString("updated")
if updated != "" {
header := e.Request.Header.Get("If-Unmodified-Since")
if header == "" || header != updated {
e.Response.Header().Add("Last-Modified", updated)
if header == "" {
return e.Error(http.StatusPreconditionRequired, "Header If-Unmodified-Since is required", nil)
} else if header != updated {
return e.Error(http.StatusPreconditionFailed, "Record was modified after retrieval", nil)
}
}
}
return e.Next()
}