[MM-16473] Make plugins' ServerHTTP http.ResponseWriter hijackable (#14822)
* Make plugins' ServerHTTP http.ResponseWriter hijackable * Rename brw to align with docs * Fix error handling
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
0118db9d23
Коммит
d0e035467c
@@ -1578,6 +1578,86 @@ func TestPluginAPIGetPostsForChannel(t *testing.T) {
|
||||
require.Equal(expectedPosts, postList.ToSlice())
|
||||
}
|
||||
|
||||
func TestPluginHTTPConnHijack(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
testFolder, found := fileutils.FindDir("mattermost-server/app/plugin_api_tests")
|
||||
require.True(t, found, "Cannot find tests folder")
|
||||
fullPath := path.Join(testFolder, "manual.test_http_hijack_plugin", "main.go")
|
||||
|
||||
pluginCode, err := ioutil.ReadFile(fullPath)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, pluginCode)
|
||||
|
||||
tearDown, ids, errors := SetAppEnvironmentWithPlugins(t, []string{string(pluginCode)}, th.App, th.App.NewPluginAPI)
|
||||
defer tearDown()
|
||||
require.NoError(t, errors[0])
|
||||
require.Len(t, ids, 1)
|
||||
|
||||
pluginID := ids[0]
|
||||
require.NotEmpty(t, pluginID)
|
||||
|
||||
reqURL := fmt.Sprintf("http://localhost:%d/plugins/%s", th.Server.ListenAddr.Port, pluginID)
|
||||
req, err := http.NewRequest("GET", reqURL, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "OK", string(body))
|
||||
}
|
||||
|
||||
func TestPluginHTTPUpgradeWebSocket(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
testFolder, found := fileutils.FindDir("mattermost-server/app/plugin_api_tests")
|
||||
require.True(t, found, "Cannot find tests folder")
|
||||
fullPath := path.Join(testFolder, "manual.test_http_upgrade_websocket_plugin", "main.go")
|
||||
|
||||
pluginCode, err := ioutil.ReadFile(fullPath)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, pluginCode)
|
||||
|
||||
tearDown, ids, errors := SetAppEnvironmentWithPlugins(t, []string{string(pluginCode)}, th.App, th.App.NewPluginAPI)
|
||||
defer tearDown()
|
||||
require.NoError(t, errors[0])
|
||||
require.Len(t, ids, 1)
|
||||
|
||||
pluginID := ids[0]
|
||||
require.NotEmpty(t, pluginID)
|
||||
|
||||
reqURL := fmt.Sprintf("ws://localhost:%d/plugins/%s", th.Server.ListenAddr.Port, pluginID)
|
||||
wsc, err := model.NewWebSocketClient(reqURL, "")
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, wsc)
|
||||
|
||||
wsc.Listen()
|
||||
defer wsc.Close()
|
||||
|
||||
resp := <-wsc.ResponseChannel
|
||||
require.Equal(t, resp.Status, model.STATUS_OK)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
wsc.SendMessage("custom_action", map[string]interface{}{"value": i})
|
||||
var resp *model.WebSocketResponse
|
||||
select {
|
||||
case resp = <-wsc.ResponseChannel:
|
||||
case <-time.After(1 * time.Second):
|
||||
}
|
||||
require.NotNil(t, resp)
|
||||
require.Equal(t, resp.Status, model.STATUS_OK)
|
||||
require.Equal(t, "custom_action", resp.Data["action"])
|
||||
require.Equal(t, float64(i), resp.Data["value"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginAPISearchPostsInTeamByUser(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
35
app/plugin_api_tests/manual.test_http_hijack_plugin/main.go
Обычный файл
35
app/plugin_api_tests/manual.test_http_hijack_plugin/main.go
Обычный файл
@@ -0,0 +1,35 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/plugin"
|
||||
)
|
||||
|
||||
type Plugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
|
||||
hj, ok := w.(http.Hijacker)
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
conn, brw, err := hj.Hijack()
|
||||
if conn == nil || brw == nil || err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
conn.Write([]byte("HTTP/1.1 200\n\nOK"))
|
||||
conn.Close()
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&Plugin{})
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/plugin"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type Plugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
|
||||
upgrader := websocket.Upgrader{}
|
||||
|
||||
ws, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer ws.Close()
|
||||
|
||||
for {
|
||||
mt, msg, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
req := model.WebSocketRequestFromJson(bytes.NewReader(msg))
|
||||
resp := model.NewWebSocketResponse("OK", req.Seq, map[string]interface{}{"action": req.Action, "value": req.Data["value"]})
|
||||
if err = ws.WriteMessage(mt, []byte(resp.ToJson())); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&Plugin{})
|
||||
}
|
||||
Ссылка в новой задаче
Block a user