From 24a28054565b40770250abe55d1771721ed4da56 Mon Sep 17 00:00:00 2001 From: Martin Schenck Date: Fri, 10 Jun 2016 15:59:24 +0200 Subject: PLT-2058 Debugging incoming web hook content (#3150) * PLT-2058 Debugging incoming web hook content This change debugs contents of incoming webhooks using l4g. The problem is that in order to debug the request body, it neads to be read. And a Reader can only be read once. Hence, the body is only read for Debugging if it is actually enabled. Furthermore, a new reader is created from the content of the old reader in order for the rest of the method to work as usual (with or without debugging). The debug statement is wrapped in a closure, so that the content is only copied if Debug is actually enabled. It is not possible to return `(string, string)` from the closure to `l4g.Debug()`. That is the reason the debugging is not done with `=%v`, but the translations strings end with a space. I tested the change with a `application/json` HTTP header as well as `payload=` The debug method is extracted into util/log.go in order to be re-usable for debugging `io.Reader` * Added a config flag to turn off incoming webhook debugging Setting `EnableWebhookDebugging` to false in the `config.json` will disable the printing of the content of incoming webhooks to the console * Defaulting webhook debugging to true * Added the setting of debugging incoming webhooks to the system console --- utils/log.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 utils/log.go (limited to 'utils/log.go') diff --git a/utils/log.go b/utils/log.go new file mode 100644 index 000000000..360c785d0 --- /dev/null +++ b/utils/log.go @@ -0,0 +1,33 @@ +// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package utils + +import ( + "bytes" + "io" + "io/ioutil" + + l4g "github.com/alecthomas/log4go" +) + +// DebugReader logs the content of the io.Reader and returns a new io.Reader +// with the same content as the received io.Reader. +// If you pass reader by reference, it won't be re-created unless the loglevel +// includes Debug. +// If an error is returned, the reader is consumed an cannot be read again. +func DebugReader(reader io.Reader, message string) (io.Reader, error) { + var err error + l4g.Debug(func() string { + content, err := ioutil.ReadAll(reader) + if err != nil { + return "" + } + + reader = bytes.NewReader(content) + + return message + string(content) + }) + + return reader, err +} -- cgit v1.2.3-1-g7c22