diff --git a/libs/meta/store.go b/libs/meta/store.go index e32158c..f4d4887 100644 --- a/libs/meta/store.go +++ b/libs/meta/store.go @@ -7,6 +7,7 @@ import ( log "github.com/golang/glog" "io/ioutil" "net/http" + "time" ) const ( @@ -27,6 +28,15 @@ const ( probeAPI = "http://%s/probe?vid=%d" ) +var ( + _client = &http.Client{ + Transport: &http.Transport{ + DisableCompression: true, + }, + Timeout: 2 * time.Second, + } +) + type StoreList []*Store func (sl StoreList) Len() int { @@ -83,12 +93,17 @@ func (s *Store) probeAPI(vid int32) string { func (s *Store) Info() (vs []*Volume, err error) { var ( body []byte + req *http.Request resp *http.Response data = new(Volumes) url = s.statAPI() ) - if resp, err = http.Get(url); err != nil { - log.Warningf("http.Get(\"%s\") error(%v)", url, err) + if req, err = http.NewRequest("GET", url, nil); err != nil { + log.Info("http.NewRequest(GET,%s) error(%v)", url, err) + return + } + if resp, err = _client.Do(req); err != nil { + log.Errorf("_client.do(%s) error(%v)", url, err) return } defer resp.Body.Close() @@ -111,13 +126,20 @@ func (s *Store) Info() (vs []*Volume, err error) { // Head send a head request to store. func (s *Store) Head(vid int32) (err error) { var ( + req *http.Request resp *http.Response url string ) url = s.probeAPI(vid) - if resp, err = http.Head(url); err != nil { + if req, err = http.NewRequest("HEAD", url, nil); err != nil { + log.Info("http.NewRequest(GET,%s) error(%v)", url, err) + return + } + if resp, err = _client.Do(req); err != nil { + log.Errorf("_client.do(%s) error(%v)", url, err) return } + defer resp.Body.Close() if resp.StatusCode == http.StatusInternalServerError { err = errors.ErrInternal } diff --git a/proxy/bfs/bfs.go b/proxy/bfs/bfs.go index d5bb22b..4fa2b9f 100644 --- a/proxy/bfs/bfs.go +++ b/proxy/bfs/bfs.go @@ -116,8 +116,12 @@ func (b *Bfs) Get(bucket, filename string) (src io.ReadCloser, ctlen int, mtime ctlen = int(resp.ContentLength) break } - if err == nil && resp.StatusCode == http.StatusServiceUnavailable { - err = errors.ErrStoreNotAvailable + if err == nil { + if resp.StatusCode == http.StatusServiceUnavailable { + err = errors.ErrStoreNotAvailable + } else if resp.StatusCode == http.StatusNotFound { + err = errors.ErrNeedleNotExist + } } return } diff --git a/proxy/http_api.go b/proxy/http_api.go index a995426..d2c67cf 100644 --- a/proxy/http_api.go +++ b/proxy/http_api.go @@ -157,8 +157,18 @@ func (s *server) parseURI(r *http.Request, upload bool) (bucket, file string, st // gentRI get uri by bucket and file. func (s *server) getURI(bucket, file string) (uri string) { + var ( + item *ibucket.Item + domain string + ) // http://domain/prefix/bucket/file - uri = s.c.Domain + path.Join(s.c.Prefix, bucket, file) + item, _ = s.bucket.Get(bucket) + if item.Domain != "" { + domain = item.Domain + } else { + domain = s.c.Domain + } + uri = domain + path.Join(s.c.Prefix, bucket, file) return } @@ -235,10 +245,16 @@ func (s *server) upload(item *ibucket.Item, bucket, file string, wr http.Respons return } r.Body.Close() - if len(body) > s.c.MaxFileSize { + length := len(body) + if length > s.c.MaxFileSize { status = http.StatusRequestEntityTooLarge return } + if length == 0 { + status = http.StatusBadRequest + log.Errorf("file size equals 0") + return + } sha = sha1.Sum(body) sha1sum = hex.EncodeToString(sha[:]) // if empty filename or endwith "/": dir