@cryptotaxi247 / kubo / commits / 7be8d01ee

response: option to disable output draining

This commit was moved from ipfs/go-ipfs-http-client@9f3d9635fa4e977b6ec202c9e4f78ca1c64fc0dd

Łukasz Magiera committed Feb 18, 2019 at 17:27 UTC 7be8d01ee732be71f08f4482e0d5c5b61731ee81
6 files changed +21 -5
client/httpapi/api.go
+1
@@ -131,6 +131,7 @@ func (api *HttpApi) request(command string, args ...string) *RequestBuilder {
131 command: command,
132 args: args,
133 shell: api,
134 + drainOut: true,
135 }
136 }
137
client/httpapi/apifile.go
+1 -1
@@ -57,7 +57,7 @@ func (f *apiFile) reset() error {
57 if f.r != nil {
58 f.r.Close()
59 }
60 - req := f.core.request("cat", f.path.String())
60 + req := f.core.request("cat", f.path.String()).NoDrain()
61 if f.at != 0 {
62 req.Option("offset", f.at)
63 }
client/httpapi/pubsub.go
+2 -2
@@ -113,8 +113,8 @@ func (api *PubsubAPI) Subscribe(ctx context.Context, topic string, opts ...caopt
113 }
114
115 resp, err := api.core().request("pubsub/sub", topic).
116 - Option("discover", options.Discover).
117 - Send(ctx)
116 + Option("discover", options.Discover).NoDrain().Send(ctx)
117 +
118 if err != nil {
119 return nil, err
120 }
client/httpapi/request.go
+2
@@ -13,6 +13,7 @@ type Request struct {
13 Opts map[string]string
14 Body io.Reader
15 Headers map[string]string
16 + DrainOut bool // if set, resp.Close will read all remaining data
17 }
18
19 func NewRequest(ctx context.Context, url, command string, args ...string) *Request {
@@ -30,5 +31,6 @@ func NewRequest(ctx context.Context, url, command string, args ...string) *Reque
31 Args: args,
32 Opts: opts,
33 Headers: make(map[string]string),
34 + DrainOut: true,
35 }
36 }
client/httpapi/requestbuilder.go
+7
@@ -19,6 +19,7 @@ type RequestBuilder struct {
19 opts map[string]string
20 headers map[string]string
21 body io.Reader
22 + drainOut bool
23
24 shell *HttpApi
25 }
@@ -84,6 +85,12 @@ func (r *RequestBuilder) Header(name, value string) *RequestBuilder {
85 return r
86 }
87
88 +// NoDrain disables output draining in response closer
89 +func (r *RequestBuilder) NoDrain() *RequestBuilder {
90 + r.drainOut = false
91 + return r
92 +}
93 +
94 // Send sends the request and return the response.
95 func (r *RequestBuilder) Send(ctx context.Context) (*Response, error) {
96 r.shell.applyGlobal(r)
client/httpapi/response.go
+8 -2
@@ -35,13 +35,18 @@ func (r *trailerReader) Close() error {
35 type Response struct {
36 Output io.ReadCloser
37 Error *Error
38 +
39 + drainOutput bool
40 }
41
42 func (r *Response) Close() error {
43 if r.Output != nil {
44
43 - // always drain output (response body) //TODO: make optional for things like cat
44 - _, err1 := io.Copy(ioutil.Discard, r.Output)
45 + // always drain output (response body)
46 + var err1 error
47 + if r.drainOutput {
48 + _, err1 = io.Copy(ioutil.Discard, r.Output)
49 + }
50 err2 := r.Output.Close()
51 if err1 != nil {
52 return err1
@@ -114,6 +119,7 @@ func (r *Request) Send(c *http.Client) (*Response, error) {
119
120 nresp := new(Response)
121
122 + nresp.drainOutput = r.DrainOut
123 nresp.Output = &trailerReader{resp}
124 if resp.StatusCode >= http.StatusBadRequest {
125 e := &Error{