Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
appsec: add examples & comments for MonitorParsedHTTPBody
- Loading branch information
Showing
3 changed files
with
60 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2022 Datadog, Inc. | ||
|
||
package appsec_test | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/appsec" | ||
echotrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/labstack/echo.v4" | ||
httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http" | ||
) | ||
|
||
func customBodyParser(body io.ReadCloser) string { | ||
b, err := ioutil.ReadAll(body) | ||
if err != nil { | ||
return "Ill formed http body" | ||
} | ||
return string(b) | ||
} | ||
|
||
// Monitor HTTP request parsed body | ||
func ExampleMonitorParsedHTTPBody() { | ||
mux := httptrace.NewServeMux() | ||
mux.HandleFunc("/body", func(w http.ResponseWriter, r *http.Request) { | ||
// Use the SDK to monitor the request's parsed body | ||
appsec.MonitorParsedHTTPBody(r.Context(), customBodyParser(r.Body)) | ||
w.Write([]byte("Body monitored using AppSec SDK\n")) | ||
}) | ||
http.ListenAndServe(":8080", mux) | ||
} | ||
|
||
// Monitor HTTP request parsed body with a framework customized context type | ||
func ExampleMonitorParsedHTTPBodyCustomContext() { | ||
r := echo.New() | ||
r.Use(echotrace.Middleware()) | ||
r.POST("/body", func(c echo.Context) error { | ||
// Use the SDK to monitor the request's parsed body | ||
appsec.MonitorParsedHTTPBody(c.Request().Context(), customBodyParser(c.Request().Body)) | ||
return c.String(http.StatusOK, "Body monitored using AppSec SDK") | ||
}) | ||
|
||
r.Start(":8080") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters