|
| 1 | +// Copyright 2025 Jakub Zelenka and The WST Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package request |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "io" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/wstool/wst/app" |
| 23 | +) |
| 24 | + |
| 25 | +// chunkControlledReader implements io.Reader with control over chunk sizes and delays. |
| 26 | +// Each Read() call returns at most chunkSize bytes, which forces the HTTP client to |
| 27 | +// make multiple Read() calls, effectively controlling the chunk sizes sent over the wire |
| 28 | +// when using chunked transfer encoding. |
| 29 | +type chunkControlledReader struct { |
| 30 | + ctx context.Context |
| 31 | + fnd app.Foundation |
| 32 | + data []byte |
| 33 | + chunkSize int |
| 34 | + chunkDelay time.Duration |
| 35 | + offset int |
| 36 | + readCount int |
| 37 | +} |
| 38 | + |
| 39 | +func (r *chunkControlledReader) Read(p []byte) (n int, err error) { |
| 40 | + // Check context cancellation |
| 41 | + select { |
| 42 | + case <-r.ctx.Done(): |
| 43 | + return 0, r.ctx.Err() |
| 44 | + default: |
| 45 | + } |
| 46 | + |
| 47 | + // If all data has been read, return EOF (no delay before EOF) |
| 48 | + if r.offset >= len(r.data) { |
| 49 | + return 0, io.EOF |
| 50 | + } |
| 51 | + |
| 52 | + // Add delay before each chunk (except the first one) |
| 53 | + if r.readCount > 0 && r.chunkDelay > 0 { |
| 54 | + if err := r.fnd.Sleep(r.ctx, r.chunkDelay); err != nil { |
| 55 | + return 0, err |
| 56 | + } |
| 57 | + } |
| 58 | + r.readCount++ |
| 59 | + |
| 60 | + // Determine how much to read in this chunk |
| 61 | + chunkSize := r.chunkSize |
| 62 | + if chunkSize <= 0 { |
| 63 | + // If chunk size not specified, use the full buffer provided by HTTP client |
| 64 | + chunkSize = len(p) |
| 65 | + } |
| 66 | + |
| 67 | + // Calculate how much data to copy |
| 68 | + remaining := len(r.data) - r.offset |
| 69 | + toRead := chunkSize |
| 70 | + if toRead > remaining { |
| 71 | + toRead = remaining |
| 72 | + } |
| 73 | + if toRead > len(p) { |
| 74 | + // Never read more than the provided buffer can hold |
| 75 | + toRead = len(p) |
| 76 | + } |
| 77 | + |
| 78 | + // Copy data to buffer |
| 79 | + n = copy(p, r.data[r.offset:r.offset+toRead]) |
| 80 | + r.offset += n |
| 81 | + |
| 82 | + return n, nil |
| 83 | +} |
0 commit comments