Skip to content

Commit 7aa4a72

Browse files
committed
Use SplitHostPort and fallback to custom logic on err
1 parent 8cae63d commit 7aa4a72

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

middlewares/http/middleware.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"io"
88
"io/ioutil"
9+
"net"
910
"net/http"
1011
"net/http/httptest"
1112
"strings"
@@ -53,14 +54,18 @@ func GetMiddleware(options *Options) (func(next http.Handler) http.Handler, erro
5354
middleware := func(next http.Handler) http.Handler {
5455
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5556
// Create a LogEntry populated with everything we know right now
57+
ip, _, err := net.SplitHostPort(r.RemoteAddr)
58+
if err != nil {
59+
ip = strings.TrimSuffix(strings.TrimPrefix(r.RemoteAddr[:strings.LastIndex(r.RemoteAddr, ":")], "["), "]")
60+
}
5661
logEntry := logging.LogEntry{
5762
Version: logging.The100Alpha,
5863
DateCreated: time.Now().UnixMilli(),
5964
Request: logging.Request{
6065
HTTPProtocol: logging.HTTPProtocol(r.Proto),
6166
Headers: r.Header,
6267
Method: logging.Method(r.Method),
63-
IP: strings.TrimSuffix(strings.TrimPrefix(r.RemoteAddr[:strings.LastIndex(r.RemoteAddr, ":")], "["), "]"),
68+
IP: ip,
6469
Resource: r.URL.Path, // We'll fill this in later if we have a router
6570
},
6671
}

0 commit comments

Comments
 (0)