Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
feature: implement the http util IsExpired
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <[email protected]>
  • Loading branch information
starnop committed May 13, 2019
1 parent 0149ba4 commit 3668427
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion common/util/http_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net"
"net/http"
"reflect"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -190,7 +191,12 @@ func Do(url string, headers map[string]string, timeout time.Duration) (string, e

// HTTPGetWithHeaders send an HTTP GET request with headers.
func HTTPGetWithHeaders(url string, headers map[string]string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
return HTTPWithHeaders("GET", url, headers)
}

// HTTPWithHeaders send an HTTP request with headers and specified method.
func HTTPWithHeaders(method, url string, headers map[string]string) (*http.Response, error) {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -253,3 +259,30 @@ func CheckConnect(ip string, port int, timeout int) (localIP string, e error) {
}
return
}

// IsExpired checks if a resource received or stored is the same.
func IsExpired(url string, headers map[string]string, lastModified int64, eTag string) (bool, error) {
if lastModified <= 0 && IsEmptyStr(eTag) {
return true, nil
}

// set headers
if headers == nil {
headers = make(map[string]string)
}
if lastModified > 0 {
headers["If-Modified-Since"] = strconv.FormatInt(lastModified, 10)
}
if !IsEmptyStr(eTag) {
headers["If-None-Match"] = eTag
}

// send request
resp, err := HTTPWithHeaders("HEAD", url, headers)
if err != nil {
return false, err
}
resp.Body.Close()

return resp.StatusCode != http.StatusNotModified, nil
}

0 comments on commit 3668427

Please sign in to comment.