2021-11-10 19:06:12 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/dcarrillo/whatismyip/internal/httputils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getHeadersAsSortedString(ctx *gin.Context) {
|
2022-05-02 16:00:36 +00:00
|
|
|
h := httputils.GetHeadersWithoutTrustedHeaders(ctx)
|
|
|
|
h.Set("Host", ctx.Request.Host)
|
2021-11-10 19:06:12 +00:00
|
|
|
|
|
|
|
ctx.String(http.StatusOK, httputils.HeadersToSortedString(h))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHeaderAsString(ctx *gin.Context) {
|
2022-05-02 16:00:36 +00:00
|
|
|
headers := httputils.GetHeadersWithoutTrustedHeaders(ctx)
|
|
|
|
|
2021-11-10 19:06:12 +00:00
|
|
|
h := ctx.Params.ByName("header")
|
2022-05-02 16:00:36 +00:00
|
|
|
if v := headers.Get(ctx.Params.ByName("header")); v != "" {
|
2021-11-10 19:06:12 +00:00
|
|
|
ctx.String(http.StatusOK, template.HTMLEscapeString(v))
|
|
|
|
} else if strings.ToLower(h) == "host" {
|
|
|
|
ctx.String(http.StatusOK, template.HTMLEscapeString(ctx.Request.Host))
|
|
|
|
} else {
|
|
|
|
ctx.String(http.StatusNotFound, http.StatusText(http.StatusNotFound))
|
|
|
|
}
|
|
|
|
}
|