mirror of
https://github.com/dcarrillo/whatismyip.git
synced 2025-07-01 19:49:27 +00:00
first commit
This commit is contained in:
66
internal/httputils/http.go
Normal file
66
internal/httputils/http.go
Normal file
@ -0,0 +1,66 @@
|
||||
package httputils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func HeadersToSortedString(headers http.Header) string {
|
||||
var output string
|
||||
|
||||
keys := make([]string, 0, len(headers))
|
||||
for k := range headers {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, k := range keys {
|
||||
if len(headers[k]) > 1 {
|
||||
for _, h := range headers[k] {
|
||||
output += k + ": " + h + "\n"
|
||||
}
|
||||
} else {
|
||||
output += k + ": " + headers[k][0] + "\n"
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func GetLogFormatter(param gin.LogFormatterParams) string {
|
||||
return fmt.Sprintf("%s - [%s] \"%s %s %s\" %d %d %d %s \"%s\" \"%s\" \"%s\"\n",
|
||||
param.ClientIP,
|
||||
param.TimeStamp.Format("02/Nov/2006:15:04:05 -0700"),
|
||||
param.Method,
|
||||
param.Path,
|
||||
param.Request.Proto,
|
||||
param.StatusCode,
|
||||
param.BodySize,
|
||||
param.Latency.Nanoseconds(),
|
||||
normalizeLog(param.Request.Referer()),
|
||||
normalizeLog(param.Request.UserAgent()),
|
||||
normalizeLog(param.Request.Header["X-Forwarded-For"]),
|
||||
normalizeLog(param.ErrorMessage),
|
||||
)
|
||||
}
|
||||
|
||||
func normalizeLog(log interface{}) interface{} {
|
||||
switch v := log.(type) {
|
||||
case string:
|
||||
if v == "" {
|
||||
return "-"
|
||||
}
|
||||
case []string:
|
||||
if len(v) == 0 {
|
||||
return "-"
|
||||
} else {
|
||||
return strings.Join(v, ", ")
|
||||
}
|
||||
}
|
||||
|
||||
return log
|
||||
}
|
61
internal/httputils/http_test.go
Normal file
61
internal/httputils/http_test.go
Normal file
@ -0,0 +1,61 @@
|
||||
package httputils
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestHeaderToString(t *testing.T) {
|
||||
expected := `Header1: One
|
||||
Header2: 2
|
||||
Header2: Two
|
||||
Header3: Three
|
||||
`
|
||||
header := http.Header{
|
||||
"Header2": []string{"2", "Two"},
|
||||
"Header1": []string{"One"},
|
||||
"Header3": []string{"Three"},
|
||||
}
|
||||
|
||||
assert.Equal(t, expected, HeadersToSortedString(header))
|
||||
}
|
||||
|
||||
func TestGetLogFormatter(t *testing.T) {
|
||||
expected := "127.0.0.1 - [01/Nov/0001:00:00:00 +0000] \"GET / HTTP/1.1\" 200 100 1000 local \"golang test 1.0\" \"1.1.1.1, 2.2.2.2\" \"-\"\n"
|
||||
|
||||
h := http.Header{}
|
||||
h.Set("User-Agent", "golang test 1.0")
|
||||
h.Set("Referer", "local")
|
||||
h.Set("X-Forwarded-For", "1.1.1.1, 2.2.2.2")
|
||||
|
||||
r := http.Request{
|
||||
Proto: "HTTP/1.1",
|
||||
Header: h,
|
||||
}
|
||||
|
||||
p := gin.LogFormatterParams{
|
||||
ClientIP: "127.0.0.1",
|
||||
TimeStamp: time.Time{},
|
||||
Method: "GET",
|
||||
Path: "/",
|
||||
StatusCode: 200,
|
||||
BodySize: 100,
|
||||
Latency: 1000,
|
||||
ErrorMessage: "",
|
||||
Request: &r,
|
||||
}
|
||||
|
||||
assert.Equal(t, expected, GetLogFormatter(p))
|
||||
}
|
||||
|
||||
func TestNormalizeLog(t *testing.T) {
|
||||
assert.Equal(t, "-", normalizeLog(""))
|
||||
assert.Equal(t, "string", normalizeLog("string"))
|
||||
assert.Equal(t, "-", normalizeLog([]string{}))
|
||||
assert.Equal(t, "1.1.1.1", normalizeLog([]string{"1.1.1.1"}))
|
||||
assert.Equal(t, "1.1.1.1, 2.2.2.2", normalizeLog([]string{"1.1.1.1", "2.2.2.2"}))
|
||||
}
|
Reference in New Issue
Block a user