CheckIfModifiedSince checks if the response is modified since the "modtime". Note that it has nothing to do with server-side caching. It does those checks by checking if the "If-Modified-Since" request header sent by client or a previous server response header (e.g with WriteWithExpiration or Handle
(modtime time.Time)
| 3468 | // |
| 3469 | // It's mostly used internally, e.g. `context#WriteWithExpiration`. |
| 3470 | func (ctx *Context) CheckIfModifiedSince(modtime time.Time) (bool, error) { |
| 3471 | if method := ctx.Method(); method != http.MethodGet && method != http.MethodHead { |
| 3472 | return false, fmt.Errorf("method: %w", ErrPreconditionFailed) |
| 3473 | } |
| 3474 | ims := ctx.GetHeader(IfModifiedSinceHeaderKey) |
| 3475 | if ims == "" || IsZeroTime(modtime) { |
| 3476 | return false, fmt.Errorf("zero time: %w", ErrPreconditionFailed) |
| 3477 | } |
| 3478 | t, err := ParseTime(ctx, ims) |
| 3479 | if err != nil { |
| 3480 | return false, err |
| 3481 | } |
| 3482 | // sub-second precision, so |
| 3483 | // use mtime < t+1s instead of mtime <= t to check for unmodified. |
| 3484 | if modtime.UTC().Before(t.Add(1 * time.Second)) { |
| 3485 | return false, nil |
| 3486 | } |
| 3487 | return true, nil |
| 3488 | } |
| 3489 | |
| 3490 | // WriteNotModified sends a 304 "Not Modified" status code to the client, |
| 3491 | // it makes sure that the content type, the content length headers |