From f04b3321fa5b9953337a0a59894b34cc2f16941d Mon Sep 17 00:00:00 2001 From: chteufleur Date: Sun, 10 Jul 2016 23:08:10 +0200 Subject: [PATCH] Add timeout configuration in HTTP request --- README.md | 3 ++- http/http.go | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a01d849..77b473b 100644 --- a/README.md +++ b/README.md @@ -36,10 +36,11 @@ To ask authorization, just send an HTTP request to the path ``/auth`` with param * domain : Domain you want to access * method : Method you access the domain * transaction_id : Transaction identifier + * timeout : Timeout of the request in second (default : 60, max : 300) Example: ``` -GET /auth?jid=user@host/resource&domain=example.org&method=POST&transaction_id=WhatEverYouWant HTTP/1.1 +GET /auth?jid=user@host/resource&domain=example.org&method=POST&transaction_id=WhatEverYouWant&timeout=120 HTTP/1.1 ``` This will send a request to the given JID. If the user accept, the server will return HTTP code 200, otherwise it will return HTTP code 401. diff --git a/http/http.go b/http/http.go index 8d8556f..9e2e3e0 100644 --- a/http/http.go +++ b/http/http.go @@ -20,6 +20,7 @@ const ( METHOD_ACCESS = "method" DOMAIN_ACCESS = "domain" TRANSACTION_ID = "transaction_id" + TIMEOUTE = "timeout" ROUTE_ROOT = "/" ROUTE_AUTH = "/auth" @@ -32,7 +33,8 @@ var ( HttpPortBind = 9090 ChanRequest = make(chan interface{}, 5) - TimeoutSec = 60 + TimeoutSec = 60 // 1 min + MaxTimeout = 300 // 5 min ) func indexHandler(w http.ResponseWriter, r *http.Request) { @@ -46,8 +48,17 @@ func authHandler(w http.ResponseWriter, r *http.Request) { method := strings.Join(r.Form[METHOD_ACCESS], "") domain := strings.Join(r.Form[DOMAIN_ACCESS], "") transaction := strings.Join(r.Form[TRANSACTION_ID], "") + timeoutStr := strings.Join(r.Form[TIMEOUTE], "") log.Printf("%sAuth %s", LogDebug, jid) + timeout, err := strconv.Atoi(timeoutStr) + if err != nil { + timeout = TimeoutSec + } + if timeout > MaxTimeout { + timeout = MaxTimeout + } + chanAnswer := make(chan bool) ChanRequest <- jid @@ -63,7 +74,7 @@ func authHandler(w http.ResponseWriter, r *http.Request) { } else { w.WriteHeader(http.StatusUnauthorized) } - case <-time.After(time.Duration(TimeoutSec) * time.Second): + case <-time.After(time.Duration(timeout) * time.Second): w.WriteHeader(http.StatusUnauthorized) delete(xmpp.WaitMessageAnswers, transaction) }