Handle channel names ending with a slash better

This commit is contained in:
Ken-Håvard Lieng 2017-06-13 04:25:59 +02:00
parent f03b30eff6
commit 0f5c3b57d2
8 changed files with 96 additions and 40 deletions

View file

@ -2,6 +2,7 @@ package server
import (
"net/http"
"net/url"
"strings"
"github.com/spf13/viper"
@ -84,17 +85,18 @@ func getIndexData(r *http.Request, session *Session) *indexData {
Channels: channels,
}
params := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if len(params) == 2 && isChannel(params[1]) {
data.addUsersAndMessages(params[0], params[1], session)
} else {
server, channel := parseTabCookie(r, r.URL.Path)
if channel != "" {
for _, ch := range channels {
if server == ch.Server && channel == ch.Name {
data.addUsersAndMessages(server, channel, session)
break
}
server, channel := getTabFromPath(r.URL.EscapedPath())
if channel != "" {
data.addUsersAndMessages(server, channel, session)
return &data
}
server, channel = parseTabCookie(r, r.URL.Path)
if channel != "" {
for _, ch := range channels {
if server == ch.Server && channel == ch.Name {
data.addUsersAndMessages(server, channel, session)
break
}
}
}
@ -102,11 +104,22 @@ func getIndexData(r *http.Request, session *Session) *indexData {
return &data
}
func getTabFromPath(rawPath string) (string, string) {
path := strings.Split(strings.Trim(rawPath, "/"), "/")
if len(path) == 2 {
name, err := url.PathUnescape(path[1])
if err == nil && isChannel(name) {
return path[0], name
}
}
return "", ""
}
func parseTabCookie(r *http.Request, path string) (string, string) {
if path == "/" {
cookie, err := r.Cookie("tab")
if err == nil {
tab := strings.Split(cookie.Value, ":")
tab := strings.Split(cookie.Value, "-")
if len(tab) == 2 && isChannel(tab[1]) {
return tab[0], tab[1]

43
server/index_data_test.go Normal file
View file

@ -0,0 +1,43 @@
package server
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetTabFromPath(t *testing.T) {
cases := []struct {
input string
expectedServer string
expectedChannel string
}{
{
"/chat.freenode.net/%23r%2Fstuff%2F/",
"chat.freenode.net",
"#r/stuff/",
}, {
"/chat.freenode.net/%23r%2Fstuff%2F",
"chat.freenode.net",
"#r/stuff/",
}, {
"/chat.freenode.net/%23r%2Fstuff",
"chat.freenode.net",
"#r/stuff",
}, {
"/chat.freenode.net/%23stuff",
"chat.freenode.net",
"#stuff",
}, {
"/chat.freenode.net/%23stuff/cake",
"",
"",
},
}
for _, tc := range cases {
server, channel := getTabFromPath(tc.input)
assert.Equal(t, tc.expectedServer, server)
assert.Equal(t, tc.expectedChannel, channel)
}
}

View file

@ -103,7 +103,7 @@ func serve(w http.ResponseWriter, r *http.Request) {
return
}
if r.URL.Path == "/ws" {
if strings.HasPrefix(r.URL.Path, "/ws") {
session := handleAuth(w, r)
if session == nil {
log.Println("[Auth] No session")

View file

@ -66,13 +66,13 @@ func (h *wsHandler) init(r *http.Request) {
h.session.numWS(), "WebSocket connections")
channels := h.session.user.GetChannels()
path := r.URL.Query().Get("path")
params := strings.Split(strings.Trim(path, "/"), "/")
tabServer, tabChannel := parseTabCookie(r, path)
path := r.URL.EscapedPath()[3:]
pathServer, pathChannel := getTabFromPath(path)
cookieServer, cookieChannel := parseTabCookie(r, path)
for _, channel := range channels {
if (len(params) == 2 && channel.Server == params[0] && channel.Name == params[1]) ||
(channel.Server == tabServer && channel.Name == tabChannel) {
if (channel.Server == pathServer && channel.Name == pathChannel) ||
(channel.Server == cookieServer && channel.Name == cookieChannel) {
continue
}