117
vendor/github.com/mholt/caddy/caddyhttp/log/log.go
generated
vendored
Normal file
117
vendor/github.com/mholt/caddy/caddyhttp/log/log.go
generated
vendored
Normal file
@@ -0,0 +1,117 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package log implements request (access) logging middleware.
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterPlugin("log", caddy.Plugin{
|
||||
ServerType: "http",
|
||||
Action: setup,
|
||||
})
|
||||
}
|
||||
|
||||
// Logger is a basic request logging middleware.
|
||||
type Logger struct {
|
||||
Next httpserver.Handler
|
||||
Rules []*Rule
|
||||
ErrorFunc func(http.ResponseWriter, *http.Request, int) // failover error handler
|
||||
}
|
||||
|
||||
func (l Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
for _, rule := range l.Rules {
|
||||
if httpserver.Path(r.URL.Path).Matches(rule.PathScope) {
|
||||
// Record the response
|
||||
responseRecorder := httpserver.NewResponseRecorder(w)
|
||||
|
||||
// Attach the Replacer we'll use so that other middlewares can
|
||||
// set their own placeholders if they want to.
|
||||
rep := httpserver.NewReplacer(r, responseRecorder, CommonLogEmptyValue)
|
||||
responseRecorder.Replacer = rep
|
||||
|
||||
// Bon voyage, request!
|
||||
status, err := l.Next.ServeHTTP(responseRecorder, r)
|
||||
|
||||
if status >= 400 {
|
||||
// There was an error up the chain, but no response has been written yet.
|
||||
// The error must be handled here so the log entry will record the response size.
|
||||
if l.ErrorFunc != nil {
|
||||
l.ErrorFunc(responseRecorder, r, status)
|
||||
} else {
|
||||
// Default failover error handler
|
||||
responseRecorder.WriteHeader(status)
|
||||
fmt.Fprintf(responseRecorder, "%d %s", status, http.StatusText(status))
|
||||
}
|
||||
status = 0
|
||||
}
|
||||
|
||||
// Write log entries
|
||||
for _, e := range rule.Entries {
|
||||
// Check if there is an exception to prevent log being written
|
||||
if !e.Log.ShouldLog(r.URL.Path) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Mask IP Address
|
||||
if e.Log.IPMaskExists {
|
||||
hostip, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err == nil {
|
||||
maskedIP := e.Log.MaskIP(hostip)
|
||||
// Overwrite log value with Masked version
|
||||
rep.Set("remote", maskedIP)
|
||||
}
|
||||
}
|
||||
e.Log.Println(rep.Replace(e.Format))
|
||||
|
||||
}
|
||||
|
||||
return status, err
|
||||
}
|
||||
}
|
||||
return l.Next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// Entry represents a log entry under a path scope
|
||||
type Entry struct {
|
||||
Format string
|
||||
Log *httpserver.Logger
|
||||
}
|
||||
|
||||
// Rule configures the logging middleware.
|
||||
type Rule struct {
|
||||
PathScope string
|
||||
Entries []*Entry
|
||||
}
|
||||
|
||||
const (
|
||||
// DefaultLogFilename is the default log filename.
|
||||
DefaultLogFilename = "access.log"
|
||||
// CommonLogFormat is the common log format.
|
||||
CommonLogFormat = `{remote} ` + CommonLogEmptyValue + ` {user} [{when}] "{method} {uri} {proto}" {status} {size}`
|
||||
// CommonLogEmptyValue is the common empty log value.
|
||||
CommonLogEmptyValue = "-"
|
||||
// CombinedLogFormat is the combined log format.
|
||||
CombinedLogFormat = CommonLogFormat + ` "{>Referer}" "{>User-Agent}"`
|
||||
// DefaultLogFormat is the default log format.
|
||||
DefaultLogFormat = CommonLogFormat
|
||||
)
|
||||
172
vendor/github.com/mholt/caddy/caddyhttp/log/setup.go
generated
vendored
Normal file
172
vendor/github.com/mholt/caddy/caddyhttp/log/setup.go
generated
vendored
Normal file
@@ -0,0 +1,172 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package log
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
// setup sets up the logging middleware.
|
||||
func setup(c *caddy.Controller) error {
|
||||
rules, err := logParse(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, rule := range rules {
|
||||
for _, entry := range rule.Entries {
|
||||
entry.Log.Attach(c)
|
||||
}
|
||||
}
|
||||
|
||||
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
||||
return Logger{Next: next, Rules: rules, ErrorFunc: httpserver.DefaultErrorFunc}
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func logParse(c *caddy.Controller) ([]*Rule, error) {
|
||||
var rules []*Rule
|
||||
var logExceptions []string
|
||||
for c.Next() {
|
||||
args := c.RemainingArgs()
|
||||
|
||||
ip4Mask := net.IPMask(net.ParseIP(DefaultIP4Mask).To4())
|
||||
ip6Mask := net.IPMask(net.ParseIP(DefaultIP6Mask))
|
||||
ipMaskExists := false
|
||||
|
||||
var logRoller *httpserver.LogRoller
|
||||
logRoller = httpserver.DefaultLogRoller()
|
||||
|
||||
for c.NextBlock() {
|
||||
what := c.Val()
|
||||
where := c.RemainingArgs()
|
||||
|
||||
if what == "ipmask" {
|
||||
|
||||
if len(where) == 0 {
|
||||
return nil, c.ArgErr()
|
||||
}
|
||||
|
||||
if where[0] != "" {
|
||||
ip4MaskStr := where[0]
|
||||
ipv4 := net.ParseIP(ip4MaskStr).To4()
|
||||
|
||||
if ipv4 == nil {
|
||||
return nil, c.Err("IPv4 Mask not valid IP Mask Format")
|
||||
} else {
|
||||
ip4Mask = net.IPMask(ipv4)
|
||||
ipMaskExists = true
|
||||
}
|
||||
}
|
||||
|
||||
if len(where) > 1 {
|
||||
|
||||
ip6MaskStr := where[1]
|
||||
ipv6 := net.ParseIP(ip6MaskStr)
|
||||
|
||||
if ipv6 == nil {
|
||||
return nil, c.Err("IPv6 Mask not valid IP Mask Format")
|
||||
} else {
|
||||
ip6Mask = net.IPMask(ipv6)
|
||||
ipMaskExists = true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else if what == "except" {
|
||||
|
||||
for i := 0; i < len(where); i++ {
|
||||
logExceptions = append(logExceptions, where[i])
|
||||
}
|
||||
|
||||
} else if httpserver.IsLogRollerSubdirective(what) {
|
||||
|
||||
if err := httpserver.ParseRoller(logRoller, what, where...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
} else {
|
||||
return nil, c.ArgErr()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
path := "/"
|
||||
format := DefaultLogFormat
|
||||
output := DefaultLogFilename
|
||||
|
||||
switch len(args) {
|
||||
case 0:
|
||||
// nothing to change
|
||||
case 1:
|
||||
// Only an output file specified
|
||||
output = args[0]
|
||||
case 2, 3:
|
||||
// Path scope, output file, and maybe a format specified
|
||||
path = args[0]
|
||||
output = args[1]
|
||||
if len(args) > 2 {
|
||||
format = strings.Replace(args[2], "{common}", CommonLogFormat, -1)
|
||||
format = strings.Replace(format, "{combined}", CombinedLogFormat, -1)
|
||||
}
|
||||
default:
|
||||
// Maximum number of args in log directive is 3.
|
||||
return nil, c.ArgErr()
|
||||
}
|
||||
|
||||
rules = appendEntry(rules, path, &Entry{
|
||||
Log: &httpserver.Logger{
|
||||
Output: output,
|
||||
Roller: logRoller,
|
||||
V4ipMask: ip4Mask,
|
||||
V6ipMask: ip6Mask,
|
||||
IPMaskExists: ipMaskExists,
|
||||
Exceptions: logExceptions,
|
||||
},
|
||||
Format: format,
|
||||
})
|
||||
}
|
||||
|
||||
return rules, nil
|
||||
}
|
||||
|
||||
func appendEntry(rules []*Rule, pathScope string, entry *Entry) []*Rule {
|
||||
for _, rule := range rules {
|
||||
if rule.PathScope == pathScope {
|
||||
rule.Entries = append(rule.Entries, entry)
|
||||
return rules
|
||||
}
|
||||
}
|
||||
|
||||
rules = append(rules, &Rule{
|
||||
PathScope: pathScope,
|
||||
Entries: []*Entry{entry},
|
||||
})
|
||||
|
||||
return rules
|
||||
}
|
||||
|
||||
const (
|
||||
// IP Masks that have no effect on IP Address
|
||||
DefaultIP4Mask = "255.255.255.255"
|
||||
|
||||
DefaultIP6Mask = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
|
||||
)
|
||||
Reference in New Issue
Block a user