Source File
writer.go
Belonging Package
mime/multipart
package multipart
import (
)
if len() < 1 || len() > 70 {
return errors.New("mime: invalid boundary length")
}
:= len() - 1
for , := range {
if 'A' <= && <= 'Z' || 'a' <= && <= 'z' || '0' <= && <= '9' {
continue
}
switch {
case '\'', '(', ')', '+', '_', ',', '-', '.', '/', ':', '=', '?':
continue
case ' ':
if != {
continue
}
}
return errors.New("mime: invalid boundary character")
}
.boundary =
return nil
}
func ( *Writer) ( textproto.MIMEHeader) (io.Writer, error) {
if .lastpart != nil {
if := .lastpart.close(); != nil {
return nil,
}
}
var bytes.Buffer
if .lastpart != nil {
fmt.Fprintf(&, "\r\n--%s\r\n", .boundary)
} else {
fmt.Fprintf(&, "--%s\r\n", .boundary)
}
:= make([]string, 0, len())
for := range {
= append(, )
}
sort.Strings()
for , := range {
for , := range [] {
fmt.Fprintf(&, "%s: %s\r\n", , )
}
}
fmt.Fprintf(&, "\r\n")
, := io.Copy(.w, &)
if != nil {
return nil,
}
:= &part{
mw: ,
}
.lastpart =
return , nil
}
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
func ( string) string {
return quoteEscaper.Replace()
}
func ( *Writer) (, string) (io.Writer, error) {
:= make(textproto.MIMEHeader)
.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
escapeQuotes(), escapeQuotes()))
.Set("Content-Type", "application/octet-stream")
return .CreatePart()
}
func ( *Writer) ( string) (io.Writer, error) {
:= make(textproto.MIMEHeader)
.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"`, escapeQuotes()))
return .CreatePart()
}
func ( *Writer) () error {
if .lastpart != nil {
if := .lastpart.close(); != nil {
return
}
.lastpart = nil
}
, := fmt.Fprintf(.w, "\r\n--%s--\r\n", .boundary)
return
}
type part struct {
mw *Writer
closed bool
we error // last error that occurred writing
}
func ( *part) () error {
.closed = true
return .we
}
func ( *part) ( []byte) ( int, error) {
if .closed {
return 0, errors.New("multipart: can't write to finished part")
}
, = .mw.w.Write()
if != nil {
.we =
}
return
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |