Skip to content

Commit

Permalink
remove http request url path cleaning logic and deprecate setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianyi Wang committed May 17, 2023
1 parent 6e99df2 commit e8df529
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 45 deletions.
2 changes: 2 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 1 addition & 39 deletions private/protocol/rest/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"math"
"net/http"
"net/url"
"path"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -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]) {
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
30 changes: 24 additions & 6 deletions private/protocol/rest/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit e8df529

Please sign in to comment.