2018-06-25 12:33:13 -07:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/rpc"
|
|
|
|
|
)
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
type httpResponseWriterRPCServer struct {
|
2018-06-25 12:33:13 -07:00
|
|
|
w http.ResponseWriter
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (w *httpResponseWriterRPCServer) Header(args struct{}, reply *http.Header) error {
|
2018-06-25 12:33:13 -07:00
|
|
|
*reply = w.w.Header()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (w *httpResponseWriterRPCServer) Write(args []byte, reply *struct{}) error {
|
2018-06-25 12:33:13 -07:00
|
|
|
_, err := w.w.Write(args)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (w *httpResponseWriterRPCServer) WriteHeader(args int, reply *struct{}) error {
|
2018-06-25 12:33:13 -07:00
|
|
|
w.w.WriteHeader(args)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (w *httpResponseWriterRPCServer) SyncHeader(args http.Header, reply *struct{}) error {
|
2018-06-25 12:33:13 -07:00
|
|
|
dest := w.w.Header()
|
|
|
|
|
for k := range dest {
|
|
|
|
|
if _, ok := args[k]; !ok {
|
|
|
|
|
delete(dest, k)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for k, v := range args {
|
|
|
|
|
dest[k] = v
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
type httpResponseWriterRPCClient struct {
|
2018-06-25 12:33:13 -07:00
|
|
|
client *rpc.Client
|
|
|
|
|
header http.Header
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
var _ http.ResponseWriter = (*httpResponseWriterRPCClient)(nil)
|
2018-06-25 12:33:13 -07:00
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (w *httpResponseWriterRPCClient) Header() http.Header {
|
2018-06-25 12:33:13 -07:00
|
|
|
if w.header == nil {
|
|
|
|
|
w.client.Call("Plugin.Header", struct{}{}, &w.header)
|
|
|
|
|
}
|
|
|
|
|
return w.header
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (w *httpResponseWriterRPCClient) Write(b []byte) (int, error) {
|
2018-06-25 12:33:13 -07:00
|
|
|
if err := w.client.Call("Plugin.SyncHeader", w.header, nil); err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
if err := w.client.Call("Plugin.Write", b, nil); err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
return len(b), nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (w *httpResponseWriterRPCClient) WriteHeader(statusCode int) {
|
2018-06-25 12:33:13 -07:00
|
|
|
if err := w.client.Call("Plugin.SyncHeader", w.header, nil); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.client.Call("Plugin.WriteHeader", statusCode, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (h *httpResponseWriterRPCClient) Close() error {
|
2018-06-25 12:33:13 -07:00
|
|
|
return h.client.Close()
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func connectHTTPResponseWriter(conn io.ReadWriteCloser) *httpResponseWriterRPCClient {
|
|
|
|
|
return &httpResponseWriterRPCClient{
|
2018-06-25 12:33:13 -07:00
|
|
|
client: rpc.NewClient(conn),
|
|
|
|
|
}
|
|
|
|
|
}
|