From 1cae4152f949f5a99609d568d4f540b8887443eb Mon Sep 17 00:00:00 2001 From: Chaitanya Bankanhal Date: Mon, 27 Jul 2020 23:31:35 +0530 Subject: [PATCH] rc: add NeedsResponse for rc calls --- fs/rc/params.go | 19 ++++++++++++++++++- fs/rc/rcserver/rcserver.go | 4 ++++ fs/rc/registry.go | 13 +++++++------ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/fs/rc/params.go b/fs/rc/params.go index ae7f932e5..e74e81b44 100644 --- a/fs/rc/params.go +++ b/fs/rc/params.go @@ -91,7 +91,7 @@ func (p Params) Get(key string) (interface{}, error) { return value, nil } -// GetHTTPRequest gets a http.Request parameter associated with the request with the key "HttpRequest" +// GetHTTPRequest gets a http.Request parameter associated with the request with the key "_request" // // If the parameter isn't found then error will be of type // ErrParamNotFound and the returned value will be nil. @@ -108,6 +108,23 @@ func (p Params) GetHTTPRequest() (*http.Request, error) { return request, nil } +// GetHTTPResponseWriter gets a http.ResponseWriter parameter associated with the request with the key "_response" +// +// If the parameter isn't found then error will be of type +// ErrParamNotFound and the returned value will be nil. +func (p Params) GetHTTPResponseWriter() (*http.ResponseWriter, error) { + key := "_response" + value, err := p.Get(key) + if err != nil { + return nil, err + } + request, ok := value.(*http.ResponseWriter) + if !ok { + return nil, ErrParamInvalid{errors.Errorf("expecting http.ResponseWriter value for key %q (was %T)", key, value)} + } + return request, nil +} + // GetString gets a string parameter from the input // // If the parameter isn't found then error will be of type diff --git a/fs/rc/rcserver/rcserver.go b/fs/rc/rcserver/rcserver.go index 7f324c21b..f6509baa6 100644 --- a/fs/rc/rcserver/rcserver.go +++ b/fs/rc/rcserver/rcserver.go @@ -265,6 +265,10 @@ func (s *Server) handlePost(w http.ResponseWriter, r *http.Request, path string) in["_request"] = r } + if call.NeedsResponse { + in["_response"] = &w + } + // Check to see if it is async or not isAsync, err := in.GetBool("_async") if rc.NotErrParamNotFound(err) { diff --git a/fs/rc/registry.go b/fs/rc/registry.go index f2f5a1fb8..4cc80541a 100644 --- a/fs/rc/registry.go +++ b/fs/rc/registry.go @@ -17,12 +17,13 @@ type Func func(ctx context.Context, in Params) (out Params, err error) // Call defines info about a remote control function and is used in // the Add function to create new entry points. type Call struct { - Path string // path to activate this RC - Fn Func `json:"-"` // function to call - Title string // help for the function - AuthRequired bool // if set then this call requires authorisation to be set - Help string // multi-line markdown formatted help - NeedsRequest bool // if set then this call will be passed the original request object as _request + Path string // path to activate this RC + Fn Func `json:"-"` // function to call + Title string // help for the function + AuthRequired bool // if set then this call requires authorisation to be set + Help string // multi-line markdown formatted help + NeedsRequest bool // if set then this call will be passed the original request object as _request + NeedsResponse bool // if set then this call will be passed the original response object as _response } // Registry holds the list of all the registered remote control functions