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

获取 Content-Type 的小问题 #16

Open
xmx opened this issue Jan 5, 2023 · 0 comments
Open

获取 Content-Type 的小问题 #16

xmx opened this issue Jan 5, 2023 · 0 comments

Comments

@xmx
Copy link
Contributor

xmx commented Jan 5, 2023

ship/context.go

Lines 410 to 417 in 405a71d

// ContentType returns the Content-Type of the request without the charset.
func (c *Context) ContentType() (ct string) {
ct = c.req.Header.Get(HeaderContentType)
if index := strings.IndexAny(ct, ";"); index > 0 {
ct = strings.TrimSpace(ct[:index])
}
return
}

ship/binder.go

Lines 72 to 83 in 405a71d

// Bind implements the interface Binder, which looks up the registered binder
// by the request header "Content-Type" and calls it to bind the value dst
// to req.
func (mb *MuxBinder) Bind(dst interface{}, req *http.Request) error {
ct := req.Header.Get("Content-Type")
if index := strings.IndexAny(ct, ";"); index > 0 {
ct = strings.TrimSpace(ct[:index])
}
if ct == "" {
return ErrMissingContentType
}

上面两处代码在获取 Content-Type 时用的是 index > 0,如果测试用例是 ; charset=utf-8,那么切割出的 Content-Type; charset=utf-8。此时我们期望结果的应该是 空字符串

故:此处使用 index >= 0 更为妥当

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant