Debian Codegraph @

http/ifmodifiedsince.go

raw
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found below.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // Note: The source code below was picked from go/src/net/http/fs.go
32
33 package http
34
35 import (
36 "net/http"
37 "time"
38 )
39
40 // condResult is the result of an HTTP request precondition check.
41 // See https://tools.ietf.org/html/rfc7232 section 3.
42 type condResult int
43
44 const (
45 condNone condResult = iota
46 condTrue
47 condFalse
48 )
49
50 func checkIfModifiedSince(r *http.Request, modtime time.Time) condResult {
51 if r.Method != "GET" && r.Method != "HEAD" {
52 return condNone
53 }
54 ims := r.Header.Get("If-Modified-Since")
55 if ims == "" || isZeroTime(modtime) {
56 return condNone
57 }
58 t, err := http.ParseTime(ims)
59 if err != nil {
60 return condNone
61 }
62 // The Last-Modified header truncates sub-second precision so
63 // the modtime needs to be truncated too.
64 modtime = modtime.Truncate(time.Second)
65 if modtime.Before(t) || modtime.Equal(t) {
66 return condFalse
67 }
68 return condTrue
69 }
70
71 var unixEpochTime = time.Unix(0, 0)
72
73 // isZeroTime reports whether t is obviously unspecified (either zero or Unix()=0).
74 func isZeroTime(t time.Time) bool {
75 return t.IsZero() || t.Equal(unixEpochTime)
76 }
77
78 func setLastModified(w http.ResponseWriter, modtime time.Time) {
79 if !isZeroTime(modtime) {
80 w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat))
81 }
82 }
83
84 func writeNotModified(w http.ResponseWriter) {
85 // RFC 7232 section 4.1:
86 // a sender SHOULD NOT generate representation metadata other than the
87 // above listed fields unless said metadata exists for the purpose of
88 // guiding cache updates (e.g., Last-Modified might be useful if the
89 // response does not have an ETag field).
90 h := w.Header()
91 delete(h, "Content-Type")
92 delete(h, "Content-Length")
93 delete(h, "Content-Encoding")
94 if h.Get("Etag") != "" {
95 delete(h, "Last-Modified")
96 }
97 w.WriteHeader(http.StatusNotModified)
98 }