Add timeout configuration in HTTP request

This commit is contained in:
chteufleur 2016-07-10 23:08:10 +02:00
parent e6cd478d81
commit f04b3321fa
2 changed files with 15 additions and 3 deletions

View File

@ -36,10 +36,11 @@ To ask authorization, just send an HTTP request to the path ``/auth`` with param
* domain : Domain you want to access * domain : Domain you want to access
* method : Method you access the domain * method : Method you access the domain
* transaction_id : Transaction identifier * transaction_id : Transaction identifier
* timeout : Timeout of the request in second (default : 60, max : 300)
Example: 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. 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.

View File

@ -20,6 +20,7 @@ const (
METHOD_ACCESS = "method" METHOD_ACCESS = "method"
DOMAIN_ACCESS = "domain" DOMAIN_ACCESS = "domain"
TRANSACTION_ID = "transaction_id" TRANSACTION_ID = "transaction_id"
TIMEOUTE = "timeout"
ROUTE_ROOT = "/" ROUTE_ROOT = "/"
ROUTE_AUTH = "/auth" ROUTE_AUTH = "/auth"
@ -32,7 +33,8 @@ var (
HttpPortBind = 9090 HttpPortBind = 9090
ChanRequest = make(chan interface{}, 5) ChanRequest = make(chan interface{}, 5)
TimeoutSec = 60 TimeoutSec = 60 // 1 min
MaxTimeout = 300 // 5 min
) )
func indexHandler(w http.ResponseWriter, r *http.Request) { 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], "") method := strings.Join(r.Form[METHOD_ACCESS], "")
domain := strings.Join(r.Form[DOMAIN_ACCESS], "") domain := strings.Join(r.Form[DOMAIN_ACCESS], "")
transaction := strings.Join(r.Form[TRANSACTION_ID], "") transaction := strings.Join(r.Form[TRANSACTION_ID], "")
timeoutStr := strings.Join(r.Form[TIMEOUTE], "")
log.Printf("%sAuth %s", LogDebug, jid) 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) chanAnswer := make(chan bool)
ChanRequest <- jid ChanRequest <- jid
@ -63,7 +74,7 @@ func authHandler(w http.ResponseWriter, r *http.Request) {
} else { } else {
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)
} }
case <-time.After(time.Duration(TimeoutSec) * time.Second): case <-time.After(time.Duration(timeout) * time.Second):
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)
delete(xmpp.WaitMessageAnswers, transaction) delete(xmpp.WaitMessageAnswers, transaction)
} }