Implement echo
action (#18)
This commit is contained in:
parent
d24b54e8b6
commit
ebfe600b0b
19
PROTOCOL.md
19
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": <anything>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Response
|
||||||
|
|
||||||
|
```
|
||||||
|
<echoResponse>
|
||||||
|
```
|
||||||
|
|
|
@ -22,10 +22,11 @@ type settings struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type request struct {
|
type request struct {
|
||||||
Action string `json:"action"`
|
Action string `json:"action"`
|
||||||
Settings settings `json:"settings"`
|
Settings settings `json:"settings"`
|
||||||
File string `json:"file"`
|
File string `json:"file"`
|
||||||
Store string `json:"store"`
|
Store string `json:"store"`
|
||||||
|
EchoResponse interface{} `json:"echoResponse"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process handles browser request
|
// Process handles browser request
|
||||||
|
@ -61,6 +62,8 @@ func Process() {
|
||||||
listFiles(request)
|
listFiles(request)
|
||||||
case "fetch":
|
case "fetch":
|
||||||
fetchDecryptedContents(request)
|
fetchDecryptedContents(request)
|
||||||
|
case "echo":
|
||||||
|
response.SendRaw(request.EchoResponse)
|
||||||
default:
|
default:
|
||||||
log.Errorf("Received a browser request with an unknown action: %+v", request)
|
log.Errorf("Received a browser request with an unknown action: %+v", request)
|
||||||
response.SendErrorAndExit(
|
response.SendErrorAndExit(
|
||||||
|
|
|
@ -64,7 +64,7 @@ func MakeFetchResponse() *FetchResponse {
|
||||||
|
|
||||||
// SendOk sends a success response to the browser extension in the predefined json format
|
// SendOk sends a success response to the browser extension in the predefined json format
|
||||||
func SendOk(data interface{}) {
|
func SendOk(data interface{}) {
|
||||||
send(&okResponse{
|
SendRaw(&okResponse{
|
||||||
Status: "ok",
|
Status: "ok",
|
||||||
Version: version.Code,
|
Version: version.Code,
|
||||||
Data: data,
|
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
|
// 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) {
|
func SendErrorAndExit(errorCode errors.Code, params *map[errors.Field]string) {
|
||||||
send(&errorResponse{
|
SendRaw(&errorResponse{
|
||||||
Status: "error",
|
Status: "error",
|
||||||
Code: errorCode,
|
Code: errorCode,
|
||||||
Version: version.Code,
|
Version: version.Code,
|
||||||
|
@ -83,24 +83,17 @@ func SendErrorAndExit(errorCode errors.Code, params *map[errors.Field]string) {
|
||||||
errors.ExitWithCode(errorCode)
|
errors.ExitWithCode(errorCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func send(data interface{}) {
|
// SendRaw sends a raw data to the browser extension
|
||||||
switch data.(type) {
|
func SendRaw(response interface{}) {
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
var bytesBuffer bytes.Buffer
|
var bytesBuffer bytes.Buffer
|
||||||
if err := json.NewEncoder(&bytesBuffer).Encode(data); err != nil {
|
if err := json.NewEncoder(&bytesBuffer).Encode(response); err != nil {
|
||||||
log.Fatal("Unable to encode data for sending: ", err)
|
log.Fatal("Unable to encode response for sending: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := binary.Write(os.Stdout, binary.LittleEndian, uint32(bytesBuffer.Len())); err != nil {
|
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 {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user