From a6508d32abcf1ea2a22469b854197041e87342c1 Mon Sep 17 00:00:00 2001 From: Kane York Date: Mon, 26 Oct 2015 18:00:29 -0700 Subject: [PATCH] can't believe i didn't already have this smdh --- socketserver/internal/server/backlog.go | 2 +- socketserver/internal/server/handlecore.go | 27 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/socketserver/internal/server/backlog.go b/socketserver/internal/server/backlog.go index 2f38f835..66f09e93 100644 --- a/socketserver/internal/server/backlog.go +++ b/socketserver/internal/server/backlog.go @@ -297,7 +297,7 @@ func GetCommandsOfType(match PushCommandCacheInfo) []Command { return ret } -func HBackendDumpCache(w http.ResponseWriter, r *http.Request) { +func HBackendDumpBacklog(w http.ResponseWriter, r *http.Request) { r.ParseForm() formData, err := UnsealRequest(r.Form) if err != nil { diff --git a/socketserver/internal/server/handlecore.go b/socketserver/internal/server/handlecore.go index 83d05ac4..1f477f97 100644 --- a/socketserver/internal/server/handlecore.go +++ b/socketserver/internal/server/handlecore.go @@ -11,6 +11,7 @@ import ( "strconv" "strings" "sync" + "html/template" ) const MAX_PACKET_SIZE = 1024 @@ -125,11 +126,35 @@ func SetupServerAndHandle(config *Config, tlsConfig *tls.Config, serveMux *http. if serveMux == nil { serveMux = http.DefaultServeMux } - serveMux.HandleFunc("/", sockServer.ServeHTTP) + serveMux.HandleFunc("/", ServeWebsocketOrCatbag(sockServer.ServeHTTP)) serveMux.HandleFunc("/pub_msg", HBackendPublishRequest) + serveMux.HandleFunc("/dump_backlog", HBackendDumpBacklog) serveMux.HandleFunc("/update_and_pub", HBackendUpdateAndPublish) } +var Catbag = template.Must(template.New(` + + + +CatBag + + + + +`).Parse("html")) + +func ServeWebsocketOrCatbag(sockfunc func(http.ResponseWriter, *http.Request)) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("Connection") == "Upgrade" { + sockfunc(w, r) + return + } else { + + Catbag.Execute(w, nil) + } + } +} + // Handle a new websocket connection from a FFZ client. // This runs in a goroutine started by net/http. func HandleSocketConnection(conn *websocket.Conn) {