summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-09-19 12:36:48 +0200
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-09-19 12:36:48 +0200
commit5f2e7ef988ac1267fe5038419b19b0154f9d5a84 (patch)
treebab00cad1d76c6572d4eb6023861c834da913dc7
parent4bf1273c3f446ac3195fb700ec41c7cae7d20ac9 (diff)
API + HTTP: Set the /disconnect timeout to 1 second
-rw-r--r--internal/http/http.go12
-rw-r--r--internal/server/api.go2
2 files changed, 11 insertions, 3 deletions
diff --git a/internal/http/http.go b/internal/http/http.go
index 0ca444d..aa559fe 100644
--- a/internal/http/http.go
+++ b/internal/http/http.go
@@ -18,6 +18,7 @@ type HTTPOptionalParams struct {
Headers http.Header
URLParameters URLParameters
Body url.Values
+ Timeout time.Duration
}
// Construct an URL including on parameters
@@ -77,7 +78,7 @@ func httpOptionalURL(url string, opts *HTTPOptionalParams) (string, error) {
func httpOptionalHeaders(req *http.Request, opts *HTTPOptionalParams) {
// Add headers
- if opts != nil && req != nil {
+ if opts != nil && req != nil && opts.Headers != nil {
for k, v := range opts.Headers {
req.Header.Add(k, v[0])
}
@@ -105,8 +106,15 @@ func HTTPMethodWithOpts(
return nil, nil, urlErr
}
+ // Default timeout is 5 seconds
+ // If a different timeout is given, set it
+ var timeout time.Duration = 5
+ if opts != nil && opts.Timeout > 0 {
+ timeout = opts.Timeout
+ }
+
// Create a client
- client := &http.Client{Timeout: 5 * time.Second}
+ client := &http.Client{Timeout: timeout * time.Second}
errorMessage := fmt.Sprintf("failed HTTP request with method %s and url %s", method, url)
diff --git a/internal/server/api.go b/internal/server/api.go
index 80ecf2e..b06b25c 100644
--- a/internal/server/api.go
+++ b/internal/server/api.go
@@ -201,5 +201,5 @@ func APIConnectOpenVPN(server Server, profile_id string) (string, time.Time, err
// This needs no further return value as it's best effort
// FIXME: doAuth should not be needed here
func APIDisconnect(server Server) {
- apiAuthorized(server, http.MethodPost, "/disconnect", nil)
+ apiAuthorized(server, http.MethodPost, "/disconnect", &httpw.HTTPOptionalParams{Timeout: 1})
}