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

net/http: ParseForm does not treat missing Content-Type as application/octet-stream #6616

Closed
gopherbot opened this issue Oct 18, 2013 · 7 comments
Milestone

Comments

@gopherbot
Copy link

by john.berthels:

What steps will reproduce the problem?

http://play.golang.org/p/hLyBQYF_W5


What is the expected output?
err1 <nil>
err2 <nil>
Cases handled the same


What do you see instead?
err1 mime: no media type
err2 <nil>
Cases handled differently

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g

Which operating system are you using?
ubuntu linux 13.04

Which version are you using?  (run 'go version')
go version go1.1.2 linux/amd64


Please provide any additional information below.

We're in the process of replacing a production service with a re-implementation in
golang. This throws an error "mime: no media type" with POST requests from an
existing application. Investigation shows the POST lacks a content-type header, which
causes ParseForm to error.

My reading of RFC2616 section 7.2.1 suggests that a server should treat an absent
'Content-type' as 'application/octet-stream'.

ParseForm is being called, even though the input content-type is not
x-www-form-urlencoded, in wrapper code which wants to extract url parameters and/or form
parameters.

Looking at the code in net/http/request.go, I would think that parsePostForm should
return err=nil in the case of an absent content type. i.e. change:

   663      ct := r.Header.Get("Content-Type")
   664      ct, _, err = mime.ParseMediaType(ct)

to

   663      ct := r.Header.Get("Content-Type")
+               if ct == "" {
+                       return
+               }
   664      ct, _, err = mime.ParseMediaType(ct)


We're working around the problem, but I thought I'd report the issue for your
consideration.

regards,

jb
@rsc
Copy link
Contributor

rsc commented Oct 18, 2013

Comment 1:

Labels changed: added priority-later, removed priority-triage.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Oct 18, 2013

Comment 2:

Labels changed: added go1.3.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 3:

Labels changed: added release-go1.3.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 4:

Labels changed: removed go1.3.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 5:

Labels changed: added repo-main.

@mattrco
Copy link

mattrco commented Dec 10, 2013

Comment 6:

The code's changed since this was filed but the test program shows the same behaviour.
For reference, the section from RFC2616:
"Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header
field defining the media type of that body. If and only if the media type is not given
by a Content-Type field, the recipient MAY attempt to guess the media type via
inspection of its content and/or the name extension(s) of the URI used to identify the
resource. If the media type remains unknown, the recipient SHOULD treat it as type
"application/octet-stream"."
I don't think a nil error from parsePostForm would be good since there's no case to
handle empty Content-Type (http://golang.org/src/pkg/net/http/request.go?#L681) so you
would get both nil error and nil Request.PostForm.
To treat absent Content-Type as application/octet-stream, a change would need to be made
to mime.checkMediaTypeDisposition (or a check made before calling it).
The question then is what ParseForm could do with application/octet-stream (probably
nothing). So maybe this just requires documenting.
(I'm looking at this because of https://golang.org/issue/6334).

@bradfitz
Copy link
Contributor

Comment 7:

This issue was closed by revision 57e27a8.

Status changed to Fixed.

@rsc rsc added this to the Go1.3 milestone Apr 14, 2015
@rsc rsc removed the release-go1.3 label Apr 14, 2015
bz2 added a commit to bz2/httprequest that referenced this issue Oct 11, 2015
Also includes drive-by fix for test failure on go 1.2 due to test
using POST but having no body, and therefor no content-type, which
tickles <golang/go#6616>.
bz2 added a commit to bz2/httprequest that referenced this issue Oct 11, 2015
Also includes drive-by fix for test failure on go 1.2 due to test
using POST but having no body, and therefora no content-type,
which tickles <golang/go#6616>.
jujubot added a commit to juju/httprequest that referenced this issue Oct 12, 2015
Update dependencies to pick up yaml.v2

Also includes drive-by fix for test failure on go 1.2 due to test
using POST but having no body, and therefore no content-type, which
tickles <golang/go#6616>.
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants