@@ -11,6 +11,7 @@ import (
11
11
"net/http"
12
12
"regexp"
13
13
"strings"
14
+ "time"
14
15
15
16
"github.com/labstack/echo"
16
17
"github.com/polaris1119/config"
@@ -38,28 +39,40 @@ func (DownloadController) GoDl(ctx echo.Context) error {
38
39
39
40
var filenameReg = regexp .MustCompile (`\d+\.\d[a-z\.]*\d+` )
40
41
41
- func (DownloadController ) FetchGoInstallPackage (ctx echo.Context ) error {
42
+ func (self DownloadController ) FetchGoInstallPackage (ctx echo.Context ) error {
42
43
filename := ctx .Param ("filename" )
43
44
44
45
officalUrl := GoStoragePrefix + filename
45
- resp , err := http . Head (officalUrl )
46
+ resp , err := self . headWithTimeout (officalUrl )
46
47
if err == nil && resp .StatusCode == http .StatusOK {
48
+ resp .Body .Close ()
47
49
return ctx .Redirect (http .StatusSeeOther , officalUrl )
48
50
}
51
+ resp .Body .Close ()
49
52
50
53
goVersion := filenameReg .FindString (filename )
51
54
filePath := fmt .Sprintf ("go/%s/%s" , goVersion , filename )
52
55
53
56
dlUrls := strings .Split (config .ConfigFile .MustValue ("download" , "dl_urls" ), "," )
54
57
for _ , dlUrl := range dlUrls {
55
58
dlUrl += filePath
56
- resp , err = http . Head (dlUrl )
59
+ resp , err = self . headWithTimeout (dlUrl )
57
60
if err == nil && resp .StatusCode == http .StatusOK {
61
+ resp .Body .Close ()
58
62
return ctx .Redirect (http .StatusSeeOther , dlUrl )
59
63
}
64
+ resp .Body .Close ()
60
65
}
61
66
62
67
getLogger (ctx ).Infoln ("download:" , filename , "from the site static directory" )
63
68
64
69
return ctx .Redirect (http .StatusSeeOther , "/static/" + filePath )
65
70
}
71
+
72
+ func (DownloadController ) headWithTimeout (dlUrl string ) (* http.Response , error ) {
73
+ client := http.Client {
74
+ Timeout : 2 * time .Second ,
75
+ }
76
+
77
+ return client .Head (dlUrl )
78
+ }
0 commit comments