FAQ
Reviewers: golang-dev_googlegroups.com,

Message:
Hello golang-dev@googlegroups.com,

I'd like you to review this change to
https://go.googlecode.com/hg/


Description:
mime/multipart: allow setting the Writer boundary

Fixes issue 4490

Please review this at https://codereview.appspot.com/6924044/

Affected files:
M src/pkg/mime/multipart/writer.go
M src/pkg/mime/multipart/writer_test.go


Index: src/pkg/mime/multipart/writer.go
===================================================================
--- a/src/pkg/mime/multipart/writer.go
+++ b/src/pkg/mime/multipart/writer.go
@@ -30,11 +30,40 @@
}
}

-// Boundary returns the Writer's randomly selected boundary string.
+// Boundary returns the Writer's boundary.
func (w *Writer) Boundary() string {
return w.boundary
}

+// SetBoundary overrides the Writer's default randomly-generated
+// boundary separator with an explicit value.
+//
+// SetBoundary must be called before any parts are created, may only
+// contain certain ASCII characters, and must be 1-69 bytes long.
+func (w *Writer) SetBoundary(boundary string) error {
+ if w.lastpart != nil {
+ return errors.New("mime: SetBoundary called after write")
+ }
+ // rfc2046#section-5.1.1
+ if len(boundary) < 1 || len(boundary) > 69 {
+ return errors.New("mime: invalid boundary length")
+ }
+ for _, b := range boundary {
+ if b < 128 {
+ if b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z' || b >= '0' && b <= '9'
{
+ continue
+ }
+ switch byte(b) {
+ case '\'', '(', ')', '+', '_', ',', '-', '.', '/', ':', '=', '?':
+ continue
+ }
+ }
+ return errors.New("mime: invalid boundary character")
+ }
+ w.boundary = boundary
+ return nil
+}
+
// FormDataContentType returns the Content-Type for an HTTP
// multipart/form-data with this Writer's Boundary.
func (w *Writer) FormDataContentType() string {
Index: src/pkg/mime/multipart/writer_test.go
===================================================================
--- a/src/pkg/mime/multipart/writer_test.go
+++ b/src/pkg/mime/multipart/writer_test.go
@@ -7,6 +7,7 @@
import (
"bytes"
"io/ioutil"
+ "strings"
"testing"
)

@@ -76,3 +77,37 @@
t.Fatalf("expected end of parts; got %v, %v", part, err)
}
}
+
+func TestWriterSetBoundary(t *testing.T) {
+ var b bytes.Buffer
+ w := NewWriter(&b)
+ tests := []struct {
+ b string
+ ok bool
+ }{
+ {"abc", true},
+ {"", false},
+ {"ungültig", false},
+ {"!", false},
+ {strings.Repeat("x", 69), true},
+ {strings.Repeat("x", 70), false},
+ {"bad!ascii!", false},
+ {"my-separator", true},
+ }
+ for i, tt := range tests {
+ err := w.SetBoundary(tt.b)
+ got := err == nil
+ if got != tt.ok {
+ t.Errorf("%d. boundary %q = %v (%v); want %v", i, tt.b, got, err, tt.ok)
+ } else if tt.ok {
+ got := w.Boundary()
+ if got != tt.b {
+ t.Errorf("boundary = %q; want %q", got, tt.b)
+ }
+ }
+ }
+ w.Close()
+ if got := b.String(); !strings.Contains(got, "\r\n--my-separator--\r\n") {
+ t.Errorf("expected my-separator in output. got: %q", got)
+ }
+}

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedDec 10, '12 at 9:07p
activeDec 10, '12 at 9:07p
posts1
users1
websitegolang.org

1 user in discussion

Bradfitz: 1 post

People

Translate

site design / logo © 2022 Grokbase