From a23ccb1e8bcca32536eb52097fc521eff3f6870a Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 13 Dec 2020 13:41:15 +0100 Subject: [PATCH] add OnRequest, OnResponse to ServerConn.Read --- serverconn.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/serverconn.go b/serverconn.go index 331f3996..41e28e28 100644 --- a/serverconn.go +++ b/serverconn.go @@ -59,7 +59,14 @@ func (sc *ServerConn) EnableReadTimeout(v bool) { } // ServerConnReadHandlers allows to set the handlers required by ServerConn.Read. +// all fields are optional. type ServerConnReadHandlers struct { + // called after receiving any request. + OnRequest func(req *base.Request) + + // called before sending any response. + OnResponse func(res *base.Response) + // called after receiving a OPTIONS request. // if nil, it is generated automatically. OnOptions func(req *base.Request) (*base.Response, error) @@ -96,6 +103,10 @@ type ServerConnReadHandlers struct { func (sc *ServerConn) backgroundRead(handlers ServerConnReadHandlers, done chan error) { handleRequest := func(req *base.Request) (*base.Response, error) { + if handlers.OnRequest != nil { + handlers.OnRequest(req) + } + switch req.Method { case base.Options: if handlers.OnOptions != nil { @@ -250,6 +261,10 @@ func (sc *ServerConn) backgroundRead(handlers ServerConnReadHandlers, done chan // add server res.Header["Server"] = base.HeaderValue{"gortsplib"} + if handlers.OnResponse != nil { + handlers.OnResponse(res) + } + sc.nconn.SetWriteDeadline(time.Now().Add(sc.s.conf.WriteTimeout)) res.Write(sc.bw)