| 127 | } |
| 128 | |
| 129 | func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) { |
| 130 | if ctx != nil { |
| 131 | req = req.WithContext(ctx) |
| 132 | } |
| 133 | resp, err := c.client.Do(req) |
| 134 | if err != nil { |
| 135 | return nil, nil, err |
| 136 | } |
| 137 | |
| 138 | var body []byte |
| 139 | done := make(chan error, 1) |
| 140 | go func() { |
| 141 | var buf bytes.Buffer |
| 142 | _, err := buf.ReadFrom(resp.Body) |
| 143 | body = buf.Bytes() |
| 144 | done <- err |
| 145 | }() |
| 146 | |
| 147 | select { |
| 148 | case <-ctx.Done(): |
| 149 | resp.Body.Close() |
| 150 | <-done |
| 151 | return resp, nil, ctx.Err() |
| 152 | case err = <-done: |
| 153 | resp.Body.Close() |
| 154 | return resp, body, err |
| 155 | } |
| 156 | } |