From 558f9d602965929a1f2ad026dbeec646c9ab580f Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Fri, 17 Mar 2017 23:43:20 +0100 Subject: [PATCH] Reorganize code to be reusable. --- http/http.go | 52 ++++++++++---------------------------------- main.go | 4 ++-- xmpp/confirmation.go | 45 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 43 deletions(-) diff --git a/http/http.go b/http/http.go index 95a4520..e6297a8 100644 --- a/http/http.go +++ b/http/http.go @@ -42,8 +42,6 @@ var ( KeyPath = "./key.pem" ChanRequest = make(chan interface{}, 5) - TimeoutSec = 60 // 1 min - MaxTimeout = 300 // 5 min BindAddressIPv4 = "127.0.0.1" BindAddressIPv6 = "[::1]" @@ -72,52 +70,24 @@ func authHandler(w http.ResponseWriter, r *http.Request) { } timeoutStr := strings.Join(r.Form[TIMEOUTE], "") - log.Printf("%sAuth %s", LogInfo, jid) timeout, err := strconv.Atoi(timeoutStr) - if err != nil || timeout <= 0 { - timeout = TimeoutSec - } - if timeout > MaxTimeout { - timeout = MaxTimeout + if err != nil { + timeout = 0 } - chanAnswer := make(chan string) + answer := xmpp.Confirm(jid, method, domain, transaction, timeout) + switch answer { + case xmpp.REPLY_OK: + w.WriteHeader(http.StatusOK) - confirmation := new(xmpp.Confirmation) - confirmation.JID = jid - confirmation.Method = method - confirmation.Domain = domain - confirmation.Transaction = transaction - confirmation.ChanReply = chanAnswer - confirmation.SendConfirmation() - - select { - case answer := <-chanAnswer: - switch answer { - case xmpp.REPLY_OK: - w.WriteHeader(http.StatusOK) - - case xmpp.REPLY_DENY: - w.WriteHeader(http.StatusUnauthorized) - - case xmpp.REPLY_UNREACHABLE: - w.WriteHeader(StatusUnreachable) - - default: - w.WriteHeader(StatusUnknownError) - } - case <-time.After(time.Duration(timeout) * time.Second): + case xmpp.REPLY_DENY: w.WriteHeader(http.StatusUnauthorized) - } - switch confirmation.TypeSend { - case xmpp.TYPE_SEND_IQ: - log.Printf("%sDelete IQ", LogDebug) - delete(xmpp.WaitIqMessages, confirmation.IdMap) + case xmpp.REPLY_UNREACHABLE: + w.WriteHeader(StatusUnreachable) - case xmpp.TYPE_SEND_MESSAGE: - log.Printf("%sDelete Message", LogDebug) - delete(xmpp.WaitMessageAnswers, confirmation.IdMap) + default: + w.WriteHeader(StatusUnknownError) } } diff --git a/main.go b/main.go index 5c0703c..64a9ad5 100644 --- a/main.go +++ b/main.go @@ -37,9 +37,9 @@ func init() { // HTTP config httpTimeout, err := strconv.Atoi(mapConfig["http_timeout_sec"]) - if err == nil && httpTimeout > 0 && httpTimeout < http.MaxTimeout { + if err == nil && httpTimeout > 0 && httpTimeout < xmpp.MaxTimeout { log.Println("Define HTTP timeout to " + strconv.Itoa(httpTimeout) + " second") - http.TimeoutSec = httpTimeout + xmpp.TimeoutSec = httpTimeout } httpPort, err := strconv.Atoi(mapConfig["http_port"]) if err == nil { diff --git a/xmpp/confirmation.go b/xmpp/confirmation.go index a7b2ffb..957a6f4 100644 --- a/xmpp/confirmation.go +++ b/xmpp/confirmation.go @@ -6,12 +6,14 @@ import ( "log" "strconv" "strings" + "time" ) const ( REPLY_UNREACHABLE = "reply_unreachable" REPLY_DENY = "reply_deny" REPLY_OK = "reply_ok" + REPLY_TIMEOUT = "reply_timeout" TYPE_SEND_MESSAGE = "type_send_message" TYPE_SEND_IQ = "type_send_iq" @@ -24,6 +26,9 @@ const ( var ( MapLangs = make(map[string]string) + + TimeoutSec = 60 // 1 min + MaxTimeout = 300 // 5 min ) type Confirmation struct { @@ -38,6 +43,46 @@ type Confirmation struct { ChanReply chan string } +func Confirm(jid, method, domain, transaction string, timeout int) string { + // TODO check param validity + ret := "" + log.Printf("%sAuth %s", LogInfo, jid) + chanAnswer := make(chan string) + + if timeout <= 0 { + timeout = TimeoutSec + } + if timeout > MaxTimeout { + timeout = MaxTimeout + } + + confirmation := new(Confirmation) + confirmation.JID = jid + confirmation.Method = method + confirmation.Domain = domain + confirmation.Transaction = transaction + confirmation.ChanReply = chanAnswer + confirmation.SendConfirmation() + + select { + case answer := <-chanAnswer: + ret = answer + case <-time.After(time.Duration(timeout) * time.Second): + ret = REPLY_TIMEOUT + } + + switch confirmation.TypeSend { + case TYPE_SEND_IQ: + log.Printf("%sDelete IQ", LogDebug) + delete(WaitIqMessages, confirmation.IdMap) + + case TYPE_SEND_MESSAGE: + log.Printf("%sDelete Message", LogDebug) + delete(WaitMessageAnswers, confirmation.IdMap) + } + return ret +} + func (confirmation *Confirmation) SendConfirmation() { log.Printf("%sQuery JID %s", LogInfo, confirmation.JID) clientJID, _ := xmpp.ParseJID(confirmation.JID)