| 143 | } |
| 144 | |
| 145 | func (hrs *httpReadSeeker) reader() (io.Reader, error) { |
| 146 | if hrs.rc != nil { |
| 147 | return hrs.rc, nil |
| 148 | } |
| 149 | |
| 150 | if hrs.size == -1 || hrs.offset < hrs.size { |
| 151 | // only try to reopen the body request if we are seeking to a value |
| 152 | // less than the actual size. |
| 153 | if hrs.open == nil { |
| 154 | return nil, fmt.Errorf("cannot open: %w", errdefs.ErrNotImplemented) |
| 155 | } |
| 156 | |
| 157 | rc, err := hrs.open(hrs.offset) |
| 158 | if err != nil { |
| 159 | return nil, fmt.Errorf("httpReadSeeker: failed open: %w", err) |
| 160 | } |
| 161 | |
| 162 | if hrs.rc != nil { |
| 163 | if err := hrs.rc.Close(); err != nil { |
| 164 | log.L.WithError(err).Error("httpReadSeeker: failed to close ReadCloser") |
| 165 | } |
| 166 | } |
| 167 | hrs.rc = rc |
| 168 | } else { |
| 169 | // There is an edge case here where offset == size of the content. If |
| 170 | // we seek, we will probably get an error for content that cannot be |
| 171 | // sought (?). In that case, we should err on committing the content, |
| 172 | // as the length is already satisfied but we just return the empty |
| 173 | // reader instead. |
| 174 | |
| 175 | hrs.rc = io.NopCloser(bytes.NewReader([]byte{})) |
| 176 | } |
| 177 | |
| 178 | return hrs.rc, nil |
| 179 | } |