]> 127.0.0.1 Git - part/.git/commitdiff
web.NotModified
authorqydysky <qydysky@foxmail.com>
Sun, 21 Jul 2024 12:05:12 +0000 (20:05 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 21 Jul 2024 12:05:12 +0000 (20:05 +0800)
web/Web.go
web/Web_test.go

index 1f5f8e2e91a1be2d6a0f5b2f0fd9fa2ebc36b901..2dd1fc8865ec2f16f03536019697978297ca83a8 100644 (file)
@@ -692,6 +692,26 @@ func IsMethod(r *http.Request, method ...string) bool {
        return false
 }
 
+func NotModified(r *http.Request, w http.ResponseWriter, cuTime time.Time) (notMod bool) {
+       modTimeS := cuTime.Format(time.RFC1123)
+
+       w.Header().Add(`Cache-Control`, `private`)
+       w.Header().Add(`ETag`, modTimeS)
+       w.Header().Add(`Last-Modified`, modTimeS)
+
+       if inm := r.Header.Get(`If-None-Match`); inm == modTimeS {
+               w.WriteHeader(http.StatusNotModified)
+               return true
+       }
+       if ims := r.Header.Get(`If-Modified-Since`); ims != "" {
+               if mod, e := time.Parse(time.RFC1123, ims); e == nil && mod.Equal(cuTime) {
+                       w.WriteHeader(http.StatusNotModified)
+                       return true
+               }
+       }
+       return false
+}
+
 func Easy_boot() *Web {
        s := New(&http.Server{
                Addr:         "127.0.0.1:" + strconv.Itoa(sys.Sys().GetFreePort()),
index 3bc1b2a5b666c466e3b861606ff91d22a58ef1df..a4eb4b346ba8003cf801f3a251fcf08cb579665f 100644 (file)
@@ -40,6 +40,52 @@ func Test_Exprier(t *testing.T) {
        }
 }
 
+func Test_Mod(t *testing.T) {
+       s := New(&http.Server{
+               Addr:         "0.0.0.0:13000",
+               WriteTimeout: time.Second * time.Duration(10),
+       })
+       defer s.Shutdown()
+       s.Handle(map[string]func(http.ResponseWriter, *http.Request){
+               `/mod`: func(w http.ResponseWriter, r *http.Request) {
+                       cu, _ := time.Parse(time.RFC1123, `Tue, 22 Feb 2022 22:00:00 GMT`)
+                       if NotModified(r, w, cu) {
+                               return
+                       }
+                       w.Write([]byte("abc强强强强"))
+               },
+               `/`: func(w http.ResponseWriter, r *http.Request) {
+                       cu, _ := time.Parse(time.RFC1123, `Tue, 22 Feb 2022 22:00:00 GMT`)
+                       if NotModified(r, w, cu) {
+                               return
+                       }
+                       w.Write([]byte(""))
+               },
+       })
+
+       time.Sleep(time.Second)
+
+       r := reqf.New()
+       {
+               r.Reqf(reqf.Rval{
+                       Url: "http://127.0.0.1:13000/mod",
+               })
+               if !bytes.Equal(r.Respon, []byte("abc强强强强")) {
+                       t.Fatal(r.Respon)
+               }
+               r.Reqf(reqf.Rval{
+                       Url: "http://127.0.0.1:13000/mod",
+                       Header: map[string]string{
+                               `If-None-Match`: r.Response.Header.Get(`ETag`),
+                       },
+               })
+               if r.Response.StatusCode != http.StatusNotModified {
+                       t.Fatal(r.Respon)
+               }
+       }
+       time.Sleep(time.Second*10)
+}
+
 func Test_Server(t *testing.T) {
        s := New(&http.Server{
                Addr:         "127.0.0.1:13000",