Skip to content

Instantly share code, notes, and snippets.

@carmark
Created July 26, 2015 14:01
Show Gist options
  • Save carmark/d53b5d1db17865ee93a4 to your computer and use it in GitHub Desktop.
Save carmark/d53b5d1db17865ee93a4 to your computer and use it in GitHub Desktop.
lei@ubuntu:~/src/pub/src/hyper$ git diff
diff --git a/client/run.go b/client/run.go
index 4a18923..4016703 100644
--- a/client/run.go
+++ b/client/run.go
@@ -14,6 +14,7 @@ import (
"hyper/engine"
"hyper/lib/promise"
"hyper/pod"
+ "hyper/utils"
)
// hyper run [OPTIONS] image [COMMAND] [ARGS...]
@@ -99,7 +100,7 @@ func (cli *HyperClient) HyperCmdRun(args ...string) error {
if err != nil {
return err
}
- jsonbody, err = json.Marshal(*userpod)
+ jsonbody, err = utils.JSONMarshal(*userpod, true)
if err != nil {
return err
}
@@ -175,7 +176,7 @@ func (cli *HyperClient) HyperCmdRun(args ...string) error {
Tty: opts.Tty,
}
- jsonString, _ := json.Marshal(userPod)
+ jsonString, _ := utils.JSONMarshal(userPod, true)
podId, err := cli.RunPod(string(jsonString))
if err != nil {
return err
diff --git a/hypervisor/vm_states.go b/hypervisor/vm_states.go
index 41e1d3b..b0208d6 100644
--- a/hypervisor/vm_states.go
+++ b/hypervisor/vm_states.go
@@ -6,6 +6,7 @@ import (
"hyper/lib/glog"
"hyper/pod"
"hyper/types"
+ "hyper/utils"
"time"
)
@@ -91,7 +92,7 @@ func (ctx *VmContext) setWindowSize(tag string, size *WindowSize) {
func (ctx *VmContext) execCmd(cmd *ExecCommand) {
cmd.Sequence = ctx.nextAttachId()
- pkg, err := json.Marshal(*cmd)
+ pkg, err := utils.JSONMarshal(*cmd, true)
if err != nil {
cmd.Streams.Callback <- &types.QemuResponse{
VmId: ctx.Id, Code: types.E_JSON_PARSE_FAIL,
@@ -129,7 +130,7 @@ func (ctx *VmContext) attachCmd(cmd *AttachCommand) {
}
func (ctx *VmContext) startPod() {
- pod, err := json.Marshal(*ctx.vmSpec)
+ pod, err := utils.JSONMarshal(*ctx.vmSpec, true)
if err != nil {
ctx.Hub <- &InitFailedEvent{
Reason: "Generated wrong run profile " + err.Error(),
diff --git a/utils/utils.go b/utils/utils.go
index 85a1c55..a32fb85 100644
--- a/utils/utils.go
+++ b/utils/utils.go
@@ -1,7 +1,9 @@
package utils
import (
+ "bytes"
"encoding/base64"
+ "encoding/json"
"fmt"
"io"
"mime"
@@ -102,3 +104,14 @@ func ConvertPermStrToInt(str string) int {
}
return res
}
+
+func JSONMarshal(v interface{}, safeEncoding bool) ([]byte, error) {
+ b, err := json.Marshal(v)
+
+ if safeEncoding {
+ b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1)
+ b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1)
+ b = bytes.Replace(b, []byte("\\u0026"), []byte("&"), -1)
+ }
+ return b, err
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment