From adbf86173c78996e9ed95827045f671b5edf53a8 Mon Sep 17 00:00:00 2001 From: Gustavo Niemeyer Date: Tue, 1 Jul 2014 10:14:15 -0300 Subject: [PATCH] Add timeouts for client http connections. --- main.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index b6cb387..2aaa0ae 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "strconv" "strings" "text/template" + "time" ) var httpFlag = flag.String("http", ":8080", "Serve HTTP at given address") @@ -28,9 +29,9 @@ func main() { func run() error { flag.Parse() - if len(flag.Args()) > 0 { - return fmt.Errorf("too many arguments: %s", flag.Args()[0]) - } + //if len(flag.Args()) > 0 { + // return fmt.Errorf("too many arguments: %s", flag.Args()[0]) + //} http.HandleFunc("/", handler) @@ -217,7 +218,7 @@ func sendNotFound(resp http.ResponseWriter, msg string, args ...interface{}) { resp.Write([]byte(msg)) } -// TODO Timeouts for these http interactions. Use the new support coming in 1.3. +var httpClient = &http.Client{Timeout: 10 * time.Second} const refsSuffix = ".git/info/refs?service=git-upload-pack" @@ -225,13 +226,15 @@ var ErrNoRepo = errors.New("repository not found in github") var ErrNoVersion = errors.New("version reference not found in github") func hackedRefs(repo *Repo) (data []byte, versions []Version, err error) { - resp, err := http.Get("https://" + repo.GitHubRoot() + refsSuffix) + resp, err := httpClient.Get("https://" + repo.GitHubRoot() + refsSuffix) if err != nil { return nil, nil, fmt.Errorf("cannot talk to GitHub: %v", err) } + defer resp.Body.Close() + switch resp.StatusCode { case 200: - defer resp.Body.Close() + // ok case 401, 404: return nil, nil, ErrNoRepo default: