Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
ef945203
Commit
ef945203
authored
May 02, 2011
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
multipart: add FileName accessor on Part
R=rsc, adg CC=golang-dev
https://golang.org/cl/4426074
parent
bc926713
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
30 deletions
+43
-30
src/pkg/mime/multipart/formdata.go
src/pkg/mime/multipart/formdata.go
+1
-4
src/pkg/mime/multipart/multipart.go
src/pkg/mime/multipart/multipart.go
+24
-9
src/pkg/mime/multipart/multipart_test.go
src/pkg/mime/multipart/multipart_test.go
+18
-17
No files found.
src/pkg/mime/multipart/formdata.go
View file @
ef945203
...
...
@@ -41,10 +41,7 @@ func (r *multiReader) ReadForm(maxMemory int64) (f *Form, err os.Error) {
if
name
==
""
{
continue
}
var
filename
string
if
p
.
dispositionParams
!=
nil
{
filename
=
p
.
dispositionParams
[
"filename"
]
}
filename
:=
p
.
FileName
()
var
b
bytes
.
Buffer
...
...
src/pkg/mime/multipart/multipart.go
View file @
ef945203
...
...
@@ -26,6 +26,8 @@ import (
var
headerRegexp
*
regexp
.
Regexp
=
regexp
.
MustCompile
(
"^([a-zA-Z0-9
\\
-]+): *([^
\r\n
]+)"
)
var
emptyParams
=
make
(
map
[
string
]
string
)
// Reader is an iterator over parts in a MIME multipart body.
// Reader's underlying parser consumes its input as needed. Seeking
// isn't supported.
...
...
@@ -51,6 +53,7 @@ type Part struct {
buffer
*
bytes
.
Buffer
mr
*
multiReader
disposition
string
dispositionParams
map
[
string
]
string
}
...
...
@@ -59,21 +62,33 @@ type Part struct {
func
(
p
*
Part
)
FormName
()
string
{
// See http://tools.ietf.org/html/rfc2183 section 2 for EBNF
// of Content-Disposition value format.
if
p
.
dispositionParams
!=
nil
{
return
p
.
dispositionParams
[
"name"
]
}
v
:=
p
.
Header
.
Get
(
"Content-Disposition"
)
if
v
==
""
{
return
""
if
p
.
dispositionParams
==
nil
{
p
.
parseContentDisposition
()
}
if
d
,
params
:=
mime
.
ParseMediaType
(
v
);
d
!=
"form-data"
{
if
p
.
disposition
!=
"form-data"
{
return
""
}
else
{
p
.
dispositionParams
=
params
}
return
p
.
dispositionParams
[
"name"
]
}
// FileName returns the filename parameter of the Part's
// Content-Disposition header.
func
(
p
*
Part
)
FileName
()
string
{
if
p
.
dispositionParams
==
nil
{
p
.
parseContentDisposition
()
}
return
p
.
dispositionParams
[
"filename"
]
}
func
(
p
*
Part
)
parseContentDisposition
()
{
v
:=
p
.
Header
.
Get
(
"Content-Disposition"
)
p
.
disposition
,
p
.
dispositionParams
=
mime
.
ParseMediaType
(
v
)
if
p
.
dispositionParams
==
nil
{
p
.
dispositionParams
=
emptyParams
}
}
// NewReader creates a new multipart Reader reading from r using the
// given MIME boundary.
func
NewReader
(
reader
io
.
Reader
,
boundary
string
)
Reader
{
...
...
src/pkg/mime/multipart/multipart_test.go
View file @
ef945203
...
...
@@ -56,24 +56,25 @@ func expectEq(t *testing.T, expected, actual, what string) {
what
,
escapeString
(
actual
),
len
(
actual
),
escapeString
(
expected
),
len
(
expected
))
}
func
Test
FormName
(
t
*
testing
.
T
)
{
p
:=
new
(
Part
)
p
.
Header
=
make
(
map
[
string
][]
string
)
tests
:=
[
...
][
2
]
string
{
{
`
form-data; name="foo"`
,
"foo
"
},
{
`
form-data ; name=foo`
,
"foo
"
},
{
`
FORM-DATA;name="foo"`
,
"foo
"
},
{
` FORM-DATA ; name=
"foo"`
,
"foo
"
},
{
` FORM-DATA ;
name="foo"`
,
"foo
"
},
{
`
FORM-DATA ; name=foo`
,
"foo
"
},
{
` FORM-DATA ; filename="foo.txt"; name=foo; baz=quux`
,
"foo"
},
}
for
_
,
test
:=
range
tests
{
func
Test
NameAccessors
(
t
*
testing
.
T
)
{
tests
:=
[
...
][
3
]
string
{
{
`form-data; name="foo"`
,
"foo"
,
""
},
{
` form-data ; name=foo`
,
"foo"
,
""
},
{
`
FORM-DATA;name="foo"`
,
"foo"
,
"
"
},
{
`
FORM-DATA ; name="foo"`
,
"foo"
,
"
"
},
{
`
FORM-DATA ; name="foo"`
,
"foo"
,
"
"
},
{
` FORM-DATA ; name=
foo`
,
"foo"
,
"
"
},
{
` FORM-DATA ;
filename="foo.txt"; name=foo; baz=quux`
,
"foo"
,
"foo.txt
"
},
{
`
not-form-data ; filename="bar.txt"; name=foo; baz=quux`
,
""
,
"bar.txt
"
},
}
for
i
,
test
:=
range
tests
{
p
:=
&
Part
{
Header
:
make
(
map
[
string
][]
string
)}
p
.
Header
.
Set
(
"Content-Disposition"
,
test
[
0
])
expected
:=
test
[
1
]
actual
:=
p
.
FormName
()
if
actual
!=
expected
{
t
.
Errorf
(
"expected
\"
%s
\"
; got:
\"
%s
\"
"
,
expected
,
actual
)
if
g
,
e
:=
p
.
FormName
(),
test
[
1
];
g
!=
e
{
t
.
Errorf
(
"test %d: FormName() = %q; want %q"
,
i
,
g
,
e
)
}
if
g
,
e
:=
p
.
FileName
(),
test
[
2
];
g
!=
e
{
t
.
Errorf
(
"test %d: FileName() = %q; want %q"
,
i
,
g
,
e
)
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment