Commit 7244c044 authored by Juan Benet's avatar Juan Benet

Merge pull request #1743 from ipfs/dht-key-escape

allow for dht keys to be escaped
parents 4ac2c46c d01ee111
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"strings"
"time" "time"
key "github.com/ipfs/go-ipfs/blocks/key" key "github.com/ipfs/go-ipfs/blocks/key"
...@@ -396,6 +397,12 @@ GetValue will return the value stored in the dht at the given key. ...@@ -396,6 +397,12 @@ GetValue will return the value stored in the dht at the given key.
events := make(chan *notif.QueryEvent) events := make(chan *notif.QueryEvent)
ctx := notif.RegisterForQueryEvents(req.Context(), events) ctx := notif.RegisterForQueryEvents(req.Context(), events)
dhtkey, err := escapeDhtKey(req.Arguments()[0])
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
go func() { go func() {
defer close(outChan) defer close(outChan)
for e := range events { for e := range events {
...@@ -405,7 +412,7 @@ GetValue will return the value stored in the dht at the given key. ...@@ -405,7 +412,7 @@ GetValue will return the value stored in the dht at the given key.
go func() { go func() {
defer close(events) defer close(events)
val, err := dht.GetValue(ctx, key.B58KeyDecode(req.Arguments()[0])) val, err := dht.GetValue(ctx, dhtkey)
if err != nil { if err != nil {
notif.PublishQueryEvent(ctx, &notif.QueryEvent{ notif.PublishQueryEvent(ctx, &notif.QueryEvent{
Type: notif.QueryError, Type: notif.QueryError,
...@@ -452,7 +459,11 @@ GetValue will return the value stored in the dht at the given key. ...@@ -452,7 +459,11 @@ GetValue will return the value stored in the dht at the given key.
fmt.Fprintf(buf, "* querying %s\n", obj.ID) fmt.Fprintf(buf, "* querying %s\n", obj.ID)
} }
case notif.Value: case notif.Value:
fmt.Fprintf(buf, "got value: '%s'\n", obj.Extra) if verbose {
fmt.Fprintf(buf, "got value: '%s'\n", obj.Extra)
} else {
buf.WriteString(obj.Extra)
}
case notif.QueryError: case notif.QueryError:
fmt.Fprintf(buf, "error: %s\n", obj.Extra) fmt.Fprintf(buf, "error: %s\n", obj.Extra)
default: default:
...@@ -505,7 +516,12 @@ PutValue will store the given key value pair in the dht. ...@@ -505,7 +516,12 @@ PutValue will store the given key value pair in the dht.
events := make(chan *notif.QueryEvent) events := make(chan *notif.QueryEvent)
ctx := notif.RegisterForQueryEvents(req.Context(), events) ctx := notif.RegisterForQueryEvents(req.Context(), events)
key := key.B58KeyDecode(req.Arguments()[0]) key, err := escapeDhtKey(req.Arguments()[0])
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
data := req.Arguments()[1] data := req.Arguments()[1]
go func() { go func() {
...@@ -581,3 +597,16 @@ PutValue will store the given key value pair in the dht. ...@@ -581,3 +597,16 @@ PutValue will store the given key value pair in the dht.
}, },
Type: notif.QueryEvent{}, Type: notif.QueryEvent{},
} }
func escapeDhtKey(s string) (key.Key, error) {
parts := strings.Split(s, "/")
switch len(parts) {
case 1:
return key.B58KeyDecode(s), nil
case 3:
k := key.B58KeyDecode(parts[2])
return key.Key(strings.Join(append(parts[:2], string(k)), "/")), nil
default:
return "", errors.New("invalid key")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment