From 4a46b84dd5296feddfa30fcf2fb2896626630f51 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sat, 27 Jul 2019 19:29:33 +0200 Subject: [PATCH] Improve http action notification Send the notification using a goroutine and set the request timeout to 15 seconds --- sftpd/sftpd.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/sftpd/sftpd.go b/sftpd/sftpd.go index 12e6920d..020d28f6 100644 --- a/sftpd/sftpd.go +++ b/sftpd/sftpd.go @@ -3,6 +3,7 @@ package sftpd import ( "fmt" "net/http" + "net/url" "os" "os/exec" "path/filepath" @@ -304,24 +305,31 @@ func executeAction(operation string, username string, path string, target string } } if len(actions.HTTPNotificationURL) > 0 { - var req *http.Request - req, err = http.NewRequest(http.MethodGet, actions.HTTPNotificationURL, nil) + var url *url.URL + url, err = url.Parse(actions.HTTPNotificationURL) if err == nil { - q := req.URL.Query() + q := url.Query() q.Add("action", operation) q.Add("username", username) q.Add("path", path) if len(target) > 0 { q.Add("target_path", target) } - req.URL.RawQuery = q.Encode() - resp, err := http.DefaultClient.Do(req) - respCode := 0 - if err == nil { - respCode = resp.StatusCode - resp.Body.Close() - } - logger.Debug(logSender, "notified action to URL: %v status code: %v err: %v", req.URL.RequestURI(), respCode, err) + url.RawQuery = q.Encode() + go func() { + startTime := time.Now() + httpClient := &http.Client{ + Timeout: 15 * time.Second, + } + resp, err := httpClient.Get(url.String()) + respCode := 0 + if err == nil { + respCode = resp.StatusCode + resp.Body.Close() + } + logger.Debug(logSender, "notified action to URL: %v status code: %v, elapsed: %v err: %v", + url.String(), respCode, time.Since(startTime), err) + }() } else { logger.Warn(logSender, "Invalid http_notification_url \"%v\" : %v", actions.HTTPNotificationURL, err) }