2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-04-17 14:39:14 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package ranger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-06-19 16:59:09 +01:00
|
|
|
"context"
|
2018-04-17 14:39:14 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2018-07-16 20:22:34 +01:00
|
|
|
"log"
|
2018-04-17 14:39:14 +01:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type httpRanger struct {
|
|
|
|
URL string
|
|
|
|
size int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPRanger turns an HTTP URL into a Ranger
|
2019-06-04 12:36:27 +01:00
|
|
|
func HTTPRanger(ctx context.Context, url string) (_ Ranger, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-05-29 14:14:25 +01:00
|
|
|
resp, err := http.Head(url)
|
2018-04-17 14:39:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-16 20:22:34 +01:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
log.Printf("Failed to close body: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-04-17 14:39:14 +01:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, Error.New("unexpected status code: %d (expected %d)",
|
|
|
|
resp.StatusCode, http.StatusOK)
|
|
|
|
}
|
|
|
|
contentLength := resp.Header.Get("Content-Length")
|
|
|
|
size, err := strconv.Atoi(contentLength)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &httpRanger{
|
2019-05-29 14:14:25 +01:00
|
|
|
URL: url,
|
2018-04-17 14:39:14 +01:00
|
|
|
size: int64(size),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-05-14 21:34:08 +01:00
|
|
|
// HTTPRangerSize creates an HTTPRanger with known size.
|
|
|
|
// Use it if you know the content size. This will safe the extra HEAD request
|
|
|
|
// for retrieving the content size.
|
2019-05-29 14:14:25 +01:00
|
|
|
func HTTPRangerSize(url string, size int64) Ranger {
|
2018-05-14 21:34:08 +01:00
|
|
|
return &httpRanger{
|
2019-05-29 14:14:25 +01:00
|
|
|
URL: url,
|
2018-05-14 21:34:08 +01:00
|
|
|
size: size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:39:14 +01:00
|
|
|
// Size implements Ranger.Size
|
|
|
|
func (r *httpRanger) Size() int64 {
|
|
|
|
return r.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Range implements Ranger.Range
|
2019-06-04 12:36:27 +01:00
|
|
|
func (r *httpRanger) Range(ctx context.Context, offset, length int64) (_ io.ReadCloser, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-04-17 14:39:14 +01:00
|
|
|
if offset < 0 {
|
2018-06-18 17:46:49 +01:00
|
|
|
return nil, Error.New("negative offset")
|
2018-04-17 14:39:14 +01:00
|
|
|
}
|
|
|
|
if length < 0 {
|
2018-06-18 17:46:49 +01:00
|
|
|
return nil, Error.New("negative length")
|
2018-04-17 14:39:14 +01:00
|
|
|
}
|
|
|
|
if offset+length > r.size {
|
2018-06-18 17:46:49 +01:00
|
|
|
return nil, Error.New("range beyond end")
|
2018-04-17 14:39:14 +01:00
|
|
|
}
|
|
|
|
if length == 0 {
|
2018-06-18 17:46:49 +01:00
|
|
|
return ioutil.NopCloser(bytes.NewReader([]byte{})), nil
|
2018-04-17 14:39:14 +01:00
|
|
|
}
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", r.URL, nil)
|
|
|
|
if err != nil {
|
2018-06-18 17:46:49 +01:00
|
|
|
return nil, err
|
2018-04-17 14:39:14 +01:00
|
|
|
}
|
|
|
|
req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+length-1))
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2018-06-18 17:46:49 +01:00
|
|
|
return nil, err
|
2018-04-17 14:39:14 +01:00
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusPartialContent {
|
2018-07-16 20:22:34 +01:00
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
return nil, Error.New("Error: (%v) Failed to close Body :: unexpected status code: %d (expected %d)",
|
|
|
|
err, resp.StatusCode, http.StatusPartialContent)
|
|
|
|
}
|
|
|
|
|
2018-06-18 17:46:49 +01:00
|
|
|
return nil, Error.New("unexpected status code: %d (expected %d)",
|
|
|
|
resp.StatusCode, http.StatusPartialContent)
|
2018-04-17 14:39:14 +01:00
|
|
|
}
|
2018-06-18 17:46:49 +01:00
|
|
|
return resp.Body, nil
|
2018-04-17 14:39:14 +01:00
|
|
|
}
|