(subtype string, callback func(*multipart.Writer) error)
| 1633 | } |
| 1634 | |
| 1635 | func (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 | |
| 1664 | func (h *handler) flush() { |
| 1665 | h.response.(http.Flusher).Flush() |
no test coverage detected