| |
| |
| |
|
|
| package multipart |
|
|
| import ( |
| "bytes" |
| "crypto/rand" |
| "errors" |
| "fmt" |
| "io" |
| "maps" |
| "net/textproto" |
| "slices" |
| "strings" |
| ) |
|
|
| |
| type Writer struct { |
| w io.Writer |
| boundary string |
| lastpart *part |
| } |
|
|
| |
| |
| func NewWriter(w io.Writer) *Writer { |
| return &Writer{ |
| w: w, |
| boundary: randomBoundary(), |
| } |
| } |
|
|
| |
| func (w *Writer) Boundary() string { |
| return w.boundary |
| } |
|
|
| |
| |
| |
| |
| |
| |
| func (w *Writer) SetBoundary(boundary string) error { |
| if w.lastpart != nil { |
| return errors.New("mime: SetBoundary called after write") |
| } |
| |
| if len(boundary) < 1 || len(boundary) > 70 { |
| return errors.New("mime: invalid boundary length") |
| } |
| end := len(boundary) - 1 |
| for i, b := range boundary { |
| if 'A' <= b && b <= 'Z' || 'a' <= b && b <= 'z' || '0' <= b && b <= '9' { |
| continue |
| } |
| switch b { |
| case '\'', '(', ')', '+', '_', ',', '-', '.', '/', ':', '=', '?': |
| continue |
| case ' ': |
| if i != end { |
| continue |
| } |
| } |
| return errors.New("mime: invalid boundary character") |
| } |
| w.boundary = boundary |
| return nil |
| } |
|
|
| |
| |
| func (w *Writer) FormDataContentType() string { |
| b := w.boundary |
| |
| |
| if strings.ContainsAny(b, `()<>@,;:\"/[]?= `) { |
| b = `"` + b + `"` |
| } |
| return "multipart/form-data; boundary=" + b |
| } |
|
|
| func randomBoundary() string { |
| var buf [30]byte |
| _, err := io.ReadFull(rand.Reader, buf[:]) |
| if err != nil { |
| panic(err) |
| } |
| return fmt.Sprintf("%x", buf[:]) |
| } |
|
|
| |
| |
| |
| |
| func (w *Writer) CreatePart(header textproto.MIMEHeader) (io.Writer, error) { |
| if w.lastpart != nil { |
| if err := w.lastpart.close(); err != nil { |
| return nil, err |
| } |
| } |
| var b bytes.Buffer |
| if w.lastpart != nil { |
| fmt.Fprintf(&b, "\r\n--%s\r\n", w.boundary) |
| } else { |
| fmt.Fprintf(&b, "--%s\r\n", w.boundary) |
| } |
|
|
| for _, k := range slices.Sorted(maps.Keys(header)) { |
| for _, v := range header[k] { |
| fmt.Fprintf(&b, "%s: %s\r\n", k, v) |
| } |
| } |
| fmt.Fprintf(&b, "\r\n") |
| _, err := io.Copy(w.w, &b) |
| if err != nil { |
| return nil, err |
| } |
| p := &part{ |
| mw: w, |
| } |
| w.lastpart = p |
| return p, nil |
| } |
|
|
| var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"", "\r", "%0D", "\n", "%0A") |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func escapeQuotes(s string) string { |
| return quoteEscaper.Replace(s) |
| } |
|
|
| |
| |
| func (w *Writer) CreateFormFile(fieldname, filename string) (io.Writer, error) { |
| h := make(textproto.MIMEHeader) |
| h.Set("Content-Disposition", FileContentDisposition(fieldname, filename)) |
| h.Set("Content-Type", "application/octet-stream") |
| return w.CreatePart(h) |
| } |
|
|
| |
| |
| func (w *Writer) CreateFormField(fieldname string) (io.Writer, error) { |
| h := make(textproto.MIMEHeader) |
| h.Set("Content-Disposition", |
| fmt.Sprintf(`form-data; name="%s"`, escapeQuotes(fieldname))) |
| return w.CreatePart(h) |
| } |
|
|
| |
| |
| func FileContentDisposition(fieldname, filename string) string { |
| return fmt.Sprintf(`form-data; name="%s"; filename="%s"`, |
| escapeQuotes(fieldname), escapeQuotes(filename)) |
| } |
|
|
| |
| func (w *Writer) WriteField(fieldname, value string) error { |
| p, err := w.CreateFormField(fieldname) |
| if err != nil { |
| return err |
| } |
| _, err = p.Write([]byte(value)) |
| return err |
| } |
|
|
| |
| |
| func (w *Writer) Close() error { |
| if w.lastpart != nil { |
| if err := w.lastpart.close(); err != nil { |
| return err |
| } |
| w.lastpart = nil |
| } |
| _, err := fmt.Fprintf(w.w, "\r\n--%s--\r\n", w.boundary) |
| return err |
| } |
|
|
| type part struct { |
| mw *Writer |
| closed bool |
| we error |
| } |
|
|
| func (p *part) close() error { |
| p.closed = true |
| return p.we |
| } |
|
|
| func (p *part) Write(d []byte) (n int, err error) { |
| if p.closed { |
| return 0, errors.New("multipart: can't write to finished part") |
| } |
| n, err = p.mw.w.Write(d) |
| if err != nil { |
| p.we = err |
| } |
| return |
| } |
|
|