Permalink
Browse files

Better IP address parsing for the host.

In particular, don't show [...] around IPv6 addresses.

Fixes #1.
  • Loading branch information...
1 parent ebdfb8b commit b0b32c6ee2e0e0cf29cb75d6d76c0ef3fc8d9b06 @cespare committed Feb 26, 2013
Showing with 15 additions and 7 deletions.
  1. +15 −7 apachelog.go
View
@@ -24,8 +24,8 @@ package apachelog
import (
"fmt"
"io"
+ "net"
"net/http"
- "strings"
"time"
)
@@ -82,14 +82,9 @@ func NewHandler(handler http.Handler, out io.Writer) *Handler {
// This delegates to the underlying handler's ServeHTTP method and writes one log line for every call.
func (h *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
- clientIP := r.RemoteAddr
- if colon := strings.LastIndex(clientIP, ":"); colon != -1 {
- clientIP = clientIP[:colon]
- }
-
record := &Record{
ResponseWriter: rw,
- ip: clientIP,
+ ip: getIP(r.RemoteAddr),
time: time.Time{},
method: r.Method,
uri: r.RequestURI,
@@ -107,3 +102,16 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
record.Log(h.out)
}
+
+// A best-effort attempt at getting the IP from http.Request.RemoteAddr. For a Go server, they typically look
+// like this:
+// 127.0.0.1:36341
+// [::1]:44092
+// I think this is standard for IPv4 and IPv6 addresses.
+func getIP(remoteAddr string) string {
+ host, _, err := net.SplitHostPort(remoteAddr)
+ if err != nil {
+ return remoteAddr
+ }
+ return host
+}

0 comments on commit b0b32c6

Please sign in to comment.