summaryrefslogtreecommitdiffstats
path: root/api/webhook.go
diff options
context:
space:
mode:
authorMartin Schenck <martinschenck@fastmail.com>2016-06-10 15:59:24 +0200
committerJoram Wilander <jwawilander@gmail.com>2016-06-10 09:59:24 -0400
commit24a28054565b40770250abe55d1771721ed4da56 (patch)
tree48babea0badf1391c0ab2601b6fbc86381d2623b /api/webhook.go
parent400bc8b46d16b1634504b875f3db212abbf04dfc (diff)
downloadchat-24a28054565b40770250abe55d1771721ed4da56.tar.gz
chat-24a28054565b40770250abe55d1771721ed4da56.tar.bz2
chat-24a28054565b40770250abe55d1771721ed4da56.zip
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
Diffstat (limited to 'api/webhook.go')
-rw-r--r--api/webhook.go26
1 files changed, 23 insertions, 3 deletions
diff --git a/api/webhook.go b/api/webhook.go
index 676fd2cbc..6297133da 100644
--- a/api/webhook.go
+++ b/api/webhook.go
@@ -4,6 +4,7 @@
package api
import (
+ "io"
"net/http"
"strings"
@@ -373,14 +374,33 @@ func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) {
r.ParseForm()
- var parsedRequest *model.IncomingWebhookRequest
+ var payload io.Reader
contentType := r.Header.Get("Content-Type")
if strings.Split(contentType, "; ")[0] == "application/x-www-form-urlencoded" {
- parsedRequest = model.IncomingWebhookRequestFromJson(strings.NewReader(r.FormValue("payload")))
+ payload = strings.NewReader(r.FormValue("payload"))
} else {
- parsedRequest = model.IncomingWebhookRequestFromJson(r.Body)
+ payload = r.Body
+ }
+
+ if utils.Cfg.LogSettings.EnableWebhookDebugging {
+ var err error
+ payload, err = utils.DebugReader(
+ payload,
+ utils.T("api.webhook.incoming.debug"),
+ )
+ if err != nil {
+ c.Err = model.NewLocAppError(
+ "incomingWebhook",
+ "api.webhook.incoming.debug.error",
+ nil,
+ err.Error(),
+ )
+ return
+ }
}
+ parsedRequest := model.IncomingWebhookRequestFromJson(payload)
+
if parsedRequest == nil {
c.Err = model.NewLocAppError("incomingWebhook", "web.incoming_webhook.parse.app_error", nil, "")
return