|
@@ -8,10 +8,9 @@ import (
|
|
"git.bvbej.com/bvbej/base-golang/pkg/downloader/fetcher"
|
|
"git.bvbej.com/bvbej/base-golang/pkg/downloader/fetcher"
|
|
"golang.org/x/sync/errgroup"
|
|
"golang.org/x/sync/errgroup"
|
|
"io"
|
|
"io"
|
|
- "io/ioutil"
|
|
|
|
"mime"
|
|
"mime"
|
|
|
|
+ "net"
|
|
"net/http"
|
|
"net/http"
|
|
- "net/http/cookiejar"
|
|
|
|
"net/url"
|
|
"net/url"
|
|
"path"
|
|
"path"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
@@ -63,11 +62,14 @@ func FetcherBuilder() ([]string, func() fetcher.Fetcher) {
|
|
}
|
|
}
|
|
|
|
|
|
func (f *Fetcher) Resolve(req *base.Request) (*base.Resource, error) {
|
|
func (f *Fetcher) Resolve(req *base.Request) (*base.Resource, error) {
|
|
- httpReq, err := buildRequest(nil, req)
|
|
|
|
|
|
+ httpReq, err := f.buildRequest(nil, req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ client, err := f.buildClient()
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
- client := buildClient()
|
|
|
|
// 只访问一个字节,测试资源是否支持Range请求
|
|
// 只访问一个字节,测试资源是否支持Range请求
|
|
httpReq.Header.Set(base.HttpHeaderRange, fmt.Sprintf(base.HttpHeaderRangeFormat, 0, 0))
|
|
httpReq.Header.Set(base.HttpHeaderRange, fmt.Sprintf(base.HttpHeaderRangeFormat, 0, 0))
|
|
httpResp, err := client.Do(httpReq)
|
|
httpResp, err := client.Do(httpReq)
|
|
@@ -75,7 +77,7 @@ func (f *Fetcher) Resolve(req *base.Request) (*base.Resource, error) {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
// 拿到响应头就关闭,不用加defer
|
|
// 拿到响应头就关闭,不用加defer
|
|
- httpResp.Body.Close()
|
|
|
|
|
|
+ _ = httpResp.Body.Close()
|
|
res := &base.Resource{
|
|
res := &base.Resource{
|
|
Req: req,
|
|
Req: req,
|
|
Range: false,
|
|
Range: false,
|
|
@@ -222,7 +224,7 @@ func (f *Fetcher) fetch() {
|
|
f.ctx, f.cancel = context.WithCancel(context.Background())
|
|
f.ctx, f.cancel = context.WithCancel(context.Background())
|
|
eg, _ := errgroup.WithContext(f.ctx)
|
|
eg, _ := errgroup.WithContext(f.ctx)
|
|
for i := 0; i < f.opts.Connections; i++ {
|
|
for i := 0; i < f.opts.Connections; i++ {
|
|
- j := i //不加这一行会造成越界报错
|
|
|
|
|
|
+ j := i //TODO loop var per loop(1.22已解决,loop var per-iteration)
|
|
eg.Go(func() error {
|
|
eg.Go(func() error {
|
|
return f.fetchChunk(j)
|
|
return f.fetchChunk(j)
|
|
})
|
|
})
|
|
@@ -249,14 +251,17 @@ func (f *Fetcher) fetchChunk(index int) (err error) {
|
|
filename := f.filename()
|
|
filename := f.filename()
|
|
chunk := f.chunks[index]
|
|
chunk := f.chunks[index]
|
|
|
|
|
|
- httpReq, err := buildRequest(f.ctx, f.res.Req)
|
|
|
|
|
|
+ httpReq, err := f.buildRequest(f.ctx, f.res.Req)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- var (
|
|
|
|
- client = buildClient()
|
|
|
|
- buf = make([]byte, 8192)
|
|
|
|
- )
|
|
|
|
|
|
+
|
|
|
|
+ client, err := f.buildClient()
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var buf = make([]byte, 8192)
|
|
|
|
|
|
// 重试10次
|
|
// 重试10次
|
|
for i := 0; i < 10; i++ {
|
|
for i := 0; i < 10; i++ {
|
|
@@ -342,16 +347,24 @@ func (f *Fetcher) fetchChunk(index int) (err error) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-func buildClient() *http.Client {
|
|
|
|
- // Cookie handle
|
|
|
|
- jar, _ := cookiejar.New(nil)
|
|
|
|
- return &http.Client{
|
|
|
|
- Jar: jar,
|
|
|
|
- Timeout: time.Second * 10,
|
|
|
|
|
|
+func (f *Fetcher) buildClient() (*http.Client, error) {
|
|
|
|
+ dialer, err := f.Ctl.ContextDialer()
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ transport := &http.Transport{
|
|
|
|
+ DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
+ return dialer.Dial(network, addr)
|
|
|
|
+ },
|
|
}
|
|
}
|
|
|
|
+ return &http.Client{
|
|
|
|
+ Jar: f.Ctl.ContextCookie(),
|
|
|
|
+ Timeout: f.Ctl.ContextTimeout(),
|
|
|
|
+ Transport: transport,
|
|
|
|
+ }, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request, err error) {
|
|
|
|
|
|
+func (f *Fetcher) buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request, err error) {
|
|
reqUrl, err := url.Parse(req.URL)
|
|
reqUrl, err := url.Parse(req.URL)
|
|
if err != nil {
|
|
if err != nil {
|
|
return
|
|
return
|
|
@@ -377,7 +390,7 @@ func buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if extra.Body != "" {
|
|
if extra.Body != "" {
|
|
- body = ioutil.NopCloser(bytes.NewBufferString(extra.Body))
|
|
|
|
|
|
+ body = io.NopCloser(bytes.NewBufferString(extra.Body))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|