From 6f19ec03eeb95a8bd6f423a8b030bc589a875bb1 Mon Sep 17 00:00:00 2001 From: sada-sigsci Date: Wed, 13 Nov 2024 16:58:13 -0800 Subject: [PATCH] address review comments --- module.go | 7 ++++++- responsewriter.go | 18 ++++++++++++------ rpc.go => schema/rpc.go | 2 +- rpc_gen.go => schema/rpc_gen.go | 2 +- scripts/build.sh | 4 ++-- 5 files changed, 22 insertions(+), 11 deletions(-) rename rpc.go => schema/rpc.go (99%) rename rpc_gen.go => schema/rpc_gen.go (99%) diff --git a/module.go b/module.go index 425480b..84f7b10 100644 --- a/module.go +++ b/module.go @@ -12,9 +12,14 @@ import ( "sync" "time" + "github.com/signalsciences/sigsci-module-golang/schema" "github.com/signalsciences/tlstext" ) +type RPCMsgIn = schema.RPCMsgIn +type RPCMsgIn2 = schema.RPCMsgIn2 +type RPCMsgOut = schema.RPCMsgOut + // Module is an http.Handler that wraps an existing handler with // data collection and sends it to the Signal Sciences Agent for // inspection. @@ -115,7 +120,7 @@ func (m *Module) ServeHTTP(w http.ResponseWriter, req *http.Request) { return } - rw := NewResponseWriter(w, out.RespActions) + rw := newResponseWriter(w, out.RespActions) wafresponse := out.WAFResponse switch { diff --git a/responsewriter.go b/responsewriter.go index a6c51ed..761a7c4 100644 --- a/responsewriter.go +++ b/responsewriter.go @@ -6,6 +6,8 @@ import ( "io" "net" "net/http" + + "github.com/signalsciences/sigsci-module-golang/schema" ) // ResponseWriter is a http.ResponseWriter allowing extraction of data needed for inspection @@ -23,7 +25,11 @@ type ResponseWriterFlusher interface { } // NewResponseWriter returns a ResponseWriter or ResponseWriterFlusher depending on the base http.ResponseWriter. -func NewResponseWriter(base http.ResponseWriter, actions []Action) ResponseWriter { +func NewResponseWriter(base http.ResponseWriter) ResponseWriter { + return newResponseWriter(base, nil) +} + +func newResponseWriter(base http.ResponseWriter, actions []schema.Action) ResponseWriter { // NOTE: according to net/http docs, if WriteHeader is not called explicitly, // the first call to Write will trigger an implicit WriteHeader(http.StatusOK). // this is why the default code is 200 and it only changes if WriteHeader is called. @@ -43,7 +49,7 @@ type responseRecorder struct { base http.ResponseWriter code int size int64 - actions []Action + actions []schema.Action } // BaseResponseWriter returns the base http.ResponseWriter allowing access if needed @@ -79,15 +85,15 @@ func (w *responseRecorder) mergeHeader() { hdr := w.base.Header() for _, a := range w.actions { switch a.Code { - case AddHdr: + case schema.AddHdr: hdr.Add(a.Args[0], a.Args[1]) - case SetHdr: + case schema.SetHdr: hdr.Set(a.Args[0], a.Args[1]) - case SetNEHdr: + case schema.SetNEHdr: if len(hdr.Get(a.Args[0])) == 0 { hdr.Set(a.Args[0], a.Args[1]) } - case DelHdr: + case schema.DelHdr: hdr.Del(a.Args[0]) } } diff --git a/rpc.go b/schema/rpc.go similarity index 99% rename from rpc.go rename to schema/rpc.go index b7e0e23..49cb0a4 100644 --- a/rpc.go +++ b/schema/rpc.go @@ -1,4 +1,4 @@ -package sigsci +package schema //go:generate go run github.com/tinylib/msgp@v1.2.4 -unexported -tests=false diff --git a/rpc_gen.go b/schema/rpc_gen.go similarity index 99% rename from rpc_gen.go rename to schema/rpc_gen.go index ced7e99..9ff9705 100644 --- a/rpc_gen.go +++ b/schema/rpc_gen.go @@ -1,4 +1,4 @@ -package sigsci +package schema // Code generated by github.com/tinylib/msgp DO NOT EDIT. diff --git a/scripts/build.sh b/scripts/build.sh index c49fcb6..76d2d53 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -30,9 +30,9 @@ go test . rm -rf artifacts/ mkdir -p artifacts/sigsci-module-golang -cp -rf \ +cp --parents -rf \ VERSION CHANGELOG.md LICENSE.md README.md \ - clientcodec.go rpc.go rpc_gen.go rpcinspector.go inspector.go responsewriter.go module.go version.go config.go \ + clientcodec.go schema/rpc.go schema/rpc_gen.go rpcinspector.go inspector.go responsewriter.go module.go version.go config.go \ responsewriter_test.go module_test.go config_test.go \ examples \ artifacts/sigsci-module-golang/