From ebfe600b0b3da7947c6ae77b84173f0db3964615 Mon Sep 17 00:00:00 2001 From: Maxim Baz Date: Thu, 19 Apr 2018 23:53:34 +0200 Subject: [PATCH] Implement `echo` action (#18) --- PROTOCOL.md | 19 +++++++++++++++++++ request/process.go | 11 +++++++---- response/response.go | 23 ++++++++--------------- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/PROTOCOL.md b/PROTOCOL.md index bfe93c1..4801196 100644 --- a/PROTOCOL.md +++ b/PROTOCOL.md @@ -182,3 +182,22 @@ Get the decrypted contents of a specific file. } } ``` + +### Echo + +Send the `echoResponse` in the request as a response. + +#### Request + +``` +{ + "action": "echo", + "echoResponse": +} +``` + +#### Response + +``` + +``` diff --git a/request/process.go b/request/process.go index 8bb5031..979749f 100644 --- a/request/process.go +++ b/request/process.go @@ -22,10 +22,11 @@ type settings struct { } type request struct { - Action string `json:"action"` - Settings settings `json:"settings"` - File string `json:"file"` - Store string `json:"store"` + Action string `json:"action"` + Settings settings `json:"settings"` + File string `json:"file"` + Store string `json:"store"` + EchoResponse interface{} `json:"echoResponse"` } // Process handles browser request @@ -61,6 +62,8 @@ func Process() { listFiles(request) case "fetch": fetchDecryptedContents(request) + case "echo": + response.SendRaw(request.EchoResponse) default: log.Errorf("Received a browser request with an unknown action: %+v", request) response.SendErrorAndExit( diff --git a/response/response.go b/response/response.go index b684bc2..5493de6 100644 --- a/response/response.go +++ b/response/response.go @@ -64,7 +64,7 @@ func MakeFetchResponse() *FetchResponse { // SendOk sends a success response to the browser extension in the predefined json format func SendOk(data interface{}) { - send(&okResponse{ + SendRaw(&okResponse{ Status: "ok", Version: version.Code, Data: data, @@ -73,7 +73,7 @@ func SendOk(data interface{}) { // SendErrorAndExit sends an error response to the browser extension in the predefined json format and exits with the specified exit code func SendErrorAndExit(errorCode errors.Code, params *map[errors.Field]string) { - send(&errorResponse{ + SendRaw(&errorResponse{ Status: "error", Code: errorCode, Version: version.Code, @@ -83,24 +83,17 @@ func SendErrorAndExit(errorCode errors.Code, params *map[errors.Field]string) { errors.ExitWithCode(errorCode) } -func send(data interface{}) { - switch data.(type) { - case *okResponse: - case *errorResponse: - break - default: - log.Fatalf("Only data of type OkResponse and ErrorResponse is allowed to be sent to the browser extension, attempted to send: %+v", data) - } - +// SendRaw sends a raw data to the browser extension +func SendRaw(response interface{}) { var bytesBuffer bytes.Buffer - if err := json.NewEncoder(&bytesBuffer).Encode(data); err != nil { - log.Fatal("Unable to encode data for sending: ", err) + if err := json.NewEncoder(&bytesBuffer).Encode(response); err != nil { + log.Fatal("Unable to encode response for sending: ", err) } if err := binary.Write(os.Stdout, binary.LittleEndian, uint32(bytesBuffer.Len())); err != nil { - log.Fatal("Unable to send the length of the response data: ", err) + log.Fatal("Unable to send the length of the response: ", err) } if _, err := bytesBuffer.WriteTo(os.Stdout); err != nil { - log.Fatal("Unable to send the response data: ", err) + log.Fatal("Unable to send the response: ", err) } }