feat: log 404 requests with traceid (#1554)
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
package cors
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/internal/response"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -30,7 +29,7 @@ const (
|
||||
// At most one origin can be specified, other origins are ignored if given, default to be *.
|
||||
func NotAllowedHandler(fn func(w http.ResponseWriter), origins ...string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gw := &guardedResponseWriter{w: w}
|
||||
gw := response.NewHeaderOnceResponseWriter(w)
|
||||
checkAndSetHeaders(gw, r, origins)
|
||||
if fn != nil {
|
||||
fn(gw)
|
||||
@@ -62,44 +61,6 @@ func Middleware(fn func(w http.Header), origins ...string) func(http.HandlerFunc
|
||||
}
|
||||
}
|
||||
|
||||
type guardedResponseWriter struct {
|
||||
w http.ResponseWriter
|
||||
wroteHeader bool
|
||||
}
|
||||
|
||||
func (w *guardedResponseWriter) Flush() {
|
||||
if flusher, ok := w.w.(http.Flusher); ok {
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
func (w *guardedResponseWriter) Header() http.Header {
|
||||
return w.w.Header()
|
||||
}
|
||||
|
||||
// Hijack implements the http.Hijacker interface.
|
||||
// This expands the Response to fulfill http.Hijacker if the underlying http.ResponseWriter supports it.
|
||||
func (w *guardedResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
if hijacked, ok := w.w.(http.Hijacker); ok {
|
||||
return hijacked.Hijack()
|
||||
}
|
||||
|
||||
return nil, nil, errors.New("server doesn't support hijacking")
|
||||
}
|
||||
|
||||
func (w *guardedResponseWriter) Write(bytes []byte) (int, error) {
|
||||
return w.w.Write(bytes)
|
||||
}
|
||||
|
||||
func (w *guardedResponseWriter) WriteHeader(code int) {
|
||||
if w.wroteHeader {
|
||||
return
|
||||
}
|
||||
|
||||
w.w.WriteHeader(code)
|
||||
w.wroteHeader = true
|
||||
}
|
||||
|
||||
func checkAndSetHeaders(w http.ResponseWriter, r *http.Request, origins []string) {
|
||||
setVaryHeaders(w, r)
|
||||
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package cors
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -131,48 +129,3 @@ func TestCorsHandlerWithOrigins(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGuardedResponseWriter_Flush(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
handler := NotAllowedHandler(func(w http.ResponseWriter) {
|
||||
w.Header().Set("X-Test", "test")
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
_, err := w.Write([]byte("content"))
|
||||
assert.Nil(t, err)
|
||||
|
||||
flusher, ok := w.(http.Flusher)
|
||||
assert.True(t, ok)
|
||||
flusher.Flush()
|
||||
}, "foo.com")
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
|
||||
assert.Equal(t, "test", resp.Header().Get("X-Test"))
|
||||
assert.Equal(t, "content", resp.Body.String())
|
||||
}
|
||||
|
||||
func TestGuardedResponseWriter_Hijack(t *testing.T) {
|
||||
resp := httptest.NewRecorder()
|
||||
writer := &guardedResponseWriter{
|
||||
w: resp,
|
||||
}
|
||||
assert.NotPanics(t, func() {
|
||||
writer.Hijack()
|
||||
})
|
||||
|
||||
writer = &guardedResponseWriter{
|
||||
w: mockedHijackable{resp},
|
||||
}
|
||||
assert.NotPanics(t, func() {
|
||||
writer.Hijack()
|
||||
})
|
||||
}
|
||||
|
||||
type mockedHijackable struct {
|
||||
*httptest.ResponseRecorder
|
||||
}
|
||||
|
||||
func (m mockedHijackable) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
57
rest/internal/response/headeronceresponsewriter.go
Normal file
57
rest/internal/response/headeronceresponsewriter.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// HeaderOnceResponseWriter is a http.ResponseWriter implementation
|
||||
// that only the first WriterHeader takes effect.
|
||||
type HeaderOnceResponseWriter struct {
|
||||
w http.ResponseWriter
|
||||
wroteHeader bool
|
||||
}
|
||||
|
||||
// NewHeaderOnceResponseWriter returns a HeaderOnceResponseWriter.
|
||||
func NewHeaderOnceResponseWriter(w http.ResponseWriter) http.ResponseWriter {
|
||||
return &HeaderOnceResponseWriter{w: w}
|
||||
}
|
||||
|
||||
// Flush flushes the response writer.
|
||||
func (w *HeaderOnceResponseWriter) Flush() {
|
||||
if flusher, ok := w.w.(http.Flusher); ok {
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
// Header returns the http header.
|
||||
func (w *HeaderOnceResponseWriter) Header() http.Header {
|
||||
return w.w.Header()
|
||||
}
|
||||
|
||||
// Hijack implements the http.Hijacker interface.
|
||||
// This expands the Response to fulfill http.Hijacker if the underlying http.ResponseWriter supports it.
|
||||
func (w *HeaderOnceResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
if hijacked, ok := w.w.(http.Hijacker); ok {
|
||||
return hijacked.Hijack()
|
||||
}
|
||||
|
||||
return nil, nil, errors.New("server doesn't support hijacking")
|
||||
}
|
||||
|
||||
// Write writes bytes into w.
|
||||
func (w *HeaderOnceResponseWriter) Write(bytes []byte) (int, error) {
|
||||
return w.w.Write(bytes)
|
||||
}
|
||||
|
||||
// WriteHeader writes code into w, and not sealing the writer.
|
||||
func (w *HeaderOnceResponseWriter) WriteHeader(code int) {
|
||||
if w.wroteHeader {
|
||||
return
|
||||
}
|
||||
|
||||
w.w.WriteHeader(code)
|
||||
w.wroteHeader = true
|
||||
}
|
||||
58
rest/internal/response/headeronceresponsewriter_test.go
Normal file
58
rest/internal/response/headeronceresponsewriter_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestHeaderOnceResponseWriter_Flush(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
cw := NewHeaderOnceResponseWriter(w)
|
||||
cw.Header().Set("X-Test", "test")
|
||||
cw.WriteHeader(http.StatusServiceUnavailable)
|
||||
cw.WriteHeader(http.StatusExpectationFailed)
|
||||
_, err := cw.Write([]byte("content"))
|
||||
assert.Nil(t, err)
|
||||
|
||||
flusher, ok := cw.(http.Flusher)
|
||||
assert.True(t, ok)
|
||||
flusher.Flush()
|
||||
})
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
|
||||
assert.Equal(t, "test", resp.Header().Get("X-Test"))
|
||||
assert.Equal(t, "content", resp.Body.String())
|
||||
}
|
||||
|
||||
func TestHeaderOnceResponseWriter_Hijack(t *testing.T) {
|
||||
resp := httptest.NewRecorder()
|
||||
writer := &HeaderOnceResponseWriter{
|
||||
w: resp,
|
||||
}
|
||||
assert.NotPanics(t, func() {
|
||||
writer.Hijack()
|
||||
})
|
||||
|
||||
writer = &HeaderOnceResponseWriter{
|
||||
w: mockedHijackable{resp},
|
||||
}
|
||||
assert.NotPanics(t, func() {
|
||||
writer.Hijack()
|
||||
})
|
||||
}
|
||||
|
||||
type mockedHijackable struct {
|
||||
*httptest.ResponseRecorder
|
||||
}
|
||||
|
||||
func (m mockedHijackable) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
return nil, nil, nil
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package security
|
||||
package response
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
@@ -27,7 +28,11 @@ func (w *WithCodeResponseWriter) Header() http.Header {
|
||||
// Hijack implements the http.Hijacker interface.
|
||||
// This expands the Response to fulfill http.Hijacker if the underlying http.ResponseWriter supports it.
|
||||
func (w *WithCodeResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
return w.Writer.(http.Hijacker).Hijack()
|
||||
if hijacked, ok := w.Writer.(http.Hijacker); ok {
|
||||
return hijacked.Hijack()
|
||||
}
|
||||
|
||||
return nil, nil, errors.New("server doesn't support hijacking")
|
||||
}
|
||||
|
||||
// Write writes bytes into w.
|
||||
@@ -1,4 +1,4 @@
|
||||
package security
|
||||
package response
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -31,3 +31,20 @@ func TestWithCodeResponseWriter(t *testing.T) {
|
||||
assert.Equal(t, "test", resp.Header().Get("X-Test"))
|
||||
assert.Equal(t, "content", resp.Body.String())
|
||||
}
|
||||
|
||||
func TestWithCodeResponseWriter_Hijack(t *testing.T) {
|
||||
resp := httptest.NewRecorder()
|
||||
writer := &WithCodeResponseWriter{
|
||||
Writer: resp,
|
||||
}
|
||||
assert.NotPanics(t, func() {
|
||||
writer.Hijack()
|
||||
})
|
||||
|
||||
writer = &WithCodeResponseWriter{
|
||||
Writer: mockedHijackable{resp},
|
||||
}
|
||||
assert.NotPanics(t, func() {
|
||||
writer.Hijack()
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user