Fix memory leak
This commit is contained in:
parent
731183c637
commit
5ae82dfff6
23
http/http.go
23
http/http.go
|
|
@ -74,11 +74,13 @@ func authHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
chanAnswer := make(chan string)
|
||||
|
||||
ChanRequest <- jid
|
||||
ChanRequest <- method
|
||||
ChanRequest <- domain
|
||||
ChanRequest <- transaction
|
||||
ChanRequest <- chanAnswer
|
||||
client := new(xmpp.Client)
|
||||
client.JID = jid
|
||||
client.Method = method
|
||||
client.Domain = domain
|
||||
client.Transaction = transaction
|
||||
client.ChanReply = chanAnswer
|
||||
client.QueryClient()
|
||||
|
||||
select {
|
||||
case answer := <-chanAnswer:
|
||||
|
|
@ -97,7 +99,16 @@ func authHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
case <-time.After(time.Duration(timeout) * time.Second):
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
delete(xmpp.WaitMessageAnswers, transaction)
|
||||
}
|
||||
|
||||
switch client.TypeSend {
|
||||
case xmpp.TYPE_SEND_IQ:
|
||||
log.Printf("%sDelete IQ", LogDebug)
|
||||
delete(xmpp.WaitIqMessages, client.IdMap)
|
||||
|
||||
case xmpp.TYPE_SEND_MESSAGE:
|
||||
log.Printf("%sDelete Message", LogDebug)
|
||||
delete(xmpp.WaitMessageAnswers, client.IdMap)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
23
main.go
23
main.go
|
|
@ -15,7 +15,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
Version = "v0.3"
|
||||
Version = "v0.3.1"
|
||||
configurationFilePath = "httpAuth.cfg"
|
||||
|
||||
default_xmpp_server_address = "127.0.0.1"
|
||||
|
|
@ -69,24 +69,6 @@ func init() {
|
|||
xmpp.Debug = mapConfig["xmpp_debug"] == "true"
|
||||
}
|
||||
|
||||
func request() {
|
||||
for {
|
||||
client := new(xmpp.Client)
|
||||
|
||||
client.JID = getChanString(http.ChanRequest)
|
||||
client.Method = getChanString(http.ChanRequest)
|
||||
client.Domain = getChanString(http.ChanRequest)
|
||||
client.Transaction = getChanString(http.ChanRequest)
|
||||
|
||||
chanResult := <-http.ChanRequest
|
||||
if v, ok := chanResult.(chan string); ok {
|
||||
client.ChanReply = v
|
||||
}
|
||||
|
||||
go client.QueryClient()
|
||||
}
|
||||
}
|
||||
|
||||
func getChanString(c chan interface{}) string {
|
||||
ret := ""
|
||||
i := <-c
|
||||
|
|
@ -100,7 +82,6 @@ func main() {
|
|||
|
||||
go http.Run()
|
||||
go xmpp.Run()
|
||||
go request()
|
||||
|
||||
sigchan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigchan, os.Interrupt)
|
||||
|
|
@ -108,8 +89,6 @@ func main() {
|
|||
signal.Notify(sigchan, os.Kill)
|
||||
<-sigchan
|
||||
|
||||
// TODO close all ressources
|
||||
|
||||
log.Println("Exit main()")
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,9 @@ const (
|
|||
REPLY_UNREACHABLE = "reply_unreachable"
|
||||
REPLY_DENY = "reply_deny"
|
||||
REPLY_OK = "reply_ok"
|
||||
|
||||
TYPE_SEND_MESSAGE = "type_send_message"
|
||||
TYPE_SEND_IQ = "type_send_iq"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
|
|
@ -22,6 +25,9 @@ type Client struct {
|
|||
Domain string
|
||||
Transaction string
|
||||
|
||||
TypeSend string
|
||||
IdMap string
|
||||
|
||||
ChanReply chan string
|
||||
}
|
||||
|
||||
|
|
@ -30,7 +36,7 @@ func (client *Client) QueryClient() {
|
|||
isAutoGeneratedTranctionID := false
|
||||
if client.Transaction == "" {
|
||||
// Random transaction ID generation
|
||||
client.Transaction = xmpp.SessionID()
|
||||
client.Transaction = TransactionID()
|
||||
isAutoGeneratedTranctionID = true
|
||||
}
|
||||
clientJID, _ := xmpp.ParseJID(client.JID)
|
||||
|
|
@ -49,6 +55,9 @@ func (client *Client) askViaIQ() {
|
|||
m.PayloadEncode(confirm)
|
||||
WaitIqMessages[stanzaIDstr] = client
|
||||
comp.Out <- m
|
||||
|
||||
client.TypeSend = TYPE_SEND_IQ
|
||||
client.IdMap = stanzaIDstr
|
||||
}
|
||||
|
||||
func (client *Client) askViaMessage(isAutoGeneratedTranctionID bool) {
|
||||
|
|
@ -67,9 +76,12 @@ func (client *Client) askViaMessage(isAutoGeneratedTranctionID bool) {
|
|||
log.Printf("%sSend message %v", LogInfo, m)
|
||||
WaitMessageAnswers[client.Transaction] = client
|
||||
comp.Out <- m
|
||||
|
||||
client.TypeSend = TYPE_SEND_MESSAGE
|
||||
client.IdMap = client.Transaction
|
||||
}
|
||||
|
||||
func SessionID() string {
|
||||
func TransactionID() string {
|
||||
var bytes = make([]byte, 8)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
panic(err)
|
||||
|
|
|
|||
|
|
@ -55,7 +55,6 @@ func mainXMPP() {
|
|||
confirm := v.Confir
|
||||
if confirm != nil {
|
||||
client := WaitMessageAnswers[confirm.Id]
|
||||
delete(WaitMessageAnswers, confirm.Id)
|
||||
processConfirm(v, client)
|
||||
} else {
|
||||
// If body is the confirmation id, it will be considerated as accepted.
|
||||
|
|
@ -63,7 +62,6 @@ func mainXMPP() {
|
|||
client := WaitMessageAnswers[v.Body]
|
||||
jidFrom, _ := xmpp.ParseJID(v.From)
|
||||
if client != nil && client.JID == jidFrom.Bare() {
|
||||
delete(WaitMessageAnswers, v.Body)
|
||||
processConfirm(v, client)
|
||||
}
|
||||
}
|
||||
|
|
@ -88,13 +86,11 @@ func mainXMPP() {
|
|||
confirm := &xmpp.Confirm{}
|
||||
v.PayloadDecode(confirm)
|
||||
client := WaitIqMessages[v.Id]
|
||||
delete(WaitIqMessages, v.Id)
|
||||
processConfirm(v, client)
|
||||
|
||||
default:
|
||||
// Handle reply iq that doesn't contain HTTP-Auth namespace
|
||||
client := WaitIqMessages[v.Id]
|
||||
delete(WaitIqMessages, v.Id)
|
||||
processConfirm(v, client)
|
||||
|
||||
if client == nil {
|
||||
|
|
|
|||
Loading…
Reference in New Issue