From 3598c872e3b01dfda95331bc1cf8afafbaf0f8cf Mon Sep 17 00:00:00 2001 From: 502647092 Date: Fri, 31 Aug 2018 10:04:59 +0800 Subject: [PATCH] fix: Fix space and LF can't transfer --- .gitignore | 3 ++- README.MD | 9 ++++++++- main.go | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index b1fbaac..447db7a 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,5 @@ _testmain.go /vendor debug -*.exe \ No newline at end of file +*.exe +clipboard-sync diff --git a/README.MD b/README.MD index 12ff56e..3bdf196 100644 --- a/README.MD +++ b/README.MD @@ -12,4 +12,11 @@ ./clipboard-sync -t s -b :8080 ### Client -./clipboard-sync -a http://172.30.34.2:8080 \ No newline at end of file +./clipboard-sync -a http://172.30.34.2:8080 + +#### Changelog + +- 2018-08-30 + - First Alphe Version +- 2018-08-31 + - Fix space and LF can't transfer \ No newline at end of file diff --git a/main.go b/main.go index 0378fdb..b94b4b0 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/base64" "flag" "fmt" "net/http" @@ -59,13 +60,13 @@ func readClipboard(a string) { old = text updateTime = time.Now().Unix() request, _ := http.NewRequest("POST", - fmt.Sprintf(address, text, strconv.FormatInt(updateTime, 10)), nil) + fmt.Sprintf(address, encode(text), strconv.FormatInt(updateTime, 10)), nil) client.Do(request) } else { response, _ := http.Get(a) texts, ok := response.Header[textHeader] if ok { - sText := texts[0] + sText := decode(texts[0]) sTime, _ := strconv.ParseInt(response.Header[timeHeader][0], 10, 64) if sTime > updateTime && sText != old { old = sText @@ -77,3 +78,12 @@ func readClipboard(a string) { } } } + +func encode(text string) string { + return base64.URLEncoding.EncodeToString([]byte(text)) +} + +func decode(text string) string { + origin, _ := base64.URLEncoding.DecodeString(text) + return string(origin) +}