feat: Add Content-Encoding support for brotli, gzip, and deflate (#24)
feat: Add Content-Encoding support for brotli, gzip, and deflate Co-authored-by: Jon Lundy <jon@xuu.cc> Reviewed-on: https://git.mills.io/prologic/msgbus/pulls/24 Co-authored-by: xuu <xuu@noreply@mills.io> Co-committed-by: xuu <xuu@noreply@mills.io>
Este cometimento está contido em:
ascendente
e6ab9d9c70
cometimento
7181b6df1b
1
go.mod
1
go.mod
|
@ -17,6 +17,7 @@ require (
|
|||
)
|
||||
|
||||
require (
|
||||
github.com/andybalholm/brotli v1.0.4 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -57,6 +57,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
|
|||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
|
||||
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
|
||||
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
|
||||
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
|
||||
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
|
||||
|
|
35
msgbus.go
35
msgbus.go
|
@ -1,8 +1,11 @@
|
|||
package msgbus
|
||||
|
||||
import (
|
||||
"compress/flate"
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
@ -10,6 +13,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/andybalholm/brotli"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
@ -477,6 +481,7 @@ func (mb *MessageBus) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}()
|
||||
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Accept-Encoding", "br, gzip, deflate")
|
||||
|
||||
if r.Method == "GET" && (r.URL.Path == "/" || r.URL.Path == "") {
|
||||
// XXX: guard with a mutex?
|
||||
|
@ -500,13 +505,40 @@ func (mb *MessageBus) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
switch r.Method {
|
||||
case "POST", "PUT":
|
||||
defer r.Body.Close()
|
||||
|
||||
if r.ContentLength > int64(mb.maxPayloadSize) {
|
||||
msg := "payload exceeds max-payload-size"
|
||||
http.Error(w, msg, http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
var rd io.Reader = r.Body
|
||||
ce := r.Header.Get("Content-Encoding")
|
||||
switch ce {
|
||||
case "":
|
||||
case "br":
|
||||
rd = brotli.NewReader(rd)
|
||||
case "gzip":
|
||||
gz, err := gzip.NewReader(rd)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("error reading payload: %s", err)
|
||||
http.Error(w, msg, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer gz.Close()
|
||||
rd = gz
|
||||
case "deflate":
|
||||
fl := flate.NewReader(rd)
|
||||
defer fl.Close()
|
||||
rd = fl
|
||||
default:
|
||||
msg := fmt.Sprintf("error reading payload: not acceptable: %v", ce)
|
||||
http.Error(w, msg, http.StatusNotAcceptable)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(rd)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("error reading payload: %s", err)
|
||||
http.Error(w, msg, http.StatusBadRequest)
|
||||
|
@ -517,7 +549,6 @@ func (mb *MessageBus) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
http.Error(w, msg, http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
|
||||
mb.Put(mb.NewMessage(t, body))
|
||||
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
|
|
Carregando…
Criar uma nova questão referindo esta