blob: 3bf5c50428561dbdd69c22c2b74025f08cf8e72c [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreauc7e42382012-08-24 19:22:53 +02002 * Channel management functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreaua27dc192014-11-27 22:10:04 +01004 * Copyright 2000-2014 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +020013#include <ctype.h>
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010014#include <stdarg.h>
15#include <stdio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <string.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020017
18#include <common/config.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020019#include <common/buffer.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreau9b28e032012-10-12 23:49:43 +020021#include <proto/channel.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020022
Willy Tarreau7341d942007-05-13 19:56:02 +020023
Willy Tarreau8263d2b2012-08-28 00:06:31 +020024/* Schedule up to <bytes> more bytes to be forwarded via the channel without
25 * notifying the owner task. Any data pending in the buffer are scheduled to be
Willy Tarreau8bf242b2016-05-04 14:05:58 +020026 * sent as well, within the limit of the number of bytes to forward. This must
27 * be the only method to use to schedule bytes to be forwarded. If the requested
Willy Tarreau8263d2b2012-08-28 00:06:31 +020028 * number is too large, it is automatically adjusted. The number of bytes taken
29 * into account is returned. Directly touching ->to_forward will cause lockups
30 * when buf->o goes down to zero if nobody is ready to push the remaining data.
Willy Tarreau0bc34932011-03-28 16:25:58 +020031 */
Willy Tarreau55a69062012-10-26 00:21:52 +020032unsigned long long __channel_forward(struct channel *chn, unsigned long long bytes)
Willy Tarreau0bc34932011-03-28 16:25:58 +020033{
Willy Tarreau8bf242b2016-05-04 14:05:58 +020034 unsigned int budget;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010035 unsigned int forwarded;
Willy Tarreau0bc34932011-03-28 16:25:58 +020036
Willy Tarreau8bf242b2016-05-04 14:05:58 +020037 /* This is more of a safety measure as it's not supposed to happen in
38 * regular code paths.
Willy Tarreau0bc34932011-03-28 16:25:58 +020039 */
Willy Tarreau8bf242b2016-05-04 14:05:58 +020040 if (unlikely(chn->to_forward == CHN_INFINITE_FORWARD)) {
Willy Tarreaucd9e60d2018-06-19 07:33:28 +020041 c_adv(chn, ci_data(chn));
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010042 return bytes;
43 }
44
Willy Tarreau8bf242b2016-05-04 14:05:58 +020045 /* Bound the transferred size to a 32-bit count since all our values
46 * are 32-bit, and we don't want to reach CHN_INFINITE_FORWARD.
47 */
48 budget = MIN(bytes, CHN_INFINITE_FORWARD - 1);
Willy Tarreau0bc34932011-03-28 16:25:58 +020049
Willy Tarreau8bf242b2016-05-04 14:05:58 +020050 /* transfer as much as we can of buf->i */
Willy Tarreaucd9e60d2018-06-19 07:33:28 +020051 forwarded = MIN(ci_data(chn), budget);
Willy Tarreaubcbd3932018-06-06 07:13:22 +020052 c_adv(chn, forwarded);
Willy Tarreau8bf242b2016-05-04 14:05:58 +020053 budget -= forwarded;
Willy Tarreau0bc34932011-03-28 16:25:58 +020054
Willy Tarreau8bf242b2016-05-04 14:05:58 +020055 if (!budget)
56 return forwarded;
57
58 /* Now we must ensure chn->to_forward sats below CHN_INFINITE_FORWARD,
59 * which also implies it won't overflow. It's less operations in 64-bit.
60 */
61 bytes = (unsigned long long)chn->to_forward + budget;
62 if (bytes >= CHN_INFINITE_FORWARD)
63 bytes = CHN_INFINITE_FORWARD - 1;
64 budget = bytes - chn->to_forward;
65
66 chn->to_forward += budget;
67 forwarded += budget;
68 return forwarded;
Willy Tarreau0bc34932011-03-28 16:25:58 +020069}
Willy Tarreaubaaee002006-06-26 02:48:02 +020070
Willy Tarreau8263d2b2012-08-28 00:06:31 +020071/* writes <len> bytes from message <msg> to the channel's buffer. Returns -1 in
72 * case of success, -2 if the message is larger than the buffer size, or the
73 * number of bytes available otherwise. The send limit is automatically
74 * adjusted to the amount of data written. FIXME-20060521: handle unaligned
75 * data. Note: this function appends data to the buffer's output and possibly
76 * overwrites any pending input data which are assumed not to exist.
Willy Tarreaubaaee002006-06-26 02:48:02 +020077 */
Willy Tarreau06d80a92017-10-19 14:32:15 +020078int co_inject(struct channel *chn, const char *msg, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +020079{
80 int max;
81
Willy Tarreauaeac3192009-08-31 08:09:57 +020082 if (len == 0)
83 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020084
Willy Tarreaucd9e60d2018-06-19 07:33:28 +020085 if (len < 0 || len > c_size(chn)) {
Willy Tarreau078e2942009-08-18 07:19:39 +020086 /* we can't write this chunk and will never be able to, because
87 * it is larger than the buffer. This must be reported as an
88 * error. Then we return -2 so that writers that don't care can
89 * ignore it and go on, and others can check for this value.
90 */
91 return -2;
92 }
93
Willy Tarreaud5b343b2018-06-06 06:42:46 +020094 c_realign_if_empty(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +020095 max = b_contig_space(&chn->buf);
Willy Tarreaubaaee002006-06-26 02:48:02 +020096 if (len > max)
97 return max;
98
Willy Tarreaucd9e60d2018-06-19 07:33:28 +020099 memcpy(ci_tail(chn), msg, len);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200100 b_add(&chn->buf, len);
Olivier Houchardacd14032018-06-28 18:17:23 +0200101 c_adv(chn, len);
Willy Tarreau974ced62012-10-12 23:11:02 +0200102 chn->total += len;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200103 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104}
105
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200106/* Tries to copy character <c> into the channel's buffer after some length
Willy Tarreau974ced62012-10-12 23:11:02 +0200107 * controls. The chn->o and to_forward pointers are updated. If the channel
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200108 * input is closed, -2 is returned. If there is not enough room left in the
109 * buffer, -1 is returned. Otherwise the number of bytes copied is returned
110 * (1). Channel flag READ_PARTIAL is updated if some data can be transferred.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100111 */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200112int ci_putchr(struct channel *chn, char c)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100113{
Willy Tarreau974ced62012-10-12 23:11:02 +0200114 if (unlikely(channel_input_closed(chn)))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200115 return -2;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100116
Willy Tarreaubc18da12015-03-13 14:00:47 +0100117 if (!channel_may_recv(chn))
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200118 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100119
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200120 *ci_tail(chn) = c;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200121
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200122 b_add(&chn->buf, 1);
Willy Tarreau974ced62012-10-12 23:11:02 +0200123 chn->flags |= CF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200124
Willy Tarreau974ced62012-10-12 23:11:02 +0200125 if (chn->to_forward >= 1) {
126 if (chn->to_forward != CHN_INFINITE_FORWARD)
127 chn->to_forward--;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200128 c_adv(chn, 1);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200129 }
130
Willy Tarreau974ced62012-10-12 23:11:02 +0200131 chn->total++;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200132 return 1;
133}
134
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200135/* Tries to copy block <blk> at once into the channel's buffer after length
Willy Tarreau974ced62012-10-12 23:11:02 +0200136 * controls. The chn->o and to_forward pointers are updated. If the channel
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200137 * input is closed, -2 is returned. If the block is too large for this buffer,
138 * -3 is returned. If there is not enough room left in the buffer, -1 is
139 * returned. Otherwise the number of bytes copied is returned (0 being a valid
140 * number). Channel flag READ_PARTIAL is updated if some data can be
Willy Tarreaubc18da12015-03-13 14:00:47 +0100141 * transferred.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200142 */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200143int ci_putblk(struct channel *chn, const char *blk, int len)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200144{
145 int max;
146
Willy Tarreau974ced62012-10-12 23:11:02 +0200147 if (unlikely(channel_input_closed(chn)))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200148 return -2;
149
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100150 if (len < 0)
151 return -3;
152
Willy Tarreau3f5096d2015-01-14 20:21:43 +0100153 max = channel_recv_limit(chn);
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200154 if (unlikely(len > max - c_data(chn))) {
Willy Tarreau591fedc2010-08-10 15:28:21 +0200155 /* we can't write this chunk right now because the buffer is
156 * almost full or because the block is too large. Return the
157 * available space or -2 if impossible.
Willy Tarreau078e2942009-08-18 07:19:39 +0200158 */
Willy Tarreau591fedc2010-08-10 15:28:21 +0200159 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200160 return -3;
Willy Tarreau078e2942009-08-18 07:19:39 +0200161
Willy Tarreau74b08c92010-09-08 17:04:31 +0200162 return -1;
Willy Tarreau591fedc2010-08-10 15:28:21 +0200163 }
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100164
Willy Tarreau74b08c92010-09-08 17:04:31 +0200165 if (unlikely(len == 0))
166 return 0;
167
Willy Tarreau591fedc2010-08-10 15:28:21 +0200168 /* OK so the data fits in the buffer in one or two blocks */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200169 max = b_contig_space(&chn->buf);
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200170 memcpy(ci_tail(chn), blk, MIN(len, max));
Willy Tarreauaeac3192009-08-31 08:09:57 +0200171 if (len > max)
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200172 memcpy(c_orig(chn), blk + max, len - max);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100173
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200174 b_add(&chn->buf, len);
Willy Tarreau974ced62012-10-12 23:11:02 +0200175 chn->total += len;
176 if (chn->to_forward) {
Willy Tarreau31971e52009-09-20 12:07:52 +0200177 unsigned long fwd = len;
Willy Tarreau974ced62012-10-12 23:11:02 +0200178 if (chn->to_forward != CHN_INFINITE_FORWARD) {
179 if (fwd > chn->to_forward)
180 fwd = chn->to_forward;
181 chn->to_forward -= fwd;
Willy Tarreau31971e52009-09-20 12:07:52 +0200182 }
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200183 c_adv(chn, fwd);
Willy Tarreauaeac3192009-08-31 08:09:57 +0200184 }
185
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200186 /* notify that some data was read from the SI into the buffer */
Willy Tarreau974ced62012-10-12 23:11:02 +0200187 chn->flags |= CF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200188 return len;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100189}
190
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200191/* Gets one text line out of a channel's buffer from a stream interface.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200192 * Return values :
193 * >0 : number of bytes read. Includes the \n if present before len or end.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200194 * =0 : no '\n' before end found. <str> is left undefined.
195 * <0 : no more bytes readable because output is shut.
Willy Tarreau06d80a92017-10-19 14:32:15 +0200196 * The channel status is not changed. The caller must call co_skip() to
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200197 * update it. The '\n' is waited for as long as neither the buffer nor the
198 * output are full. If either of them is full, the string may be returned
199 * as is, without the '\n'.
200 */
Willy Tarreau41ab8682017-10-19 14:58:40 +0200201int co_getline(const struct channel *chn, char *str, int len)
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200202{
203 int ret, max;
204 char *p;
205
206 ret = 0;
207 max = len;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200208
209 /* closed or empty + imminent close = -1; empty = 0 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200210 if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) {
211 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200212 ret = -1;
213 goto out;
214 }
215
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200216 p = co_head(chn);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200217
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200218 if (max > co_data(chn)) {
219 max = co_data(chn);
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200220 str[max-1] = 0;
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200221 }
222 while (max) {
223 *str++ = *p;
224 ret++;
225 max--;
226
227 if (*p == '\n')
228 break;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200229 p = b_next(&chn->buf, p);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200230 }
Willy Tarreau82de2b62013-12-10 18:58:23 +0100231 if (ret > 0 && ret < len &&
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200232 (ret < co_data(chn) || channel_may_recv(chn)) &&
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200233 *(str-1) != '\n' &&
Willy Tarreau974ced62012-10-12 23:11:02 +0200234 !(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200235 ret = 0;
236 out:
237 if (max)
238 *str = 0;
239 return ret;
240}
241
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200242/* Gets one full block of data at once from a channel's buffer, optionally from
243 * a specific offset. Return values :
Willy Tarreau74b08c92010-09-08 17:04:31 +0200244 * >0 : number of bytes read, equal to requested size.
245 * =0 : not enough data available. <blk> is left undefined.
246 * <0 : no more bytes readable because output is shut.
Willy Tarreau06d80a92017-10-19 14:32:15 +0200247 * The channel status is not changed. The caller must call co_skip() to
Willy Tarreau74b08c92010-09-08 17:04:31 +0200248 * update it.
249 */
Willy Tarreau41ab8682017-10-19 14:58:40 +0200250int co_getblk(const struct channel *chn, char *blk, int len, int offset)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200251{
Willy Tarreau974ced62012-10-12 23:11:02 +0200252 if (chn->flags & CF_SHUTW)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200253 return -1;
254
Willy Tarreau90ed3832018-06-15 14:20:26 +0200255 if (len + offset > co_data(chn)) {
Willy Tarreau974ced62012-10-12 23:11:02 +0200256 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200257 return -1;
258 return 0;
259 }
260
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200261 return b_getblk(&chn->buf, blk, len, offset);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200262}
263
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100264/* Gets one or two blocks of data at once from a channel's output buffer.
265 * Return values :
266 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
267 * =0 : not enough data available. <blk*> are left undefined.
268 * <0 : no more bytes readable because output is shut.
Willy Tarreau06d80a92017-10-19 14:32:15 +0200269 * The channel status is not changed. The caller must call co_skip() to
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100270 * update it. Unused buffers are left in an undefined state.
271 */
Willy Tarreau55f3ce12018-07-18 11:49:27 +0200272int co_getblk_nc(const struct channel *chn, const char **blk1, size_t *len1, const char **blk2, size_t *len2)
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100273{
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200274 if (unlikely(co_data(chn) == 0)) {
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100275 if (chn->flags & CF_SHUTW)
276 return -1;
277 return 0;
278 }
279
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200280 return b_getblk_nc(&chn->buf, blk1, len1, blk2, len2, 0, co_data(chn));
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100281}
282
283/* Gets one text line out of a channel's output buffer from a stream interface.
284 * Return values :
285 * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2.
286 * =0 : not enough data available.
287 * <0 : no more bytes readable because output is shut.
288 * The '\n' is waited for as long as neither the buffer nor the output are
289 * full. If either of them is full, the string may be returned as is, without
290 * the '\n'. Unused buffers are left in an undefined state.
291 */
Willy Tarreau41ab8682017-10-19 14:58:40 +0200292int co_getline_nc(const struct channel *chn,
Willy Tarreau55f3ce12018-07-18 11:49:27 +0200293 const char **blk1, size_t *len1,
294 const char **blk2, size_t *len2)
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100295{
296 int retcode;
297 int l;
298
Willy Tarreau06d80a92017-10-19 14:32:15 +0200299 retcode = co_getblk_nc(chn, blk1, len1, blk2, len2);
Tim Duesterhus45be38c2018-04-24 19:20:43 +0200300 if (unlikely(retcode <= 0))
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100301 return retcode;
302
303 for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++);
304 if (l < *len1 && (*blk1)[l] == '\n') {
305 *len1 = l + 1;
306 return 1;
307 }
308
309 if (retcode >= 2) {
310 for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++);
311 if (l < *len2 && (*blk2)[l] == '\n') {
312 *len2 = l + 1;
313 return 2;
314 }
315 }
316
317 if (chn->flags & CF_SHUTW) {
318 /* If we have found no LF and the buffer is shut, then
319 * the resulting string is made of the concatenation of
320 * the pending blocks (1 or 2).
321 */
322 return retcode;
323 }
324
325 /* No LF yet and not shut yet */
326 return 0;
327}
328
329/* Gets one full block of data at once from a channel's input buffer.
330 * This function can return the data slitted in one or two blocks.
331 * Return values :
332 * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2.
333 * =0 : not enough data available.
334 * <0 : no more bytes readable because input is shut.
335 */
Willy Tarreau41ab8682017-10-19 14:58:40 +0200336int ci_getblk_nc(const struct channel *chn,
Willy Tarreau55f3ce12018-07-18 11:49:27 +0200337 char **blk1, size_t *len1,
338 char **blk2, size_t *len2)
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100339{
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200340 if (unlikely(ci_data(chn) == 0)) {
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100341 if (chn->flags & CF_SHUTR)
342 return -1;
343 return 0;
344 }
345
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200346 if (unlikely(ci_head(chn) + ci_data(chn) > c_wrap(chn))) {
347 *blk1 = ci_head(chn);
348 *len1 = c_wrap(chn) - ci_head(chn);
349 *blk2 = c_orig(chn);
350 *len2 = ci_data(chn) - *len1;
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100351 return 2;
352 }
353
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200354 *blk1 = ci_head(chn);
355 *len1 = ci_data(chn);
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100356 return 1;
357}
358
359/* Gets one text line out of a channel's input buffer from a stream interface.
360 * Return values :
361 * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2.
362 * =0 : not enough data available.
363 * <0 : no more bytes readable because output is shut.
364 * The '\n' is waited for as long as neither the buffer nor the input are
365 * full. If either of them is full, the string may be returned as is, without
366 * the '\n'. Unused buffers are left in an undefined state.
367 */
Willy Tarreau41ab8682017-10-19 14:58:40 +0200368int ci_getline_nc(const struct channel *chn,
Willy Tarreau55f3ce12018-07-18 11:49:27 +0200369 char **blk1, size_t *len1,
370 char **blk2, size_t *len2)
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100371{
372 int retcode;
373 int l;
374
Willy Tarreau06d80a92017-10-19 14:32:15 +0200375 retcode = ci_getblk_nc(chn, blk1, len1, blk2, len2);
Thierry FOURNIER / OZON.IO500d11e2016-11-12 17:39:58 +0100376 if (unlikely(retcode <= 0))
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100377 return retcode;
378
379 for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++);
380 if (l < *len1 && (*blk1)[l] == '\n') {
381 *len1 = l + 1;
382 return 1;
383 }
384
385 if (retcode >= 2) {
386 for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++);
387 if (l < *len2 && (*blk2)[l] == '\n') {
388 *len2 = l + 1;
389 return 2;
390 }
391 }
392
393 if (chn->flags & CF_SHUTW) {
394 /* If we have found no LF and the buffer is shut, then
395 * the resulting string is made of the concatenation of
396 * the pending blocks (1 or 2).
397 */
398 return retcode;
399 }
400
401 /* No LF yet and not shut yet */
402 return 0;
403}
404
Willy Tarreau4d893d42018-07-12 15:43:32 +0200405/* Inserts <str> followed by "\r\n" at position <pos> relative to channel <c>'s
406 * input head. The <len> argument informs about the length of string <str> so
407 * that we don't have to measure it. <str> must be a valid pointer and must not
408 * include the trailing "\r\n".
409 *
410 * The number of bytes added is returned on success. 0 is returned on failure.
411 */
412int ci_insert_line2(struct channel *c, int pos, const char *str, int len)
413{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200414 struct buffer *b = &c->buf;
Willy Tarreau4d893d42018-07-12 15:43:32 +0200415 char *dst = c_ptr(c, pos);
416 int delta;
417
418 delta = len + 2;
419
Olivier Houchard363c7452018-09-26 15:09:58 +0200420 if (__b_tail(b) + delta >= b_wrap(b))
Willy Tarreau4d893d42018-07-12 15:43:32 +0200421 return 0; /* no space left */
422
423 if (b_data(b) &&
424 b_tail(b) + delta > b_head(b) &&
425 b_head(b) >= b_tail(b))
426 return 0; /* no space left before wrapping data */
427
428 /* first, protect the end of the buffer */
429 memmove(dst + delta, dst, b_tail(b) - dst);
430
431 /* now, copy str over dst */
432 memcpy(dst, str, len);
433 dst[len] = '\r';
434 dst[len + 1] = '\n';
435
436 b_add(b, delta);
437 return delta;
438}
439
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200440/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200441 * Local variables:
442 * c-indent-level: 8
443 * c-basic-offset: 8
444 * End:
445 */