From be637fca8150e04c7475da6c811644efc3ab86fd Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Fri, 15 Dec 2023 00:00:47 +0800 Subject: [PATCH] fix: fix wrong POST param logic in custom HTTP providers --- email/http.go | 31 +++++++++++++++++++------------ notification/custom_http.go | 30 +++++++++++++++++------------- object/sms_custom.go | 27 +++++++++++++++++---------- 3 files changed, 53 insertions(+), 35 deletions(-) diff --git a/email/http.go b/email/http.go index e0d3383d..b81073f1 100644 --- a/email/http.go +++ b/email/http.go @@ -15,9 +15,10 @@ package email import ( - "bytes" "fmt" "net/http" + "net/url" + "strings" "github.com/casdoor/casdoor/proxy" ) @@ -36,20 +37,26 @@ func NewHttpEmailProvider(endpoint string, method string) *HttpEmailProvider { } func (c *HttpEmailProvider) Send(fromAddress string, fromName string, toAddress string, subject string, content string) error { - req, err := http.NewRequest(c.method, c.endpoint, bytes.NewBufferString(content)) - if err != nil { - return err - } - + var req *http.Request + var err error if c.method == "POST" { - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - req.PostForm = map[string][]string{ - "fromName": {fromName}, - "toAddress": {toAddress}, - "subject": {subject}, - "content": {content}, + formValues := url.Values{} + formValues.Set("fromName", fromName) + formValues.Set("toAddress", toAddress) + formValues.Set("subject", subject) + formValues.Set("content", content) + req, err = http.NewRequest(c.method, c.endpoint, strings.NewReader(formValues.Encode())) + if err != nil { + return err } + + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") } else if c.method == "GET" { + req, err = http.NewRequest(c.method, c.endpoint, nil) + if err != nil { + return err + } + q := req.URL.Query() q.Add("fromName", fromName) q.Add("toAddress", toAddress) diff --git a/notification/custom_http.go b/notification/custom_http.go index 736e4278..ec5122e6 100644 --- a/notification/custom_http.go +++ b/notification/custom_http.go @@ -15,10 +15,11 @@ package notification import ( - "bytes" "context" "fmt" "net/http" + "net/url" + "strings" "github.com/casdoor/casdoor/proxy" ) @@ -39,26 +40,29 @@ func NewCustomHttpProvider(endpoint string, method string, paramName string) (*H } func (c *HttpNotificationClient) Send(ctx context.Context, subject string, content string) error { + var req *http.Request var err error - - httpClient := proxy.DefaultHttpClient - - req, err := http.NewRequest(c.method, c.endpoint, bytes.NewBufferString(content)) - if err != nil { - return err - } - if c.method == "POST" { - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - req.PostForm = map[string][]string{ - c.paramName: {content}, + formValues := url.Values{} + formValues.Set(c.paramName, content) + req, err = http.NewRequest(c.method, c.endpoint, strings.NewReader(formValues.Encode())) + if err != nil { + return err } + + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") } else if c.method == "GET" { + req, err = http.NewRequest(c.method, c.endpoint, nil) + if err != nil { + return err + } + q := req.URL.Query() q.Add(c.paramName, content) req.URL.RawQuery = q.Encode() } + httpClient := proxy.DefaultHttpClient resp, err := httpClient.Do(req) if err != nil { return err @@ -66,7 +70,7 @@ func (c *HttpNotificationClient) Send(ctx context.Context, subject string, conte defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return fmt.Errorf("SendMessage() error, custom HTTP Notification request failed with status: %s", resp.Status) + return fmt.Errorf("HttpNotificationClient's SendMessage() error, custom HTTP Notification request failed with status: %s", resp.Status) } return err diff --git a/object/sms_custom.go b/object/sms_custom.go index acddfe0f..03d3f8ab 100644 --- a/object/sms_custom.go +++ b/object/sms_custom.go @@ -15,9 +15,10 @@ package object import ( - "bytes" "fmt" "net/http" + "net/url" + "strings" "github.com/casdoor/casdoor/proxy" ) @@ -41,18 +42,24 @@ func (c *HttpSmsClient) SendMessage(param map[string]string, targetPhoneNumber . phoneNumber := targetPhoneNumber[0] content := param["code"] - req, err := http.NewRequest(c.method, c.endpoint, bytes.NewBufferString(content)) - if err != nil { - return err - } - + var req *http.Request + var err error if c.method == "POST" { - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - req.PostForm = map[string][]string{ - "phoneNumber": targetPhoneNumber, - c.paramName: {content}, + formValues := url.Values{} + formValues.Set("phoneNumber", phoneNumber) + formValues.Set(c.paramName, content) + req, err = http.NewRequest(c.method, c.endpoint, strings.NewReader(formValues.Encode())) + if err != nil { + return err } + + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") } else if c.method == "GET" { + req, err = http.NewRequest(c.method, c.endpoint, nil) + if err != nil { + return err + } + q := req.URL.Query() q.Add("phoneNumber", phoneNumber) q.Add(c.paramName, content)