danielgross / whatsapp-gpt

MIT License
3.07k stars 596 forks source link

go/connector: redundant HTTP body reading and leaking body #15

Open odeke-em opened 1 year ago

odeke-em commented 1 year ago

If we look at this code https://github.com/danielgross/whatsapp-gpt/blob/d12693276e1e88d5d335276aa42bcfd849d56023/main.go#L52-L57

there is unnecessary process with the bytes.Buffer.ReadFrom then buf.String() then string(newMsg) but really it can be trivially done with

newMsg, err := io.ReadAll(resp.Body)
if err != nil {
    ...
    return
}
response := &waProto.Message{Conversation: proto.String(string(newMsg))}

and even more, resp.Body is never closed. We need a defer resp.Body.Close() or resp.Body.Close() right after the read.