1
0
mirror of https://github.com/golang/go synced 2024-11-20 00:04:43 -07:00

test for presence of filename instead of content-type, add a regression test

This commit is contained in:
Henry Clifford 2018-02-24 13:54:39 -05:00
parent c879153831
commit 14973d7c2b
3 changed files with 35 additions and 2 deletions

View File

@ -58,8 +58,7 @@ func (r *Reader) readForm(maxMemory int64) (_ *Form, err error) {
var b bytes.Buffer var b bytes.Buffer
_, hasContentTypeHeader := p.Header["Content-Type"] if !p.hasFileName() {
if !hasContentTypeHeader && filename == "" {
// value, store as string in memory // value, store as string in memory
n, err := io.CopyN(&b, p, maxValueBytes+1) n, err := io.CopyN(&b, p, maxValueBytes+1)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {

View File

@ -55,6 +55,21 @@ func TestReadFormWithNamelessFile(t *testing.T) {
} }
func TestReadFormWithTextContentType(t *testing.T) {
// From https://github.com/golang/go/issues/24041
b := strings.NewReader(strings.Replace(messageWithTextContentType, "\n", "\r\n", -1))
r := NewReader(b, boundary)
f, err := r.ReadForm(25)
if err != nil {
t.Fatal("ReadForm:", err)
}
defer f.RemoveAll()
if g, e := f.Value["texta"][0], textaValue; g != e {
t.Errorf("texta value = %q, want %q", g, e)
}
}
func testFile(t *testing.T, fh *FileHeader, efn, econtent string) File { func testFile(t *testing.T, fh *FileHeader, efn, econtent string) File {
if fh.Filename != efn { if fh.Filename != efn {
t.Errorf("filename = %q, want %q", fh.Filename, efn) t.Errorf("filename = %q, want %q", fh.Filename, efn)
@ -94,6 +109,15 @@ Content-Type: text/plain
--MyBoundary-- --MyBoundary--
` `
const messageWithTextContentType = `
--MyBoundary
Content-Disposition: form-data; name="texta"
Content-Type: text/plain
` + textaValue + `
--MyBoundary
`
const message = ` const message = `
--MyBoundary --MyBoundary
Content-Disposition: form-data; name="filea"; filename="filea.txt" Content-Disposition: form-data; name="filea"; filename="filea.txt"

View File

@ -81,6 +81,16 @@ func (p *Part) FileName() string {
return p.dispositionParams["filename"] return p.dispositionParams["filename"]
} }
// hasFileName determines if a (empty or otherwise)
// filename parameter was included in the Content-Disposition header
func (p *Part) hasFileName() bool {
if p.dispositionParams == nil {
p.parseContentDisposition()
}
_, ok := p.dispositionParams["filename"]
return ok
}
func (p *Part) parseContentDisposition() { func (p *Part) parseContentDisposition() {
v := p.Header.Get("Content-Disposition") v := p.Header.Get("Content-Disposition")
var err error var err error