From bdfef9010fe745f2029c65c582040f45fb848875 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Wed, 23 Aug 2023 13:39:14 +0200 Subject: [PATCH] cmd/stdiscosrv: Serve compressed responses --- cmd/stdiscosrv/apisrv.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/cmd/stdiscosrv/apisrv.go b/cmd/stdiscosrv/apisrv.go index 1140e073d..06081287c 100644 --- a/cmd/stdiscosrv/apisrv.go +++ b/cmd/stdiscosrv/apisrv.go @@ -8,6 +8,7 @@ package main import ( "bytes" + "compress/gzip" "context" "crypto/tls" "encoding/base64" @@ -15,6 +16,7 @@ import ( "encoding/pem" "errors" "fmt" + io "io" "log" "math/rand" "net" @@ -220,12 +222,21 @@ func (s *apiSrv) handleGET(ctx context.Context, w http.ResponseWriter, req *http lookupRequestsTotal.WithLabelValues("success").Inc() - bs, _ := json.Marshal(announcement{ - Seen: time.Unix(0, rec.Seen), + w.Header().Set("Content-Type", "application/json") + var bw io.Writer = w + + // Use compression if the client asks for it + if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { + w.Header().Set("Content-Encoding", "gzip") + gw := gzip.NewWriter(bw) + defer gw.Close() + bw = gw + } + + json.NewEncoder(bw).Encode(announcement{ + Seen: time.Unix(0, rec.Seen).Truncate(time.Second), Addresses: addressStrs(rec.Addresses), }) - w.Header().Set("Content-Type", "application/json") - w.Write(bs) } func (s *apiSrv) handlePOST(ctx context.Context, remoteAddr *net.TCPAddr, w http.ResponseWriter, req *http.Request) {