ParseMultipartForm()在遇到ErrNotMultipart时返回nil。

huangapple go评论91阅读模式
英文:

ParseMultipartForm() returning nil when ErrNotMultipart

问题

为什么在 ErrNotMultipart 时返回 nil?

在这段代码中,当出现 ErrNotMultipart 错误时,会返回 nil:

mr, err := r.multipartReader()
if err == ErrNotMultipart {
    return nil 
} else if err != nil {
    return err
}

在我的情况下,我在头部有一个拼写错误:

multipart/form-data, boundary=xxxx

上面的逗号应该是一个分号。因此,我追踪了几个小时,因为我以为 ParseMultipartForm 成功处理了数据。

从代码来看,可能有一个很强的原因导致它被强制设置为 nil。

(我在 golang-nuts 组中发布了同样的问题,但当我回复第一个合作者时,我的回复没有成功发布。所以,我认为 stackoverflow 是一个更好的工具来发布这个问题)

英文:

http://golang.org/src/pkg/net/http/request.go

Why it returns to nil when ErrNotMultipart?

mr, err := r.multipartReader()
767		if err == ErrNotMultipart {
768			return nil 
769		} else if err != nil {
770			return err
771		}

In my case, I had a typo from my header

multipart/form-data, boundary=xxxx

The above should have a semi-colon instead of a comma. Now, I was tracing this for hours, because I thought ParseMultipartForm processed the data successfully.

There could be a strong reason why it was forced to nil, judging from the code.

(I posted this same question at golang-nuts group, but when I posted a reply to the first collaborator, my reply was not posted successfully. So, I think stackoverflow is a better tool for posting this question)

答案1

得分: 2

我认为你一开始就正确地写信给了golang-nuts,而不是在这里发帖。

这确实看起来像是一个bug,因此应该在问题跟踪器golang nuts上进行讨论。请注意,golang-nuts是经过审核的,因此你的帖子不会立即显示出来(这可能发生在你身上)。

作为一种解决方法,你可以使用Request.MultipartReader并自己进行读取(从ParseMultipartForm中复制):

mr, err := r.multipartReader() // 不要忘记处理错误
f, err := mr.ReadForm(maxMemory) // 不要忘记处理错误

for k, v := range f.Value {
    r.Form[k] = append(r.Form[k], v...)
}
r.MultipartForm = f

从我从代码历史中推断的情况来看,这显然是被忽视了。这些更改来自于2011年,有关特定代码行的审查可以在这里找到。测试似乎没有覆盖这个特定的解析错误。审查甚至说:

>> 在2011/04/28 00:08:40,bradfitz写道:
>> 我认为你在这里对多部分表单总是返回一个错误。请参见上面的注释。
>
>已修复。

几乎所有的错误都已经修复了,但这个没有。所以,正如我所说,我认为这是一个bug。

英文:

I think you did it right in the first place to write to golang-nuts instead of here.

This does indeed look like a bug and should therefore be discussed in the issue tracker or
on golang nuts. Note that golang-nuts is moderated and therefore your posts will not appear
immediately (which might have happened to you).

As a workaround you can use Request.MultipartReader and do the reading by yourself
(copied from ParseMultipartForm):

mr, err := r.multipartReader() // don't forget to handle err
f, err := mr.ReadForm(maxMemory) // don't forget to handle err

for k, v := range f.Value {
	r.Form[k] = append(r.Form[k], v...)
}
r.MultipartForm = f

Judging from what I can deduce from the code history, this was simply overlooked. The
changes are from 2011 and the review for the particular lines of code can be found
here. The tests don't seem to cover this particular parse error. The review
even says:

>> On 2011/04/28 00:08:40, bradfitz wrote:
>> I think you'll always return an error here for multipart forms. See comment
>> above.
>
>Fixed.

Which was done for almost all errors but this one. So, as I said, I think it is a bug.

huangapple
  • 本文由 发表于 2013年9月5日 12:53:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/18628009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定