mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-14 15:56:44 +03:00
Added RequestCtx.LocalAddr() for determining server address for the current connection
This commit is contained in:
@@ -178,8 +178,8 @@ func (cl *ctxLogger) Printf(format string, args ...interface{}) {
|
||||
ctx := cl.ctx
|
||||
req := &ctx.Request
|
||||
req.ParseURI()
|
||||
cl.logger.Printf("%.3f #%016X - %s - %s %s - %s",
|
||||
time.Since(ctx.Time).Seconds(), ctx.ID, ctx.RemoteAddr(), req.Header.Method, req.URI.URI, s)
|
||||
cl.logger.Printf("%.3f #%016X - %s<->%s - %s %s - %s",
|
||||
time.Since(ctx.Time).Seconds(), ctx.ID, ctx.LocalAddr(), ctx.RemoteAddr(), req.Header.Method, req.URI.URI, s)
|
||||
ctxLoggerLock.Unlock()
|
||||
}
|
||||
|
||||
@@ -198,6 +198,17 @@ func (ctx *RequestCtx) RemoteAddr() net.Addr {
|
||||
return addr
|
||||
}
|
||||
|
||||
// LocalAddr returns server address for the given request.
|
||||
//
|
||||
// Always returns non-nil result.
|
||||
func (ctx *RequestCtx) LocalAddr() net.Addr {
|
||||
addr := ctx.c.LocalAddr()
|
||||
if addr == nil {
|
||||
return zeroTCPAddr
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// RemoteIP returns client ip for the given request.
|
||||
//
|
||||
// Always returns non-nil result.
|
||||
@@ -656,6 +667,10 @@ func (fa *fakeAddrer) RemoteAddr() net.Addr {
|
||||
return fa.addr
|
||||
}
|
||||
|
||||
func (fa *fakeAddrer) LocalAddr() net.Addr {
|
||||
return fa.addr
|
||||
}
|
||||
|
||||
func (fa *fakeAddrer) Read(p []byte) (int, error) {
|
||||
panic("BUG: unexpected Read call")
|
||||
}
|
||||
|
||||
+13
-5
@@ -22,7 +22,7 @@ func TestRequestCtxInit(t *testing.T) {
|
||||
}
|
||||
ctx.Logger().Printf("foo bar %d", 10)
|
||||
|
||||
expectedLog := "0.000 #0012345700000000 - 0.0.0.0:0 - http:// - foo bar 10\n"
|
||||
expectedLog := "0.000 #0012345700000000 - 0.0.0.0:0<->0.0.0.0:0 - http:// - foo bar 10\n"
|
||||
if logger.out != expectedLog {
|
||||
t.Fatalf("Unexpected log output: %q. Expected %q", logger.out, expectedLog)
|
||||
}
|
||||
@@ -247,10 +247,10 @@ func TestServerLogger(t *testing.T) {
|
||||
verifyResponse(t, br, 200, "text/html", "requestURI=/foo1, body=\"\", remoteAddr=1.2.3.4:8765")
|
||||
verifyResponse(t, br, 200, "text/html", "requestURI=/foo2, body=\"abcde\", remoteAddr=1.2.3.4:8765")
|
||||
|
||||
expectedLogOut := `0.000 #0000000100000001 - 1.2.3.4:8765 - GET http://google.com/foo1 - begin
|
||||
0.000 #0000000100000001 - 1.2.3.4:8765 - GET http://google.com/foo1 - end
|
||||
0.000 #0000000100000002 - 1.2.3.4:8765 - POST http://aaa.com/foo2 - begin
|
||||
0.000 #0000000100000002 - 1.2.3.4:8765 - POST http://aaa.com/foo2 - end
|
||||
expectedLogOut := `0.000 #0000000100000001 - 1.2.3.4:8765<->1.2.3.4:8765 - GET http://google.com/foo1 - begin
|
||||
0.000 #0000000100000001 - 1.2.3.4:8765<->1.2.3.4:8765 - GET http://google.com/foo1 - end
|
||||
0.000 #0000000100000002 - 1.2.3.4:8765<->1.2.3.4:8765 - POST http://aaa.com/foo2 - begin
|
||||
0.000 #0000000100000002 - 1.2.3.4:8765<->1.2.3.4:8765 - POST http://aaa.com/foo2 - end
|
||||
`
|
||||
if cl.out != expectedLogOut {
|
||||
t.Fatalf("Unexpected logger output: %q. Expected %q", cl.out, expectedLogOut)
|
||||
@@ -317,6 +317,10 @@ func (rw *readWriterRemoteAddr) RemoteAddr() net.Addr {
|
||||
return rw.addr
|
||||
}
|
||||
|
||||
func (rw *readWriterRemoteAddr) LocalAddr() net.Addr {
|
||||
return rw.addr
|
||||
}
|
||||
|
||||
func TestServerConnError(t *testing.T) {
|
||||
s := &Server{
|
||||
Handler: func(ctx *RequestCtx) {
|
||||
@@ -452,3 +456,7 @@ func (rw *readWriter) Write(b []byte) (int, error) {
|
||||
func (rw *readWriter) RemoteAddr() net.Addr {
|
||||
return zeroTCPAddr
|
||||
}
|
||||
|
||||
func (rw *readWriter) LocalAddr() net.Addr {
|
||||
return zeroTCPAddr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user