mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-22 05:59:00 +08:00
fix lint warnings (issue 2541) (#2551)
* Lint: fix some errcheck #2541 * Lint: fix passing structcheck #2541 * Lint: update fix structcheck #2541 * Lint: fix errcheck for basicauth, browse, fastcgi_test #2541 * Lint: fix errcheck for browse, fastcgi_test, fcgiclient, fcgiclient_test #2541 * Lint: fix errcheck for responsefilter_test, fcgilient_test #2541 * Lint: fix errcheck for header_test #2541 * Lint: update errcheck for fcgiclient_test #2541 * Lint: fix errcheck for server, header_test, fastcgi_test, https_test, recorder_test #2541 * Lint: fix errcheck for tplcontext, vhosttrie_test, internal_test, handler_test #2541 * Lint: fix errcheck for log_test, markdown mholt#2541 * Lint: fix errcheck for policy, body_test, proxy_test #2541 * Lint: fix errcheck for on multiple packages #2541 - reverseproxy - reverseproxy_test - upstream - upstream_test - body_test * Lint: fix errcheck in multiple packages mholt#2541 - handler_test - redirect_test - requestid_test - rewrite_test - fileserver_test * Lint: fix errcheck in multiple packages mholt#2541 - websocket - setup - collection - redirect_test - templates_test * Lint: fix errcheck in logger test #2541 run goimports against #2551 - lexer_test - log_test - markdown * Update caddyhttp/httpserver/logger_test.go Co-Authored-By: Inconnu08 <taufiqrx8@gmail.com> * Update log_test.go * Lint: fix scope in logger_test #2541 * remove redundant err check in logger_test #2541 * fix alias in logger_test #2541 * fix import for format #2541 * refactor variable names and error check #2541
This commit is contained in:
parent
0c3d90ed21
commit
c32a0f5f71
|
@ -16,6 +16,7 @@ package caddy
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
|
@ -103,13 +104,20 @@ func TestCaddyRestartCallbacks(t *testing.T) {
|
|||
return nil
|
||||
})
|
||||
|
||||
c.instance.Restart(CaddyfileInput{Contents: []byte(""), ServerTypeName: serverName})
|
||||
_, err := c.instance.Restart(CaddyfileInput{Contents: []byte(""), ServerTypeName: serverName})
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] Restart failed: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(calls, test.expectedCalls) {
|
||||
t.Errorf("Test %d: Callbacks expected: %v, got: %v", i, test.expectedCalls, calls)
|
||||
}
|
||||
|
||||
c.instance.Stop()
|
||||
err = c.instance.Stop()
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] Stop failed: %v", err)
|
||||
}
|
||||
|
||||
c.instance.Wait()
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package caddyfile
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -158,7 +159,9 @@ func TestLexer(t *testing.T) {
|
|||
|
||||
func tokenize(input string) (tokens []Token) {
|
||||
l := lexer{}
|
||||
l.load(strings.NewReader(input))
|
||||
if err := l.load(strings.NewReader(input)); err != nil {
|
||||
log.Printf("[ERROR] load failed: %v", err)
|
||||
}
|
||||
for l.next() {
|
||||
tokens = append(tokens, l.token)
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ import (
|
|||
"crypto/subtle"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -193,11 +194,15 @@ func PlainMatcher(passw string) PasswordMatcher {
|
|||
// compare hashes of equal length instead of actual password
|
||||
// to avoid leaking password length
|
||||
passwHash := sha1.New()
|
||||
passwHash.Write([]byte(passw))
|
||||
if _, err := passwHash.Write([]byte(passw)); err != nil {
|
||||
log.Printf("[ERROR] unable to write password hash: %v", err)
|
||||
}
|
||||
passwSum := passwHash.Sum(nil)
|
||||
return func(pw string) bool {
|
||||
pwHash := sha1.New()
|
||||
pwHash.Write([]byte(pw))
|
||||
if _, err := pwHash.Write([]byte(pw)); err != nil {
|
||||
log.Printf("[ERROR] unable to write password hash: %v", err)
|
||||
}
|
||||
pwSum := pwHash.Sum(nil)
|
||||
return subtle.ConstantTimeCompare([]byte(pwSum), []byte(passwSum)) == 1
|
||||
}
|
||||
|
|
|
@ -59,34 +59,34 @@ type Config struct {
|
|||
|
||||
// A Listing is the context used to fill out a template.
|
||||
type Listing struct {
|
||||
// The name of the directory (the last element of the path)
|
||||
// The name of the directory (the last element of the path).
|
||||
Name string
|
||||
|
||||
// The full path of the request
|
||||
// The full path of the request.
|
||||
Path string
|
||||
|
||||
// Whether the parent directory is browsable
|
||||
// Whether the parent directory is browse-able.
|
||||
CanGoUp bool
|
||||
|
||||
// The items (files and folders) in the path
|
||||
// The items (files and folders) in the path.
|
||||
Items []FileInfo
|
||||
|
||||
// The number of directories in the listing
|
||||
// The number of directories in the listing.
|
||||
NumDirs int
|
||||
|
||||
// The number of files (items that aren't directories) in the listing
|
||||
// The number of files (items that aren't directories) in the listing.
|
||||
NumFiles int
|
||||
|
||||
// Which sorting order is used
|
||||
// Which sorting order is used.
|
||||
Sort string
|
||||
|
||||
// And which order
|
||||
// And which order.
|
||||
Order string
|
||||
|
||||
// If ≠0 then Items have been limited to that many elements
|
||||
// If ≠0 then Items have been limited to that many elements.
|
||||
ItemsLimitedTo int
|
||||
|
||||
// Optional custom variables for use in browse templates
|
||||
// Optional custom variables for use in browse templates.
|
||||
User interface{}
|
||||
|
||||
httpserver.Context
|
||||
|
@ -244,7 +244,7 @@ func (l Listing) applySort() {
|
|||
|
||||
func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, config *Config) (Listing, bool) {
|
||||
var (
|
||||
fileinfos []FileInfo
|
||||
fileInfos []FileInfo
|
||||
dirCount, fileCount int
|
||||
hasIndexFile bool
|
||||
)
|
||||
|
@ -272,14 +272,14 @@ func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, config
|
|||
continue
|
||||
}
|
||||
|
||||
url := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
|
||||
u := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
|
||||
|
||||
fileinfos = append(fileinfos, FileInfo{
|
||||
fileInfos = append(fileInfos, FileInfo{
|
||||
IsDir: isDir,
|
||||
IsSymlink: isSymlink(f),
|
||||
Name: f.Name(),
|
||||
Size: f.Size(),
|
||||
URL: url.String(),
|
||||
URL: u.String(),
|
||||
ModTime: f.ModTime().UTC(),
|
||||
Mode: f.Mode(),
|
||||
})
|
||||
|
@ -289,7 +289,7 @@ func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, config
|
|||
Name: path.Base(urlPath),
|
||||
Path: urlPath,
|
||||
CanGoUp: canGoUp,
|
||||
Items: fileinfos,
|
||||
Items: fileInfos,
|
||||
NumDirs: dirCount,
|
||||
NumFiles: fileCount,
|
||||
}, hasIndexFile
|
||||
|
@ -504,7 +504,7 @@ func (b Browse) ServeListing(w http.ResponseWriter, r *http.Request, requestedFi
|
|||
|
||||
}
|
||||
|
||||
buf.WriteTo(w)
|
||||
_, _ = buf.WriteTo(w)
|
||||
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ package fastcgi
|
|||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/fcgi"
|
||||
|
@ -39,11 +40,20 @@ func TestServeHTTP(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("Unable to create listener for test: %v", err)
|
||||
}
|
||||
defer listener.Close()
|
||||
go fcgi.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Length", bodyLenStr)
|
||||
w.Write([]byte(body))
|
||||
}))
|
||||
defer func() { _ = listener.Close() }()
|
||||
|
||||
go func() {
|
||||
err := fcgi.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Length", bodyLenStr)
|
||||
_, err := w.Write([]byte(body))
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] unable to write header: %v", err)
|
||||
}
|
||||
}))
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] unable to start server: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
handler := Handler{
|
||||
Next: nil,
|
||||
|
@ -147,7 +157,7 @@ func TestBuildEnv(t *testing.T) {
|
|||
SplitPath: ".php",
|
||||
IndexFiles: []string{"index.php"},
|
||||
}
|
||||
url, err := url.Parse("http://localhost:2015/fgci_test.php?test=foobar")
|
||||
u, err := url.Parse("http://localhost:2015/fgci_test.php?test=foobar")
|
||||
if err != nil {
|
||||
t.Error("Unexpected error:", err.Error())
|
||||
}
|
||||
|
@ -155,7 +165,7 @@ func TestBuildEnv(t *testing.T) {
|
|||
var newReq = func() *http.Request {
|
||||
r := http.Request{
|
||||
Method: "GET",
|
||||
URL: url,
|
||||
URL: u,
|
||||
Proto: "HTTP/1.1",
|
||||
ProtoMajor: 1,
|
||||
ProtoMinor: 1,
|
||||
|
@ -274,7 +284,7 @@ func TestReadTimeout(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("Test %d: Unable to create listener for test: %v", i, err)
|
||||
}
|
||||
defer listener.Close()
|
||||
defer func() { _ = listener.Close() }()
|
||||
|
||||
handler := Handler{
|
||||
Next: nil,
|
||||
|
@ -293,11 +303,16 @@ func TestReadTimeout(t *testing.T) {
|
|||
w := httptest.NewRecorder()
|
||||
|
||||
wg.Add(1)
|
||||
go fcgi.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(test.sleep)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
wg.Done()
|
||||
}))
|
||||
go func() {
|
||||
err := fcgi.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(test.sleep)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
wg.Done()
|
||||
}))
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] unable to start server: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
got, err := handler.ServeHTTP(w, r)
|
||||
if test.shouldErr {
|
||||
|
@ -334,7 +349,7 @@ func TestSendTimeout(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("Test %d: Unable to create listener for test: %v", i, err)
|
||||
}
|
||||
defer listener.Close()
|
||||
defer func() { _ = listener.Close() }()
|
||||
|
||||
handler := Handler{
|
||||
Next: nil,
|
||||
|
@ -352,9 +367,14 @@ func TestSendTimeout(t *testing.T) {
|
|||
}
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
go fcgi.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
go func() {
|
||||
err := fcgi.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] unable to start server: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
got, err := handler.ServeHTTP(w, r)
|
||||
if test.shouldErr {
|
||||
|
|
|
@ -175,15 +175,13 @@ func (rec *record) read(r io.Reader) (buf []byte, err error) {
|
|||
// FCGIClient implements a FastCGI client, which is a standard for
|
||||
// interfacing external applications with Web servers.
|
||||
type FCGIClient struct {
|
||||
mutex sync.Mutex
|
||||
rwc io.ReadWriteCloser
|
||||
h header
|
||||
buf bytes.Buffer
|
||||
stderr bytes.Buffer
|
||||
keepAlive bool
|
||||
reqID uint16
|
||||
readTimeout time.Duration
|
||||
sendTimeout time.Duration
|
||||
mutex sync.Mutex
|
||||
rwc io.ReadWriteCloser
|
||||
h header
|
||||
buf bytes.Buffer
|
||||
stderr bytes.Buffer
|
||||
keepAlive bool
|
||||
reqID uint16
|
||||
}
|
||||
|
||||
// DialWithDialerContext connects to the fcgi responder at the specified network address, using custom net.Dialer
|
||||
|
@ -397,7 +395,7 @@ func (c *FCGIClient) Do(p map[string]string, req io.Reader) (r io.Reader, err er
|
|||
|
||||
body := newWriter(c, Stdin)
|
||||
if req != nil {
|
||||
io.Copy(body, req)
|
||||
_, _ = io.Copy(body, req)
|
||||
}
|
||||
body.Close()
|
||||
|
||||
|
|
|
@ -59,7 +59,9 @@ type FastCGIServer struct{}
|
|||
|
||||
func (s FastCGIServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
||||
|
||||
req.ParseMultipartForm(100000000)
|
||||
if err := req.ParseMultipartForm(100000000); err != nil {
|
||||
log.Printf("[ERROR] failed to parse: %v", err)
|
||||
}
|
||||
|
||||
stat := "PASSED"
|
||||
fmt.Fprintln(resp, "-")
|
||||
|
@ -68,15 +70,15 @@ func (s FastCGIServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
|||
length := 0
|
||||
for k0, v0 := range req.Form {
|
||||
h := md5.New()
|
||||
io.WriteString(h, v0[0])
|
||||
md5 := fmt.Sprintf("%x", h.Sum(nil))
|
||||
_, _ = io.WriteString(h, v0[0])
|
||||
_md5 := fmt.Sprintf("%x", h.Sum(nil))
|
||||
|
||||
length += len(k0)
|
||||
length += len(v0[0])
|
||||
|
||||
// echo error when key != md5(val)
|
||||
if md5 != k0 {
|
||||
fmt.Fprintln(resp, "server:err ", md5, k0)
|
||||
// echo error when key != _md5(val)
|
||||
if _md5 != k0 {
|
||||
fmt.Fprintln(resp, "server:err ", _md5, k0)
|
||||
stat = "FAILED"
|
||||
}
|
||||
}
|
||||
|
@ -197,8 +199,12 @@ func generateRandFile(size int) (p string, m string) {
|
|||
for i := 0; i < size/16; i++ {
|
||||
buf := make([]byte, 16)
|
||||
binary.PutVarint(buf, rand.Int63())
|
||||
fo.Write(buf)
|
||||
h.Write(buf)
|
||||
if _, err := fo.Write(buf); err != nil {
|
||||
log.Printf("[ERROR] failed to write buffer: %v\n", err)
|
||||
}
|
||||
if _, err := h.Write(buf); err != nil {
|
||||
log.Printf("[ERROR] failed to write buffer: %v\n", err)
|
||||
}
|
||||
}
|
||||
m = fmt.Sprintf("%x", h.Sum(nil))
|
||||
return
|
||||
|
@ -214,12 +220,13 @@ func DisabledTest(t *testing.T) {
|
|||
go func() {
|
||||
listener, err := net.Listen("tcp", ipPort)
|
||||
if err != nil {
|
||||
// handle error
|
||||
log.Println("listener creation failed: ", err)
|
||||
}
|
||||
|
||||
srv := new(FastCGIServer)
|
||||
fcgi.Serve(listener, srv)
|
||||
if err := fcgi.Serve(listener, srv); err != nil {
|
||||
log.Print("[ERROR] failed to start server: ", err)
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
@ -244,7 +251,7 @@ func DisabledTest(t *testing.T) {
|
|||
for i := 0x00; i < 0xff; i++ {
|
||||
v0 := strings.Repeat(string(i), 256)
|
||||
h := md5.New()
|
||||
io.WriteString(h, v0)
|
||||
_, _ = io.WriteString(h, v0)
|
||||
k0 := fmt.Sprintf("%x", h.Sum(nil))
|
||||
data += k0 + "=" + url.QueryEscape(v0) + "&"
|
||||
}
|
||||
|
@ -261,7 +268,7 @@ func DisabledTest(t *testing.T) {
|
|||
for i := 0x00; i < 0xff; i++ {
|
||||
v0 := strings.Repeat(string(i), 4096)
|
||||
h := md5.New()
|
||||
io.WriteString(h, v0)
|
||||
_, _ = io.WriteString(h, v0)
|
||||
k0 := fmt.Sprintf("%x", h.Sum(nil))
|
||||
p1[k0] = v0
|
||||
}
|
||||
|
@ -285,6 +292,10 @@ func DisabledTest(t *testing.T) {
|
|||
delete(f0, "m0")
|
||||
sendFcgi(1, fcgiParams, nil, nil, f0)
|
||||
|
||||
os.Remove(path0)
|
||||
os.Remove(path1)
|
||||
if err := os.Remove(path0); err != nil {
|
||||
log.Println("[ERROR] failed to remove path: ", err)
|
||||
}
|
||||
if err := os.Remove(path1); err != nil {
|
||||
log.Println("[ERROR] failed to remove path: ", err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ package gzip
|
|||
import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
@ -77,7 +78,9 @@ func TestResponseFilterWriter(t *testing.T) {
|
|||
for i, ts := range tests {
|
||||
server.Next = httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
w.Header().Set("Content-Length", fmt.Sprint(len(ts.body)))
|
||||
w.Write([]byte(ts.body))
|
||||
if _, err := w.Write([]byte(ts.body)); err != nil {
|
||||
log.Println("[ERROR] failed to write response: ", err)
|
||||
}
|
||||
return 200, nil
|
||||
})
|
||||
|
||||
|
@ -86,7 +89,9 @@ func TestResponseFilterWriter(t *testing.T) {
|
|||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
server.ServeHTTP(w, r)
|
||||
if _, err := server.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] unable to serve a gzipped response: ", err)
|
||||
}
|
||||
|
||||
resp := w.Body.String()
|
||||
|
||||
|
@ -109,7 +114,9 @@ func TestResponseGzippedOutput(t *testing.T) {
|
|||
|
||||
server.Next = httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
w.Write([]byte("gzipped"))
|
||||
if _, err := w.Write([]byte("gzipped")); err != nil {
|
||||
log.Println("[ERROR] failed to write response: ", err)
|
||||
}
|
||||
return 200, nil
|
||||
})
|
||||
|
||||
|
@ -117,7 +124,9 @@ func TestResponseGzippedOutput(t *testing.T) {
|
|||
r.Header.Set("Accept-Encoding", "gzip")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
server.ServeHTTP(w, r)
|
||||
if _, err := server.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] unable to serve a gzipped response: ", err)
|
||||
}
|
||||
resp := w.Body.String()
|
||||
|
||||
if resp != "gzipped" {
|
||||
|
|
|
@ -16,6 +16,7 @@ package header
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
|
@ -69,7 +70,9 @@ func TestHeader(t *testing.T) {
|
|||
// preset header
|
||||
rec.Header().Set("Server", "Caddy")
|
||||
|
||||
he.ServeHTTP(rec, req)
|
||||
if _, err := he.ServeHTTP(rec, req); err != nil {
|
||||
log.Println("[ERROR] ServeHTTP failed: ", err)
|
||||
}
|
||||
|
||||
if got := rec.Header().Get(test.name); got != test.value {
|
||||
t.Errorf("Test %d: Expected %s header to be %q but was %q",
|
||||
|
@ -81,7 +84,9 @@ func TestHeader(t *testing.T) {
|
|||
func TestMultipleHeaders(t *testing.T) {
|
||||
he := Headers{
|
||||
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
fmt.Fprint(w, "This is a test")
|
||||
if _, err := fmt.Fprint(w, "This is a test"); err != nil {
|
||||
log.Println("[ERROR] Fprint failed: ", err)
|
||||
}
|
||||
return 0, nil
|
||||
}),
|
||||
Rules: []Rule{
|
||||
|
@ -97,7 +102,9 @@ func TestMultipleHeaders(t *testing.T) {
|
|||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
he.ServeHTTP(rec, req)
|
||||
if _, err := he.ServeHTTP(rec, req); err != nil {
|
||||
log.Println("[ERROR] ServeHTTP failed: ", err)
|
||||
}
|
||||
|
||||
desiredHeaders := []string{"</css/main.css>; rel=preload", "</images/image.png>; rel=preload"}
|
||||
actualHeaders := rec.HeaderMap[http.CanonicalHeaderKey("Link")]
|
||||
|
|
|
@ -16,6 +16,7 @@ package httpserver
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
@ -180,7 +181,9 @@ func TestEnableAutoHTTPS(t *testing.T) {
|
|||
{}, // not managed - no changes!
|
||||
}
|
||||
|
||||
enableAutoHTTPS(configs, false)
|
||||
if err := enableAutoHTTPS(configs, false); err != nil {
|
||||
log.Println("[ERROR] enableAutoHTTPS failed: ", err)
|
||||
}
|
||||
|
||||
if !configs[0].TLS.Enabled {
|
||||
t.Errorf("Expected config 0 to have TLS.Enabled == true, but it was false")
|
||||
|
|
|
@ -20,6 +20,7 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
@ -179,9 +180,13 @@ func bootServer(location string, ch chan format.LogParts) (*syslog.Server, error
|
|||
|
||||
switch address.network {
|
||||
case "tcp":
|
||||
server.ListenTCP(address.address)
|
||||
if err := server.ListenTCP(address.address); err != nil {
|
||||
log.Println("[ERROR] server failed to listen on TCP address: ", err)
|
||||
}
|
||||
case "udp":
|
||||
server.ListenUDP(address.address)
|
||||
if err := server.ListenUDP(address.address); err != nil {
|
||||
log.Println("[ERROR] server failed to listen on UDP address: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
server.SetHandler(syslog.NewChannelHandler(ch))
|
||||
|
|
|
@ -44,7 +44,7 @@ func TestWrite(t *testing.T) {
|
|||
responseTestString := "test"
|
||||
recordRequest := NewResponseRecorder(w)
|
||||
buf := []byte(responseTestString)
|
||||
recordRequest.Write(buf)
|
||||
_, _ = recordRequest.Write(buf)
|
||||
if recordRequest.size != len(buf) {
|
||||
t.Fatalf("Expected the bytes written counter to be %d, but instead found %d\n", len(buf), recordRequest.size)
|
||||
}
|
||||
|
|
|
@ -234,7 +234,9 @@ func makeHTTPServerWithTimeouts(addr string, group []*SiteConfig) *http.Server {
|
|||
|
||||
func (s *Server) wrapWithSvcHeaders(previousHandler http.Handler) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
s.quicServer.SetQuicHeaders(w.Header())
|
||||
if err := s.quicServer.SetQuicHeaders(w.Header()); err != nil {
|
||||
log.Println("[Error] failed to set proper headers for QUIC: ", err)
|
||||
}
|
||||
previousHandler.ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
|
@ -243,7 +245,7 @@ func (s *Server) wrapWithSvcHeaders(previousHandler http.Handler) http.HandlerFu
|
|||
// used to serve requests.
|
||||
func (s *Server) Listen() (net.Listener, error) {
|
||||
if s.Server == nil {
|
||||
return nil, fmt.Errorf("Server field is nil")
|
||||
return nil, fmt.Errorf("server field is nil")
|
||||
}
|
||||
|
||||
ln, err := net.Listen("tcp", s.Server.Addr)
|
||||
|
@ -324,7 +326,9 @@ func (s *Server) Serve(ln net.Listener) error {
|
|||
|
||||
defer func() {
|
||||
if s.quicServer != nil {
|
||||
s.quicServer.Close()
|
||||
if err := s.quicServer.Close(); err != nil {
|
||||
log.Println("[ERROR] failed to close QUIC server: ", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
|
@ -555,8 +559,12 @@ func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
tc.SetKeepAlive(true)
|
||||
tc.SetKeepAlivePeriod(3 * time.Minute)
|
||||
if err = tc.SetKeepAlive(true); err != nil {
|
||||
return
|
||||
}
|
||||
if err = tc.SetKeepAlivePeriod(3 * time.Minute); err != nil {
|
||||
return
|
||||
}
|
||||
return tc, nil
|
||||
}
|
||||
|
||||
|
@ -594,7 +602,9 @@ func WriteTextResponse(w http.ResponseWriter, status int, body string) {
|
|||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.WriteHeader(status)
|
||||
w.Write([]byte(body))
|
||||
if _, err := w.Write([]byte(body)); err != nil {
|
||||
log.Println("[Error] failed to write body: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// SafePath joins siteRoot and reqPath and converts it to a path that can
|
||||
|
|
|
@ -19,6 +19,7 @@ import (
|
|||
"crypto/rand"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
mathrand "math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
|
@ -420,7 +421,9 @@ func (c Context) RandomString(minLen, maxLen int) string {
|
|||
// secureRandomBytes returns a number of bytes using crypto/rand.
|
||||
secureRandomBytes := func(numBytes int) []byte {
|
||||
randomBytes := make([]byte, numBytes)
|
||||
rand.Read(randomBytes)
|
||||
if _, err := rand.Read(randomBytes); err != nil {
|
||||
log.Println("[ERROR] failed to read bytes: ", err)
|
||||
}
|
||||
return randomBytes
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package httpserver
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
@ -103,7 +104,9 @@ func populateTestTrie(trie *vhostTrie, keys []string) {
|
|||
func(key string) {
|
||||
site := &SiteConfig{
|
||||
middlewareChain: HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
w.Write([]byte(key))
|
||||
if _, err := w.Write([]byte(key)); err != nil {
|
||||
log.Println("[ERROR] failed to write bytes: ", err)
|
||||
}
|
||||
return 0, nil
|
||||
}),
|
||||
}
|
||||
|
@ -139,7 +142,9 @@ func assertTestTrie(t *testing.T, trie *vhostTrie, tests []vhostTrieTest, hasWil
|
|||
|
||||
// And it must be the correct value
|
||||
resp := httptest.NewRecorder()
|
||||
site.middlewareChain.ServeHTTP(resp, nil)
|
||||
if _, err := site.middlewareChain.ServeHTTP(resp, nil); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
actualHandlerKey := resp.Body.String()
|
||||
if actualHandlerKey != test.expectedKey {
|
||||
t.Errorf("Test %d: Expected match '%s' but matched '%s'",
|
||||
|
|
|
@ -16,6 +16,7 @@ package internalsrv
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
@ -126,7 +127,9 @@ func internalTestHandlerFunc(w http.ResponseWriter, r *http.Request) (int, error
|
|||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", contentTypeOctetStream)
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(internalProtectedData)))
|
||||
w.Write([]byte(internalProtectedData))
|
||||
if _, err := w.Write([]byte(internalProtectedData)); err != nil {
|
||||
log.Println("[ERROR] failed to write bytes: ", err)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ package limits
|
|||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
|
@ -39,7 +40,9 @@ func TestBodySizeLimit(t *testing.T) {
|
|||
}
|
||||
|
||||
r := httptest.NewRequest("GET", "/", strings.NewReader(expectContent+expectContent))
|
||||
l.ServeHTTP(httptest.NewRecorder(), r)
|
||||
if _, err := l.ServeHTTP(httptest.NewRecorder(), r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
if got := string(gotContent); got != expectContent {
|
||||
t.Errorf("expected content[%s], got[%s]", expectContent, got)
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ package log
|
|||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
|
@ -89,7 +90,9 @@ func TestLogRequestBody(t *testing.T) {
|
|||
}},
|
||||
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// drain up body
|
||||
ioutil.ReadAll(r.Body)
|
||||
if _, err := ioutil.ReadAll(r.Body); err != nil {
|
||||
log.Println("[ERROR] failed to read request body: ", err)
|
||||
}
|
||||
return 0, nil
|
||||
}),
|
||||
}
|
||||
|
@ -153,7 +156,9 @@ func TestMultiEntries(t *testing.T) {
|
|||
}},
|
||||
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// drain up body
|
||||
ioutil.ReadAll(r.Body)
|
||||
if _, err := ioutil.ReadAll(r.Body); err != nil {
|
||||
log.Println("[ERROR] failed to read request body: ", err)
|
||||
}
|
||||
return 0, nil
|
||||
}),
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package markdown
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
|
@ -168,7 +169,9 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
|
|||
w.Header().Set("Content-Length", strconv.Itoa(len(html)))
|
||||
httpserver.SetLastModifiedHeader(w, lastModTime)
|
||||
if r.Method == http.MethodGet {
|
||||
w.Write(html)
|
||||
if _, err := w.Write(html); err != nil {
|
||||
log.Println("[ERROR] failed to write html response: ", err)
|
||||
}
|
||||
}
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ import (
|
|||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
@ -25,13 +26,15 @@ import (
|
|||
|
||||
func TestBodyRetry(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.Copy(w, r.Body)
|
||||
r.Body.Close()
|
||||
if _, err := io.Copy(w, r.Body); err != nil {
|
||||
log.Println("[ERROR] failed to copy response body: ", err)
|
||||
}
|
||||
_ = r.Body.Close()
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
testcase := "test content"
|
||||
req, err := http.NewRequest(http.MethodPost, ts.URL, bytes.NewBufferString(testcase))
|
||||
testCase := "test content"
|
||||
req, err := http.NewRequest(http.MethodPost, ts.URL, bytes.NewBufferString(testCase))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -47,12 +50,16 @@ func TestBodyRetry(t *testing.T) {
|
|||
// simulate fail request
|
||||
host := req.URL.Host
|
||||
req.URL.Host = "example.com"
|
||||
body.rewind()
|
||||
if err := body.rewind(); err != nil {
|
||||
log.Println("[ERROR] failed re-read bufferedBody: ", err)
|
||||
}
|
||||
_, _ = http.DefaultTransport.RoundTrip(req)
|
||||
|
||||
// retry request
|
||||
req.URL.Host = host
|
||||
body.rewind()
|
||||
if err := body.rewind(); err != nil {
|
||||
log.Println("[ERROR] failed re-read bufferedBody: ", err)
|
||||
}
|
||||
resp, err := http.DefaultTransport.RoundTrip(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -61,13 +68,15 @@ func TestBodyRetry(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if string(result) != testcase {
|
||||
t.Fatalf("result = %s, want %s", result, testcase)
|
||||
_ = resp.Body.Close()
|
||||
if string(result) != testCase {
|
||||
t.Fatalf("result = %s, want %s", result, testCase)
|
||||
}
|
||||
|
||||
// try one more time for body reuse
|
||||
body.rewind()
|
||||
if err := body.rewind(); err != nil {
|
||||
log.Println("[ERROR] failed re-read bufferedBody: ", err)
|
||||
}
|
||||
resp, err = http.DefaultTransport.RoundTrip(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -76,8 +85,8 @@ func TestBodyRetry(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if string(result) != testcase {
|
||||
t.Fatalf("result = %s, want %s", result, testcase)
|
||||
_ = resp.Body.Close()
|
||||
if string(result) != testCase {
|
||||
t.Fatalf("result = %s, want %s", result, testCase)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ package proxy
|
|||
|
||||
import (
|
||||
"hash/fnv"
|
||||
"log"
|
||||
"math"
|
||||
"math/rand"
|
||||
"net"
|
||||
|
@ -139,7 +140,9 @@ func hostByHashing(pool HostPool, s string) *UpstreamHost {
|
|||
// hash calculates a hash based on string s
|
||||
func hash(s string) uint32 {
|
||||
h := fnv.New32a()
|
||||
h.Write([]byte(s))
|
||||
if _, err := h.Write([]byte(s)); err != nil {
|
||||
log.Println("[ERROR] failed to write bytes: ", err)
|
||||
}
|
||||
return h.Sum32()
|
||||
}
|
||||
|
||||
|
|
|
@ -97,7 +97,9 @@ func TestReverseProxy(t *testing.T) {
|
|||
requestReceived := false
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// read the body (even if it's empty) to make Go parse trailers
|
||||
io.Copy(ioutil.Discard, r.Body)
|
||||
if _, err := io.Copy(ioutil.Discard, r.Body); err != nil {
|
||||
log.Println("[ERROR] failed to copy bytes: ", err)
|
||||
}
|
||||
|
||||
verifyHeadersTrailers(r.Header, r.Trailer)
|
||||
requestReceived = true
|
||||
|
@ -113,7 +115,9 @@ func TestReverseProxy(t *testing.T) {
|
|||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("Hello, client"))
|
||||
if _, err := w.Write([]byte("Hello, client")); err != nil {
|
||||
log.Println("[ERROR] failed to write response: ", err)
|
||||
}
|
||||
|
||||
// Set trailers.
|
||||
shallowCopyTrailers(w.Header(), testTrailers, true)
|
||||
|
@ -149,7 +153,9 @@ func TestReverseProxy(t *testing.T) {
|
|||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
res := w.Result()
|
||||
|
||||
if !requestReceived {
|
||||
|
@ -165,7 +171,9 @@ func TestReverseProxy(t *testing.T) {
|
|||
})
|
||||
rr.Replacer = httpserver.NewReplacer(r, rr, "-")
|
||||
|
||||
p.ServeHTTP(rr, r)
|
||||
if _, err := p.ServeHTTP(rr, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
if got, want := rr.Replacer.Replace("{upstream}"), backend.URL; got != want {
|
||||
t.Errorf("Expected custom placeholder {upstream} to be set (%s), but it wasn't; got: %s", want, got)
|
||||
|
@ -196,7 +204,9 @@ func TestReverseProxyInsecureSkipVerify(t *testing.T) {
|
|||
backend := newTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestReceived = true
|
||||
requestWasHTTP2 = r.ProtoAtLeast(2, 0)
|
||||
w.Write([]byte("Hello, client"))
|
||||
if _, err := w.Write([]byte("Hello, client")); err != nil {
|
||||
log.Println("[ERROR] failed to write response: ", err)
|
||||
}
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
||||
|
@ -210,7 +220,9 @@ func TestReverseProxyInsecureSkipVerify(t *testing.T) {
|
|||
r := httptest.NewRequest("GET", "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
if !requestReceived {
|
||||
t.Error("Even with insecure HTTPS, expected backend to receive request, but it didn't")
|
||||
|
@ -306,7 +318,9 @@ func TestReverseProxyTimeout(t *testing.T) {
|
|||
w := httptest.NewRecorder()
|
||||
|
||||
start := time.Now()
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
took := time.Since(start)
|
||||
|
||||
if took > timeout+errorMargin {
|
||||
|
@ -342,7 +356,9 @@ func TestWebSocketReverseProxyNonHijackerPanic(t *testing.T) {
|
|||
}
|
||||
|
||||
nonHijacker := httptest.NewRecorder()
|
||||
p.ServeHTTP(nonHijacker, r)
|
||||
if _, err := p.ServeHTTP(nonHijacker, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebSocketReverseProxyBackendShutDown(t *testing.T) {
|
||||
|
@ -360,7 +376,9 @@ func TestWebSocketReverseProxyBackendShutDown(t *testing.T) {
|
|||
// Get proxy to use for the test
|
||||
p := newWebSocketTestProxy(backend.URL, false, 30*time.Second)
|
||||
backendProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
}))
|
||||
defer backendProxy.Close()
|
||||
|
||||
|
@ -404,7 +422,9 @@ func TestWebSocketReverseProxyServeHTTPHandler(t *testing.T) {
|
|||
w := &recorderHijacker{httptest.NewRecorder(), new(fakeConn)}
|
||||
|
||||
// Booya! Do the test.
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
// Make sure the backend accepted the WS connection.
|
||||
// Mostly interested in the Upgrade and Connection response headers
|
||||
|
@ -429,7 +449,9 @@ func TestWebSocketReverseProxyFromWSClient(t *testing.T) {
|
|||
// Echo server allows us to test that socket bytes are properly
|
||||
// being proxied.
|
||||
wsEcho := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
|
||||
io.Copy(ws, ws)
|
||||
if _, err := io.Copy(ws, ws); err != nil {
|
||||
log.Println("[ERROR] failed to copy: ", err)
|
||||
}
|
||||
}))
|
||||
defer wsEcho.Close()
|
||||
|
||||
|
@ -441,13 +463,15 @@ func TestWebSocketReverseProxyFromWSClient(t *testing.T) {
|
|||
// WS client will connect to this test server, not
|
||||
// the echo client directly.
|
||||
echoProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
}))
|
||||
defer echoProxy.Close()
|
||||
|
||||
// Set up WebSocket client
|
||||
url := strings.Replace(echoProxy.URL, "http://", "ws://", 1)
|
||||
ws, err := websocket.Dial(url, "", echoProxy.URL)
|
||||
u := strings.Replace(echoProxy.URL, "http://", "ws://", 1)
|
||||
ws, err := websocket.Dial(u, "", echoProxy.URL)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -475,20 +499,24 @@ func TestWebSocketReverseProxyFromWSClient(t *testing.T) {
|
|||
|
||||
func TestWebSocketReverseProxyFromWSSClient(t *testing.T) {
|
||||
wsEcho := newTLSServer(websocket.Handler(func(ws *websocket.Conn) {
|
||||
io.Copy(ws, ws)
|
||||
if _, err := io.Copy(ws, ws); err != nil {
|
||||
log.Println("[ERROR] failed to copy: ", err)
|
||||
}
|
||||
}))
|
||||
defer wsEcho.Close()
|
||||
|
||||
p := newWebSocketTestProxy(wsEcho.URL, true, 30*time.Second)
|
||||
|
||||
echoProxy := newTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
}))
|
||||
defer echoProxy.Close()
|
||||
|
||||
// Set up WebSocket client
|
||||
url := strings.Replace(echoProxy.URL, "https://", "wss://", 1)
|
||||
wsCfg, err := websocket.NewConfig(url, echoProxy.URL)
|
||||
u := strings.Replace(echoProxy.URL, "https://", "wss://", 1)
|
||||
wsCfg, err := websocket.NewConfig(u, echoProxy.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -558,7 +586,9 @@ func TestUnixSocketProxy(t *testing.T) {
|
|||
p := newWebSocketTestProxy(url, false, 30*time.Second)
|
||||
|
||||
echoProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
}))
|
||||
defer echoProxy.Close()
|
||||
|
||||
|
@ -599,14 +629,14 @@ func GetSocketProxy(messageFormat string, prefix string) (*Proxy, *httptest.Serv
|
|||
|
||||
dir, err := ioutil.TempDir("", "caddy_proxytest")
|
||||
if err != nil {
|
||||
return nil, nil, dir, fmt.Errorf("Failed to make temp dir to contain unix socket. %v", err)
|
||||
return nil, nil, dir, fmt.Errorf("failed to make temp dir to contain unix socket. %v", err)
|
||||
}
|
||||
socketPath := filepath.Join(dir, "test_socket")
|
||||
|
||||
ln, err := net.Listen("unix", socketPath)
|
||||
if err != nil {
|
||||
os.RemoveAll(dir)
|
||||
return nil, nil, dir, fmt.Errorf("Unable to listen: %v", err)
|
||||
return nil, nil, dir, fmt.Errorf("unable to listen: %v", err)
|
||||
}
|
||||
ts.Listener = ln
|
||||
|
||||
|
@ -619,7 +649,9 @@ func GetSocketProxy(messageFormat string, prefix string) (*Proxy, *httptest.Serv
|
|||
|
||||
func GetTestServerMessage(p *Proxy, ts *httptest.Server, path string) (string, error) {
|
||||
echoProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
}))
|
||||
|
||||
// *httptest.Server is passed so it can be `defer`red properly
|
||||
|
@ -628,13 +660,13 @@ func GetTestServerMessage(p *Proxy, ts *httptest.Server, path string) (string, e
|
|||
|
||||
res, err := http.Get(echoProxy.URL + path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Unable to GET: %v", err)
|
||||
return "", fmt.Errorf("unable to GET: %v", err)
|
||||
}
|
||||
|
||||
greeting, err := ioutil.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Unable to read body: %v", err)
|
||||
return "", fmt.Errorf("unable to read body: %v", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s", greeting), nil
|
||||
|
@ -707,7 +739,9 @@ func TestUpstreamHeadersUpdate(t *testing.T) {
|
|||
var actualHeaders http.Header
|
||||
var actualHost string
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("Hello, client"))
|
||||
if _, err := w.Write([]byte("Hello, client")); err != nil {
|
||||
log.Println("[ERROR] failed to write response: ", err)
|
||||
}
|
||||
actualHeaders = r.Header
|
||||
actualHost = r.Host
|
||||
}))
|
||||
|
@ -752,7 +786,9 @@ func TestUpstreamHeadersUpdate(t *testing.T) {
|
|||
r.Header.Add("Regex-Me", "I was originally this")
|
||||
r.Header.Add("Regexreplace-Me", "The host is bad")
|
||||
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
replacer := httpserver.NewReplacer(r, nil, "")
|
||||
|
||||
|
@ -790,7 +826,9 @@ func TestDownstreamHeadersUpdate(t *testing.T) {
|
|||
w.Header().Add("Overwrite-Me", "Overwrite-Value")
|
||||
w.Header().Add("Regex-Me", "I was originally this")
|
||||
w.Header().Add("Regexreplace-Me", "The host is bad")
|
||||
w.Write([]byte("Hello, client"))
|
||||
if _, err := w.Write([]byte("Hello, client")); err != nil {
|
||||
log.Println("[ERROR] failed to write response: ", err)
|
||||
}
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
||||
|
@ -822,7 +860,9 @@ func TestDownstreamHeadersUpdate(t *testing.T) {
|
|||
// set a predefined overwritten header
|
||||
w.Header().Set("Overwrite-Me", "Initial")
|
||||
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
replacer := httpserver.NewReplacer(r, nil, "")
|
||||
actualHeaders := w.Header()
|
||||
|
@ -880,7 +920,9 @@ func TestMultiReverseProxyFromClient(t *testing.T) {
|
|||
|
||||
// This is a full end-end test, so the proxy handler.
|
||||
proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
}))
|
||||
defer proxy.Close()
|
||||
|
||||
|
@ -936,7 +978,9 @@ func TestHostSimpleProxyNoHeaderForward(t *testing.T) {
|
|||
var requestHost string
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestHost = r.Host
|
||||
w.Write([]byte("Hello, client"))
|
||||
if _, err := w.Write([]byte("Hello, client")); err != nil {
|
||||
log.Println("[ERROR] failed to write response: ", err)
|
||||
}
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
||||
|
@ -951,7 +995,9 @@ func TestHostSimpleProxyNoHeaderForward(t *testing.T) {
|
|||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(backend.URL, "//") {
|
||||
t.Fatalf("The URL of the backend server doesn't contains //: %s", backend.URL)
|
||||
|
@ -1015,7 +1061,9 @@ func testReverseProxyTransparentHeaders(t *testing.T, remoteAddr, forwardedForHe
|
|||
w := httptest.NewRecorder()
|
||||
|
||||
// Act
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
// Assert
|
||||
if got := actualHeaders["X-Forwarded-For"]; !reflect.DeepEqual(got, expected) {
|
||||
|
@ -1028,7 +1076,9 @@ func TestHostHeaderReplacedUsingForward(t *testing.T) {
|
|||
var requestHost string
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestHost = r.Host
|
||||
w.Write([]byte("Hello, client"))
|
||||
if _, err := w.Write([]byte("Hello, client")); err != nil {
|
||||
log.Println("[ERROR] failed to write response: ", err)
|
||||
}
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
||||
|
@ -1046,7 +1096,9 @@ func TestHostHeaderReplacedUsingForward(t *testing.T) {
|
|||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
if proxyHostHeader != requestHost {
|
||||
t.Fatalf("Expected %s as a Host header got %s\n", proxyHostHeader, requestHost)
|
||||
|
@ -1077,11 +1129,17 @@ func basicAuthTestcase(t *testing.T, upstreamUser, clientUser *url.Userinfo) {
|
|||
u, p, ok := r.BasicAuth()
|
||||
|
||||
if ok {
|
||||
w.Write([]byte(u))
|
||||
if _, err := w.Write([]byte(u)); err != nil {
|
||||
log.Println("[ERROR] failed to write bytes: ", err)
|
||||
}
|
||||
}
|
||||
if ok && p != "" {
|
||||
w.Write([]byte(":"))
|
||||
w.Write([]byte(p))
|
||||
if _, err := w.Write([]byte(":")); err != nil {
|
||||
log.Println("[ERROR] failed to write bytes: ", err)
|
||||
}
|
||||
if _, err := w.Write([]byte(p)); err != nil {
|
||||
log.Println("[ERROR] failed to write bytes: ", err)
|
||||
}
|
||||
}
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
@ -1107,7 +1165,9 @@ func basicAuthTestcase(t *testing.T, upstreamUser, clientUser *url.Userinfo) {
|
|||
}
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("Invalid response code: %d", w.Code)
|
||||
|
@ -1243,7 +1303,9 @@ func TestReverseProxyRetry(t *testing.T) {
|
|||
|
||||
// set up proxy
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.Copy(w, r.Body)
|
||||
if _, err := io.Copy(w, r.Body); err != nil {
|
||||
log.Println("[ERROR] failed to copy: ", err)
|
||||
}
|
||||
r.Body.Close()
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
@ -1275,8 +1337,8 @@ func TestReverseProxyRetry(t *testing.T) {
|
|||
}))
|
||||
defer middle.Close()
|
||||
|
||||
testcase := "test content"
|
||||
r, err := http.NewRequest("POST", middle.URL, bytes.NewBufferString(testcase))
|
||||
testCase := "test content"
|
||||
r, err := http.NewRequest("POST", middle.URL, bytes.NewBufferString(testCase))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -1289,8 +1351,8 @@ func TestReverseProxyRetry(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(b) != testcase {
|
||||
t.Fatalf("string(b) = %s, want %s", string(b), testcase)
|
||||
if string(b) != testCase {
|
||||
t.Fatalf("string(b) = %s, want %s", string(b), testCase)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1300,7 +1362,10 @@ func TestReverseProxyLargeBody(t *testing.T) {
|
|||
|
||||
// set up proxy
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.Copy(ioutil.Discard, r.Body)
|
||||
if _, err := io.Copy(ioutil.Discard, r.Body); err != nil {
|
||||
log.Println("[ERROR] failed to copy: ", err)
|
||||
}
|
||||
|
||||
r.Body.Close()
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
@ -1329,8 +1394,8 @@ func TestReverseProxyLargeBody(t *testing.T) {
|
|||
|
||||
// We want to see how much memory the proxy module requires for this request.
|
||||
// So lets record the mem stats before we start it.
|
||||
begMemstats := &runtime.MemStats{}
|
||||
runtime.ReadMemStats(begMemstats)
|
||||
begMemStats := &runtime.MemStats{}
|
||||
runtime.ReadMemStats(begMemStats)
|
||||
|
||||
r, err := http.NewRequest("POST", middle.URL, &noopReader{len: bodySize})
|
||||
if err != nil {
|
||||
|
@ -1347,7 +1412,7 @@ func TestReverseProxyLargeBody(t *testing.T) {
|
|||
runtime.ReadMemStats(endMemstats)
|
||||
|
||||
// ...to calculate the total amount of allocated memory during the request.
|
||||
totalAlloc := endMemstats.TotalAlloc - begMemstats.TotalAlloc
|
||||
totalAlloc := endMemstats.TotalAlloc - begMemStats.TotalAlloc
|
||||
|
||||
// If that's as much as the size of the body itself it's a serious sign that the
|
||||
// request was not "streamed" to the upstream without buffering it first.
|
||||
|
@ -1369,7 +1434,9 @@ func TestCancelRequest(t *testing.T) {
|
|||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("Hello, client"))
|
||||
if _, err := w.Write([]byte("Hello, client")); err != nil {
|
||||
log.Println("[ERROR] failed to write response: ", err)
|
||||
}
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
||||
|
@ -1502,11 +1569,10 @@ func newPrefixedWebSocketTestProxy(backendAddr string, prefix string) *Proxy {
|
|||
}
|
||||
|
||||
type fakeWsUpstream struct {
|
||||
name string
|
||||
without string
|
||||
insecure bool
|
||||
timeout time.Duration
|
||||
fallbackDelay time.Duration
|
||||
name string
|
||||
without string
|
||||
insecure bool
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func (u *fakeWsUpstream) From() string {
|
||||
|
@ -1574,7 +1640,9 @@ var _ httpserver.HTTPInterfaces = testResponseRecorder{}
|
|||
|
||||
func BenchmarkProxy(b *testing.B) {
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("Hello, client"))
|
||||
if _, err := w.Write([]byte("Hello, client")); err != nil {
|
||||
log.Println("[ERROR] failed to write response: ", err)
|
||||
}
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
||||
|
@ -1602,7 +1670,9 @@ func BenchmarkProxy(b *testing.B) {
|
|||
b.Fatalf("Failed to create request: %v", err)
|
||||
}
|
||||
b.StartTimer()
|
||||
p.ServeHTTP(w, r)
|
||||
if _, err := p.ServeHTTP(w, r); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1684,7 +1754,9 @@ func TestQuic(t *testing.T) {
|
|||
return
|
||||
}
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(content))
|
||||
if _, err := w.Write([]byte(content)); err != nil {
|
||||
log.Println("[ERROR] failed to write bytes: ", err)
|
||||
}
|
||||
w.WriteHeader(200)
|
||||
})
|
||||
err = h2quic.ListenAndServeQUIC(
|
||||
|
|
|
@ -31,6 +31,7 @@ import (
|
|||
"crypto/x509"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
@ -69,7 +70,9 @@ func pooledIoCopy(dst io.Writer, src io.Reader) {
|
|||
// Due to that we extend buf's length to its capacity here and
|
||||
// ensure it's always non-zero.
|
||||
bufCap := cap(buf)
|
||||
io.CopyBuffer(dst, src, buf[0:bufCap:bufCap])
|
||||
if _, err := io.CopyBuffer(dst, src, buf[0:bufCap:bufCap]); err != nil {
|
||||
log.Println("[ERROR] failed to copy buffer: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// onExitFlushLoop is a callback set by tests to detect the state of the
|
||||
|
@ -132,12 +135,12 @@ func (rp *ReverseProxy) srvDialerFunc(locator string, timeout time.Duration) fun
|
|||
}
|
||||
|
||||
func singleJoiningSlash(a, b string) string {
|
||||
aslash := strings.HasSuffix(a, "/")
|
||||
bslash := strings.HasPrefix(b, "/")
|
||||
aSlash := strings.HasSuffix(a, "/")
|
||||
bSlash := strings.HasPrefix(b, "/")
|
||||
switch {
|
||||
case aslash && bslash:
|
||||
case aSlash && bSlash:
|
||||
return a + b[1:]
|
||||
case !aslash && !bslash && b != "":
|
||||
case !aSlash && !bSlash && b != "":
|
||||
return a + "/" + b
|
||||
}
|
||||
return a + b
|
||||
|
@ -275,7 +278,9 @@ func NewSingleHostReverseProxy(target *url.URL, without string, keepalive int, t
|
|||
transport.MaxIdleConnsPerHost = keepalive
|
||||
}
|
||||
if httpserver.HTTP2 {
|
||||
http2.ConfigureTransport(transport)
|
||||
if err := http2.ConfigureTransport(transport); err != nil {
|
||||
log.Println("[ERROR] failed to configure transport to use HTTP/2: ", err)
|
||||
}
|
||||
}
|
||||
rp.Transport = transport
|
||||
} else {
|
||||
|
@ -284,7 +289,9 @@ func NewSingleHostReverseProxy(target *url.URL, without string, keepalive int, t
|
|||
Dial: rp.dialer.Dial,
|
||||
}
|
||||
if httpserver.HTTP2 {
|
||||
http2.ConfigureTransport(transport)
|
||||
if err := http2.ConfigureTransport(transport); err != nil {
|
||||
log.Println("[ERROR] failed to configure transport to use HTTP/2: ", err)
|
||||
}
|
||||
}
|
||||
rp.Transport = transport
|
||||
}
|
||||
|
@ -394,7 +401,9 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request,
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outreq.Write(backendConn)
|
||||
if err := outreq.Write(backendConn); err != nil {
|
||||
log.Println("[ERROR] failed to write: ", err)
|
||||
}
|
||||
}
|
||||
defer backendConn.Close()
|
||||
|
||||
|
@ -416,7 +425,9 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request,
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
backendConn.Write(rbuf)
|
||||
if _, err := backendConn.Write(rbuf); err != nil {
|
||||
log.Println("[ERROR] failed to write data to connection: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
go func() {
|
||||
|
@ -434,7 +445,7 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request,
|
|||
bodyOpen := true
|
||||
closeBody := func() {
|
||||
if bodyOpen {
|
||||
res.Body.Close()
|
||||
_ = res.Body.Close()
|
||||
bodyOpen = false
|
||||
}
|
||||
}
|
||||
|
@ -681,7 +692,7 @@ func getTransportDialTLS(t *http.Transport) func(network, addr string) (net.Conn
|
|||
errc <- err
|
||||
}()
|
||||
if err := <-errc; err != nil {
|
||||
plainConn.Close()
|
||||
_ = plainConn.Close()
|
||||
return nil, err
|
||||
}
|
||||
if !tlsClientConfig.InsecureSkipVerify {
|
||||
|
@ -690,7 +701,7 @@ func getTransportDialTLS(t *http.Transport) func(network, addr string) (net.Conn
|
|||
hostname = stripPort(addr)
|
||||
}
|
||||
if err := tlsConn.VerifyHostname(hostname); err != nil {
|
||||
plainConn.Close()
|
||||
_ = plainConn.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package proxy
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
@ -39,10 +40,14 @@ func setupTLSServer() {
|
|||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/test-path" {
|
||||
w.WriteHeader(expectedStatus)
|
||||
w.Write([]byte(expectedResponse))
|
||||
if _, err := w.Write([]byte(expectedResponse)); err != nil {
|
||||
log.Println("[ERROR] failed to write bytes: ", err)
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(404)
|
||||
w.Write([]byte("Not found"))
|
||||
if _, err := w.Write([]byte("Not found")); err != nil {
|
||||
log.Println("[ERROR] failed to write bytes: ", err)
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
@ -52,10 +57,14 @@ func setupTest() {
|
|||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/test-path" {
|
||||
w.WriteHeader(expectedStatus)
|
||||
w.Write([]byte(expectedResponse))
|
||||
if _, err := w.Write([]byte(expectedResponse)); err != nil {
|
||||
log.Println("[ERROR] failed to write bytes: ", err)
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(404)
|
||||
w.Write([]byte("Not found"))
|
||||
if _, err := w.Write([]byte("Not found")); err != nil {
|
||||
log.Println("[ERROR] failed to write bytes: ", err)
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
|
@ -632,8 +633,10 @@ func (u *staticUpstream) healthCheck() {
|
|||
return true
|
||||
}
|
||||
defer func() {
|
||||
io.Copy(ioutil.Discard, r.Body)
|
||||
r.Body.Close()
|
||||
if _, err := io.Copy(ioutil.Discard, r.Body); err != nil {
|
||||
log.Println("[ERROR] failed to copy: ", err)
|
||||
}
|
||||
_ = r.Body.Close()
|
||||
}()
|
||||
if r.StatusCode < 200 || r.StatusCode >= 400 {
|
||||
return true
|
||||
|
|
|
@ -18,6 +18,7 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
@ -249,7 +250,9 @@ func TestParseBlockHealthCheck(t *testing.T) {
|
|||
u := staticUpstream{}
|
||||
c := caddyfile.NewDispenser("Testfile", strings.NewReader(test.config))
|
||||
for c.Next() {
|
||||
parseBlock(&c, &u, false)
|
||||
if err := parseBlock(&c, &u, false); err != nil {
|
||||
log.Println("[ERROR] failed to parse block: ", err)
|
||||
}
|
||||
}
|
||||
if u.HealthCheck.Interval.String() != test.interval {
|
||||
t.Errorf(
|
||||
|
@ -300,7 +303,7 @@ func TestStop(t *testing.T) {
|
|||
// Set up proxy.
|
||||
var counter int64
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body.Close()
|
||||
_ = r.Body.Close()
|
||||
atomic.AddInt64(&counter, 1)
|
||||
}))
|
||||
|
||||
|
@ -522,7 +525,7 @@ func TestHealthCheckPort(t *testing.T) {
|
|||
var counter int64
|
||||
|
||||
healthCounter := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body.Close()
|
||||
_ = r.Body.Close()
|
||||
atomic.AddInt64(&counter, 1)
|
||||
}))
|
||||
|
||||
|
@ -592,8 +595,8 @@ func TestHealthCheckPort(t *testing.T) {
|
|||
|
||||
func TestHealthCheckContentString(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "blablabla good blablabla")
|
||||
r.Body.Close()
|
||||
_, _ = fmt.Fprintf(w, "blablabla good blablabla")
|
||||
_ = r.Body.Close()
|
||||
}))
|
||||
_, port, err := net.SplitHostPort(server.Listener.Addr().String())
|
||||
if err != nil {
|
||||
|
@ -638,7 +641,9 @@ func TestHealthCheckContentString(t *testing.T) {
|
|||
}
|
||||
t.Errorf("Health check bad response")
|
||||
}
|
||||
upstream.Stop()
|
||||
if err := upstream.Stop(); err != nil {
|
||||
log.Println("[ERROR] failed to stop upstream: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ package push
|
|||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
|
@ -67,7 +68,9 @@ func TestMiddlewareWillPushResources(t *testing.T) {
|
|||
pushingWriter := &MockedPusher{ResponseWriter: writer}
|
||||
|
||||
// when
|
||||
middleware.ServeHTTP(pushingWriter, request)
|
||||
if _, err := middleware.ServeHTTP(pushingWriter, request); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
// then
|
||||
expectedPushedResources := map[string]*http.PushOptions{
|
||||
|
@ -111,7 +114,9 @@ func TestMiddlewareWillPushResourcesWithMergedHeaders(t *testing.T) {
|
|||
pushingWriter := &MockedPusher{ResponseWriter: writer}
|
||||
|
||||
// when
|
||||
middleware.ServeHTTP(pushingWriter, request)
|
||||
if _, err := middleware.ServeHTTP(pushingWriter, request); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
// then
|
||||
expectedPushedResources := map[string]*http.PushOptions{
|
||||
|
@ -156,7 +161,9 @@ func TestMiddlewareShouldntDoRecursivePush(t *testing.T) {
|
|||
pushingWriter := &MockedPusher{ResponseWriter: writer}
|
||||
|
||||
// when
|
||||
middleware.ServeHTTP(pushingWriter, request)
|
||||
if _, err := middleware.ServeHTTP(pushingWriter, request); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
// then
|
||||
if len(pushingWriter.pushed) > 0 {
|
||||
|
@ -190,7 +197,9 @@ func TestMiddlewareShouldStopPushingOnError(t *testing.T) {
|
|||
pushingWriter := &MockedPusher{ResponseWriter: writer, returnedError: errors.New("Cannot push right now")}
|
||||
|
||||
// when
|
||||
middleware.ServeHTTP(pushingWriter, request)
|
||||
if _, err := middleware.ServeHTTP(pushingWriter, request); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
// then
|
||||
expectedPushedResources := map[string]*http.PushOptions{
|
||||
|
@ -406,7 +415,7 @@ func TestMiddlewareShouldPushIndexFile(t *testing.T) {
|
|||
|
||||
pushingWriter := &MockedPusher{
|
||||
ResponseWriter: httptest.NewRecorder(),
|
||||
returnedError: errors.New("Cannot push right now"),
|
||||
returnedError: errors.New("cannot push right now"),
|
||||
}
|
||||
|
||||
// when
|
||||
|
@ -461,7 +470,7 @@ func TestMiddlewareShouldNotPushIndexFileWhenNotARule(t *testing.T) {
|
|||
|
||||
pushingWriter := &MockedPusher{
|
||||
ResponseWriter: httptest.NewRecorder(),
|
||||
returnedError: errors.New("Cannot push right now"),
|
||||
returnedError: errors.New("cannot push right now"),
|
||||
}
|
||||
|
||||
// when
|
||||
|
|
|
@ -19,6 +19,7 @@ import (
|
|||
"context"
|
||||
"crypto/tls"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
|
@ -84,7 +85,9 @@ func TestRedirect(t *testing.T) {
|
|||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
re.ServeHTTP(rec, req)
|
||||
if _, err := re.ServeHTTP(rec, req); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
if rec.Header().Get("Location") != test.expectedLocation {
|
||||
t.Errorf("Test %d: Expected Location header to be %q but was %q",
|
||||
|
@ -117,7 +120,9 @@ func TestParametersRedirect(t *testing.T) {
|
|||
req = req.WithContext(ctx)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
re.ServeHTTP(rec, req)
|
||||
if _, err := re.ServeHTTP(rec, req); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
if got, want := rec.Header().Get("Location"), "http://example.com/a?b=c"; got != want {
|
||||
t.Fatalf("Test 1: expected location header %s but was %s", want, got)
|
||||
|
@ -136,7 +141,9 @@ func TestParametersRedirect(t *testing.T) {
|
|||
ctx = context.WithValue(req.Context(), httpserver.OriginalURLCtxKey, *req.URL)
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
re.ServeHTTP(rec, req)
|
||||
if _, err := re.ServeHTTP(rec, req); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
if got, want := rec.Header().Get("Location"), "http://example.com/a/d?b=c&e=f"; got != want {
|
||||
t.Fatalf("Test 2: expected location header %s but was %s", want, got)
|
||||
|
@ -158,7 +165,9 @@ func TestMetaRedirect(t *testing.T) {
|
|||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
re.ServeHTTP(rec, req)
|
||||
if _, err := re.ServeHTTP(rec, req); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(rec.Body)
|
||||
if err != nil {
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package requestid
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
@ -39,7 +40,9 @@ func TestRequestIDHandler(t *testing.T) {
|
|||
}
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(rec, req)
|
||||
if _, err := handler.ServeHTTP(rec, req); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestIDFromHeader(t *testing.T) {
|
||||
|
@ -63,5 +66,7 @@ func TestRequestIDFromHeader(t *testing.T) {
|
|||
req.Header.Set(headerName, headerValue)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(rec, req)
|
||||
if _, err := handler.ServeHTTP(rec, req); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ package rewrite
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
|
@ -123,7 +124,9 @@ func TestRewrite(t *testing.T) {
|
|||
req = req.WithContext(ctx)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
rw.ServeHTTP(rec, req)
|
||||
if _, err := rw.ServeHTTP(rec, req); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
if got, want := rec.Body.String(), test.expectedTo; got != want {
|
||||
t.Errorf("Test %d: Expected URL to be '%s' but was '%s'", i, want, got)
|
||||
|
@ -162,7 +165,9 @@ func TestWordpress(t *testing.T) {
|
|||
req = req.WithContext(ctx)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
rw.ServeHTTP(rec, req)
|
||||
if _, err := rw.ServeHTTP(rec, req); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
if got, want := rec.Body.String(), test.expectedTo; got != want {
|
||||
t.Errorf("Test %d: Expected URL to be '%s' but was '%s'", i, want, got)
|
||||
|
@ -171,6 +176,6 @@ func TestWordpress(t *testing.T) {
|
|||
}
|
||||
|
||||
func urlPrinter(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
fmt.Fprint(w, r.URL.String())
|
||||
_, _ = fmt.Fprint(w, r.URL.String())
|
||||
return 0, nil
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
|
@ -35,7 +36,7 @@ func TestServeHTTP(t *testing.T) {
|
|||
tmpWebRootDir := beforeServeHTTPTest(t)
|
||||
defer afterServeHTTPTest(t, tmpWebRootDir)
|
||||
|
||||
fileserver := FileServer{
|
||||
fileServer := FileServer{
|
||||
Root: http.Dir(filepath.Join(tmpWebRootDir, webrootName)),
|
||||
Hide: []string{"dir/hidden.html"},
|
||||
IndexPages: DefaultIndexPages,
|
||||
|
@ -288,7 +289,7 @@ func TestServeHTTP(t *testing.T) {
|
|||
}
|
||||
|
||||
// perform the test
|
||||
status, err := fileserver.ServeHTTP(responseRecorder, request)
|
||||
status, err := fileServer.ServeHTTP(responseRecorder, request)
|
||||
etag := responseRecorder.Header().Get("Etag")
|
||||
body := responseRecorder.Body.String()
|
||||
vary := responseRecorder.Header().Get("Vary")
|
||||
|
@ -357,7 +358,9 @@ func beforeServeHTTPTest(t *testing.T) string {
|
|||
parentDir := filepath.Dir(absFile)
|
||||
_, err = os.Stat(parentDir)
|
||||
if err != nil {
|
||||
os.MkdirAll(parentDir, os.ModePerm)
|
||||
if err := os.MkdirAll(parentDir, os.ModePerm); err != nil {
|
||||
log.Println("[ERROR] MkdirAll failed: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// now create the test files
|
||||
|
@ -371,7 +374,7 @@ func beforeServeHTTPTest(t *testing.T) string {
|
|||
if err != nil {
|
||||
t.Fatalf("Failed to write to %s. Error was: %v", absFile, err)
|
||||
}
|
||||
f.Close()
|
||||
_ = f.Close()
|
||||
|
||||
// and set the last modified time
|
||||
err = os.Chtimes(absFile, fixedTime, fixedTime)
|
||||
|
@ -511,7 +514,7 @@ func TestServeHTTPFailingStat(t *testing.T) {
|
|||
|
||||
for i, test := range tests {
|
||||
// initialize a file server. The FileSystem will not fail, but calls to the Stat method of the returned File object will
|
||||
fileserver := FileServer{Root: failingFS{err: nil, fileImpl: failingFile{err: test.statErr}}}
|
||||
fileServer := FileServer{Root: failingFS{err: nil, fileImpl: failingFile{err: test.statErr}}}
|
||||
|
||||
// prepare the request and response
|
||||
request, err := http.NewRequest("GET", "https://foo/", nil)
|
||||
|
@ -520,7 +523,7 @@ func TestServeHTTPFailingStat(t *testing.T) {
|
|||
}
|
||||
responseRecorder := httptest.NewRecorder()
|
||||
|
||||
status, actualErr := fileserver.ServeHTTP(responseRecorder, request)
|
||||
status, actualErr := fileServer.ServeHTTP(responseRecorder, request)
|
||||
|
||||
// check the status
|
||||
if status != test.expectedStatus {
|
||||
|
|
|
@ -17,6 +17,7 @@ package templates
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
|
@ -127,7 +128,9 @@ func TestTemplates(t *testing.T) {
|
|||
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
c.tpl.ServeHTTP(rec, req)
|
||||
if _, err := c.tpl.ServeHTTP(rec, req); err != nil {
|
||||
log.Println("[ERROR] failed to serve HTTP: ", err)
|
||||
}
|
||||
|
||||
if rec.Code != c.respCode {
|
||||
t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, c.respCode)
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -80,9 +81,9 @@ type (
|
|||
|
||||
// ServeHTTP converts the HTTP request to a WebSocket connection and serves it up.
|
||||
func (ws WebSocket) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
for _, sockconfig := range ws.Sockets {
|
||||
if httpserver.Path(r.URL.Path).Matches(sockconfig.Path) {
|
||||
return serveWS(w, r, &sockconfig)
|
||||
for _, sockConfig := range ws.Sockets {
|
||||
if httpserver.Path(r.URL.Path).Matches(sockConfig.Path) {
|
||||
return serveWS(w, r, &sockConfig)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -135,7 +136,7 @@ func serveWS(w http.ResponseWriter, r *http.Request, config *Config) (int, error
|
|||
go pumpStdout(conn, stdout, done)
|
||||
pumpStdin(conn, stdin)
|
||||
|
||||
stdin.Close() // close stdin to end the process
|
||||
_ = stdin.Close() // close stdin to end the process
|
||||
|
||||
if err := cmd.Process.Signal(os.Interrupt); err != nil { // signal an interrupt to kill the process
|
||||
return http.StatusInternalServerError, err
|
||||
|
@ -155,7 +156,9 @@ func serveWS(w http.ResponseWriter, r *http.Request, config *Config) (int, error
|
|||
// status for an "exited" process is greater
|
||||
// than 0, but isn't really an error per se.
|
||||
// just going to ignore it for now.
|
||||
cmd.Wait()
|
||||
if err := cmd.Wait(); err != nil {
|
||||
log.Println("[ERROR] failed to release resources: ", err)
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
@ -221,8 +224,15 @@ func pumpStdin(conn *websocket.Conn, stdin io.WriteCloser) {
|
|||
// Setup our connection's websocket ping/pong handlers from our const values.
|
||||
defer conn.Close()
|
||||
conn.SetReadLimit(maxMessageSize)
|
||||
conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||
conn.SetPongHandler(func(string) error { conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
||||
if err := conn.SetReadDeadline(time.Now().Add(pongWait)); err != nil {
|
||||
log.Println("[ERROR] failed to set read deadline: ", err)
|
||||
}
|
||||
conn.SetPongHandler(func(string) error {
|
||||
if err := conn.SetReadDeadline(time.Now().Add(pongWait)); err != nil {
|
||||
log.Println("[ERROR] failed to set read deadline: ", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
for {
|
||||
_, message, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
|
@ -240,19 +250,24 @@ func pumpStdin(conn *websocket.Conn, stdin io.WriteCloser) {
|
|||
func pumpStdout(conn *websocket.Conn, stdout io.Reader, done chan struct{}) {
|
||||
go pinger(conn, done)
|
||||
defer func() {
|
||||
conn.Close()
|
||||
_ = conn.Close()
|
||||
close(done) // make sure to close the pinger when we are done.
|
||||
}()
|
||||
|
||||
s := bufio.NewScanner(stdout)
|
||||
for s.Scan() {
|
||||
conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
if err := conn.SetWriteDeadline(time.Now().Add(writeWait)); err != nil {
|
||||
log.Println("[ERROR] failed to set write deadline: ", err)
|
||||
}
|
||||
if err := conn.WriteMessage(websocket.TextMessage, bytes.TrimSpace(s.Bytes())); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if s.Err() != nil {
|
||||
conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseGoingAway, s.Err().Error()), time.Time{})
|
||||
err := conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseGoingAway, s.Err().Error()), time.Time{})
|
||||
if err != nil {
|
||||
log.Println("[ERROR] WriteControl failed: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -265,7 +280,10 @@ func pinger(conn *websocket.Conn, done chan struct{}) {
|
|||
select {
|
||||
case <-ticker.C:
|
||||
if err := conn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(writeWait)); err != nil {
|
||||
conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseGoingAway, err.Error()), time.Time{})
|
||||
err := conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseGoingAway, err.Error()), time.Time{})
|
||||
if err != nil {
|
||||
log.Println("[ERROR] WriteControl failed: ", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
case <-done:
|
||||
|
|
|
@ -36,7 +36,9 @@ import (
|
|||
func init() {
|
||||
// opt-in TLS 1.3 for Go1.12
|
||||
// TODO: remove this line when Go1.13 is released.
|
||||
os.Setenv("GODEBUG", os.Getenv("GODEBUG")+",tls13=1")
|
||||
if err := os.Setenv("GODEBUG", os.Getenv("GODEBUG")+",tls13=1"); err != nil {
|
||||
log.Println("[ERROR] failed to set environment variable: ", err)
|
||||
}
|
||||
|
||||
caddy.RegisterPlugin("tls", caddy.Plugin{Action: setupTLS})
|
||||
|
||||
|
@ -409,26 +411,34 @@ func loadCertsInDir(cfg *Config, c *caddy.Controller, dir string) error {
|
|||
|
||||
if derBlock.Type == "CERTIFICATE" {
|
||||
// Re-encode certificate as PEM, appending to certificate chain
|
||||
pem.Encode(certBuilder, derBlock)
|
||||
if err := pem.Encode(certBuilder, derBlock); err != nil {
|
||||
log.Println("[ERROR] failed to write PEM encoding: ", err)
|
||||
}
|
||||
} else if derBlock.Type == "EC PARAMETERS" {
|
||||
// EC keys generated from openssl can be composed of two blocks:
|
||||
// parameters and key (parameter block should come first)
|
||||
if !foundKey {
|
||||
// Encode parameters
|
||||
pem.Encode(keyBuilder, derBlock)
|
||||
if err := pem.Encode(keyBuilder, derBlock); err != nil {
|
||||
log.Println("[ERROR] failed to write PEM encoding: ", err)
|
||||
}
|
||||
|
||||
// Key must immediately follow
|
||||
derBlock, bundle = pem.Decode(bundle)
|
||||
if derBlock == nil || derBlock.Type != "EC PRIVATE KEY" {
|
||||
return c.Errf("%s: expected elliptic private key to immediately follow EC parameters", path)
|
||||
}
|
||||
pem.Encode(keyBuilder, derBlock)
|
||||
if err := pem.Encode(keyBuilder, derBlock); err != nil {
|
||||
log.Println("[ERROR] failed to write PEM encoding: ", err)
|
||||
}
|
||||
foundKey = true
|
||||
}
|
||||
} else if derBlock.Type == "PRIVATE KEY" || strings.HasSuffix(derBlock.Type, " PRIVATE KEY") {
|
||||
// RSA key
|
||||
if !foundKey {
|
||||
pem.Encode(keyBuilder, derBlock)
|
||||
if err := pem.Encode(keyBuilder, derBlock); err != nil {
|
||||
log.Println("[ERROR] failed to write PEM encoding: ", err)
|
||||
}
|
||||
foundKey = true
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -283,7 +283,10 @@ func atomicAdd(key string, amount int) {
|
|||
// Do not use this for cryptographic purposes.
|
||||
func FastHash(input []byte) string {
|
||||
h := fnv.New32a()
|
||||
h.Write(input)
|
||||
if _, err := h.Write(input); err != nil {
|
||||
log.Println("[ERROR] failed to write bytes: ", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%x", h.Sum32())
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user