MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / writeMultipart

Method writeMultipart

rest/handler.go:1635–1662  ·  view source on GitHub ↗
(subtype string, callback func(*multipart.Writer) error)

Source from the content-addressed store, hash-verified

1633}
1634
1635func (h *handler) writeMultipart(subtype string, callback func(*multipart.Writer) error) error {
1636 if !h.requestAccepts("multipart/") {
1637 return base.HTTPErrorf(http.StatusNotAcceptable, "Response is multipart")
1638 }
1639
1640 // Get the output stream. Due to a CouchDB bug, if we're sending to it we need to buffer the
1641 // output in memory so we can trim the final bytes.
1642 var output io.Writer
1643 var buffer bytes.Buffer
1644 if h.userAgentIs("CouchDB") {
1645 output = &buffer
1646 } else {
1647 output = h.response
1648 }
1649
1650 writer := multipart.NewWriter(output)
1651 h.setHeader("Content-Type",
1652 fmt.Sprintf("multipart/%s; boundary=%q", subtype, writer.Boundary()))
1653
1654 err := callback(writer)
1655 _ = writer.Close()
1656
1657 if err == nil && output == &buffer {
1658 // Trim trailing newline; CouchDB is allergic to it:
1659 _, err = h.response.Write(bytes.TrimRight(buffer.Bytes(), "\r\n"))
1660 }
1661 return err
1662}
1663
1664func (h *handler) flush() {
1665 h.response.(http.Flusher).Flush()

Callers 2

handleGetDocMethod · 0.95
handleBulkGetMethod · 0.95

Calls 6

requestAcceptsMethod · 0.95
userAgentIsMethod · 0.95
setHeaderMethod · 0.95
HTTPErrorfFunction · 0.92
CloseMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected