2015-05-16 01:50:17 +02:00
|
|
|
package transport
|
2015-05-16 01:25:00 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2015-05-20 19:09:37 +02:00
|
|
|
// ReadSeekCloser combines io.ReadSeeker with io.Closer.
|
|
|
|
type ReadSeekCloser interface {
|
|
|
|
io.ReadSeeker
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
2015-05-16 02:37:32 +02:00
|
|
|
// NewHTTPReadSeeker handles reading from an HTTP endpoint using a GET
|
|
|
|
// request. When seeking and starting a read from a non-zero offset
|
|
|
|
// the a "Range" header will be added which sets the offset.
|
2015-05-20 19:09:37 +02:00
|
|
|
// TODO(dmcgowan): Move this into a separate utility package
|
2015-12-01 03:35:19 +01:00
|
|
|
func NewHTTPReadSeeker(client *http.Client, url string, errorHandler func(*http.Response) error) ReadSeekCloser {
|
2015-05-16 01:25:00 +02:00
|
|
|
return &httpReadSeeker{
|
2015-12-01 03:35:19 +01:00
|
|
|
client: client,
|
|
|
|
url: url,
|
|
|
|
errorHandler: errorHandler,
|
2015-05-16 01:25:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type httpReadSeeker struct {
|
|
|
|
client *http.Client
|
|
|
|
url string
|
|
|
|
|
2015-12-01 03:35:19 +01:00
|
|
|
// errorHandler creates an error from an unsuccessful HTTP response.
|
|
|
|
// This allows the error to be created with the HTTP response body
|
|
|
|
// without leaking the body through a returned error.
|
|
|
|
errorHandler func(*http.Response) error
|
|
|
|
|
2015-05-16 01:25:00 +02:00
|
|
|
size int64
|
|
|
|
|
2015-12-01 03:35:19 +01:00
|
|
|
// rc is the remote read closer.
|
|
|
|
rc io.ReadCloser
|
|
|
|
// brd is a buffer for internal buffered io.
|
|
|
|
brd *bufio.Reader
|
|
|
|
// readerOffset tracks the offset as of the last read.
|
|
|
|
readerOffset int64
|
|
|
|
// seekOffset allows Seek to override the offset. Seek changes
|
|
|
|
// seekOffset instead of changing readOffset directly so that
|
|
|
|
// connection resets can be delayed and possibly avoided if the
|
|
|
|
// seek is undone (i.e. seeking to the end and then back to the
|
|
|
|
// beginning).
|
|
|
|
seekOffset int64
|
|
|
|
err error
|
2015-05-16 01:25:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hrs *httpReadSeeker) Read(p []byte) (n int, err error) {
|
|
|
|
if hrs.err != nil {
|
|
|
|
return 0, hrs.err
|
|
|
|
}
|
|
|
|
|
2015-12-01 03:35:19 +01:00
|
|
|
// If we seeked to a different position, we need to reset the
|
|
|
|
// connection. This logic is here instead of Seek so that if
|
|
|
|
// a seek is undone before the next read, the connection doesn't
|
|
|
|
// need to be closed and reopened. A common example of this is
|
|
|
|
// seeking to the end to determine the length, and then seeking
|
|
|
|
// back to the original position.
|
|
|
|
if hrs.readerOffset != hrs.seekOffset {
|
|
|
|
hrs.reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
hrs.readerOffset = hrs.seekOffset
|
|
|
|
|
2015-05-16 01:25:00 +02:00
|
|
|
rd, err := hrs.reader()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err = rd.Read(p)
|
2015-12-01 03:35:19 +01:00
|
|
|
hrs.seekOffset += int64(n)
|
|
|
|
hrs.readerOffset += int64(n)
|
2015-05-16 01:25:00 +02:00
|
|
|
|
|
|
|
// Simulate io.EOF error if we reach filesize.
|
2015-12-01 03:35:19 +01:00
|
|
|
if err == nil && hrs.size >= 0 && hrs.readerOffset >= hrs.size {
|
2015-05-16 01:25:00 +02:00
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hrs *httpReadSeeker) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
if hrs.err != nil {
|
|
|
|
return 0, hrs.err
|
|
|
|
}
|
|
|
|
|
2015-12-01 03:35:19 +01:00
|
|
|
_, err := hrs.reader()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newOffset := hrs.seekOffset
|
2015-05-16 01:25:00 +02:00
|
|
|
|
|
|
|
switch whence {
|
|
|
|
case os.SEEK_CUR:
|
|
|
|
newOffset += int64(offset)
|
|
|
|
case os.SEEK_END:
|
2015-12-01 03:35:19 +01:00
|
|
|
if hrs.size < 0 {
|
|
|
|
return 0, errors.New("content length not known")
|
|
|
|
}
|
2015-05-16 01:25:00 +02:00
|
|
|
newOffset = hrs.size + int64(offset)
|
|
|
|
case os.SEEK_SET:
|
|
|
|
newOffset = int64(offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
if newOffset < 0 {
|
|
|
|
err = errors.New("cannot seek to negative position")
|
|
|
|
} else {
|
2015-12-01 03:35:19 +01:00
|
|
|
hrs.seekOffset = newOffset
|
2015-05-16 01:25:00 +02:00
|
|
|
}
|
|
|
|
|
2015-12-01 03:35:19 +01:00
|
|
|
return hrs.seekOffset, err
|
2015-05-16 01:25:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hrs *httpReadSeeker) Close() error {
|
|
|
|
if hrs.err != nil {
|
|
|
|
return hrs.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// close and release reader chain
|
|
|
|
if hrs.rc != nil {
|
|
|
|
hrs.rc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
hrs.rc = nil
|
|
|
|
hrs.brd = nil
|
|
|
|
|
|
|
|
hrs.err = errors.New("httpLayer: closed")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hrs *httpReadSeeker) reset() {
|
|
|
|
if hrs.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if hrs.rc != nil {
|
|
|
|
hrs.rc.Close()
|
|
|
|
hrs.rc = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hrs *httpReadSeeker) reader() (io.Reader, error) {
|
|
|
|
if hrs.err != nil {
|
|
|
|
return nil, hrs.err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hrs.rc != nil {
|
|
|
|
return hrs.brd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", hrs.url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-01 03:35:19 +01:00
|
|
|
if hrs.readerOffset > 0 {
|
2015-05-16 01:25:00 +02:00
|
|
|
// TODO(stevvooe): Get this working correctly.
|
|
|
|
|
|
|
|
// If we are at different offset, issue a range request from there.
|
|
|
|
req.Header.Add("Range", "1-")
|
|
|
|
// TODO: get context in here
|
|
|
|
// context.GetLogger(hrs.context).Infof("Range: %s", req.Header.Get("Range"))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := hrs.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-25 01:14:04 +02:00
|
|
|
// Normally would use client.SuccessStatus, but that would be a cyclic
|
|
|
|
// import
|
|
|
|
if resp.StatusCode >= 200 && resp.StatusCode <= 399 {
|
2015-05-16 01:25:00 +02:00
|
|
|
hrs.rc = resp.Body
|
2015-12-01 03:35:19 +01:00
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
hrs.size = resp.ContentLength
|
|
|
|
} else {
|
|
|
|
hrs.size = -1
|
|
|
|
}
|
2015-07-25 01:14:04 +02:00
|
|
|
} else {
|
2015-05-16 01:25:00 +02:00
|
|
|
defer resp.Body.Close()
|
2015-12-01 03:35:19 +01:00
|
|
|
if hrs.errorHandler != nil {
|
|
|
|
return nil, hrs.errorHandler(resp)
|
|
|
|
}
|
2015-05-16 01:25:00 +02:00
|
|
|
return nil, fmt.Errorf("unexpected status resolving reader: %v", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hrs.brd == nil {
|
|
|
|
hrs.brd = bufio.NewReader(hrs.rc)
|
|
|
|
} else {
|
|
|
|
hrs.brd.Reset(hrs.rc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hrs.brd, nil
|
|
|
|
}
|