mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-06 22:40:31 -05:00
Proper host/port splitting; also log file perms
This commit is contained in:
parent
da72a5fbcd
commit
09aad777f4
2 changed files with 14 additions and 12 deletions
|
@ -43,7 +43,7 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
} else if outputFile == "stderr" {
|
} else if outputFile == "stderr" {
|
||||||
file = os.Stderr
|
file = os.Stderr
|
||||||
} else {
|
} else {
|
||||||
file, err = os.OpenFile(outputFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
file, err = os.OpenFile(outputFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -13,11 +14,11 @@ import (
|
||||||
// NewReplacer to get one of these.
|
// NewReplacer to get one of these.
|
||||||
type replacer map[string]string
|
type replacer map[string]string
|
||||||
|
|
||||||
// NewReplacer makes a new replacer based on r and rw.
|
// NewReplacer makes a new replacer based on r and rr.
|
||||||
// Do not create a new replacer until r and rw 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, rw *responseRecorder) replacer {
|
func NewReplacer(r *http.Request, rr *responseRecorder) replacer {
|
||||||
rep := replacer{
|
rep := replacer{
|
||||||
"{method}": r.Method,
|
"{method}": r.Method,
|
||||||
"{scheme}": func() string {
|
"{scheme}": func() string {
|
||||||
|
@ -32,24 +33,25 @@ func NewReplacer(r *http.Request, rw *responseRecorder) replacer {
|
||||||
"{fragment}": r.URL.Fragment,
|
"{fragment}": r.URL.Fragment,
|
||||||
"{proto}": r.Proto,
|
"{proto}": r.Proto,
|
||||||
"{remote}": func() string {
|
"{remote}": func() string {
|
||||||
if idx := strings.Index(r.RemoteAddr, ":"); idx > -1 {
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||||
return r.RemoteAddr[:idx] // IP address only
|
if err != nil {
|
||||||
} else {
|
|
||||||
return r.RemoteAddr
|
return r.RemoteAddr
|
||||||
}
|
}
|
||||||
|
return host
|
||||||
}(),
|
}(),
|
||||||
"{port}": func() string {
|
"{port}": func() string {
|
||||||
if idx := strings.Index(r.Host, ":"); idx > -1 {
|
_, port, err := net.SplitHostPort(r.RemoteAddr)
|
||||||
return r.Host[idx+1:] // port only
|
if err != nil {
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
return ""
|
return port
|
||||||
}(),
|
}(),
|
||||||
"{uri}": r.RequestURI,
|
"{uri}": r.RequestURI,
|
||||||
"{when}": func() string {
|
"{when}": func() string {
|
||||||
return time.Now().Format(timeFormat)
|
return time.Now().Format(timeFormat)
|
||||||
}(),
|
}(),
|
||||||
"{status}": strconv.Itoa(rw.status),
|
"{status}": strconv.Itoa(rr.status),
|
||||||
"{size}": strconv.Itoa(rw.size),
|
"{size}": strconv.Itoa(rr.size),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Header placeholders
|
// Header placeholders
|
||||||
|
|
Loading…
Reference in a new issue