golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.03k stars 17.54k forks source link

mime: FormatMediaType doesn't allow to create Content-Disposition #9624

Closed rasky closed 5 years ago

rasky commented 9 years ago

On Go 1.4 (darwin), mime.FormatMediaType checks whether the first argument (aptly called mediatype) contains a forward slash, and returns an empty string if it doesn't. This doesn't allow to use this function to format a Content-Disposition header, because the values for such header are not proper media types (they are usually inline or attachment). Notice that the documentation of the reverse function mime.ParseMediaType explicitly says that it can be used to parse Content-Disposition headers, so it looks like this limitation should be lifted.

bradfitz commented 9 years ago

Yeah, looks like we're inconsistent here. Content-Dispositions are different, but we've supported them in ParseMediaType forever (my fault, I guess), despite them not being media types technically. So I guess we could be looser in FormatMediaType for symmetry.

rsc commented 9 years ago

Too late for Go 1.5.

odeke-em commented 8 years ago

If you wouldn't mind @rasky, could you provide me with some failing test samples or just values that should be accepted yet are failing? Thanks.

zensh commented 7 years ago

@odeke-em

fmt.Println(mime.ParseMediaType(`attachment; filename="????.png"; filename*=UTF-8''%E6%95%B0%E6%8D%AE%E7%BB%9F%E8%AE%A1.png`))
fmt.Println(mime.FormatMediaType("attachment", map[string]string{"filename": "数据统计.png"}))
fmt.Println(mime.FormatMediaType("attachment", map[string]string{"filename": "test.png"}))
andrius4669 commented 5 years ago

Seems to be working with current Go (go1.11).

bradfitz commented 5 years ago

@andrius4669, does it? https://play.golang.org/p/ndwGaJDMBO4

I see:

attachment map[filename:数据统计.png] <nil>

attachment; filename=test.png

@zensh, do you expect the middle case to work? I guess it should encode it with UTF-8 the same way as the first line parses?

andrius4669 commented 5 years ago

@bradfitz sorry, I didn't meant samples specified by zensh. Initial issue about FormatMediaType and ParseMediaType not being symmetric about both allowing disposition has been fixed apparently, it now allows that. It currently cannot encode non-ASCII unicode chatacters because it doesn't implement encoding specified in RFC 2231 section 4, and instead intentionally fail if it detects such input. https://github.com/golang/go/issues/28849 was somewhat related to this (and I was told to fill separate feature request about RFC2231 encoding). I guess this issue could be used for discussion about that encoding, if you think this is right.

andrius4669 commented 5 years ago

Sorta working on this now.

gopherbot commented 5 years ago

Change https://golang.org/cl/154760 mentions this issue: mime: encode CTL and non-US-ASCII characters in FormatMediaType

andrius4669 commented 5 years ago

https://github.com/golang/go/pull/29328

zensh commented 5 years ago

@andrius4669 👍