Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 907e3ccec9 |
@@ -116,12 +116,6 @@ func main() {
|
||||
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 {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -135,33 +129,3 @@ func defaultPublicDir() string {
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user