@@ -40,7 +40,7 @@ type Units []Unit
4040
4141func Run (option Option ) {
4242 fmt .Printf ("%+v\n " , option )
43- err := option .contentLength ()
43+ err := option .checkingHeaders ()
4444 if err != nil {
4545 fmt .Errorf ("%s" , err )
4646 }
@@ -66,8 +66,7 @@ func Run(option Option) {
6666
6767}
6868
69- func (o * Option ) contentLength () error {
70- //resp, err := http.Head(url)
69+ func (o * Option ) checkingHeaders () error {
7170 resp , err := http .Head (o .URL )
7271 if err != nil {
7372 return err
@@ -77,20 +76,51 @@ func (o *Option) contentLength() error {
7776 err := fmt .Errorf ("%s URL cannot support Ranges Requests" , o .URL )
7877 return err
7978 }
79+
8080 if resp .Header ["Accept-Ranges" ][0 ] == "none" {
8181 err := fmt .Errorf ("%s cannot support Ranges Requests" , o .URL )
8282 return err
8383 }
84+
8485 if resp .ContentLength == 0 {
8586 err := fmt .Errorf ("%s size is %s" , o .URL , resp .Header ["Content-Length" ][0 ])
8687 return err
8788 }
8889
90+ redirectURL := resp .Request .URL .String ()
91+ if err != nil {
92+ return err
93+ }
94+
8995 o .ContentLength = resp .ContentLength
90- //return resp.ContentLength, nil
96+
97+ // keep the redirect URL that accept Ranges Requests because some mirror sites may deny.
98+ // TODO: redirectURL should set by Unit separately.
99+ if o .URL != redirectURL {
100+ o .URL = redirectURL
101+ }
102+
91103 return err
92104}
93105
106+ func (o * Option ) acceptRangesHeaderCheck () (string , error ) {
107+ resp , err := http .Head (o .URL )
108+ redirectURL := resp .Request .URL .String ()
109+ if err != nil {
110+ return redirectURL , err
111+ }
112+
113+ if resp .Header .Get ("Accept-Ranges" ) == "" {
114+ err := fmt .Errorf ("%s URL cannot support Ranges Requests" , o .URL )
115+ return redirectURL , err
116+ }
117+ if resp .Header ["Accept-Ranges" ][0 ] == "none" {
118+ err := fmt .Errorf ("%s cannot support Ranges Requests" , o .URL )
119+ return redirectURL , err
120+ }
121+ return redirectURL , nil
122+ }
123+
94124//func divide(contentLength int64, concurrency int) Units {
95125func (o * Option ) divide () {
96126 var units []Unit
@@ -161,6 +191,8 @@ func (o *Option) downloadWithContext(
161191 req .Header .Set ("Range" , byteRange )
162192
163193 client := http .DefaultClient
194+ // TODO: should check resp.StatusCode.
195+ // client.Do cannot seems to return the err when statusCode is 50x etc.
164196 resp , err := client .Do (req )
165197 if err != nil {
166198 fmt .Printf ("client err: %s" , err )
0 commit comments