aboutsummaryrefslogtreecommitdiff
path: root/src/pkg
diff options
context:
space:
mode:
authorRui Ueyama <ruiu@google.com>2014-07-29 18:24:50 -0700
committerRui Ueyama <ruiu@google.com>2014-07-29 18:24:50 -0700
commitfe4fc94b044df5e6d08ad9e480f0bce70cc4e5d5 (patch)
treed6847001fe3404ad8dd567c9c2493ffe47948de3 /src/pkg
parent678ba85e5370e509c1fbfe9adb9a70d2b9212bca (diff)
downloadgo-fe4fc94b044df5e6d08ad9e480f0bce70cc4e5d5.tar.xz
mime/multipart: delay reading random source
If a user sets his/her own boundary string with SetBoundary, we don't need to call randomBoundary at all. LGTM=bradfitz R=golang-codereviews, bradfitz CC=golang-codereviews https://golang.org/cl/95760043
Diffstat (limited to 'src/pkg')
-rw-r--r--src/pkg/mime/multipart/writer.go14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/pkg/mime/multipart/writer.go b/src/pkg/mime/multipart/writer.go
index e13a956afe..6b5f4a6b31 100644
--- a/src/pkg/mime/multipart/writer.go
+++ b/src/pkg/mime/multipart/writer.go
@@ -25,13 +25,15 @@ type Writer struct {
// writing to w.
func NewWriter(w io.Writer) *Writer {
return &Writer{
- w: w,
- boundary: randomBoundary(),
+ w: w,
}
}
// Boundary returns the Writer's boundary.
func (w *Writer) Boundary() string {
+ if w.boundary == "" {
+ w.boundary = randomBoundary()
+ }
return w.boundary
}
@@ -65,7 +67,7 @@ func (w *Writer) SetBoundary(boundary string) error {
// FormDataContentType returns the Content-Type for an HTTP
// multipart/form-data with this Writer's Boundary.
func (w *Writer) FormDataContentType() string {
- return "multipart/form-data; boundary=" + w.boundary
+ return "multipart/form-data; boundary=" + w.Boundary()
}
func randomBoundary() string {
@@ -89,9 +91,9 @@ func (w *Writer) CreatePart(header textproto.MIMEHeader) (io.Writer, error) {
}
var b bytes.Buffer
if w.lastpart != nil {
- fmt.Fprintf(&b, "\r\n--%s\r\n", w.boundary)
+ fmt.Fprintf(&b, "\r\n--%s\r\n", w.Boundary())
} else {
- fmt.Fprintf(&b, "--%s\r\n", w.boundary)
+ fmt.Fprintf(&b, "--%s\r\n", w.Boundary())
}
// TODO(bradfitz): move this to textproto.MimeHeader.Write(w), have it sort
// and clean, like http.Header.Write(w) does.
@@ -157,7 +159,7 @@ func (w *Writer) Close() error {
}
w.lastpart = nil
}
- _, err := fmt.Fprintf(w.w, "\r\n--%s--\r\n", w.boundary)
+ _, err := fmt.Fprintf(w.w, "\r\n--%s--\r\n", w.Boundary())
return err
}