File tree 2 files changed +20
-1
lines changed 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -72,7 +72,13 @@ func (w *Writer) SetBoundary(boundary string) error {
72
72
// FormDataContentType returns the Content-Type for an HTTP
73
73
// multipart/form-data with this Writer's Boundary.
74
74
func (w * Writer ) FormDataContentType () string {
75
- return "multipart/form-data; boundary=" + w .boundary
75
+ b := w .boundary
76
+ // We must quote the boundary if it contains any of the
77
+ // tspecials characters defined by RFC 2045, or space.
78
+ if strings .ContainsAny (b , `()<>@,;:\"/[]?= ` ) {
79
+ b = `"` + b + `"`
80
+ }
81
+ return "multipart/form-data; boundary=" + b
76
82
}
77
83
78
84
func randomBoundary () string {
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ package multipart
7
7
import (
8
8
"bytes"
9
9
"io/ioutil"
10
+ "mime"
10
11
"net/textproto"
11
12
"strings"
12
13
"testing"
@@ -94,6 +95,7 @@ func TestWriterSetBoundary(t *testing.T) {
94
95
{"my-separator" , true },
95
96
{"with space" , true },
96
97
{"badspace " , false },
98
+ {"(boundary)" , true },
97
99
}
98
100
for i , tt := range tests {
99
101
var b bytes.Buffer
@@ -107,6 +109,17 @@ func TestWriterSetBoundary(t *testing.T) {
107
109
if got != tt .b {
108
110
t .Errorf ("boundary = %q; want %q" , got , tt .b )
109
111
}
112
+
113
+ ct := w .FormDataContentType ()
114
+ mt , params , err := mime .ParseMediaType (ct )
115
+ if err != nil {
116
+ t .Errorf ("could not parse Content-Type %q: %v" , ct , err )
117
+ } else if mt != "multipart/form-data" {
118
+ t .Errorf ("unexpected media type %q; want %q" , mt , "multipart/form-data" )
119
+ } else if b := params ["boundary" ]; b != tt .b {
120
+ t .Errorf ("unexpected boundary parameter %q; want %q" , b , tt .b )
121
+ }
122
+
110
123
w .Close ()
111
124
wantSub := "\r \n --" + tt .b + "--\r \n "
112
125
if got := b .String (); ! strings .Contains (got , wantSub ) {
You can’t perform that action at this time.
0 commit comments