From e8df52967e7995b36d28cf00a6ed9eee4da72f61 Mon Sep 17 00:00:00 2001 From: Tianyi Wang Date: Tue, 16 May 2023 23:35:10 -0400 Subject: [PATCH] remove http request url path cleaning logic and deprecate setting --- aws/config.go | 2 ++ private/protocol/rest/build.go | 40 +---------------------------- private/protocol/rest/build_test.go | 30 +++++++++++++++++----- 3 files changed, 27 insertions(+), 45 deletions(-) diff --git a/aws/config.go b/aws/config.go index 776e31b21d6..a64d720f45e 100644 --- a/aws/config.go +++ b/aws/config.go @@ -265,6 +265,8 @@ type Config struct { // Bucket: aws.String("bucketname"), // Key: aws.String("//foo//bar//moo"), // }) + // Deprecated: DisableRestProtocolURICleaning exists for historical compatibility of + // http request path cleaning setting and should not be used. DisableRestProtocolURICleaning *bool // EnableEndpointDiscovery will allow for endpoint discovery on operations that diff --git a/private/protocol/rest/build.go b/private/protocol/rest/build.go index b5685c476ee..00d4d91c5f5 100644 --- a/private/protocol/rest/build.go +++ b/private/protocol/rest/build.go @@ -9,7 +9,6 @@ import ( "math" "net/http" "net/url" - "path" "reflect" "strconv" "strings" @@ -77,10 +76,6 @@ func buildLocationElements(r *request.Request, v reflect.Value, buildGETQuery bo // stored in RawPath that will be used by the Go client. r.HTTPRequest.URL.RawPath = r.HTTPRequest.URL.Path - // for services including iotdataplane, topic needs to be preserved while encoded into url - topic := "" - var err error - for i := 0; i < v.NumField(); i++ { m := v.Field(i) if n := v.Type().Field(i).Name; n[0:1] == strings.ToLower(n[0:1]) { @@ -93,13 +88,6 @@ func buildLocationElements(r *request.Request, v reflect.Value, buildGETQuery bo if name == "" { name = field.Name } - if name == "topic" { - topic, err = convertType(m, field.Tag) - if err != nil { - return - } - } - if kind := m.Kind(); kind == reflect.Ptr { m = m.Elem() } else if kind == reflect.Interface { @@ -122,6 +110,7 @@ func buildLocationElements(r *request.Request, v reflect.Value, buildGETQuery bo m = m.Convert(byteSliceType) } + var err error switch field.Tag.Get("location") { case "headers": // header maps err = buildHeaderMap(&r.HTTPRequest.Header, m, field.Tag) @@ -144,9 +133,6 @@ func buildLocationElements(r *request.Request, v reflect.Value, buildGETQuery bo } r.HTTPRequest.URL.RawQuery = query.Encode() - if !aws.BoolValue(r.Config.DisableRestProtocolURICleaning) { - cleanPath(r.HTTPRequest.URL, topic) - } } func buildBody(r *request.Request, v reflect.Value) { @@ -254,30 +240,6 @@ func buildQueryString(query url.Values, v reflect.Value, name string, tag reflec return nil } -func cleanPath(u *url.URL, topic string) { - hasSlash := strings.HasSuffix(u.Path, "/") - - // clean up path, removing duplicate `/` - u.Path = path.Clean(u.Path) - u.RawPath = path.Clean(u.RawPath) - - if hasSlash && !strings.HasSuffix(u.Path, "/") { - u.Path += "/" - u.RawPath += "/" - } - - // restore topic if exists - if topic != "" { - start := strings.Index(u.Path, "topics/") - end := strings.Index(u.Path, "?qos") - if end == -1 { - end = len(u.Path) - } - runes := []rune(u.Path) - u.Path = string(runes[:start+7]) + topic + string(runes[end:]) - } -} - // EscapePath escapes part of a URL path in Amazon style func EscapePath(path string, encodeSep bool) string { var buf bytes.Buffer diff --git a/private/protocol/rest/build_test.go b/private/protocol/rest/build_test.go index 6f9dddf650a..12305e13530 100644 --- a/private/protocol/rest/build_test.go +++ b/private/protocol/rest/build_test.go @@ -14,17 +14,35 @@ import ( "github.com/aws/aws-sdk-go/aws/request" ) -func TestCleanPath(t *testing.T) { +func TestBuildURI(t *testing.T) { + in := struct { + Topic *string `location:"uri" locationName:"topic" type:"string" required:"true"` + }{ + Topic: aws.String("///devices/123/test"), + } + uri := &url.URL{ - Path: "//foo//bar", + Path: "/topics/{topic}", Scheme: "https", Host: "host", } - cleanPath(uri, "") - expected := "https://host/foo/bar" - if a, e := uri.String(), expected; a != e { - t.Errorf("expect %q URI, got %q", e, a) + req := &request.Request{ + HTTPRequest: &http.Request{ + URL: uri, + }, + Params: &in, + } + + Build(req) + + expectedPath := "/topics////devices/123/test" + expectedRawPath := "/topics/%2F%2F%2Fdevices%2F123%2Ftest" + if a, e := uri.Path, expectedPath; a != e { + t.Errorf("expect %q Path, got %q", e, a) + } + if a, e := uri.RawPath, expectedRawPath; a != e { + t.Errorf("expect %q RawPath, got %q", e, a) } }