Skip to content

Commit

Permalink
Added RequestCtx.Init2 function for the upcoming httpteleport project
Browse files Browse the repository at this point in the history
  • Loading branch information
valyala committed Sep 20, 2016
1 parent 95be7a5 commit ec59ce3
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,23 @@ func (s *Server) acquireCtx(c net.Conn) *RequestCtx {
return ctx
}

// Init2 prepares ctx for passing to RequestHandler.
//
// conn is used only for determining local and remote addresses.
//
// This function is intended for custom Server implementations.
// See https://github.com/valyala/httpteleport for details.
func (ctx *RequestCtx) Init2(conn net.Conn, logger Logger) {
ctx.c = conn
ctx.logger.logger = logger
ctx.connID = nextConnID()
ctx.s = &fakeServer
ctx.connRequestNum = 0
ctx.connTime = time.Now()
ctx.time = ctx.connTime
ctx.Response.Reset()
}

// Init prepares ctx for passing to RequestHandler.
//
// remoteAddr and logger are optional. They are used by RequestCtx.Logger().
Expand All @@ -1852,35 +1869,31 @@ func (ctx *RequestCtx) Init(req *Request, remoteAddr net.Addr, logger Logger) {
if remoteAddr == nil {
remoteAddr = zeroTCPAddr
}
ctx.c = &fakeAddrer{
addr: remoteAddr,
c := &fakeAddrer{
laddr: zeroTCPAddr,
raddr: remoteAddr,
}
if logger == nil {
logger = defaultLogger
}
ctx.connID = nextConnID()
ctx.logger.logger = logger
ctx.s = &fakeServer
ctx.Init2(c, logger)
req.CopyTo(&ctx.Request)
ctx.Response.Reset()
ctx.connRequestNum = 0
ctx.connTime = time.Now()
ctx.time = ctx.connTime
}

var fakeServer Server

type fakeAddrer struct {
net.Conn
addr net.Addr
laddr net.Addr
raddr net.Addr
}

func (fa *fakeAddrer) RemoteAddr() net.Addr {
return fa.addr
return fa.raddr
}

func (fa *fakeAddrer) LocalAddr() net.Addr {
return fa.addr
return fa.laddr
}

func (fa *fakeAddrer) Read(p []byte) (int, error) {
Expand Down

0 comments on commit ec59ce3

Please sign in to comment.