Skip to content

Commit

Permalink
Merge dev_1.5.0 into master
Browse files Browse the repository at this point in the history
  • Loading branch information
likui2 committed Nov 7, 2024
2 parents d4de411 + e9333a6 commit c30e72e
Show file tree
Hide file tree
Showing 19 changed files with 2,074 additions and 481 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor
doc
.idea/
.DS_Store
.DS_Store
go-sdk-test*
36 changes: 28 additions & 8 deletions internal/protocol/query/unmarshal_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,56 @@ package query

import (
"encoding/xml"
"io"
"io/ioutil"
"log"

"github.com/ks3sdklib/aws-sdk-go/aws"
"github.com/ks3sdklib/aws-sdk-go/internal/apierr"
"io"
"regexp"
"strings"
)

type XmlErrorResponse struct {
XMLName xml.Name `xml:"Error"`
Code string `xml:"Code"`
StatusCode int `"StatusCode"`
StatusCode int `xml:"StatusCode"`
Message string `xml:"Message"`
Resource string `xml:"Resource"`
RequestID string `xml:"RequestId"`
}

// UnmarshalError unmarshals an error response for an AWS Query service.
// UnmarshalError unmarshal an error response for an AWS Query service.
func UnmarshalError(r *aws.Request) {
defer r.HTTPResponse.Body.Close()

resp := &XmlErrorResponse{}
body, err := ioutil.ReadAll(r.HTTPResponse.Body)
body, err := io.ReadAll(r.HTTPResponse.Body)
if err != nil {
log.Printf("read body err, %v\n", err)
r.Error = apierr.New("Unmarshal", "failed to read body", err)
return
}

// 如果响应类型是html,则解析html文本
if strings.Contains(r.HTTPResponse.Header.Get("Content-Type"), "text/html") {
// 获取HTML文本中title标签的内容
re := regexp.MustCompile(`<title>(.*?)</title>`)
matches := re.FindStringSubmatch(string(body))

title := ""
if len(matches) > 1 {
title = matches[1]
}

r.Error = apierr.NewRequestError(apierr.New(title, "", nil), r.HTTPResponse.StatusCode, "")
return
}

err = xml.Unmarshal(body, &resp)
resp.StatusCode = r.HTTPResponse.StatusCode

// head请求无法从body中获取request id,如果是head请求,则从header中获取
if resp.RequestID == "" && r.HTTPRequest.Method == "HEAD" {
resp.RequestID = r.HTTPResponse.Header.Get("X-Kss-Request-Id")
}

if err != nil && err != io.EOF {
r.Error = apierr.New("Unmarshal", "failed to decode query XML error response", err)
} else {
Expand Down
17 changes: 16 additions & 1 deletion internal/protocol/rest/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func buildLocationElements(r *aws.Request, v reflect.Value) {
buildURI(r, m, name)
case "querystring":
buildQueryString(r, m, name, query)
case "parameters":
buildParameters(r, m, query)
}
}
if r.Error != nil {
Expand Down Expand Up @@ -149,6 +151,19 @@ func buildQueryString(r *aws.Request, v reflect.Value, name string, query url.Va
r.Error = apierr.New("Marshal", "failed to encode REST request", err)
} else if str != nil {
query.Set(name, *str)
} else if str == nil {
query.Set(name, "")
}
}

func buildParameters(r *aws.Request, v reflect.Value, query url.Values) {
for _, key := range v.MapKeys() {
str, err := convertType(v.MapIndex(key))
if err != nil {
r.Error = apierr.New("Marshal", "failed to encode REST request", err)
} else {
buildQueryString(r, reflect.ValueOf(str), key.String(), query)
}
}
}

Expand Down Expand Up @@ -230,7 +245,7 @@ func convertType(v reflect.Value) (*string, error) {
case time.Time:
str = value.UTC().Format(RFC822)
default:
err := fmt.Errorf("Unsupported value for param %v (%s)", v.Interface(), v.Type())
err := fmt.Errorf("unsupported value for param %v (%s)", v.Interface(), v.Type())
return nil, err
}
return &str, nil
Expand Down
8 changes: 8 additions & 0 deletions internal/signer/v2/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ var signQuerys = map[string]bool{
"append": true,
"position": true,
"decompresspolicy": true,
"retention": true,
"crr": true,
"inventory": true,
"recycle": true,
"recover": true,
"clear": true,
"id": true,
"continuation-token": true,
}

type signer struct {
Expand Down
Loading

0 comments on commit c30e72e

Please sign in to comment.