Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(net/gclient): using the wrong header to update file #3775

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion net/gclient/gclient_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,17 @@ func (c *Client) prepareRequest(ctx context.Context, method, url string, data ..
return nil, err
}
} else {
if strings.Contains(params, httpParamFileHolder) {
if strings.Contains(params, httpParamFileHolder) && c.allowUploadHeader() {
// File uploading request.
var (
buffer = bytes.NewBuffer(nil)
writer = multipart.NewWriter(buffer)
)
for _, item := range strings.Split(params, "&") {
array := strings.Split(item, "=")
if len(array) < 2 {
continue
}
if len(array[1]) > 6 && strings.Compare(array[1][0:6], httpParamFileHolder) == 0 {
path := array[1][6:]
if !gfile.Exists(path) {
Expand Down Expand Up @@ -370,3 +373,11 @@ func (c *Client) callRequest(req *http.Request) (resp *Response, err error) {
}
return resp, err
}

func (c *Client) allowUploadHeader() bool {
var notAllows = []string{
httpHeaderContentTypeJson,
httpHeaderContentTypeXml,
httpHeaderContentTypeForm}
return !gstr.InArray(notAllows, c.header[httpHeaderContentType])
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

等等,我看了下这个issue #3748 只是提了会报panic,应该是参数处理不严谨。而你这个pr做了一些枚举来允许文件上传,好像是两码事了诶?!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前这块的上传逻辑不严谨,如果 header content-type 头是 json 或者 xml 这种不允许上传文件的头,应该将其做为一个普通的字符串上传

82 changes: 82 additions & 0 deletions net/gclient/gclient_z_unit_issues_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gclient_test

import (
"fmt"
"strings"
"testing"
"time"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gclient"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
)

func Test_Issue3748(t *testing.T) {
s := g.Server(guid.S())
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Write(
r.GetBody(),
)
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

clientHost := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
time.Sleep(100 * time.Millisecond)

gtest.C(t, func(t *gtest.T) {
client := gclient.New()
client.SetHeader("Content-Type", "application/json")
data := map[string]interface{}{
"name": "@file:",
"value": "json",
}
client.SetPrefix(clientHost)
content := client.PostContent(ctx, "/", data)
t.Assert(content, `{"name":"@file:","value":"json"}`)
})

gtest.C(t, func(t *gtest.T) {
client := gclient.New()
client.SetHeader("Content-Type", "application/xml")
data := map[string]interface{}{
"name": "@file:",
"value": "xml",
}
client.SetPrefix(clientHost)
content := client.PostContent(ctx, "/", data)
t.Assert(content, `<doc><name>@file:</name><value>xml</value></doc>`)
})

gtest.C(t, func(t *gtest.T) {
client := gclient.New()
client.SetHeader("Content-Type", "application/x-www-form-urlencoded")
data := map[string]interface{}{
"name": "@file:",
"value": "x-www-form-urlencoded",
}
client.SetPrefix(clientHost)
content := client.PostContent(ctx, "/", data)
contentSlice := strings.Split(content, "&")
t.Assert(len(contentSlice), 2)
t.Assert(strings.Contains(content, `name=@file:`), true)
t.Assert(strings.Contains(content, `value=x-www-form-urlencoded`), true)
})

gtest.C(t, func(t *gtest.T) {
client := gclient.New()
data := "@file:"
client.SetPrefix(clientHost)
_, err := client.Post(ctx, "/", data)
t.AssertNil(err)
})
}
Loading