HEAD operation changes to fasthttp
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
package operation
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/chrislusf/seaweedfs/weed/glog"
|
||||
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/security"
|
||||
"github.com/chrislusf/seaweedfs/weed/util"
|
||||
"google.golang.org/grpc"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type VolumeAssignRequest struct {
|
||||
@@ -89,12 +93,16 @@ func Assign(server string, grpcDialOption grpc.DialOption, primaryRequest *Volum
|
||||
func LookupJwt(master string, fileId string) security.EncodedJwt {
|
||||
|
||||
tokenStr := ""
|
||||
lookupUrl := fmt.Sprintf("http://%s/dir/lookup?fileId=%s", master, fileId)
|
||||
|
||||
if h, e := util.Head(fmt.Sprintf("http://%s/dir/lookup?fileId=%s", master, fileId)); e == nil {
|
||||
bearer := h.Get("Authorization")
|
||||
if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
|
||||
tokenStr = bearer[7:]
|
||||
err := util.Head(lookupUrl, func(header fasthttp.ResponseHeader) {
|
||||
bearer := header.Peek("Authorization")
|
||||
if len(bearer) > 7 && string(bytes.ToUpper(bearer[0:6])) == "BEARER" {
|
||||
tokenStr = string(bearer[7:])
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
glog.V(0).Infof("failed to lookup jwt %s: %v", lookupUrl, err)
|
||||
}
|
||||
|
||||
return security.EncodedJwt(tokenStr)
|
||||
|
||||
@@ -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