HEAD operation changes to fasthttp
This commit is contained in:
@@ -12,6 +12,8 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
|
||||
"github.com/chrislusf/seaweedfs/weed/glog"
|
||||
)
|
||||
|
||||
@@ -83,16 +85,23 @@ func Get(url string) ([]byte, error) {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func Head(url string) (http.Header, error) {
|
||||
r, err := client.Head(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func Head(url string, fn func(header fasthttp.ResponseHeader)) error {
|
||||
req := fasthttp.AcquireRequest()
|
||||
resp := fasthttp.AcquireResponse()
|
||||
defer fasthttp.ReleaseRequest(req) // <- do not forget to release
|
||||
defer fasthttp.ReleaseResponse(resp) // <- do not forget to release
|
||||
|
||||
c := fasthttp.Client{}
|
||||
req.SetRequestURI(url)
|
||||
req.Header.SetMethod(fasthttp.MethodHead)
|
||||
if err := c.Do(req, resp); err != nil {
|
||||
return err
|
||||
}
|
||||
defer r.Body.Close()
|
||||
if r.StatusCode >= 400 {
|
||||
return nil, fmt.Errorf("%s: %s", url, r.Status)
|
||||
if resp.StatusCode() >= 400 {
|
||||
return fmt.Errorf("%s: %d", url, resp.StatusCode())
|
||||
}
|
||||
return r.Header, nil
|
||||
fn(resp.Header)
|
||||
return nil
|
||||
}
|
||||
|
||||
func Delete(url string, jwt string) error {
|
||||
|
||||
19
weed/util/http_util_test.go
Normal file
19
weed/util/http_util_test.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
func TestFasthttpClientHead(t *testing.T) {
|
||||
err := Head("https://www.google.com", func(header fasthttp.ResponseHeader) {
|
||||
header.VisitAll(func(key, value []byte) {
|
||||
println(string(key) + ": " + string(value))
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user