RealXLFD
2 months ago
4 changed files with 152 additions and 10 deletions
@ -0,0 +1,11 @@ |
|||||
|
package mdown |
||||
|
|
||||
|
type Distributor struct { |
||||
|
running bool |
||||
|
MaxThread uint |
||||
|
MaxThreadPerReq uint |
||||
|
currentThread uint |
||||
|
release chan uint |
||||
|
acquire chan uint |
||||
|
getter chan uint |
||||
|
} |
@ -0,0 +1,75 @@ |
|||||
|
package mdown |
||||
|
|
||||
|
import ( |
||||
|
`net/http` |
||||
|
`net/url` |
||||
|
`os` |
||||
|
`strconv` |
||||
|
) |
||||
|
|
||||
|
type Downloader struct { |
||||
|
client *http.Client |
||||
|
targetURL *url.URL |
||||
|
file *os.File |
||||
|
headers http.Header |
||||
|
ContentSize int |
||||
|
AcceptRanges bool |
||||
|
State State |
||||
|
threads []*worker |
||||
|
} |
||||
|
type State int |
||||
|
|
||||
|
const ( |
||||
|
FAILED State = iota - 1 |
||||
|
WAITING |
||||
|
DOWNLOADING |
||||
|
SUCCESS |
||||
|
) |
||||
|
|
||||
|
func createDownloader( |
||||
|
client *Client, file *os.File, URL *url.URL, header http.Header, |
||||
|
) *Downloader { |
||||
|
downloader := &Downloader{ |
||||
|
&http.Client{ |
||||
|
Transport: client.Transport, |
||||
|
}, |
||||
|
URL, |
||||
|
file, |
||||
|
header, |
||||
|
0, |
||||
|
false, |
||||
|
WAITING, |
||||
|
nil, |
||||
|
} |
||||
|
return downloader |
||||
|
} |
||||
|
|
||||
|
func (d *Downloader) head(distri *Distributor) { |
||||
|
headQuery := &http.Request{ |
||||
|
Method: "HEAD", |
||||
|
URL: d.targetURL, |
||||
|
Header: d.headers, |
||||
|
} |
||||
|
resp, err := d.client.Do(headQuery) |
||||
|
if err != nil { |
||||
|
d.State = FAILED |
||||
|
return |
||||
|
} |
||||
|
defer func() { |
||||
|
_ = resp.Body.Close() |
||||
|
}() |
||||
|
contentLength := resp.Header.Get("content-length") |
||||
|
if d.ContentSize, err = strconv.Atoi(contentLength); contentLength == "" || err != nil { |
||||
|
d.State = FAILED |
||||
|
return |
||||
|
} |
||||
|
acceptRanges := resp.Header.Get("accept-ranges") |
||||
|
d.AcceptRanges = acceptRanges == "bytes" |
||||
|
d.State = WAITING |
||||
|
client.acquire(client.) |
||||
|
d.generator() |
||||
|
} |
||||
|
|
||||
|
func (d *Downloader) generator(thread uint) { |
||||
|
|
||||
|
} |
Loading…
Reference in new issue