MCPcopy
hub / github.com/valyala/fasthttp / WriteMultipartForm

Function WriteMultipartForm

http.go:1189–1236  ·  view source on GitHub ↗

WriteMultipartForm writes the given multipart form f with the given boundary to w.

(w io.Writer, f *multipart.Form, boundary string)

Source from the content-addressed store, hash-verified

1187// WriteMultipartForm writes the given multipart form f with the given
1188// boundary to w.
1189func WriteMultipartForm(w io.Writer, f *multipart.Form, boundary string) error {
1190 // Do not care about memory allocations here, since multipart
1191 // form processing is slow.
1192 if boundary == "" {
1193 return errors.New("form boundary cannot be empty")
1194 }
1195
1196 mw := multipart.NewWriter(w)
1197 if err := mw.SetBoundary(boundary); err != nil {
1198 return fmt.Errorf("cannot use form boundary %q: %w", boundary, err)
1199 }
1200
1201 // marshal values
1202 for k, vv := range f.Value {
1203 for _, v := range vv {
1204 if err := mw.WriteField(k, v); err != nil {
1205 return fmt.Errorf("cannot write form field %q value %q: %w", k, v, err)
1206 }
1207 }
1208 }
1209
1210 // marshal files
1211 for k, fvv := range f.File {
1212 for _, fv := range fvv {
1213 vw, err := mw.CreatePart(fv.Header)
1214 if err != nil {
1215 return fmt.Errorf("cannot create form file %q (%q): %w", k, fv.Filename, err)
1216 }
1217 fh, err := fv.Open()
1218 if err != nil {
1219 return fmt.Errorf("cannot open form file %q (%q): %w", k, fv.Filename, err)
1220 }
1221 if _, err = copyZeroAlloc(vw, fh); err != nil {
1222 _ = fh.Close()
1223 return fmt.Errorf("error when copying form file %q (%q): %w", k, fv.Filename, err)
1224 }
1225 if err = fh.Close(); err != nil {
1226 return fmt.Errorf("cannot close form file %q (%q): %w", k, fv.Filename, err)
1227 }
1228 }
1229 }
1230
1231 if err := mw.Close(); err != nil {
1232 return fmt.Errorf("error when closing multipart form writer: %w", err)
1233 }
1234
1235 return nil
1236}
1237
1238func readMultipartForm(r io.Reader, boundary string, size, maxInMemoryFileSize int) (*multipart.Form, error) {
1239 // Do not care about memory allocations here, since they are tiny

Callers 3

BodyWriteToMethod · 0.85
marshalMultipartFormFunction · 0.85
TestWriteMultipartFormFunction · 0.85

Calls 4

CloseMethod · 0.95
copyZeroAllocFunction · 0.85
OpenMethod · 0.80
CloseMethod · 0.65

Tested by 1

TestWriteMultipartFormFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…