TUN-528: Move cloudflared into a separate repo

This commit is contained in:
Areg Harutyunyan
2018-05-01 18:45:06 -05:00
parent e8c621a648
commit d06fc520c7
4726 changed files with 1763680 additions and 0 deletions

107
vendor/github.com/mholt/caddy/caddyhttp/status/setup.go generated vendored Normal file
View File

@@ -0,0 +1,107 @@
// 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 status
import (
"strconv"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
// init registers Status plugin
func init() {
caddy.RegisterPlugin("status", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
// setup configures new Status middleware instance.
func setup(c *caddy.Controller) error {
rules, err := statusParse(c)
if err != nil {
return err
}
cfg := httpserver.GetConfig(c)
mid := func(next httpserver.Handler) httpserver.Handler {
return Status{Rules: rules, Next: next}
}
cfg.AddMiddleware(mid)
return nil
}
// statusParse parses status directive
func statusParse(c *caddy.Controller) ([]httpserver.HandlerConfig, error) {
var rules []httpserver.HandlerConfig
for c.Next() {
hadBlock := false
args := c.RemainingArgs()
switch len(args) {
case 1:
status, err := strconv.Atoi(args[0])
if err != nil {
return rules, c.Errf("Expecting a numeric status code, got '%s'", args[0])
}
for c.NextBlock() {
hadBlock = true
basePath := c.Val()
for _, cfg := range rules {
rule := cfg.(*Rule)
if rule.Base == basePath {
return rules, c.Errf("Duplicate path: '%s'", basePath)
}
}
rule := NewRule(basePath, status)
rules = append(rules, rule)
if c.NextArg() {
return rules, c.ArgErr()
}
}
if !hadBlock {
return rules, c.ArgErr()
}
case 2:
status, err := strconv.Atoi(args[0])
if err != nil {
return rules, c.Errf("Expecting a numeric status code, got '%s'", args[0])
}
basePath := args[1]
for _, cfg := range rules {
rule := cfg.(*Rule)
if rule.Base == basePath {
return rules, c.Errf("Duplicate path: '%s'", basePath)
}
}
rule := NewRule(basePath, status)
rules = append(rules, rule)
default:
return rules, c.ArgErr()
}
}
return rules, nil
}

View File

@@ -0,0 +1,145 @@
// 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 status
import (
"testing"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
func TestSetup(t *testing.T) {
c := caddy.NewTestController("http", `status 404 /foo`)
err := setup(c)
if err != nil {
t.Errorf("Expected no errors, but got: %v", err)
}
mids := httpserver.GetConfig(c).Middleware()
if len(mids) == 0 {
t.Fatal("Expected middleware, had 0 instead")
}
handler := mids[0](httpserver.EmptyNext)
myHandler, ok := handler.(Status)
if !ok {
t.Fatalf("Expected handler to be type Status, got: %#v",
handler)
}
if !httpserver.SameNext(myHandler.Next, httpserver.EmptyNext) {
t.Error("'Next' field of handler was not set properly")
}
if len(myHandler.Rules) != 1 {
t.Errorf("Expected handler to have %d rule, has %d instead", 1, len(myHandler.Rules))
}
}
func TestStatusParse(t *testing.T) {
tests := []struct {
input string
shouldErr bool
expected []*Rule
}{
{`status`, true, []*Rule{}},
{`status /foo`, true, []*Rule{}},
{`status bar /foo`, true, []*Rule{}},
{`status 404 /foo bar`, true, []*Rule{}},
{`status 404 /foo`, false, []*Rule{
{Base: "/foo", StatusCode: 404},
},
},
{`status {
}`,
true,
[]*Rule{},
},
{`status 404 {
}`,
true,
[]*Rule{},
},
{`status 404 {
/foo
/foo
}`,
true,
[]*Rule{},
},
{`status 404 {
404 /foo
}`,
true,
[]*Rule{},
},
{`status 404 {
/foo
/bar
}`,
false,
[]*Rule{
{Base: "/foo", StatusCode: 404},
{Base: "/bar", StatusCode: 404},
},
},
}
for i, test := range tests {
actual, err := statusParse(caddy.NewTestController("http",
test.input))
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)
} else if err != nil && test.shouldErr {
continue
}
if len(actual) != len(test.expected) {
t.Fatalf("Test %d expected %d rules, but got %d",
i, len(test.expected), len(actual))
}
findRule := func(basePath string) (bool, *Rule) {
for _, cfg := range actual {
actualRule := cfg.(*Rule)
if actualRule.Base == basePath {
return true, actualRule
}
}
return false, nil
}
for _, expectedRule := range test.expected {
found, actualRule := findRule(expectedRule.Base)
if !found {
t.Errorf("Test %d: Missing rule for path '%s'",
i, expectedRule.Base)
}
if actualRule.StatusCode != expectedRule.StatusCode {
t.Errorf("Test %d: Expected status code %d for path '%s'. Got %d",
i, expectedRule.StatusCode, expectedRule.Base, actualRule.StatusCode)
}
}
}
}

View File

@@ -0,0 +1,73 @@
// 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 status is middleware for returning status code for requests
package status
import (
"net/http"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
// Rule describes status rewriting rule
type Rule struct {
// Base path. Request to this path and sub-paths will be answered with StatusCode
Base string
// Status code to return
StatusCode int
// Request matcher
httpserver.RequestMatcher
}
// NewRule creates new Rule.
func NewRule(basePath string, status int) *Rule {
return &Rule{
Base: basePath,
StatusCode: status,
RequestMatcher: httpserver.PathMatcher(basePath),
}
}
// BasePath implements httpserver.HandlerConfig interface
func (rule *Rule) BasePath() string {
return rule.Base
}
// Status is a middleware to return status code for request
type Status struct {
Rules []httpserver.HandlerConfig
Next httpserver.Handler
}
// ServeHTTP implements the httpserver.Handler interface
func (status Status) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if cfg := httpserver.ConfigSelector(status.Rules).Select(r); cfg != nil {
rule := cfg.(*Rule)
if rule.StatusCode < 400 {
// There's no ability to return response body --
// write the response status code in header and signal
// to other handlers that response is already handled
w.WriteHeader(rule.StatusCode)
return 0, nil
}
return rule.StatusCode, nil
}
return status.Next.ServeHTTP(w, r)
}

View File

@@ -0,0 +1,90 @@
// 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 status
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
func TestStatus(t *testing.T) {
status := Status{
Rules: []httpserver.HandlerConfig{
NewRule("/foo", http.StatusNotFound),
NewRule("/teapot", http.StatusTeapot),
NewRule("/foo/bar1", http.StatusInternalServerError),
NewRule("/temporary-redirected", http.StatusTemporaryRedirect),
},
Next: httpserver.HandlerFunc(urlPrinter),
}
tests := []struct {
path string
statusExpected bool
status int
}{
{"/foo", true, http.StatusNotFound},
{"/teapot", true, http.StatusTeapot},
{"/foo/bar", true, http.StatusNotFound},
{"/foo/bar1", true, http.StatusInternalServerError},
{"/someotherpath", false, 0},
{"/temporary-redirected", false, http.StatusTemporaryRedirect},
}
for i, test := range tests {
req, err := http.NewRequest("GET", test.path, nil)
if err != nil {
t.Fatalf("Test %d: Could not create HTTP request: %v",
i, err)
}
rec := httptest.NewRecorder()
actualStatus, err := status.ServeHTTP(rec, req)
if err != nil {
t.Fatalf("Test %d: Serving request failed with error %v",
i, err)
}
if test.statusExpected {
if test.status != actualStatus {
t.Errorf("Test %d: Expected status code %d, got %d",
i, test.status, actualStatus)
}
if rec.Body.String() != "" {
t.Errorf("Test %d: Expected empty body, got '%s'",
i, rec.Body.String())
}
} else {
if test.status != 0 { // Expecting status in response
if test.status != rec.Code {
t.Errorf("Test %d: Expected status code %d, got %d",
i, test.status, rec.Code)
}
} else if rec.Body.String() != test.path {
t.Errorf("Test %d: Expected body '%s', got '%s'",
i, test.path, rec.Body.String())
}
}
}
}
func urlPrinter(w http.ResponseWriter, r *http.Request) (int, error) {
fmt.Fprint(w, r.URL.String())
return 0, nil
}