diff --git a/client.html b/client/index.html similarity index 63% rename from client.html rename to client/index.html index bdda703..897311e 100644 --- a/client.html +++ b/client/index.html @@ -2,12 +2,36 @@ Sync v3 experiments @@ -112,8 +126,14 @@
- -

+ +
+
\ No newline at end of file diff --git a/client/placeholder.svg b/client/placeholder.svg new file mode 100644 index 0000000..cbbf393 --- /dev/null +++ b/client/placeholder.svg @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/synclive/handler.go b/synclive/handler.go index 0519173..429d82d 100644 --- a/synclive/handler.go +++ b/synclive/handler.go @@ -86,8 +86,11 @@ func (h *SyncLiveHandler) serve(w http.ResponseWriter, req *http.Request) error } } } + if requestBody.SessionID == "" { + requestBody.SessionID = DefaultSessionID + } - conn, err := h.setupConnection(req, &requestBody) + conn, err := h.setupConnection(req, &requestBody, req.URL.Query().Get("pos") != "") if err != nil { hlog.FromRequest(req).Err(err).Msg("failed to get or create Conn") return err @@ -127,7 +130,7 @@ func (h *SyncLiveHandler) serve(w http.ResponseWriter, req *http.Request) error // setupConnection associates this request with an existing connection or makes a new connection. // It also sets a v2 sync poll loop going if one didn't exist already for this user. // When this function returns, the connection is alive and active. -func (h *SyncLiveHandler) setupConnection(req *http.Request, syncReq *Request) (*Conn, error) { +func (h *SyncLiveHandler) setupConnection(req *http.Request, syncReq *Request, containsPos bool) (*Conn, error) { log := hlog.FromRequest(req) var conn *Conn @@ -142,7 +145,7 @@ func (h *SyncLiveHandler) setupConnection(req *http.Request, syncReq *Request) ( } // client thinks they have a connection - if syncReq.SessionID != "" { + if containsPos { // Lookup the connection // we need to map based on both as the session ID isn't crypto secure but the device ID is (Auth header) conn = h.ConnMap.Conn(ConnID{ @@ -202,7 +205,7 @@ func (h *SyncLiveHandler) setupConnection(req *http.Request, syncReq *Request) ( // to check for an existing connection though, as it's possible for the client to call /sync // twice for a new connection and get the same session ID. conn, created := h.ConnMap.GetOrCreateConn(ConnID{ - SessionID: DefaultSessionID, + SessionID: syncReq.SessionID, DeviceID: deviceID, }, v2device.UserID) if created { @@ -210,7 +213,6 @@ func (h *SyncLiveHandler) setupConnection(req *http.Request, syncReq *Request) ( } else { log.Info().Str("conn_id", conn.ConnID.String()).Msg("using existing connection") } - syncReq.SessionID = conn.ConnID.SessionID return conn, nil } diff --git a/v3.go b/v3.go index da42425..798a539 100644 --- a/v3.go +++ b/v3.go @@ -3,7 +3,6 @@ package syncv3 import ( "encoding/json" "fmt" - "io/ioutil" "net/http" "os" "time" @@ -31,7 +30,7 @@ func (s *server) ServeHTTP(w http.ResponseWriter, req *http.Request) { h.ServeHTTP(w, req) } -func jsClient(file []byte) http.HandlerFunc { +func allowCORS(next http.Handler) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { if req.Method == "OPTIONS" { w.Header().Set("Access-Control-Allow-Origin", "*") @@ -40,29 +39,20 @@ func jsClient(file []byte) http.HandlerFunc { w.WriteHeader(200) return } - // TODO: remove when don't need live updates - jsFile, err := ioutil.ReadFile("client.html") - if err != nil { - logger.Fatal().Err(err).Msg("failed to read client.html") - } - w.WriteHeader(200) - w.Write(jsFile) + next.ServeHTTP(w, req) } - } // RunSyncV3Server is the main entry point to the server func RunSyncV3Server(h http.Handler, bindAddr string) { - - jsFile, err := ioutil.ReadFile("client.html") - if err != nil { - logger.Fatal().Err(err).Msg("failed to read client.html") - } - // HTTP path routing r := mux.NewRouter() r.Handle("/_matrix/client/v3/sync", h) - r.HandleFunc("/client", jsClient(jsFile)).Methods("GET", "OPTIONS", "HEAD") + r.PathPrefix("/client/").HandlerFunc( + allowCORS( + http.StripPrefix("/client/", http.FileServer(http.Dir("./client"))), + ), + ) srv := &server{ chain: []func(next http.Handler) http.Handler{ @@ -73,7 +63,7 @@ func RunSyncV3Server(h http.Handler, bindAddr string) { Int("status", status). Int("size", size). Dur("duration", duration). - Str("since", r.URL.Query().Get("since")). + Str("path", r.URL.Path). Msg("") }), hlog.RemoteAddrHandler("ip"),