Commit 04571ff3 authored by Matthew Holt's avatar Matthew Holt

NewReplacer takes third argument for empty value string

parent 7adff28a
...@@ -33,7 +33,7 @@ func (l Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { ...@@ -33,7 +33,7 @@ func (l Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
} }
status = 0 status = 0
} }
rep := middleware.NewReplacer(r, responseRecorder) rep := middleware.NewReplacer(r, responseRecorder, CommonLogEmptyValue)
rule.Log.Println(rep.Replace(rule.Format)) rule.Log.Println(rep.Replace(rule.Format))
return status, err return status, err
} }
...@@ -50,8 +50,9 @@ type Rule struct { ...@@ -50,8 +50,9 @@ type Rule struct {
} }
const ( const (
DefaultLogFilename = "access.log" DefaultLogFilename = "access.log"
CommonLogFormat = `{remote} ` + middleware.EmptyStringReplacer + ` [{when}] "{method} {uri} {proto}" {status} {size}` CommonLogFormat = `{remote} ` + CommonLogEmptyValue + ` [{when}] "{method} {uri} {proto}" {status} {size}`
CombinedLogFormat = CommonLogFormat + ` "{>Referer}" "{>User-Agent}"` CommonLogEmptyValue = "-"
DefaultLogFormat = CommonLogFormat CombinedLogFormat = CommonLogFormat + ` "{>Referer}" "{>User-Agent}"`
DefaultLogFormat = CommonLogFormat
) )
...@@ -89,7 +89,7 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { ...@@ -89,7 +89,7 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if replacer == nil { if replacer == nil {
rHost := r.Host rHost := r.Host
r.Host = requestHost r.Host = requestHost
replacer = middleware.NewReplacer(r, nil) replacer = middleware.NewReplacer(r, nil, "")
r.Host = rHost r.Host = rHost
} }
for header, values := range host.ExtraHeaders { for header, values := range host.ExtraHeaders {
......
...@@ -16,57 +16,63 @@ type Replacer interface { ...@@ -16,57 +16,63 @@ type Replacer interface {
Replace(string) string Replace(string) string
} }
type replacer map[string]string type replacer struct {
replacements map[string]string
emptyValue string
}
// NewReplacer makes a new replacer based on r and rr. // NewReplacer makes a new replacer based on r and rr.
// Do not create a new replacer until r and rr have all // Do not create a new replacer until r and rr have all
// the needed values, because this function copies those // the needed values, because this function copies those
// values into the replacer. // values into the replacer.
func NewReplacer(r *http.Request, rr *responseRecorder) Replacer { func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Replacer {
rep := replacer{ rep := replacer{
"{method}": r.Method, replacements: map[string]string{
"{scheme}": func() string { "{method}": r.Method,
if r.TLS != nil { "{scheme}": func() string {
return "https" if r.TLS != nil {
} return "https"
return "http" }
}(), return "http"
"{host}": r.Host, }(),
"{path}": r.URL.Path, "{host}": r.Host,
"{query}": r.URL.RawQuery, "{path}": r.URL.Path,
"{fragment}": r.URL.Fragment, "{query}": r.URL.RawQuery,
"{proto}": r.Proto, "{fragment}": r.URL.Fragment,
"{remote}": func() string { "{proto}": r.Proto,
if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" { "{remote}": func() string {
return fwdFor if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" {
} return fwdFor
host, _, err := net.SplitHostPort(r.RemoteAddr) }
if err != nil { host, _, err := net.SplitHostPort(r.RemoteAddr)
return r.RemoteAddr if err != nil {
} return r.RemoteAddr
return host }
}(), return host
"{port}": func() string { }(),
_, port, err := net.SplitHostPort(r.RemoteAddr) "{port}": func() string {
if err != nil { _, port, err := net.SplitHostPort(r.RemoteAddr)
return "" if err != nil {
} return ""
return port }
}(), return port
"{uri}": r.RequestURI, }(),
"{when}": func() string { "{uri}": r.URL.RequestURI(),
return time.Now().Format(timeFormat) "{when}": func() string {
}(), return time.Now().Format(timeFormat)
}(),
},
emptyValue: emptyValue,
} }
if rr != nil { if rr != nil {
rep["{status}"] = strconv.Itoa(rr.status) rep.replacements["{status}"] = strconv.Itoa(rr.status)
rep["{size}"] = strconv.Itoa(rr.size) rep.replacements["{size}"] = strconv.Itoa(rr.size)
rep["{latency}"] = time.Since(rr.start).String() rep.replacements["{latency}"] = time.Since(rr.start).String()
} }
// Header placeholders // Header placeholders
for header, val := range r.Header { for header, val := range r.Header {
rep[headerReplacer+header+"}"] = strings.Join(val, ",") rep.replacements[headerReplacer+header+"}"] = strings.Join(val, ",")
} }
return rep return rep
...@@ -75,9 +81,9 @@ func NewReplacer(r *http.Request, rr *responseRecorder) Replacer { ...@@ -75,9 +81,9 @@ func NewReplacer(r *http.Request, rr *responseRecorder) Replacer {
// Replace performs a replacement of values on s and returns // Replace performs a replacement of values on s and returns
// the string with the replaced values. // the string with the replaced values.
func (r replacer) Replace(s string) string { func (r replacer) Replace(s string) string {
for placeholder, replacement := range r { for placeholder, replacement := range r.replacements {
if replacement == "" { if replacement == "" {
replacement = EmptyStringReplacer replacement = r.emptyValue
} }
s = strings.Replace(s, placeholder, replacement, -1) s = strings.Replace(s, placeholder, replacement, -1)
} }
...@@ -88,7 +94,7 @@ func (r replacer) Replace(s string) string { ...@@ -88,7 +94,7 @@ func (r replacer) Replace(s string) string {
endOffset := idxStart + len(headerReplacer) endOffset := idxStart + len(headerReplacer)
idxEnd := strings.Index(s[endOffset:], "}") idxEnd := strings.Index(s[endOffset:], "}")
if idxEnd > -1 { if idxEnd > -1 {
s = s[:idxStart] + EmptyStringReplacer + s[endOffset+idxEnd+1:] s = s[:idxStart] + r.emptyValue + s[endOffset+idxEnd+1:]
} else { } else {
break break
} }
...@@ -97,7 +103,6 @@ func (r replacer) Replace(s string) string { ...@@ -97,7 +103,6 @@ func (r replacer) Replace(s string) string {
} }
const ( const (
timeFormat = "02/Jan/2006:15:04:05 -0700" timeFormat = "02/Jan/2006:15:04:05 -0700"
headerReplacer = "{>" headerReplacer = "{>"
EmptyStringReplacer = "-"
) )
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment