github.com/cjdelisle/matterfoss@v5.11.1+incompatible/plugin/http.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package plugin 5 6 import ( 7 "io" 8 "net/http" 9 "net/rpc" 10 ) 11 12 type httpResponseWriterRPCServer struct { 13 w http.ResponseWriter 14 } 15 16 func (w *httpResponseWriterRPCServer) Header(args struct{}, reply *http.Header) error { 17 *reply = w.w.Header() 18 return nil 19 } 20 21 func (w *httpResponseWriterRPCServer) Write(args []byte, reply *struct{}) error { 22 _, err := w.w.Write(args) 23 return err 24 } 25 26 func (w *httpResponseWriterRPCServer) WriteHeader(args int, reply *struct{}) error { 27 w.w.WriteHeader(args) 28 return nil 29 } 30 31 func (w *httpResponseWriterRPCServer) SyncHeader(args http.Header, reply *struct{}) error { 32 dest := w.w.Header() 33 for k := range dest { 34 if _, ok := args[k]; !ok { 35 delete(dest, k) 36 } 37 } 38 for k, v := range args { 39 dest[k] = v 40 } 41 return nil 42 } 43 44 type httpResponseWriterRPCClient struct { 45 client *rpc.Client 46 header http.Header 47 } 48 49 var _ http.ResponseWriter = (*httpResponseWriterRPCClient)(nil) 50 51 func (w *httpResponseWriterRPCClient) Header() http.Header { 52 if w.header == nil { 53 w.client.Call("Plugin.Header", struct{}{}, &w.header) 54 } 55 return w.header 56 } 57 58 func (w *httpResponseWriterRPCClient) Write(b []byte) (int, error) { 59 if err := w.client.Call("Plugin.SyncHeader", w.header, nil); err != nil { 60 return 0, err 61 } 62 if err := w.client.Call("Plugin.Write", b, nil); err != nil { 63 return 0, err 64 } 65 return len(b), nil 66 } 67 68 func (w *httpResponseWriterRPCClient) WriteHeader(statusCode int) { 69 if err := w.client.Call("Plugin.SyncHeader", w.header, nil); err != nil { 70 return 71 } 72 w.client.Call("Plugin.WriteHeader", statusCode, nil) 73 } 74 75 func (h *httpResponseWriterRPCClient) Close() error { 76 return h.client.Close() 77 } 78 79 func connectHTTPResponseWriter(conn io.ReadWriteCloser) *httpResponseWriterRPCClient { 80 return &httpResponseWriterRPCClient{ 81 client: rpc.NewClient(conn), 82 } 83 }