mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-02 12:20:03 +08:00
commands/http: Use net/url querystring encoder
This commit is contained in:

committed by
Juan Batiz-Benet

parent
33b0990a3a
commit
e57cd9b857
@ -6,13 +6,14 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
cmds "github.com/jbenet/go-ipfs/commands"
|
cmds "github.com/jbenet/go-ipfs/commands"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ApiUrlFormat = "http://%s%s/%s"
|
ApiUrlFormat = "http://%s%s/%s?%s"
|
||||||
ApiPath = "/api/v0" // TODO: make configurable
|
ApiPath = "/api/v0" // TODO: make configurable
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,9 +31,6 @@ func NewClient(address string) Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) Send(req cmds.Request) (cmds.Response, error) {
|
func (c *client) Send(req cmds.Request) (cmds.Response, error) {
|
||||||
path := strings.Join(req.Path(), "/")
|
|
||||||
url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, ApiPath, path)
|
|
||||||
|
|
||||||
var userEncoding string
|
var userEncoding string
|
||||||
if enc, found := req.Option(cmds.EncShort); found {
|
if enc, found := req.Option(cmds.EncShort); found {
|
||||||
userEncoding = enc.(string)
|
userEncoding = enc.(string)
|
||||||
@ -46,9 +44,9 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
|
|||||||
// TODO: handle multiple files with multipart
|
// TODO: handle multiple files with multipart
|
||||||
var in io.Reader
|
var in io.Reader
|
||||||
|
|
||||||
query := "?"
|
query := url.Values{}
|
||||||
for k, v := range req.Options() {
|
for k, v := range req.Options() {
|
||||||
query += "&" + k + "=" + v.(string)
|
query.Set(k, v.(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
args := req.Arguments()
|
args := req.Arguments()
|
||||||
@ -61,7 +59,7 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if argDef.Type == cmds.ArgString {
|
if argDef.Type == cmds.ArgString {
|
||||||
query += "&arg=" + arg.(string)
|
query.Add("arg", arg.(string))
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// TODO: multipart
|
// TODO: multipart
|
||||||
@ -72,7 +70,10 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
httpRes, err := http.Post(url+query, "application/octet-stream", in)
|
path := strings.Join(req.Path(), "/")
|
||||||
|
url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, ApiPath, path, query.Encode())
|
||||||
|
|
||||||
|
httpRes, err := http.Post(url, "application/octet-stream", in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user