@@ -132,18 +132,16 @@ func (client *OriginClient) GetContentLength(url string, headers map[string]stri
132132
133133// IsSupportRange checks if the source url support partial requests.
134134func (client * OriginClient ) IsSupportRange (url string , headers map [string ]string ) (bool , error ) {
135- // set headers
136- if headers == nil {
137- headers = make (map [string ]string )
138- }
139- headers ["Range" ] = "bytes=0-0"
135+ // set headers: headers is a reference to map, should not change it
136+ copied := CopyHeader (nil , headers )
137+ copied ["Range" ] = "bytes=0-0"
140138
141139 // send request
142- resp , err := client .HTTPWithHeaders (http .MethodGet , url , headers , 4 * time .Second )
140+ resp , err := client .HTTPWithHeaders (http .MethodGet , url , copied , 4 * time .Second )
143141 if err != nil {
144142 return false , err
145143 }
146- resp .Body .Close ()
144+ _ = resp .Body .Close ()
147145
148146 if resp .StatusCode == http .StatusPartialContent {
149147 return true , nil
@@ -157,20 +155,18 @@ func (client *OriginClient) IsExpired(url string, headers map[string]string, las
157155 return true , nil
158156 }
159157
160- // set headers
161- if headers == nil {
162- headers = make (map [string ]string )
163- }
158+ // set headers: headers is a reference to map, should not change it
159+ copied := CopyHeader (nil , headers )
164160 if lastModified > 0 {
165161 lastModifiedStr , _ := netutils .ConvertTimeIntToString (lastModified )
166- headers ["If-Modified-Since" ] = lastModifiedStr
162+ copied ["If-Modified-Since" ] = lastModifiedStr
167163 }
168164 if ! stringutils .IsEmptyStr (eTag ) {
169- headers ["If-None-Match" ] = eTag
165+ copied ["If-None-Match" ] = eTag
170166 }
171167
172168 // send request
173- resp , err := client .HTTPWithHeaders (http .MethodGet , url , headers , 4 * time .Second )
169+ resp , err := client .HTTPWithHeaders (http .MethodGet , url , copied , 4 * time .Second )
174170 if err != nil {
175171 return false , err
176172 }
@@ -222,3 +218,14 @@ func (client *OriginClient) HTTPWithHeaders(method, url string, headers map[stri
222218 }
223219 return httpClient .Do (req )
224220}
221+
222+ // CopyHeader copies the src to dst and return a non-nil dst map.
223+ func CopyHeader (dst , src map [string ]string ) map [string ]string {
224+ if dst == nil {
225+ dst = make (map [string ]string )
226+ }
227+ for k , v := range src {
228+ dst [k ] = v
229+ }
230+ return dst
231+ }
0 commit comments