summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorArne Juul <arnej@yahooinc.com>2023-02-17 12:56:35 +0000
committerArne Juul <arnej@yahooinc.com>2023-02-17 12:56:35 +0000
commitb9f10138d4a80e1e2cc826809af41e0b3a8bb16d (patch)
treee39f87fb4bdce4ea997ae08cb185154acc3abb6d /client
parent58cc508408d72d861f997fae3cb121fcde8b1efa (diff)
add quoting
Diffstat (limited to 'client')
-rw-r--r--client/go/internal/cli/cmd/visit.go27
-rw-r--r--client/go/internal/cli/cmd/visit_test.go25
2 files changed, 49 insertions, 3 deletions
diff --git a/client/go/internal/cli/cmd/visit.go b/client/go/internal/cli/cmd/visit.go
index 12442223bae..219f125534e 100644
--- a/client/go/internal/cli/cmd/visit.go
+++ b/client/go/internal/cli/cmd/visit.go
@@ -214,13 +214,34 @@ func runVisit(vArgs visitArgs, service *vespa.Service) (res util.OperationResult
return
}
+func quoteArgForUrl(arg string) string {
+ var buf strings.Builder
+ buf.Grow(len(arg))
+ for _, r := range arg {
+ switch {
+ case 'a' <= r && r <= 'z':
+ buf.WriteRune(r)
+ case 'A' <= r && r <= 'Z':
+ buf.WriteRune(r)
+ case '0' <= r && r <= '9':
+ buf.WriteRune(r)
+ case r <= ' ' || r > '~':
+ buf.WriteRune('+')
+ default:
+ s := fmt.Sprintf("%s%02X", "%", r)
+ buf.WriteString(s)
+ }
+ }
+ return buf.String()
+}
+
func runOneVisit(vArgs visitArgs, service *vespa.Service, contToken string) (*VespaVisitOutput, util.OperationResult) {
- urlPath := service.BaseURL + "/document/v1/?cluster=" + vArgs.contentCluster
+ urlPath := service.BaseURL + "/document/v1/?cluster=" + quoteArgForUrl(vArgs.contentCluster)
if vArgs.fieldSet != "" {
- urlPath = urlPath + "&fieldSet=" + vArgs.fieldSet
+ urlPath = urlPath + "&fieldSet=" + quoteArgForUrl(vArgs.fieldSet)
}
if vArgs.selection != "" {
- urlPath = urlPath + "&selection=" + vArgs.selection
+ urlPath = urlPath + "&selection=" + quoteArgForUrl(vArgs.selection)
}
if contToken != "" {
urlPath = urlPath + "&continuation=" + contToken
diff --git a/client/go/internal/cli/cmd/visit_test.go b/client/go/internal/cli/cmd/visit_test.go
new file mode 100644
index 00000000000..9f4fbb66e00
--- /dev/null
+++ b/client/go/internal/cli/cmd/visit_test.go
@@ -0,0 +1,25 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package cmd
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestQuoteFunc(t *testing.T) {
+ var buf []byte = make([]byte, 3)
+ buf[0] = 'a'
+ buf[2] = 'z'
+ for i := 0; i < 256; i++ {
+ buf[1] = byte(i)
+ s := string(buf)
+ res := quoteArgForUrl(s)
+ if i < 32 || i > 127 {
+ assert.Equal(t, "a+z", res)
+ } else {
+ fmt.Printf("res %3d => '%s'\n", i, res)
+ }
+ }
+}