mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-08-24 08:15:28 +08:00
toughen up error handling for invalid fid
This commit is contained in:
parent
512899e6a6
commit
1bf75f7f73
@ -25,8 +25,8 @@ func ParseFileId(fid string) (*FileId, error) {
|
|||||||
}
|
}
|
||||||
vid_string, key_hash_string := a[0], a[1]
|
vid_string, key_hash_string := a[0], a[1]
|
||||||
volumeId, _ := NewVolumeId(vid_string)
|
volumeId, _ := NewVolumeId(vid_string)
|
||||||
key, hash := ParseKeyHash(key_hash_string)
|
key, hash, e := ParseKeyHash(key_hash_string)
|
||||||
return &FileId{VolumeId: volumeId, Key: key, Hashcode: hash}, nil
|
return &FileId{VolumeId: volumeId, Key: key, Hashcode: hash}, e
|
||||||
}
|
}
|
||||||
func (n *FileId) String() string {
|
func (n *FileId) String() string {
|
||||||
bytes := make([]byte, 12)
|
bytes := make([]byte, 12)
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"code.google.com/p/weed-fs/go/glog"
|
"code.google.com/p/weed-fs/go/glog"
|
||||||
"code.google.com/p/weed-fs/go/util"
|
"code.google.com/p/weed-fs/go/util"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -123,37 +124,42 @@ func NewNeedle(r *http.Request) (n *Needle, e error) {
|
|||||||
fid = r.URL.Path[commaSep+1 : dotSep]
|
fid = r.URL.Path[commaSep+1 : dotSep]
|
||||||
}
|
}
|
||||||
|
|
||||||
n.ParsePath(fid)
|
e = n.ParsePath(fid)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
func (n *Needle) ParsePath(fid string) {
|
func (n *Needle) ParsePath(fid string) (err error) {
|
||||||
length := len(fid)
|
length := len(fid)
|
||||||
if length <= 8 {
|
if length <= 8 {
|
||||||
return
|
return errors.New("Invalid fid:" + fid)
|
||||||
}
|
}
|
||||||
delta := ""
|
delta := ""
|
||||||
deltaIndex := strings.LastIndex(fid, "_")
|
deltaIndex := strings.LastIndex(fid, "_")
|
||||||
if deltaIndex > 0 {
|
if deltaIndex > 0 {
|
||||||
fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
|
fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
|
||||||
}
|
}
|
||||||
n.Id, n.Cookie = ParseKeyHash(fid)
|
n.Id, n.Cookie, err = ParseKeyHash(fid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if delta != "" {
|
if delta != "" {
|
||||||
d, e := strconv.ParseUint(delta, 10, 64)
|
if d, e := strconv.ParseUint(delta, 10, 64); e == nil {
|
||||||
if e == nil {
|
|
||||||
n.Id += d
|
n.Id += d
|
||||||
|
} else {
|
||||||
|
return e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseKeyHash(key_hash_string string) (uint64, uint32) {
|
func ParseKeyHash(key_hash_string string) (uint64, uint32, error) {
|
||||||
key_hash_bytes, khe := hex.DecodeString(key_hash_string)
|
key_hash_bytes, khe := hex.DecodeString(key_hash_string)
|
||||||
key_hash_len := len(key_hash_bytes)
|
key_hash_len := len(key_hash_bytes)
|
||||||
if khe != nil || key_hash_len <= 4 {
|
if khe != nil || key_hash_len <= 4 {
|
||||||
glog.V(0).Infoln("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe)
|
glog.V(0).Infoln("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe)
|
||||||
return 0, 0
|
return 0, 0, errors.New("Invalid key and hash:" + key_hash_string)
|
||||||
}
|
}
|
||||||
key := util.BytesToUint64(key_hash_bytes[0 : key_hash_len-4])
|
key := util.BytesToUint64(key_hash_bytes[0 : key_hash_len-4])
|
||||||
hash := util.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len])
|
hash := util.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len])
|
||||||
return key, hash
|
return key, hash, nil
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,11 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request,
|
|||||||
glog.V(2).Infoln("parsing error:", err, r.URL.Path)
|
glog.V(2).Infoln("parsing error:", err, r.URL.Path)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
n.ParsePath(fid)
|
err = n.ParsePath(fid)
|
||||||
|
if err != nil {
|
||||||
|
glog.V(2).Infoln("parsing fid error:", err, r.URL.Path)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
glog.V(2).Infoln("volume", volumeId, "reading", n)
|
glog.V(2).Infoln("volume", volumeId, "reading", n)
|
||||||
if !vs.store.HasVolume(volumeId) {
|
if !vs.store.HasVolume(volumeId) {
|
||||||
|
Loading…
Reference in New Issue
Block a user