goto end;
}
+/* Returns 1 if <count> bytes of the HTX message <htx> can be sent by swapping
+ * the caller's buffer <buf> with the mux's output buffer in h1_make_data(), 0
+ * otherwise. It is only possible if the output buffer is empty and if the HTX
+ * message contains exactly one DATA block of <count> bytes. The whole block is
+ * then emitted at once, it cannot be split. So, for a chunked message, it must
+ * fit in the chunk being sent or in the new one, whose size is announced from
+ * the <kop> hint when set. <kop> is a minimal guarantee, it may be lower than
+ * the block size. In this case, or when the end of message is reached before
+ * the end of the announced chunk, the copy path must be used instead: it
+ * splits the block or reports the error before anything is emitted. Here,
+ * there is no way back once the buffers were swapped.
+ */
+static inline int h1_zero_copy_data_allowed(const struct h1s *h1s, const struct h1m *h1m,
+ const struct htx *htx, const struct buffer *buf,
+ size_t count)
+{
+ const struct h1c *h1c = h1s->h1c;
+ const struct htx_blk *blk;
+ uint64_t chksz;
+
+ if ((!(h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_REQ)) ||
+ ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)))
+ return 0;
+
+ if (b_data(&h1c->obuf) || b_size(&h1c->obuf) != b_size(buf))
+ return 0;
+
+ /* the HTX message must contain exactly one DATA block of <count> bytes */
+ if (htx_nbblks(htx) != 1)
+ return 0;
+ blk = htx_get_head_blk(htx);
+ if (htx_get_blk_type(blk) != HTX_BLK_DATA || htx_get_blksz(blk) != count)
+ return 0;
+
+ if (!(h1m->flags & H1_MF_CHNK))
+ return 1;
+
+ if (h1m->state != H1_MSG_CHUNK_CRLF && h1m->state != H1_MSG_CHUNK_SIZE) {
+ /* the end of the current chunk only */
+ return (count == h1m->curr_len);
+ }
+
+ /* A new chunk is started */
+ if (h1m->curr_len)
+ return 0;
+ chksz = (h1s->sd->kop ? h1s->sd->kop : count);
+ if (count > chksz)
+ return 0;
+ if ((blk->flags & HTX_BLK_FL_EOM) && count != chksz)
+ return 0;
+ return 1;
+}
+
/* Try to send the message payload from the HTX message <htx> for the stream
* <h1s>. In this case, we are not in TUNNEL mode. It returns the number of
* bytes consumed or zero if nothing was done or if an error occurred. No more
* end-to-end. This is the situation that happens all the time with
* large files.
*/
- if (((!(h1m->flags & H1_MF_RESP) && !(h1s->flags & H1S_F_BODYLESS_REQ)) ||
- ((h1m->flags & H1_MF_RESP) && !(h1s->flags & H1S_F_BODYLESS_RESP))) &&
- !b_data(&h1c->obuf) &&
- (!(h1m->flags & H1_MF_CHNK) || ((h1m->flags & H1_MF_CHNK) && (!h1m->curr_len || count == h1m->curr_len))) &&
- htx_nbblks(htx) == 1 &&
- htx_get_blk_type(blk) == HTX_BLK_DATA &&
- htx_get_blk_value(htx, blk).len == count &&
- b_size(&h1c->obuf) == b_size(buf)) {
+ if (h1_zero_copy_data_allowed(h1s, h1m, htx, buf, count)) {
void *old_area;
int eom = (blk->flags & HTX_BLK_FL_EOM);
/* If is a new chunk, prepend the chunk size */
if (h1m->state == H1_MSG_CHUNK_CRLF || h1m->state == H1_MSG_CHUNK_SIZE) {
- if (h1m->curr_len) {
- COUNT("chunk bigger than announced (0-copy)");
- TRACE_ERROR("chunk bigger than announced",
- H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
- goto error;
- }
h1m->curr_len = (h1s->sd->kop ? h1s->sd->kop : count);
h1s_consume_kop(h1s, h1m->curr_len);
h1m->state = H1_MSG_DATA;
}
- if (count > h1m->curr_len) {
- COUNT("chunk bigger than announced (0-copy)");
- TRACE_ERROR("chunk bigger than announced",
- H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
- goto error;
- }
+ /* h1_zero_copy_data_allowed() ensures the block fits in the chunk */
h1m->curr_len -= count;
/* It is the end of the chunk, append the CRLF */
/* It is the end of the message, add the last chunk with the extra CRLF */
if (eom) {
- if (h1m->curr_len) {
- COUNT("chunk smaller than announced (0-copy)");
- TRACE_ERROR("chunk smaller than announced",
- H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
- goto error;
- }
/* Emit the last chunk too at the buffer's end */
b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
h1m->state = H1_MSG_DONE;