Server header

This commit is contained in:
Daniel 2024-10-22 10:21:11 -04:00
parent 5b5b02b5fd
commit daabec4941
5 changed files with 29 additions and 6 deletions

View file

@ -29,8 +29,14 @@ var (
username = flag.String("user", "", "username")
password = flag.String("password", "", "password")
secure = flag.Bool("s", false, "secure (https/wss)")
uaString = version.HttpString(AppName)
)
func userAgent(h http.Header) {
h.Set("User-Agent", uaString)
}
func main() {
flag.Parse()
log.SetFlags(0)
@ -56,7 +62,7 @@ func main() {
log.Fatal(err)
}
loginReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
version.UserAgent(loginReq.Header, AppName)
userAgent(loginReq.Header)
jar, err := cookiejar.New(nil)
if err != nil {
@ -90,7 +96,7 @@ func main() {
Jar: jar,
}
wsHdr := make(http.Header)
version.UserAgent(wsHdr, AppName)
userAgent(wsHdr)
c, _, err := dialer.Dial(u.String(), wsHdr)
if err != nil {
log.Fatal("dial:", err)

View file

@ -2,7 +2,6 @@ package version
import (
"fmt"
"net/http"
"runtime"
)
@ -17,6 +16,6 @@ func String() string {
Version, Built, runtime.GOOS, runtime.GOARCH)
}
func UserAgent(hdr http.Header, app string) {
hdr.Set("User-Agent", fmt.Sprintf("stillbox %s/%s (%s/%s)", app, Version, runtime.GOOS, runtime.GOARCH))
func HttpString(app string) string {
return fmt.Sprintf("stillbox %s/%s (%s/%s)", app, Version, runtime.GOOS, runtime.GOARCH)
}

View file

@ -6,6 +6,7 @@ import (
"strings"
"dynatron.me/x/stillbox/client"
"dynatron.me/x/stillbox/internal/version"
"dynatron.me/x/stillbox/pkg/gordio/config"
"dynatron.me/x/stillbox/pkg/gordio/database"
"github.com/go-chi/chi/v5"
@ -14,6 +15,10 @@ import (
"github.com/go-chi/render"
)
const (
serverHeader = "Server"
)
func (s *Server) setupRoutes() {
clientRoot, err := fs.Sub(client.Calls, "calls")
if err != nil {
@ -65,3 +70,12 @@ func (s *Server) clientRoute(r chi.Router, clientRoot fs.FS) {
fs.ServeHTTP(w, r)
})
}
func ServerHeaderAdd(next http.Handler) http.Handler {
serverString := version.HttpString("gordio")
hfn := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", serverString)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(hfn)
}

View file

@ -61,6 +61,7 @@ func New(ctx context.Context, cfg *config.Config) (*Server, error) {
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(RequestLogger())
r.Use(ServerHeaderAdd)
r.Use(cors.Handler(cors.Options{
AllowedOrigins: srv.conf.CORS.AllowedOrigins,
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},

View file

@ -136,7 +136,10 @@ func (h *RdioHTTP) routeCallUpload(w http.ResponseWriter, r *http.Request) {
log.Info().Int("system", cur.System).Int("tgid", cur.Talkgroup).Msg("ingested")
_, _ = w.Write([]byte("Call imported successfully."))
written, err := w.Write([]byte("Call imported successfully."))
if err != nil {
log.Error().Err(err).Int("written", written).Msg("upload response failed")
}
}
func (car *callUploadRequest) fill(r *http.Request) error {