blob: ba8ab638a1552ed488613615e46faab3a4972e8d [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
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020018#include <haproxy/api.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020019#include <haproxy/buf.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020020#include <haproxy/channel.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020021
Willy Tarreau7341d942007-05-13 19:56:02 +020022
Willy Tarreau8263d2b2012-08-28 00:06:31 +020023/* Schedule up to <bytes> more bytes to be forwarded via the channel without
24 * notifying the owner task. Any data pending in the buffer are scheduled to be
Willy Tarreau8bf242b2016-05-04 14:05:58 +020025 * sent as well, within the limit of the number of bytes to forward. This must
26 * be the only method to use to schedule bytes to be forwarded. If the requested
Willy Tarreau8263d2b2012-08-28 00:06:31 +020027 * number is too large, it is automatically adjusted. The number of bytes taken
28 * into account is returned. Directly touching ->to_forward will cause lockups
29 * when buf->o goes down to zero if nobody is ready to push the remaining data.
Willy Tarreau0bc34932011-03-28 16:25:58 +020030 */
Willy Tarreau55a69062012-10-26 00:21:52 +020031unsigned long long __channel_forward(struct channel *chn, unsigned long long bytes)
Willy Tarreau0bc34932011-03-28 16:25:58 +020032{
Willy Tarreau8bf242b2016-05-04 14:05:58 +020033 unsigned int budget;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010034 unsigned int forwarded;
Willy Tarreau0bc34932011-03-28 16:25:58 +020035
Willy Tarreau8bf242b2016-05-04 14:05:58 +020036 /* This is more of a safety measure as it's not supposed to happen in
37 * regular code paths.
Willy Tarreau0bc34932011-03-28 16:25:58 +020038 */
Willy Tarreau8bf242b2016-05-04 14:05:58 +020039 if (unlikely(chn->to_forward == CHN_INFINITE_FORWARD)) {
Willy Tarreaucd9e60d2018-06-19 07:33:28 +020040 c_adv(chn, ci_data(chn));
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010041 return bytes;
42 }
43
Willy Tarreau8bf242b2016-05-04 14:05:58 +020044 /* Bound the transferred size to a 32-bit count since all our values
45 * are 32-bit, and we don't want to reach CHN_INFINITE_FORWARD.
46 */
47 budget = MIN(bytes, CHN_INFINITE_FORWARD - 1);
Willy Tarreau0bc34932011-03-28 16:25:58 +020048
Willy Tarreau8bf242b2016-05-04 14:05:58 +020049 /* transfer as much as we can of buf->i */
Willy Tarreaucd9e60d2018-06-19 07:33:28 +020050 forwarded = MIN(ci_data(chn), budget);
Willy Tarreaubcbd3932018-06-06 07:13:22 +020051 c_adv(chn, forwarded);
Willy Tarreau8bf242b2016-05-04 14:05:58 +020052 budget -= forwarded;
Willy Tarreau0bc34932011-03-28 16:25:58 +020053
Willy Tarreau8bf242b2016-05-04 14:05:58 +020054 if (!budget)
55 return forwarded;
56
57 /* Now we must ensure chn->to_forward sats below CHN_INFINITE_FORWARD,
58 * which also implies it won't overflow. It's less operations in 64-bit.
59 */
60 bytes = (unsigned long long)chn->to_forward + budget;
61 if (bytes >= CHN_INFINITE_FORWARD)
62 bytes = CHN_INFINITE_FORWARD - 1;
63 budget = bytes - chn->to_forward;
64
65 chn->to_forward += budget;
66 forwarded += budget;
67 return forwarded;
Willy Tarreau0bc34932011-03-28 16:25:58 +020068}
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Willy Tarreau8263d2b2012-08-28 00:06:31 +020070/* writes <len> bytes from message <msg> to the channel's buffer. Returns -1 in
71 * case of success, -2 if the message is larger than the buffer size, or the
72 * number of bytes available otherwise. The send limit is automatically
73 * adjusted to the amount of data written. FIXME-20060521: handle unaligned
74 * data. Note: this function appends data to the buffer's output and possibly
75 * overwrites any pending input data which are assumed not to exist.
Willy Tarreaubaaee002006-06-26 02:48:02 +020076 */
Willy Tarreau06d80a92017-10-19 14:32:15 +020077int co_inject(struct channel *chn, const char *msg, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +020078{
79 int max;
80
Willy Tarreauaeac3192009-08-31 08:09:57 +020081 if (len == 0)
82 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020083
Willy Tarreaucd9e60d2018-06-19 07:33:28 +020084 if (len < 0 || len > c_size(chn)) {
Willy Tarreau078e2942009-08-18 07:19:39 +020085 /* we can't write this chunk and will never be able to, because
86 * it is larger than the buffer. This must be reported as an
87 * error. Then we return -2 so that writers that don't care can
88 * ignore it and go on, and others can check for this value.
89 */
90 return -2;
91 }
92
Willy Tarreaud5b343b2018-06-06 06:42:46 +020093 c_realign_if_empty(chn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +020094 max = b_contig_space(&chn->buf);
Willy Tarreaubaaee002006-06-26 02:48:02 +020095 if (len > max)
96 return max;
97
Christopher Faulet584348b2020-01-07 10:01:57 +010098 memcpy(co_tail(chn), msg, len);
Willy Tarreauc9fa0482018-07-10 17:43:27 +020099 b_add(&chn->buf, len);
Olivier Houchardacd14032018-06-28 18:17:23 +0200100 c_adv(chn, len);
Willy Tarreau974ced62012-10-12 23:11:02 +0200101 chn->total += len;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200102 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103}
104
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200105/* Tries to copy character <c> into the channel's buffer after some length
Willy Tarreau974ced62012-10-12 23:11:02 +0200106 * controls. The chn->o and to_forward pointers are updated. If the channel
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200107 * input is closed, -2 is returned. If there is not enough room left in the
108 * buffer, -1 is returned. Otherwise the number of bytes copied is returned
109 * (1). Channel flag READ_PARTIAL is updated if some data can be transferred.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100110 */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200111int ci_putchr(struct channel *chn, char c)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100112{
Willy Tarreau974ced62012-10-12 23:11:02 +0200113 if (unlikely(channel_input_closed(chn)))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200114 return -2;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100115
Willy Tarreaubc18da12015-03-13 14:00:47 +0100116 if (!channel_may_recv(chn))
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200117 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100118
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200119 *ci_tail(chn) = c;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200120
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200121 b_add(&chn->buf, 1);
Willy Tarreau974ced62012-10-12 23:11:02 +0200122 chn->flags |= CF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200123
Willy Tarreau974ced62012-10-12 23:11:02 +0200124 if (chn->to_forward >= 1) {
125 if (chn->to_forward != CHN_INFINITE_FORWARD)
126 chn->to_forward--;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200127 c_adv(chn, 1);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200128 }
129
Willy Tarreau974ced62012-10-12 23:11:02 +0200130 chn->total++;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200131 return 1;
132}
133
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200134/* Tries to copy block <blk> at once into the channel's buffer after length
Willy Tarreau974ced62012-10-12 23:11:02 +0200135 * controls. The chn->o and to_forward pointers are updated. If the channel
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200136 * input is closed, -2 is returned. If the block is too large for this buffer,
137 * -3 is returned. If there is not enough room left in the buffer, -1 is
138 * returned. Otherwise the number of bytes copied is returned (0 being a valid
139 * number). Channel flag READ_PARTIAL is updated if some data can be
Willy Tarreaubc18da12015-03-13 14:00:47 +0100140 * transferred.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200141 */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200142int ci_putblk(struct channel *chn, const char *blk, int len)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200143{
144 int max;
145
Willy Tarreau974ced62012-10-12 23:11:02 +0200146 if (unlikely(channel_input_closed(chn)))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200147 return -2;
148
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100149 if (len < 0)
150 return -3;
151
Willy Tarreau3f5096d2015-01-14 20:21:43 +0100152 max = channel_recv_limit(chn);
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200153 if (unlikely(len > max - c_data(chn))) {
Willy Tarreau591fedc2010-08-10 15:28:21 +0200154 /* we can't write this chunk right now because the buffer is
155 * almost full or because the block is too large. Return the
156 * available space or -2 if impossible.
Willy Tarreau078e2942009-08-18 07:19:39 +0200157 */
Willy Tarreau591fedc2010-08-10 15:28:21 +0200158 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200159 return -3;
Willy Tarreau078e2942009-08-18 07:19:39 +0200160
Willy Tarreau74b08c92010-09-08 17:04:31 +0200161 return -1;
Willy Tarreau591fedc2010-08-10 15:28:21 +0200162 }
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100163
Willy Tarreau74b08c92010-09-08 17:04:31 +0200164 if (unlikely(len == 0))
165 return 0;
166
Willy Tarreau591fedc2010-08-10 15:28:21 +0200167 /* OK so the data fits in the buffer in one or two blocks */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200168 max = b_contig_space(&chn->buf);
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200169 memcpy(ci_tail(chn), blk, MIN(len, max));
Willy Tarreauaeac3192009-08-31 08:09:57 +0200170 if (len > max)
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200171 memcpy(c_orig(chn), blk + max, len - max);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100172
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200173 b_add(&chn->buf, len);
Christopher Faulete6458292019-01-02 14:24:35 +0100174 channel_add_input(chn, len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200175 return len;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100176}
177
Emeric Brun6d756162020-10-05 14:35:21 +0200178/* Gets one text word out of a channel's buffer from a stream interface.
179 * Return values :
180 * >0 : number of bytes read. Includes the sep if present before len or end.
181 * =0 : no sep before end found. <str> is left undefined.
182 * <0 : no more bytes readable because output is shut.
183 * The channel status is not changed. The caller must call co_skip() to
184 * update it. The line separator is waited for as long as neither the buffer
185 * nor the output are full. If either of them is full, the string may be
186 * returned as is, without the line separator.
187 */
188int co_getword(const struct channel *chn, char *str, int len, char sep)
189{
190 int ret, max;
191 char *p;
192
193 ret = 0;
194 max = len;
195
196 /* closed or empty + imminent close = -1; empty = 0 */
197 if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) {
198 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
199 ret = -1;
200 goto out;
201 }
202
203 p = co_head(chn);
204
205 if (max > co_data(chn)) {
206 max = co_data(chn);
207 str[max-1] = 0;
208 }
209 while (max) {
210 *str++ = *p;
211 ret++;
212 max--;
213
214 if (*p == sep)
215 break;
216 p = b_next(&chn->buf, p);
217 }
218 if (ret > 0 && ret < len &&
219 (ret < co_data(chn) || channel_may_recv(chn)) &&
220 *(str-1) != sep &&
221 !(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
222 ret = 0;
223 out:
224 if (max)
225 *str = 0;
226 return ret;
227}
228
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200229/* Gets one text line out of a channel's buffer from a stream interface.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200230 * Return values :
231 * >0 : number of bytes read. Includes the \n if present before len or end.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200232 * =0 : no '\n' before end found. <str> is left undefined.
233 * <0 : no more bytes readable because output is shut.
Willy Tarreau06d80a92017-10-19 14:32:15 +0200234 * The channel status is not changed. The caller must call co_skip() to
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200235 * update it. The '\n' is waited for as long as neither the buffer nor the
236 * output are full. If either of them is full, the string may be returned
237 * as is, without the '\n'.
238 */
Willy Tarreau41ab8682017-10-19 14:58:40 +0200239int co_getline(const struct channel *chn, char *str, int len)
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200240{
241 int ret, max;
242 char *p;
243
244 ret = 0;
245 max = len;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200246
247 /* closed or empty + imminent close = -1; empty = 0 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200248 if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) {
249 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200250 ret = -1;
251 goto out;
252 }
253
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200254 p = co_head(chn);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200255
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200256 if (max > co_data(chn)) {
257 max = co_data(chn);
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200258 str[max-1] = 0;
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200259 }
260 while (max) {
261 *str++ = *p;
262 ret++;
263 max--;
264
265 if (*p == '\n')
266 break;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200267 p = b_next(&chn->buf, p);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200268 }
Willy Tarreau82de2b62013-12-10 18:58:23 +0100269 if (ret > 0 && ret < len &&
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200270 (ret < co_data(chn) || channel_may_recv(chn)) &&
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200271 *(str-1) != '\n' &&
Willy Tarreau974ced62012-10-12 23:11:02 +0200272 !(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200273 ret = 0;
274 out:
275 if (max)
276 *str = 0;
277 return ret;
278}
279
Emeric Brun6d756162020-10-05 14:35:21 +0200280/* Gets one char of data from a channel's buffer,
281 * Return values :
282 * 1 : number of bytes read, equal to requested size.
283 * =0 : not enough data available. <c> is left undefined.
284 * <0 : no more bytes readable because output is shut.
285 * The channel status is not changed. The caller must call co_skip() to
286 * update it.
287 */
288int co_getchar(const struct channel *chn, char *c)
289{
290 if (chn->flags & CF_SHUTW)
291 return -1;
292
293 if (unlikely(co_data(chn) == 0)) {
294 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
295 return -1;
296 return 0;
297 }
298
299 *c = *(co_head(chn));
300 return 1;
301}
302
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200303/* Gets one full block of data at once from a channel's buffer, optionally from
304 * a specific offset. Return values :
Willy Tarreau74b08c92010-09-08 17:04:31 +0200305 * >0 : number of bytes read, equal to requested size.
306 * =0 : not enough data available. <blk> is left undefined.
307 * <0 : no more bytes readable because output is shut.
Willy Tarreau06d80a92017-10-19 14:32:15 +0200308 * The channel status is not changed. The caller must call co_skip() to
Willy Tarreau74b08c92010-09-08 17:04:31 +0200309 * update it.
310 */
Willy Tarreau41ab8682017-10-19 14:58:40 +0200311int co_getblk(const struct channel *chn, char *blk, int len, int offset)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200312{
Willy Tarreau974ced62012-10-12 23:11:02 +0200313 if (chn->flags & CF_SHUTW)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200314 return -1;
315
Willy Tarreau90ed3832018-06-15 14:20:26 +0200316 if (len + offset > co_data(chn)) {
Willy Tarreau974ced62012-10-12 23:11:02 +0200317 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200318 return -1;
319 return 0;
320 }
321
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200322 return b_getblk(&chn->buf, blk, len, offset);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200323}
324
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100325/* Gets one or two blocks of data at once from a channel's output buffer.
326 * Return values :
327 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
328 * =0 : not enough data available. <blk*> are left undefined.
329 * <0 : no more bytes readable because output is shut.
Willy Tarreau06d80a92017-10-19 14:32:15 +0200330 * The channel status is not changed. The caller must call co_skip() to
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100331 * update it. Unused buffers are left in an undefined state.
332 */
Willy Tarreau55f3ce12018-07-18 11:49:27 +0200333int 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 +0100334{
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200335 if (unlikely(co_data(chn) == 0)) {
Christopher Fauletf706a792020-07-16 11:43:46 +0200336 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100337 return -1;
338 return 0;
339 }
340
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200341 return b_getblk_nc(&chn->buf, blk1, len1, blk2, len2, 0, co_data(chn));
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100342}
343
344/* Gets one text line out of a channel's output buffer from a stream interface.
345 * Return values :
346 * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2.
347 * =0 : not enough data available.
348 * <0 : no more bytes readable because output is shut.
349 * The '\n' is waited for as long as neither the buffer nor the output are
350 * full. If either of them is full, the string may be returned as is, without
351 * the '\n'. Unused buffers are left in an undefined state.
352 */
Willy Tarreau41ab8682017-10-19 14:58:40 +0200353int co_getline_nc(const struct channel *chn,
Willy Tarreau55f3ce12018-07-18 11:49:27 +0200354 const char **blk1, size_t *len1,
355 const char **blk2, size_t *len2)
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100356{
357 int retcode;
358 int l;
359
Willy Tarreau06d80a92017-10-19 14:32:15 +0200360 retcode = co_getblk_nc(chn, blk1, len1, blk2, len2);
Tim Duesterhus45be38c2018-04-24 19:20:43 +0200361 if (unlikely(retcode <= 0))
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100362 return retcode;
363
364 for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++);
365 if (l < *len1 && (*blk1)[l] == '\n') {
366 *len1 = l + 1;
367 return 1;
368 }
369
370 if (retcode >= 2) {
371 for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++);
372 if (l < *len2 && (*blk2)[l] == '\n') {
373 *len2 = l + 1;
374 return 2;
375 }
376 }
377
Christopher Fauletf706a792020-07-16 11:43:46 +0200378 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100379 /* If we have found no LF and the buffer is shut, then
380 * the resulting string is made of the concatenation of
381 * the pending blocks (1 or 2).
382 */
383 return retcode;
384 }
385
386 /* No LF yet and not shut yet */
387 return 0;
388}
389
390/* Gets one full block of data at once from a channel's input buffer.
391 * This function can return the data slitted in one or two blocks.
392 * Return values :
393 * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2.
394 * =0 : not enough data available.
395 * <0 : no more bytes readable because input is shut.
396 */
Willy Tarreau41ab8682017-10-19 14:58:40 +0200397int ci_getblk_nc(const struct channel *chn,
Willy Tarreau55f3ce12018-07-18 11:49:27 +0200398 char **blk1, size_t *len1,
399 char **blk2, size_t *len2)
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100400{
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200401 if (unlikely(ci_data(chn) == 0)) {
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100402 if (chn->flags & CF_SHUTR)
403 return -1;
404 return 0;
405 }
406
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200407 if (unlikely(ci_head(chn) + ci_data(chn) > c_wrap(chn))) {
408 *blk1 = ci_head(chn);
409 *len1 = c_wrap(chn) - ci_head(chn);
410 *blk2 = c_orig(chn);
411 *len2 = ci_data(chn) - *len1;
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100412 return 2;
413 }
414
Willy Tarreaucd9e60d2018-06-19 07:33:28 +0200415 *blk1 = ci_head(chn);
416 *len1 = ci_data(chn);
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100417 return 1;
418}
419
420/* Gets one text line out of a channel's input buffer from a stream interface.
421 * Return values :
422 * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2.
423 * =0 : not enough data available.
424 * <0 : no more bytes readable because output is shut.
425 * The '\n' is waited for as long as neither the buffer nor the input are
426 * full. If either of them is full, the string may be returned as is, without
427 * the '\n'. Unused buffers are left in an undefined state.
428 */
Willy Tarreau41ab8682017-10-19 14:58:40 +0200429int ci_getline_nc(const struct channel *chn,
Willy Tarreau55f3ce12018-07-18 11:49:27 +0200430 char **blk1, size_t *len1,
431 char **blk2, size_t *len2)
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100432{
433 int retcode;
434 int l;
435
Willy Tarreau06d80a92017-10-19 14:32:15 +0200436 retcode = ci_getblk_nc(chn, blk1, len1, blk2, len2);
Thierry FOURNIER / OZON.IO500d11e2016-11-12 17:39:58 +0100437 if (unlikely(retcode <= 0))
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100438 return retcode;
439
440 for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++);
441 if (l < *len1 && (*blk1)[l] == '\n') {
442 *len1 = l + 1;
443 return 1;
444 }
445
446 if (retcode >= 2) {
447 for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++);
448 if (l < *len2 && (*blk2)[l] == '\n') {
449 *len2 = l + 1;
450 return 2;
451 }
452 }
453
454 if (chn->flags & CF_SHUTW) {
455 /* If we have found no LF and the buffer is shut, then
456 * the resulting string is made of the concatenation of
457 * the pending blocks (1 or 2).
458 */
459 return retcode;
460 }
461
462 /* No LF yet and not shut yet */
463 return 0;
464}
465
Willy Tarreau4d893d42018-07-12 15:43:32 +0200466/* Inserts <str> followed by "\r\n" at position <pos> relative to channel <c>'s
467 * input head. The <len> argument informs about the length of string <str> so
468 * that we don't have to measure it. <str> must be a valid pointer and must not
469 * include the trailing "\r\n".
470 *
471 * The number of bytes added is returned on success. 0 is returned on failure.
472 */
473int ci_insert_line2(struct channel *c, int pos, const char *str, int len)
474{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200475 struct buffer *b = &c->buf;
Willy Tarreau4d893d42018-07-12 15:43:32 +0200476 char *dst = c_ptr(c, pos);
477 int delta;
478
479 delta = len + 2;
480
Olivier Houchard363c7452018-09-26 15:09:58 +0200481 if (__b_tail(b) + delta >= b_wrap(b))
Willy Tarreau4d893d42018-07-12 15:43:32 +0200482 return 0; /* no space left */
483
484 if (b_data(b) &&
485 b_tail(b) + delta > b_head(b) &&
486 b_head(b) >= b_tail(b))
487 return 0; /* no space left before wrapping data */
488
489 /* first, protect the end of the buffer */
490 memmove(dst + delta, dst, b_tail(b) - dst);
491
492 /* now, copy str over dst */
493 memcpy(dst, str, len);
494 dst[len] = '\r';
495 dst[len + 1] = '\n';
496
497 b_add(b, delta);
498 return delta;
499}
500
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200501/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200502 * Local variables:
503 * c-indent-level: 8
504 * c-basic-offset: 8
505 * End:
506 */