@cryptotaxi247 / kubo / commits / 71e92dace

explain(commands/http)

we've gotta allow the code to speak for itself. I wouldn't have been able to safely modify this code without having my hand held. I am but a lowly programmer with a simple mind. cc @jbenet @whyrusleeping @mappum

Brian Tiger Chow committed Nov 11, 2014 at 23:28 UTC 71e92dace23b9b52de1710406e13993156522086
1 file changed +15 -3
commands/http/client.go
+15 -3
@@ -32,7 +32,14 @@ func NewClient(address string) Client {
32 }
33
34 func (c *client) Send(req cmds.Request) (cmds.Response, error) {
35 - userEncoding, _ := req.Option(cmds.EncShort).String()
35 +
36 + // save user-provided encoding
37 + previousUserProvidedEncoding, found, err := req.Option(cmds.EncShort).String()
38 + if err != nil {
39 + return nil, err
40 + }
41 +
42 + // override with json to send to server
43 req.SetOption(cmds.EncShort, cmds.JSON)
44
45 query, inputStream, err := getQuery(req)
@@ -43,18 +50,23 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
50 path := strings.Join(req.Path(), "/")
51 url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, ApiPath, path, query)
52
53 + // TODO extract string const?
54 httpRes, err := http.Post(url, "application/octet-stream", inputStream)
55 if err != nil {
56 return nil, err
57 }
58
59 + // using the overridden JSON encoding in request
60 res, err := getResponse(httpRes, req)
61 if err != nil {
62 return nil, err
63 }
64
56 - if len(userEncoding) > 0 {
57 - req.SetOption(cmds.EncShort, userEncoding)
65 + if found && len(previousUserProvidedEncoding) > 0 {
66 + // reset to user provided encoding after sending request
67 + // NB: if user has provided an encoding but it is the empty string,
68 + // still leave it as JSON.
69 + req.SetOption(cmds.EncShort, previousUserProvidedEncoding)
70 }
71
72 return res, nil