mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-29 09:19:57 +00:00
TUN-528: Move cloudflared into a separate repo
This commit is contained in:
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
|
||||
)
|
261
vendor/github.com/mholt/caddy/caddyhttp/log/log_test.go
generated
vendored
Normal file
261
vendor/github.com/mholt/caddy/caddyhttp/log/log_test.go
generated
vendored
Normal file
@@ -0,0 +1,261 @@
|
||||
// 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 (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
type erroringMiddleware struct{}
|
||||
|
||||
func (erroringMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
if rr, ok := w.(*httpserver.ResponseRecorder); ok {
|
||||
rr.Replacer.Set("testval", "foobar")
|
||||
}
|
||||
return http.StatusNotFound, nil
|
||||
}
|
||||
|
||||
func TestLoggedStatus(t *testing.T) {
|
||||
var f bytes.Buffer
|
||||
var next erroringMiddleware
|
||||
rule := Rule{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Format: DefaultLogFormat + " {testval}",
|
||||
Log: httpserver.NewTestLogger(&f),
|
||||
}},
|
||||
}
|
||||
|
||||
logger := Logger{
|
||||
Rules: []*Rule{&rule},
|
||||
Next: next,
|
||||
}
|
||||
|
||||
r, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
status, err := logger.ServeHTTP(rec, r)
|
||||
if status != 0 {
|
||||
t.Errorf("Expected status to be 0, but was %d", status)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected error to be nil, instead got: %v", err)
|
||||
}
|
||||
|
||||
logged := f.String()
|
||||
if !strings.Contains(logged, "404 13") {
|
||||
t.Errorf("Expected log entry to contain '404 13', but it didn't: %s", logged)
|
||||
}
|
||||
|
||||
// check custom placeholder
|
||||
if !strings.Contains(logged, "foobar") {
|
||||
t.Errorf("Expected the log entry to contain 'foobar' (custom placeholder), but it didn't: %s", logged)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogRequestBody(t *testing.T) {
|
||||
var got bytes.Buffer
|
||||
logger := Logger{
|
||||
Rules: []*Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Format: "{request_body}",
|
||||
Log: httpserver.NewTestLogger(&got),
|
||||
}},
|
||||
}},
|
||||
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// drain up body
|
||||
ioutil.ReadAll(r.Body)
|
||||
return 0, nil
|
||||
}),
|
||||
}
|
||||
|
||||
for i, c := range []struct {
|
||||
body string
|
||||
expect string
|
||||
}{
|
||||
{"", "\n"},
|
||||
{"{hello} world!", "{hello} world!\n"},
|
||||
{func() string {
|
||||
length := httpserver.MaxLogBodySize + 100
|
||||
b := make([]byte, length)
|
||||
for i := 0; i < length; i++ {
|
||||
b[i] = 0xab
|
||||
}
|
||||
return string(b)
|
||||
}(), func() string {
|
||||
b := make([]byte, httpserver.MaxLogBodySize)
|
||||
for i := 0; i < httpserver.MaxLogBodySize; i++ {
|
||||
b[i] = 0xab
|
||||
}
|
||||
return string(b) + "\n"
|
||||
}(),
|
||||
},
|
||||
} {
|
||||
got.Reset()
|
||||
r := httptest.NewRequest("POST", "/", bytes.NewBufferString(c.body))
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
status, err := logger.ServeHTTP(httptest.NewRecorder(), r)
|
||||
if status != 0 {
|
||||
t.Errorf("case %d: Expected status to be 0, but was %d", i, status)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("case %d: Expected error to be nil, instead got: %v", i, err)
|
||||
}
|
||||
if got.String() != c.expect {
|
||||
t.Errorf("case %d: Expected body %q, but got %q", i, c.expect, got.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiEntries(t *testing.T) {
|
||||
var (
|
||||
got1 bytes.Buffer
|
||||
got2 bytes.Buffer
|
||||
)
|
||||
logger := Logger{
|
||||
Rules: []*Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{
|
||||
{
|
||||
Format: "foo {request_body}",
|
||||
Log: httpserver.NewTestLogger(&got1),
|
||||
},
|
||||
{
|
||||
Format: "{method} {request_body}",
|
||||
Log: httpserver.NewTestLogger(&got2),
|
||||
},
|
||||
},
|
||||
}},
|
||||
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// drain up body
|
||||
ioutil.ReadAll(r.Body)
|
||||
return 0, nil
|
||||
}),
|
||||
}
|
||||
|
||||
r, err := http.NewRequest("POST", "/", bytes.NewBufferString("hello world"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
status, err := logger.ServeHTTP(httptest.NewRecorder(), r)
|
||||
if status != 0 {
|
||||
t.Errorf("Expected status to be 0, but was %d", status)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Expected error to be nil, instead got: %v", err)
|
||||
}
|
||||
if got, expect := got1.String(), "foo hello world\n"; got != expect {
|
||||
t.Errorf("Expected %q, but got %q", expect, got)
|
||||
}
|
||||
if got, expect := got2.String(), "POST hello world\n"; got != expect {
|
||||
t.Errorf("Expected %q, but got %q", expect, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogExcept(t *testing.T) {
|
||||
tests := []struct {
|
||||
LogRules []Rule
|
||||
logPath string
|
||||
shouldLog bool
|
||||
}{
|
||||
{[]Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
|
||||
Exceptions: []string{"/soup"},
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}, `/soup`, false},
|
||||
{[]Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
|
||||
Exceptions: []string{"/tart"},
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}, `/soup`, true},
|
||||
{[]Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
|
||||
Exceptions: []string{"/soup"},
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}, `/tomatosoup`, true},
|
||||
{[]Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
|
||||
Exceptions: []string{"/pie/"},
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
// Check exception with a trailing slash does not match without
|
||||
}}, `/pie`, true},
|
||||
{[]Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
|
||||
Exceptions: []string{"/pie.php"},
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}, `/pie`, true},
|
||||
{[]Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
|
||||
Exceptions: []string{"/pie"},
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
// Check that a word without trailing slash will match a filename
|
||||
}}, `/pie.php`, false},
|
||||
}
|
||||
for i, test := range tests {
|
||||
for _, LogRule := range test.LogRules {
|
||||
for _, e := range LogRule.Entries {
|
||||
shouldLog := e.Log.ShouldLog(test.logPath)
|
||||
if shouldLog != test.shouldLog {
|
||||
t.Fatalf("Test %d expected shouldLog=%t but got shouldLog=%t,", i, test.shouldLog, shouldLog)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
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"
|
||||
)
|
379
vendor/github.com/mholt/caddy/caddyhttp/log/setup_test.go
generated
vendored
Normal file
379
vendor/github.com/mholt/caddy/caddyhttp/log/setup_test.go
generated
vendored
Normal file
@@ -0,0 +1,379 @@
|
||||
// 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"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
func TestSetup(t *testing.T) {
|
||||
c := caddy.NewTestController("http", `log`)
|
||||
err := setup(c)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no errors, got: %v", err)
|
||||
}
|
||||
cfg := httpserver.GetConfig(c)
|
||||
mids := cfg.Middleware()
|
||||
if mids == nil {
|
||||
t.Fatal("Expected middleware, was nil instead")
|
||||
}
|
||||
|
||||
handler := mids[0](httpserver.EmptyNext)
|
||||
myHandler, ok := handler.(Logger)
|
||||
|
||||
if !ok {
|
||||
t.Fatalf("Expected handler to be type Logger, got: %#v", handler)
|
||||
}
|
||||
|
||||
if myHandler.Rules[0].PathScope != "/" {
|
||||
t.Errorf("Expected / as the default PathScope")
|
||||
}
|
||||
|
||||
expectedLogger := &httpserver.Logger{
|
||||
Output: DefaultLogFilename,
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(myHandler.Rules[0].Entries[0].Log, expectedLogger) {
|
||||
t.Errorf("Expected %v as the default Log, got: %v", expectedLogger, myHandler.Rules[0].Entries[0].Log)
|
||||
}
|
||||
if myHandler.Rules[0].Entries[0].Format != DefaultLogFormat {
|
||||
t.Errorf("Expected %s as the default Log Format", DefaultLogFormat)
|
||||
}
|
||||
if !httpserver.SameNext(myHandler.Next, httpserver.EmptyNext) {
|
||||
t.Error("'Next' field of handler was not set properly")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogParse(t *testing.T) {
|
||||
tests := []struct {
|
||||
inputLogRules string
|
||||
shouldErr bool
|
||||
expectedLogRules []Rule
|
||||
}{
|
||||
{`log`, false, []Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: DefaultLogFilename,
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}},
|
||||
{`log log.txt`, false, []Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "log.txt",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}},
|
||||
{`log syslog://127.0.0.1:5000`, false, []Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "syslog://127.0.0.1:5000",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}},
|
||||
{`log syslog+tcp://127.0.0.1:5000`, false, []Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "syslog+tcp://127.0.0.1:5000",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}},
|
||||
{`log /api log.txt`, false, []Rule{{
|
||||
PathScope: "/api",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "log.txt",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}},
|
||||
{`log /serve stdout`, false, []Rule{{
|
||||
PathScope: "/serve",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "stdout",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}},
|
||||
{`log /myapi log.txt {common}`, false, []Rule{{
|
||||
PathScope: "/myapi",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "log.txt",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: CommonLogFormat,
|
||||
}},
|
||||
}}},
|
||||
{`log /myapi log.txt "prefix {common} suffix"`, false, []Rule{{
|
||||
PathScope: "/myapi",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "log.txt",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: "prefix " + CommonLogFormat + " suffix",
|
||||
}},
|
||||
}}},
|
||||
{`log /test accesslog.txt {combined}`, false, []Rule{{
|
||||
PathScope: "/test",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "accesslog.txt",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: CombinedLogFormat,
|
||||
}},
|
||||
}}},
|
||||
{`log /test accesslog.txt "prefix {combined} suffix"`, false, []Rule{{
|
||||
PathScope: "/test",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "accesslog.txt",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: "prefix " + CombinedLogFormat + " suffix",
|
||||
}},
|
||||
}}},
|
||||
{`log /api1 log.txt
|
||||
log /api2 accesslog.txt {combined}`, false, []Rule{{
|
||||
PathScope: "/api1",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "log.txt",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}, {
|
||||
PathScope: "/api2",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "accesslog.txt",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: CombinedLogFormat,
|
||||
}},
|
||||
}}},
|
||||
{`log /api3 stdout {host}
|
||||
log /api4 log.txt {when}`, false, []Rule{{
|
||||
PathScope: "/api3",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "stdout",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: "{host}",
|
||||
}},
|
||||
}, {
|
||||
PathScope: "/api4",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "log.txt",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: "{when}",
|
||||
}},
|
||||
}}},
|
||||
{`log access.log {
|
||||
rotate_size 2
|
||||
rotate_age 10
|
||||
rotate_keep 3
|
||||
rotate_compress
|
||||
}`, false, []Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "access.log",
|
||||
Roller: &httpserver.LogRoller{
|
||||
MaxSize: 2,
|
||||
MaxAge: 10,
|
||||
MaxBackups: 3,
|
||||
Compress: true,
|
||||
LocalTime: true,
|
||||
},
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}},
|
||||
{`log access0.log {
|
||||
ipmask 255.255.255.0
|
||||
}`, false, []Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "access0.log",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP("255.255.255.0").To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
IPMaskExists: true,
|
||||
},
|
||||
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}},
|
||||
{`log access1.log {
|
||||
ipmask "" ffff:ffff:ffff:ff00::
|
||||
}`, false, []Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "access1.log",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP("ffff:ffff:ffff:ff00::")),
|
||||
IPMaskExists: true,
|
||||
},
|
||||
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}},
|
||||
{`log access2.log {
|
||||
ipmask 255.255.255.0 ffff:ffff:ffff:ff00::
|
||||
}`, false, []Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "access2.log",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP("255.255.255.0").To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP("ffff:ffff:ffff:ff00::")),
|
||||
IPMaskExists: true,
|
||||
},
|
||||
|
||||
Format: DefaultLogFormat,
|
||||
}},
|
||||
}}},
|
||||
{`log / stdout {host}
|
||||
log / log.txt {when}`, false, []Rule{{
|
||||
PathScope: "/",
|
||||
Entries: []*Entry{{
|
||||
Log: &httpserver.Logger{
|
||||
Output: "stdout",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: "{host}",
|
||||
}, {
|
||||
Log: &httpserver.Logger{
|
||||
Output: "log.txt",
|
||||
Roller: httpserver.DefaultLogRoller(),
|
||||
V4ipMask: net.IPMask(net.ParseIP(DefaultIP4Mask).To4()),
|
||||
V6ipMask: net.IPMask(net.ParseIP(DefaultIP6Mask)),
|
||||
},
|
||||
Format: "{when}",
|
||||
}},
|
||||
}}},
|
||||
{`log access.log { rotate_size 2 rotate_age 10 rotate_keep 3 }`, true, nil},
|
||||
{`log access.log { rotate_compress invalid }`, true, nil},
|
||||
{`log access.log { rotate_size }`, true, nil},
|
||||
{`log access.log { ipmask }`, true, nil},
|
||||
{`log access.log { invalid_option 1 }`, true, nil},
|
||||
{`log / acccess.log "{remote} - [{when}] "{method} {port}" {scheme} {mitm} "`, true, nil},
|
||||
}
|
||||
for i, test := range tests {
|
||||
c := caddy.NewTestController("http", test.inputLogRules)
|
||||
actualLogRules, err := logParse(c)
|
||||
|
||||
if err == nil && test.shouldErr {
|
||||
t.Errorf("Test %d didn't error, but it should have", i)
|
||||
} else if err != nil && !test.shouldErr {
|
||||
t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
|
||||
}
|
||||
if len(actualLogRules) != len(test.expectedLogRules) {
|
||||
t.Fatalf("Test %d expected %d no of Log rules, but got %d ",
|
||||
i, len(test.expectedLogRules), len(actualLogRules))
|
||||
}
|
||||
for j, actualLogRule := range actualLogRules {
|
||||
|
||||
if actualLogRule.PathScope != test.expectedLogRules[j].PathScope {
|
||||
t.Errorf("Test %d expected %dth LogRule PathScope to be %s , but got %s",
|
||||
i, j, test.expectedLogRules[j].PathScope, actualLogRule.PathScope)
|
||||
}
|
||||
|
||||
if got, expect := len(actualLogRule.Entries), len(test.expectedLogRules[j].Entries); got != expect {
|
||||
t.Fatalf("Test %d expected %dth LogRule with %d no of Log entries, but got %d ",
|
||||
i, j, expect, got)
|
||||
}
|
||||
|
||||
for k, actualEntry := range actualLogRule.Entries {
|
||||
if !reflect.DeepEqual(actualEntry.Log, test.expectedLogRules[j].Entries[k].Log) {
|
||||
t.Errorf("Test %d expected %dth LogRule Log to be %v , but got %v",
|
||||
i, j, test.expectedLogRules[j].Entries[k].Log, actualEntry.Log)
|
||||
}
|
||||
|
||||
if actualEntry.Format != test.expectedLogRules[j].Entries[k].Format {
|
||||
t.Errorf("Test %d expected %dth LogRule Format to be %s , but got %s",
|
||||
i, j, test.expectedLogRules[j].Entries[k].Format, actualEntry.Format)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user