@@ -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,17 +76,30 @@ 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
0 commit comments