@@ -40,7 +40,7 @@ type Units []Unit
40
40
41
41
func Run (option Option ) {
42
42
fmt .Printf ("%+v\n " , option )
43
- err := option .contentLength ()
43
+ err := option .checkingHeaders ()
44
44
if err != nil {
45
45
fmt .Errorf ("%s" , err )
46
46
}
@@ -66,8 +66,7 @@ func Run(option Option) {
66
66
67
67
}
68
68
69
- func (o * Option ) contentLength () error {
70
- //resp, err := http.Head(url)
69
+ func (o * Option ) checkingHeaders () error {
71
70
resp , err := http .Head (o .URL )
72
71
if err != nil {
73
72
return err
@@ -77,20 +76,51 @@ func (o *Option) contentLength() error {
77
76
err := fmt .Errorf ("%s URL cannot support Ranges Requests" , o .URL )
78
77
return err
79
78
}
79
+
80
80
if resp .Header ["Accept-Ranges" ][0 ] == "none" {
81
81
err := fmt .Errorf ("%s cannot support Ranges Requests" , o .URL )
82
82
return err
83
83
}
84
+
84
85
if resp .ContentLength == 0 {
85
86
err := fmt .Errorf ("%s size is %s" , o .URL , resp .Header ["Content-Length" ][0 ])
86
87
return err
87
88
}
88
89
90
+ redirectURL := resp .Request .URL .String ()
91
+ if err != nil {
92
+ return err
93
+ }
94
+
89
95
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
+
91
103
return err
92
104
}
93
105
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
+
94
124
//func divide(contentLength int64, concurrency int) Units {
95
125
func (o * Option ) divide () {
96
126
var units []Unit
@@ -161,6 +191,8 @@ func (o *Option) downloadWithContext(
161
191
req .Header .Set ("Range" , byteRange )
162
192
163
193
client := http .DefaultClient
194
+ // TODO: should check resp.StatusCode.
195
+ // client.Do cannot seems to return the err when statusCode is 50x etc.
164
196
resp , err := client .Do (req )
165
197
if err != nil {
166
198
fmt .Printf ("client err: %s" , err )
0 commit comments