(ctx context.Context, req pluginapi.HTTPRequest)
| 58 | } |
| 59 | |
| 60 | func (c *hostHTTPClient) DoStream(ctx context.Context, req pluginapi.HTTPRequest) (pluginapi.HTTPStreamResponse, error) { |
| 61 | if ctx == nil { |
| 62 | ctx = context.Background() |
| 63 | } |
| 64 | resp, cfg, errDo := c.doHTTP(ctx, req) |
| 65 | if errDo != nil { |
| 66 | return pluginapi.HTTPStreamResponse{}, errDo |
| 67 | } |
| 68 | helps.RecordAPIResponseMetadata(ctx, cfg, resp.StatusCode, resp.Header.Clone()) |
| 69 | chunks := make(chan pluginapi.HTTPStreamChunk) |
| 70 | go func() { |
| 71 | defer close(chunks) |
| 72 | defer func() { |
| 73 | if errClose := resp.Body.Close(); errClose != nil { |
| 74 | log.Warnf("pluginhost: stream response body close error: %v", errClose) |
| 75 | } |
| 76 | }() |
| 77 | buf := make([]byte, 32*1024) |
| 78 | for { |
| 79 | n, errRead := resp.Body.Read(buf) |
| 80 | if n > 0 { |
| 81 | payload := bytes.Clone(buf[:n]) |
| 82 | helps.AppendAPIResponseChunk(ctx, cfg, payload) |
| 83 | select { |
| 84 | case <-ctx.Done(): |
| 85 | return |
| 86 | case chunks <- pluginapi.HTTPStreamChunk{Payload: payload}: |
| 87 | } |
| 88 | } |
| 89 | if errRead != nil { |
| 90 | if errRead != io.EOF { |
| 91 | helps.RecordAPIResponseError(ctx, cfg, errRead) |
| 92 | select { |
| 93 | case <-ctx.Done(): |
| 94 | case chunks <- pluginapi.HTTPStreamChunk{Err: errRead}: |
| 95 | } |
| 96 | } |
| 97 | return |
| 98 | } |
| 99 | } |
| 100 | }() |
| 101 | return pluginapi.HTTPStreamResponse{ |
| 102 | StatusCode: resp.StatusCode, |
| 103 | Headers: cloneHeader(resp.Header), |
| 104 | Chunks: chunks, |
| 105 | }, nil |
| 106 | } |
| 107 | |
| 108 | func (c *hostHTTPClient) doHTTP(ctx context.Context, req pluginapi.HTTPRequest) (*http.Response, *config.Config, error) { |
| 109 | if c == nil || c.host == nil { |
nothing calls this directly
no test coverage detected