splitHTTP200ResponseToPartial splits a 200 response in multiple streams as specified by the chunks
(streams chan io.ReadCloser, errs chan error, body io.ReadCloser, chunks []private.ImageSourceChunk)
| 273 | |
| 274 | // splitHTTP200ResponseToPartial splits a 200 response in multiple streams as specified by the chunks |
| 275 | func splitHTTP200ResponseToPartial(streams chan io.ReadCloser, errs chan error, body io.ReadCloser, chunks []private.ImageSourceChunk) { |
| 276 | defer close(streams) |
| 277 | defer close(errs) |
| 278 | currentOffset := uint64(0) |
| 279 | |
| 280 | body = makeBufferedNetworkReader(body, 64, 16384) |
| 281 | defer body.Close() |
| 282 | for _, c := range chunks { |
| 283 | if c.Offset != currentOffset { |
| 284 | if c.Offset < currentOffset { |
| 285 | errs <- fmt.Errorf("invalid chunk offset specified %v (expected >= %v)", c.Offset, currentOffset) |
| 286 | break |
| 287 | } |
| 288 | toSkip := c.Offset - currentOffset |
| 289 | if _, err := io.Copy(io.Discard, io.LimitReader(body, int64(toSkip))); err != nil { |
| 290 | errs <- err |
| 291 | break |
| 292 | } |
| 293 | currentOffset += toSkip |
| 294 | } |
| 295 | var reader io.Reader |
| 296 | if c.Length == math.MaxUint64 { |
| 297 | reader = body |
| 298 | } else { |
| 299 | reader = io.LimitReader(body, int64(c.Length)) |
| 300 | } |
| 301 | s := signalCloseReader{ |
| 302 | closed: make(chan struct{}), |
| 303 | stream: io.NopCloser(reader), |
| 304 | consumeStream: true, |
| 305 | } |
| 306 | streams <- s |
| 307 | |
| 308 | // Wait until the stream is closed before going to the next chunk |
| 309 | <-s.closed |
| 310 | currentOffset += c.Length |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // handle206Response reads a 206 response and send each part as a separate ReadCloser to the streams chan. |
| 315 | func handle206Response(streams chan io.ReadCloser, errs chan error, body io.ReadCloser, chunks []private.ImageSourceChunk, mediaType string, params map[string]string) { |
searching dependent graphs…