2020-05-31 19:00:44 +08:00
|
|
|
package webseed
|
|
|
|
|
|
|
|
import (
|
2020-06-01 16:25:45 +08:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-10-15 09:42:27 +08:00
|
|
|
"errors"
|
2020-06-01 16:25:45 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-05-31 19:00:44 +08:00
|
|
|
"net/http"
|
|
|
|
|
2020-06-01 16:25:45 +08:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
"github.com/anacrolix/torrent/segments"
|
2020-05-31 19:00:44 +08:00
|
|
|
)
|
|
|
|
|
2020-06-01 16:25:45 +08:00
|
|
|
type RequestSpec = segments.Extent
|
|
|
|
|
2020-06-02 11:54:26 +08:00
|
|
|
type requestPartResult struct {
|
2020-06-01 16:25:45 +08:00
|
|
|
resp *http.Response
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestPart struct {
|
|
|
|
req *http.Request
|
|
|
|
e segments.Extent
|
2020-06-02 11:54:26 +08:00
|
|
|
result chan requestPartResult
|
2020-06-01 16:25:45 +08:00
|
|
|
}
|
|
|
|
|
2020-06-02 11:54:26 +08:00
|
|
|
type Request struct {
|
2020-06-01 16:25:45 +08:00
|
|
|
cancel func()
|
2020-06-02 11:54:26 +08:00
|
|
|
Result chan RequestResult
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Request) Cancel() {
|
|
|
|
r.cancel()
|
2020-06-01 16:25:45 +08:00
|
|
|
}
|
2020-05-31 19:00:44 +08:00
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
HttpClient *http.Client
|
|
|
|
Url string
|
2020-06-01 16:25:45 +08:00
|
|
|
FileIndex segments.Index
|
|
|
|
Info *metainfo.Info
|
|
|
|
}
|
|
|
|
|
2020-06-02 11:54:26 +08:00
|
|
|
type RequestResult struct {
|
|
|
|
Bytes []byte
|
|
|
|
Err error
|
2020-06-01 16:25:45 +08:00
|
|
|
}
|
|
|
|
|
2020-06-02 11:54:26 +08:00
|
|
|
func (ws *Client) NewRequest(r RequestSpec) Request {
|
2020-06-01 16:25:45 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var requestParts []requestPart
|
|
|
|
if !ws.FileIndex.Locate(r, func(i int, e segments.Extent) bool {
|
|
|
|
req, err := NewRequest(ws.Url, i, ws.Info, e.Start, e.Length)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
part := requestPart{
|
|
|
|
req: req,
|
2020-06-02 11:54:26 +08:00
|
|
|
result: make(chan requestPartResult, 1),
|
2020-06-01 16:25:45 +08:00
|
|
|
e: e,
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
resp, err := ws.HttpClient.Do(req)
|
2020-06-02 11:54:26 +08:00
|
|
|
part.result <- requestPartResult{
|
2020-06-01 16:25:45 +08:00
|
|
|
resp: resp,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
requestParts = append(requestParts, part)
|
|
|
|
return true
|
|
|
|
}) {
|
|
|
|
panic("request out of file bounds")
|
|
|
|
}
|
2020-06-02 11:54:26 +08:00
|
|
|
req := Request{
|
|
|
|
cancel: cancel,
|
|
|
|
Result: make(chan RequestResult, 1),
|
2020-06-01 16:25:45 +08:00
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
b, err := readRequestPartResponses(requestParts)
|
2020-06-02 11:54:26 +08:00
|
|
|
req.Result <- RequestResult{
|
|
|
|
Bytes: b,
|
|
|
|
Err: err,
|
2020-06-01 16:25:45 +08:00
|
|
|
}
|
|
|
|
}()
|
2020-06-02 11:54:26 +08:00
|
|
|
return req
|
2020-06-01 16:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func recvPartResult(buf io.Writer, part requestPart) error {
|
|
|
|
result := <-part.result
|
|
|
|
if result.err != nil {
|
|
|
|
return result.err
|
|
|
|
}
|
|
|
|
defer result.resp.Body.Close()
|
2020-10-15 09:42:27 +08:00
|
|
|
switch result.resp.StatusCode {
|
|
|
|
case http.StatusPartialContent:
|
|
|
|
case http.StatusOK:
|
|
|
|
if part.e.Start != 0 {
|
|
|
|
return errors.New("got status ok but request was at offset")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unhandled response status code (%v)", result.resp.StatusCode)
|
2020-06-01 16:25:45 +08:00
|
|
|
}
|
|
|
|
copied, err := io.Copy(buf, result.resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if copied != part.e.Length {
|
|
|
|
return fmt.Errorf("got %v bytes, expected %v", copied, part.e.Length)
|
|
|
|
}
|
|
|
|
return nil
|
2020-05-31 19:00:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-01 16:25:45 +08:00
|
|
|
func readRequestPartResponses(parts []requestPart) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, part := range parts {
|
|
|
|
err := recvPartResult(&buf, part)
|
|
|
|
if err != nil {
|
2020-10-07 07:22:55 +08:00
|
|
|
return buf.Bytes(), fmt.Errorf("reading %q at %q: %w", part.req.URL, part.req.Header.Get("Range"), err)
|
2020-06-01 16:25:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
2020-05-31 19:00:44 +08:00
|
|
|
}
|