1
0

fix: Fix space and LF can't transfer

This commit is contained in:
502647092 2018-08-31 10:04:59 +08:00
parent 0165ab48c0
commit 3598c872e3
3 changed files with 22 additions and 4 deletions

3
.gitignore vendored
View File

@ -26,4 +26,5 @@ _testmain.go
/vendor /vendor
debug debug
*.exe *.exe
clipboard-sync

View File

@ -12,4 +12,11 @@
./clipboard-sync -t s -b :8080 ./clipboard-sync -t s -b :8080
### Client ### Client
./clipboard-sync -a http://172.30.34.2:8080 ./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

14
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/base64"
"flag" "flag"
"fmt" "fmt"
"net/http" "net/http"
@ -59,13 +60,13 @@ func readClipboard(a string) {
old = text old = text
updateTime = time.Now().Unix() updateTime = time.Now().Unix()
request, _ := http.NewRequest("POST", 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) client.Do(request)
} else { } else {
response, _ := http.Get(a) response, _ := http.Get(a)
texts, ok := response.Header[textHeader] texts, ok := response.Header[textHeader]
if ok { if ok {
sText := texts[0] sText := decode(texts[0])
sTime, _ := strconv.ParseInt(response.Header[timeHeader][0], 10, 64) sTime, _ := strconv.ParseInt(response.Header[timeHeader][0], 10, 64)
if sTime > updateTime && sText != old { if sTime > updateTime && sText != old {
old = sText 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)
}