Browse Source

toughen up error handling for invalid fid

pull/2/head
Chris Lu 11 years ago
parent
commit
1bf75f7f73
  1. 4
      go/storage/file_id.go
  2. 24
      go/storage/needle.go
  3. 6
      go/weed/weed_server/volume_server_handlers.go

4
go/storage/file_id.go

@ -25,8 +25,8 @@ func ParseFileId(fid string) (*FileId, error) {
}
vid_string, key_hash_string := a[0], a[1]
volumeId, _ := NewVolumeId(vid_string)
key, hash := ParseKeyHash(key_hash_string)
return &FileId{VolumeId: volumeId, Key: key, Hashcode: hash}, nil
key, hash, e := ParseKeyHash(key_hash_string)
return &FileId{VolumeId: volumeId, Key: key, Hashcode: hash}, e
}
func (n *FileId) String() string {
bytes := make([]byte, 12)

24
go/storage/needle.go

@ -4,6 +4,7 @@ import (
"code.google.com/p/weed-fs/go/glog"
"code.google.com/p/weed-fs/go/util"
"encoding/hex"
"errors"
"io/ioutil"
"mime"
"net/http"
@ -123,37 +124,42 @@ func NewNeedle(r *http.Request) (n *Needle, e error) {
fid = r.URL.Path[commaSep+1 : dotSep]
}
n.ParsePath(fid)
e = n.ParsePath(fid)
return
}
func (n *Needle) ParsePath(fid string) {
func (n *Needle) ParsePath(fid string) (err error) {
length := len(fid)
if length <= 8 {
return
return errors.New("Invalid fid:" + fid)
}
delta := ""
deltaIndex := strings.LastIndex(fid, "_")
if deltaIndex > 0 {
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 != "" {
d, e := strconv.ParseUint(delta, 10, 64)
if e == nil {
if d, e := strconv.ParseUint(delta, 10, 64); e == nil {
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_len := len(key_hash_bytes)
if khe != nil || key_hash_len <= 4 {
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])
hash := util.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len])
return key, hash
return key, hash, nil
}

6
go/weed/weed_server/volume_server_handlers.go

@ -92,7 +92,11 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request,
glog.V(2).Infoln("parsing error:", err, r.URL.Path)
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)
if !vs.store.HasVolume(volumeId) {

Loading…
Cancel
Save