int init_buffer();
void deinit_buffer();
int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
-int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
void buffer_dump(FILE *o, struct buffer *b, int from, int to);
/*****************************************************************/
int ci_putchr(struct channel *chn, char c);
int ci_getline_nc(const struct channel *chn, char **blk1, size_t *len1, char **blk2, size_t *len2);
int ci_getblk_nc(const struct channel *chn, char **blk1, size_t *len1, char **blk2, size_t *len2);
+int ci_insert_line2(struct channel *c, int pos, const char *str, int len);
int co_inject(struct channel *chn, const char *msg, int len);
int co_getline(const struct channel *chn, char *str, int len);
int co_getblk(const struct channel *chn, char *blk, int len, int offset);
return delta;
}
-/*
- * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
- * argument informs about the length of string <str> so that we don't have to
- * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
- * is only opened for len+2 bytes but nothing is copied in. It may be useful in
- * some circumstances. The send limit is *not* adjusted. Same comments as above
- * for the valid use cases.
- *
- * The number of bytes added is returned on success. 0 is returned on failure.
- */
-int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
-{
- int delta;
-
- delta = len + 2;
-
- if (b_tail(b) + delta >= b_wrap(b))
- return 0; /* no space left */
-
- if (b_data(b) &&
- b_tail(b) + delta > b_head(b) &&
- b_head(b) >= b_tail(b))
- return 0; /* no space left before wrapping data */
-
- /* first, protect the end of the buffer */
- memmove(pos + delta, pos, b_tail(b) - pos);
-
- /* now, copy str over pos */
- if (len && str) {
- memcpy(pos, str, len);
- pos[len] = '\r';
- pos[len + 1] = '\n';
- }
-
- b_add(b, delta);
- return delta;
-}
-
/*
* Dumps part or all of a buffer.
*/
return 0;
}
+/* Inserts <str> followed by "\r\n" at position <pos> relative to channel <c>'s
+ * input head. The <len> argument informs about the length of string <str> so
+ * that we don't have to measure it. <str> must be a valid pointer and must not
+ * include the trailing "\r\n".
+ *
+ * The number of bytes added is returned on success. 0 is returned on failure.
+ */
+int ci_insert_line2(struct channel *c, int pos, const char *str, int len)
+{
+ struct buffer *b = c->buf;
+ char *dst = c_ptr(c, pos);
+ int delta;
+
+ delta = len + 2;
+
+ if (b_tail(b) + delta >= b_wrap(b))
+ return 0; /* no space left */
+
+ if (b_data(b) &&
+ b_tail(b) + delta > b_head(b) &&
+ b_head(b) >= b_tail(b))
+ return 0; /* no space left before wrapping data */
+
+ /* first, protect the end of the buffer */
+ memmove(dst + delta, dst, b_tail(b) - dst);
+
+ /* now, copy str over dst */
+ memcpy(dst, str, len);
+ dst[len] = '\r';
+ dst[len + 1] = '\n';
+
+ b_add(b, delta);
+ return delta;
+}
+
/*
* Local variables:
* c-indent-level: 8
/*
* Adds a header and its CRLF at the tail of the message's buffer, just before
- * the last CRLF. Text length is measured first, so it cannot be NULL.
+ * the last CRLF.
* The header is also automatically added to the index <hdr_idx>, and the end
* of headers is automatically adjusted. The number of bytes added is returned
* on success, otherwise <0 is returned indicating an error.
*/
-int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
+static inline int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
{
- int bytes, len;
-
- len = strlen(text);
- bytes = buffer_insert_line2(msg->chn->buf, ci_head(msg->chn) + msg->eoh, text, len);
- if (!bytes)
- return -1;
- http_msg_move_end(msg, bytes);
- return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
+ return http_header_add_tail2(msg, hdr_idx, text, strlen(text));
}
/*
* Adds a header and its CRLF at the tail of the message's buffer, just before
- * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
- * the buffer is only opened and the space reserved, but nothing is copied.
+ * the last CRLF. <len> bytes are copied, not counting the CRLF.
* The header is also automatically added to the index <hdr_idx>, and the end
* of headers is automatically adjusted. The number of bytes added is returned
* on success, otherwise <0 is returned indicating an error.
{
int bytes;
- bytes = buffer_insert_line2(msg->chn->buf, ci_head(msg->chn) + msg->eoh, text, len);
+ bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
if (!bytes)
return -1;
http_msg_move_end(msg, bytes);