Browse Source

hls: dump requests and responses when logLevel is debug

pull/666/head
aler9 5 years ago
parent
commit
c51ba926e0
  1. 53
      internal/core/api.go
  2. 15
      internal/core/hls_server.go

53
internal/core/api.go

@ -17,6 +17,32 @@ import (
"github.com/aler9/rtsp-simple-server/internal/logger" "github.com/aler9/rtsp-simple-server/internal/logger"
) )
type httpLogWriter struct {
gin.ResponseWriter
buf bytes.Buffer
}
func (w *httpLogWriter) Write(b []byte) (int, error) {
w.buf.Write(b)
return w.ResponseWriter.Write(b)
}
func (w *httpLogWriter) WriteString(s string) (int, error) {
w.buf.WriteString(s)
return w.ResponseWriter.WriteString(s)
}
func (w *httpLogWriter) dump() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s %d %s\n", "HTTP/1.1", w.ResponseWriter.Status(), http.StatusText(w.ResponseWriter.Status()))
w.ResponseWriter.Header().Write(&buf)
buf.Write([]byte("\n"))
if w.buf.Len() > 0 {
fmt.Fprintf(&buf, "(body of %d bytes)", w.buf.Len())
}
return buf.String()
}
func interfaceIsEmpty(i interface{}) bool { func interfaceIsEmpty(i interface{}) bool {
return reflect.ValueOf(i).Kind() != reflect.Ptr || reflect.ValueOf(i).IsNil() return reflect.ValueOf(i).Kind() != reflect.Ptr || reflect.ValueOf(i).IsNil()
} }
@ -307,39 +333,18 @@ func (a *api) log(level logger.Level, format string, args ...interface{}) {
a.parent.Log(level, "[API] "+format, args...) a.parent.Log(level, "[API] "+format, args...)
} }
type logWriter struct {
gin.ResponseWriter
buf bytes.Buffer
}
func (w *logWriter) Write(b []byte) (int, error) {
w.buf.Write(b)
return w.ResponseWriter.Write(b)
}
func (w *logWriter) WriteString(s string) (int, error) {
w.buf.WriteString(s)
return w.ResponseWriter.WriteString(s)
}
func (a *api) mwLog(ctx *gin.Context) { func (a *api) mwLog(ctx *gin.Context) {
byts, _ := httputil.DumpRequest(ctx.Request, true) byts, _ := httputil.DumpRequest(ctx.Request, true)
a.log(logger.Debug, "[c->s] %s", string(byts)) a.log(logger.Debug, "[c->s] %s", string(byts))
blw := &logWriter{ResponseWriter: ctx.Writer} logw := &httpLogWriter{ResponseWriter: ctx.Writer}
ctx.Writer = blw ctx.Writer = logw
ctx.Writer.Header().Set("Server", "rtsp-simple-server") ctx.Writer.Header().Set("Server", "rtsp-simple-server")
ctx.Next() ctx.Next()
var buf bytes.Buffer a.log(logger.Debug, "[s->c] %s", logw.dump())
fmt.Fprintf(&buf, "%s %d %s\n", ctx.Request.Proto, ctx.Writer.Status(), http.StatusText(ctx.Writer.Status()))
ctx.Writer.Header().Write(&buf)
buf.Write([]byte("\n"))
buf.Write(blw.buf.Bytes())
a.log(logger.Debug, "[s->c] %s", buf.String())
} }
func (a *api) onConfigGet(ctx *gin.Context) { func (a *api) onConfigGet(ctx *gin.Context) {

15
internal/core/hls_server.go

@ -5,6 +5,7 @@ import (
"io" "io"
"net" "net"
"net/http" "net/http"
"net/http/httputil"
gopath "path" gopath "path"
"strings" "strings"
"sync" "sync"
@ -137,10 +138,13 @@ outer:
} }
func (s *hlsServer) onRequest(ctx *gin.Context) { func (s *hlsServer) onRequest(ctx *gin.Context) {
s.Log(logger.Info, "[conn %v] %s %s", ctx.Request.RemoteAddr, ctx.Request.Method, ctx.Request.URL.Path) s.Log(logger.Info, "[client %v] %s %s", ctx.Request.RemoteAddr, ctx.Request.Method, ctx.Request.URL.Path)
// remove leading prefix byts, _ := httputil.DumpRequest(ctx.Request, true)
pa := ctx.Request.URL.Path[1:] s.Log(logger.Debug, "[client %v] [c->s] %s", ctx.Request.RemoteAddr, string(byts))
logw := &httpLogWriter{ResponseWriter: ctx.Writer}
ctx.Writer = logw
ctx.Writer.Header().Set("Server", "rtsp-simple-server") ctx.Writer.Header().Set("Server", "rtsp-simple-server")
ctx.Writer.Header().Set("Access-Control-Allow-Origin", s.hlsAllowOrigin) ctx.Writer.Header().Set("Access-Control-Allow-Origin", s.hlsAllowOrigin)
@ -160,6 +164,9 @@ func (s *hlsServer) onRequest(ctx *gin.Context) {
return return
} }
// remove leading prefix
pa := ctx.Request.URL.Path[1:]
switch pa { switch pa {
case "", "favicon.ico": case "", "favicon.ico":
ctx.Writer.WriteHeader(http.StatusNotFound) ctx.Writer.WriteHeader(http.StatusNotFound)
@ -204,6 +211,8 @@ func (s *hlsServer) onRequest(ctx *gin.Context) {
case <-s.ctx.Done(): case <-s.ctx.Done():
} }
s.Log(logger.Debug, "[client %v] [s->c] %s", ctx.Request.RemoteAddr, logw.dump())
} }
func (s *hlsServer) findOrCreateMuxer(pathName string) *hlsMuxer { func (s *hlsServer) findOrCreateMuxer(pathName string) *hlsMuxer {

Loading…
Cancel
Save