|
|
@ -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 |
|
|
|
} |