Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 2 | * Channel management functions. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | * |
Willy Tarreau | a27dc19 | 2014-11-27 22:10:04 +0100 | [diff] [blame] | 4 | * Copyright 2000-2014 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 13 | #include <ctype.h> |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 14 | #include <stdarg.h> |
| 15 | #include <stdio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | #include <string.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 17 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 18 | #include <haproxy/api.h> |
Willy Tarreau | 2741c8c | 2020-06-02 11:28:02 +0200 | [diff] [blame] | 19 | #include <haproxy/buf.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 20 | #include <haproxy/channel.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 23 | /* 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 Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 25 | * 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 Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 27 | * 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 Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 30 | */ |
Willy Tarreau | 55a6906 | 2012-10-26 00:21:52 +0200 | [diff] [blame] | 31 | unsigned long long __channel_forward(struct channel *chn, unsigned long long bytes) |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 32 | { |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 33 | unsigned int budget; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 34 | unsigned int forwarded; |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 36 | /* This is more of a safety measure as it's not supposed to happen in |
| 37 | * regular code paths. |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 38 | */ |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 39 | if (unlikely(chn->to_forward == CHN_INFINITE_FORWARD)) { |
Willy Tarreau | cd9e60d | 2018-06-19 07:33:28 +0200 | [diff] [blame] | 40 | c_adv(chn, ci_data(chn)); |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 41 | return bytes; |
| 42 | } |
| 43 | |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 44 | /* 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 Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 48 | |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 49 | /* transfer as much as we can of buf->i */ |
Willy Tarreau | cd9e60d | 2018-06-19 07:33:28 +0200 | [diff] [blame] | 50 | forwarded = MIN(ci_data(chn), budget); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 51 | c_adv(chn, forwarded); |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 52 | budget -= forwarded; |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 54 | 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 Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 68 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 69 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 70 | /* 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 76 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 77 | int co_inject(struct channel *chn, const char *msg, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 78 | { |
| 79 | int max; |
| 80 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 81 | if (len == 0) |
| 82 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 83 | |
Willy Tarreau | cd9e60d | 2018-06-19 07:33:28 +0200 | [diff] [blame] | 84 | if (len < 0 || len > c_size(chn)) { |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 85 | /* 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 Tarreau | d5b343b | 2018-06-06 06:42:46 +0200 | [diff] [blame] | 93 | c_realign_if_empty(chn); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 94 | max = b_contig_space(&chn->buf); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 95 | if (len > max) |
| 96 | return max; |
| 97 | |
Christopher Faulet | 584348b | 2020-01-07 10:01:57 +0100 | [diff] [blame] | 98 | memcpy(co_tail(chn), msg, len); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 99 | b_add(&chn->buf, len); |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 100 | c_adv(chn, len); |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 101 | chn->total += len; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 102 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 105 | /* Tries to copy character <c> into the channel's buffer after some length |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 106 | * controls. The chn->o and to_forward pointers are updated. If the channel |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 107 | * 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 Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 110 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 111 | int ci_putchr(struct channel *chn, char c) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 112 | { |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 113 | if (unlikely(channel_input_closed(chn))) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 114 | return -2; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 115 | |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 116 | if (!channel_may_recv(chn)) |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 117 | return -1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 118 | |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 119 | *ci_tail(chn) = c; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 120 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 121 | b_add(&chn->buf, 1); |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 122 | chn->flags |= CF_READ_PARTIAL; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 123 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 124 | if (chn->to_forward >= 1) { |
| 125 | if (chn->to_forward != CHN_INFINITE_FORWARD) |
| 126 | chn->to_forward--; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 127 | c_adv(chn, 1); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 130 | chn->total++; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 131 | return 1; |
| 132 | } |
| 133 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 134 | /* Tries to copy block <blk> at once into the channel's buffer after length |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 135 | * controls. The chn->o and to_forward pointers are updated. If the channel |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 136 | * 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 Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 140 | * transferred. |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 141 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 142 | int ci_putblk(struct channel *chn, const char *blk, int len) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 143 | { |
| 144 | int max; |
| 145 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 146 | if (unlikely(channel_input_closed(chn))) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 147 | return -2; |
| 148 | |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 149 | if (len < 0) |
| 150 | return -3; |
| 151 | |
Willy Tarreau | 3f5096d | 2015-01-14 20:21:43 +0100 | [diff] [blame] | 152 | max = channel_recv_limit(chn); |
Willy Tarreau | cd9e60d | 2018-06-19 07:33:28 +0200 | [diff] [blame] | 153 | if (unlikely(len > max - c_data(chn))) { |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 154 | /* we can't write this chunk right now because the buffer is |
Emeric Brun | 147b3f0 | 2021-01-11 10:30:42 +0100 | [diff] [blame] | 155 | * almost full or because the block is too large. Returns |
| 156 | * -3 if block is too large for this buffer. Or -1 if the |
| 157 | * room left is not large enough. |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 158 | */ |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 159 | if (len > max) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 160 | return -3; |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 161 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 162 | return -1; |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 163 | } |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 164 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 165 | if (unlikely(len == 0)) |
| 166 | return 0; |
| 167 | |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 168 | /* OK so the data fits in the buffer in one or two blocks */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 169 | max = b_contig_space(&chn->buf); |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 170 | memcpy(ci_tail(chn), blk, MIN(len, max)); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 171 | if (len > max) |
Willy Tarreau | cd9e60d | 2018-06-19 07:33:28 +0200 | [diff] [blame] | 172 | memcpy(c_orig(chn), blk + max, len - max); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 173 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 174 | b_add(&chn->buf, len); |
Christopher Faulet | e645829 | 2019-01-02 14:24:35 +0100 | [diff] [blame] | 175 | channel_add_input(chn, len); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 176 | return len; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Willy Tarreau | c514365 | 2022-01-19 17:19:52 +0100 | [diff] [blame] | 179 | /* Locates the longest part of the channel's output buffer that is composed |
| 180 | * exclusively of characters not in the <delim> set, and delimited by one of |
| 181 | * these characters, and returns the initial part and the first of such |
| 182 | * delimiters. A single escape character in <escape> may be specified so that |
| 183 | * when not 0 and found, the character that follows it is never taken as a |
| 184 | * delimiter. Note that <delim> cannot contain the zero byte, hence this |
| 185 | * function is not usable with byte zero as a delimiter. |
| 186 | * |
| 187 | * Return values : |
| 188 | * >0 : number of bytes read. Includes the sep if present before len or end. |
| 189 | * =0 : no sep before end found. <str> is left undefined. |
| 190 | * <0 : no more bytes readable because output is shut. |
| 191 | * The channel status is not changed. The caller must call co_skip() to |
| 192 | * update it. One of the delimiters is waited for as long as neither the buffer |
| 193 | * nor the output are full. If either of them is full, the string may be |
| 194 | * returned as is, without the delimiter. |
| 195 | */ |
| 196 | int co_getdelim(const struct channel *chn, char *str, int len, const char *delim, char escape) |
| 197 | { |
| 198 | uchar delim_map[256 / 8]; |
| 199 | int found, escaped; |
| 200 | uint pos, bit; |
| 201 | int ret, max; |
| 202 | uchar b; |
| 203 | char *p; |
| 204 | |
| 205 | ret = 0; |
| 206 | max = len; |
| 207 | |
| 208 | /* closed or empty + imminent close = -1; empty = 0 */ |
| 209 | if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) { |
| 210 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
| 211 | ret = -1; |
| 212 | goto out; |
| 213 | } |
| 214 | |
| 215 | p = co_head(chn); |
| 216 | |
| 217 | if (max > co_data(chn)) { |
| 218 | max = co_data(chn); |
| 219 | str[max-1] = 0; |
| 220 | } |
| 221 | |
| 222 | /* create the byte map */ |
| 223 | memset(delim_map, 0, sizeof(delim_map)); |
| 224 | while ((b = *delim)) { |
| 225 | pos = b >> 3; |
| 226 | bit = b & 7; |
| 227 | delim_map[pos] |= 1 << bit; |
| 228 | delim++; |
| 229 | } |
| 230 | |
| 231 | found = escaped = 0; |
| 232 | while (max) { |
| 233 | *str++ = b = *p; |
| 234 | ret++; |
| 235 | max--; |
| 236 | |
| 237 | if (escape && (escaped || *p == escape)) { |
| 238 | escaped = !escaped; |
| 239 | goto skip; |
| 240 | } |
| 241 | |
| 242 | pos = b >> 3; |
| 243 | bit = b & 7; |
| 244 | if (delim_map[pos] & (1 << bit)) { |
| 245 | found = 1; |
| 246 | break; |
| 247 | } |
| 248 | skip: |
| 249 | p = b_next(&chn->buf, p); |
| 250 | } |
| 251 | |
| 252 | if (ret > 0 && ret < len && |
| 253 | (ret < co_data(chn) || channel_may_recv(chn)) && |
| 254 | !found && |
| 255 | !(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 256 | ret = 0; |
| 257 | out: |
| 258 | if (max) |
| 259 | *str = 0; |
| 260 | return ret; |
| 261 | } |
| 262 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 263 | /* Gets one text word out of a channel's buffer from a stream connector. |
Emeric Brun | 6d75616 | 2020-10-05 14:35:21 +0200 | [diff] [blame] | 264 | * Return values : |
| 265 | * >0 : number of bytes read. Includes the sep if present before len or end. |
| 266 | * =0 : no sep before end found. <str> is left undefined. |
| 267 | * <0 : no more bytes readable because output is shut. |
| 268 | * The channel status is not changed. The caller must call co_skip() to |
| 269 | * update it. The line separator is waited for as long as neither the buffer |
| 270 | * nor the output are full. If either of them is full, the string may be |
| 271 | * returned as is, without the line separator. |
| 272 | */ |
| 273 | int co_getword(const struct channel *chn, char *str, int len, char sep) |
| 274 | { |
| 275 | int ret, max; |
| 276 | char *p; |
| 277 | |
| 278 | ret = 0; |
| 279 | max = len; |
| 280 | |
| 281 | /* closed or empty + imminent close = -1; empty = 0 */ |
| 282 | if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) { |
| 283 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
| 284 | ret = -1; |
| 285 | goto out; |
| 286 | } |
| 287 | |
| 288 | p = co_head(chn); |
| 289 | |
| 290 | if (max > co_data(chn)) { |
| 291 | max = co_data(chn); |
| 292 | str[max-1] = 0; |
| 293 | } |
| 294 | while (max) { |
| 295 | *str++ = *p; |
| 296 | ret++; |
| 297 | max--; |
| 298 | |
| 299 | if (*p == sep) |
| 300 | break; |
| 301 | p = b_next(&chn->buf, p); |
| 302 | } |
| 303 | if (ret > 0 && ret < len && |
| 304 | (ret < co_data(chn) || channel_may_recv(chn)) && |
| 305 | *(str-1) != sep && |
| 306 | !(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 307 | ret = 0; |
| 308 | out: |
| 309 | if (max) |
| 310 | *str = 0; |
| 311 | return ret; |
| 312 | } |
| 313 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 314 | /* Gets one text line out of a channel's buffer from a stream connector. |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 315 | * Return values : |
| 316 | * >0 : number of bytes read. Includes the \n if present before len or end. |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 317 | * =0 : no '\n' before end found. <str> is left undefined. |
| 318 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 319 | * The channel status is not changed. The caller must call co_skip() to |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 320 | * update it. The '\n' is waited for as long as neither the buffer nor the |
| 321 | * output are full. If either of them is full, the string may be returned |
| 322 | * as is, without the '\n'. |
| 323 | */ |
Willy Tarreau | 41ab868 | 2017-10-19 14:58:40 +0200 | [diff] [blame] | 324 | int co_getline(const struct channel *chn, char *str, int len) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 325 | { |
| 326 | int ret, max; |
| 327 | char *p; |
| 328 | |
| 329 | ret = 0; |
| 330 | max = len; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 331 | |
| 332 | /* closed or empty + imminent close = -1; empty = 0 */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 333 | if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) { |
| 334 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 335 | ret = -1; |
| 336 | goto out; |
| 337 | } |
| 338 | |
Willy Tarreau | cd9e60d | 2018-06-19 07:33:28 +0200 | [diff] [blame] | 339 | p = co_head(chn); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 340 | |
Willy Tarreau | cd9e60d | 2018-06-19 07:33:28 +0200 | [diff] [blame] | 341 | if (max > co_data(chn)) { |
| 342 | max = co_data(chn); |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 343 | str[max-1] = 0; |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 344 | } |
| 345 | while (max) { |
| 346 | *str++ = *p; |
| 347 | ret++; |
| 348 | max--; |
| 349 | |
| 350 | if (*p == '\n') |
| 351 | break; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 352 | p = b_next(&chn->buf, p); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 353 | } |
Willy Tarreau | 82de2b6 | 2013-12-10 18:58:23 +0100 | [diff] [blame] | 354 | if (ret > 0 && ret < len && |
Willy Tarreau | cd9e60d | 2018-06-19 07:33:28 +0200 | [diff] [blame] | 355 | (ret < co_data(chn) || channel_may_recv(chn)) && |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 356 | *(str-1) != '\n' && |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 357 | !(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 358 | ret = 0; |
| 359 | out: |
| 360 | if (max) |
| 361 | *str = 0; |
| 362 | return ret; |
| 363 | } |
| 364 | |
Emeric Brun | 6d75616 | 2020-10-05 14:35:21 +0200 | [diff] [blame] | 365 | /* Gets one char of data from a channel's buffer, |
| 366 | * Return values : |
| 367 | * 1 : number of bytes read, equal to requested size. |
| 368 | * =0 : not enough data available. <c> is left undefined. |
| 369 | * <0 : no more bytes readable because output is shut. |
| 370 | * The channel status is not changed. The caller must call co_skip() to |
| 371 | * update it. |
| 372 | */ |
| 373 | int co_getchar(const struct channel *chn, char *c) |
| 374 | { |
| 375 | if (chn->flags & CF_SHUTW) |
| 376 | return -1; |
| 377 | |
| 378 | if (unlikely(co_data(chn) == 0)) { |
| 379 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
| 380 | return -1; |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | *c = *(co_head(chn)); |
| 385 | return 1; |
| 386 | } |
| 387 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 388 | /* Gets one full block of data at once from a channel's buffer, optionally from |
| 389 | * a specific offset. Return values : |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 390 | * >0 : number of bytes read, equal to requested size. |
| 391 | * =0 : not enough data available. <blk> is left undefined. |
| 392 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 393 | * The channel status is not changed. The caller must call co_skip() to |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 394 | * update it. |
| 395 | */ |
Willy Tarreau | 41ab868 | 2017-10-19 14:58:40 +0200 | [diff] [blame] | 396 | int co_getblk(const struct channel *chn, char *blk, int len, int offset) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 397 | { |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 398 | if (chn->flags & CF_SHUTW) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 399 | return -1; |
| 400 | |
Willy Tarreau | 90ed383 | 2018-06-15 14:20:26 +0200 | [diff] [blame] | 401 | if (len + offset > co_data(chn)) { |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 402 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 403 | return -1; |
| 404 | return 0; |
| 405 | } |
| 406 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 407 | return b_getblk(&chn->buf, blk, len, offset); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 408 | } |
| 409 | |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 410 | /* Gets one or two blocks of data at once from a channel's output buffer. |
| 411 | * Return values : |
| 412 | * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2. |
| 413 | * =0 : not enough data available. <blk*> are left undefined. |
| 414 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 415 | * The channel status is not changed. The caller must call co_skip() to |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 416 | * update it. Unused buffers are left in an undefined state. |
| 417 | */ |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 418 | int co_getblk_nc(const struct channel *chn, const char **blk1, size_t *len1, const char **blk2, size_t *len2) |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 419 | { |
Willy Tarreau | cd9e60d | 2018-06-19 07:33:28 +0200 | [diff] [blame] | 420 | if (unlikely(co_data(chn) == 0)) { |
Christopher Faulet | f706a79 | 2020-07-16 11:43:46 +0200 | [diff] [blame] | 421 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 422 | return -1; |
| 423 | return 0; |
| 424 | } |
| 425 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 426 | return b_getblk_nc(&chn->buf, blk1, len1, blk2, len2, 0, co_data(chn)); |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 427 | } |
| 428 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 429 | /* Gets one text line out of a channel's output buffer from a stream connector. |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 430 | * Return values : |
| 431 | * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2. |
| 432 | * =0 : not enough data available. |
| 433 | * <0 : no more bytes readable because output is shut. |
| 434 | * The '\n' is waited for as long as neither the buffer nor the output are |
| 435 | * full. If either of them is full, the string may be returned as is, without |
| 436 | * the '\n'. Unused buffers are left in an undefined state. |
| 437 | */ |
Willy Tarreau | 41ab868 | 2017-10-19 14:58:40 +0200 | [diff] [blame] | 438 | int co_getline_nc(const struct channel *chn, |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 439 | const char **blk1, size_t *len1, |
| 440 | const char **blk2, size_t *len2) |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 441 | { |
| 442 | int retcode; |
| 443 | int l; |
| 444 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 445 | retcode = co_getblk_nc(chn, blk1, len1, blk2, len2); |
Tim Duesterhus | 45be38c | 2018-04-24 19:20:43 +0200 | [diff] [blame] | 446 | if (unlikely(retcode <= 0)) |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 447 | return retcode; |
| 448 | |
| 449 | for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++); |
| 450 | if (l < *len1 && (*blk1)[l] == '\n') { |
| 451 | *len1 = l + 1; |
| 452 | return 1; |
| 453 | } |
| 454 | |
| 455 | if (retcode >= 2) { |
| 456 | for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++); |
| 457 | if (l < *len2 && (*blk2)[l] == '\n') { |
| 458 | *len2 = l + 1; |
| 459 | return 2; |
| 460 | } |
| 461 | } |
| 462 | |
Christopher Faulet | f706a79 | 2020-07-16 11:43:46 +0200 | [diff] [blame] | 463 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 464 | /* If we have found no LF and the buffer is shut, then |
| 465 | * the resulting string is made of the concatenation of |
| 466 | * the pending blocks (1 or 2). |
| 467 | */ |
| 468 | return retcode; |
| 469 | } |
| 470 | |
| 471 | /* No LF yet and not shut yet */ |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | /* Gets one full block of data at once from a channel's input buffer. |
| 476 | * This function can return the data slitted in one or two blocks. |
| 477 | * Return values : |
| 478 | * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2. |
| 479 | * =0 : not enough data available. |
| 480 | * <0 : no more bytes readable because input is shut. |
| 481 | */ |
Willy Tarreau | 41ab868 | 2017-10-19 14:58:40 +0200 | [diff] [blame] | 482 | int ci_getblk_nc(const struct channel *chn, |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 483 | char **blk1, size_t *len1, |
| 484 | char **blk2, size_t *len2) |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 485 | { |
Willy Tarreau | cd9e60d | 2018-06-19 07:33:28 +0200 | [diff] [blame] | 486 | if (unlikely(ci_data(chn) == 0)) { |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 487 | if (chn->flags & CF_SHUTR) |
| 488 | return -1; |
| 489 | return 0; |
| 490 | } |
| 491 | |
Willy Tarreau | cd9e60d | 2018-06-19 07:33:28 +0200 | [diff] [blame] | 492 | if (unlikely(ci_head(chn) + ci_data(chn) > c_wrap(chn))) { |
| 493 | *blk1 = ci_head(chn); |
| 494 | *len1 = c_wrap(chn) - ci_head(chn); |
| 495 | *blk2 = c_orig(chn); |
| 496 | *len2 = ci_data(chn) - *len1; |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 497 | return 2; |
| 498 | } |
| 499 | |
Willy Tarreau | cd9e60d | 2018-06-19 07:33:28 +0200 | [diff] [blame] | 500 | *blk1 = ci_head(chn); |
| 501 | *len1 = ci_data(chn); |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 502 | return 1; |
| 503 | } |
| 504 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 505 | /* Gets one text line out of a channel's input buffer from a stream connector. |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 506 | * Return values : |
| 507 | * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2. |
| 508 | * =0 : not enough data available. |
| 509 | * <0 : no more bytes readable because output is shut. |
| 510 | * The '\n' is waited for as long as neither the buffer nor the input are |
| 511 | * full. If either of them is full, the string may be returned as is, without |
| 512 | * the '\n'. Unused buffers are left in an undefined state. |
| 513 | */ |
Willy Tarreau | 41ab868 | 2017-10-19 14:58:40 +0200 | [diff] [blame] | 514 | int ci_getline_nc(const struct channel *chn, |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 515 | char **blk1, size_t *len1, |
| 516 | char **blk2, size_t *len2) |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 517 | { |
| 518 | int retcode; |
| 519 | int l; |
| 520 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 521 | retcode = ci_getblk_nc(chn, blk1, len1, blk2, len2); |
Thierry FOURNIER / OZON.IO | 500d11e | 2016-11-12 17:39:58 +0100 | [diff] [blame] | 522 | if (unlikely(retcode <= 0)) |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 523 | return retcode; |
| 524 | |
| 525 | for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++); |
| 526 | if (l < *len1 && (*blk1)[l] == '\n') { |
| 527 | *len1 = l + 1; |
| 528 | return 1; |
| 529 | } |
| 530 | |
| 531 | if (retcode >= 2) { |
| 532 | for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++); |
| 533 | if (l < *len2 && (*blk2)[l] == '\n') { |
| 534 | *len2 = l + 1; |
| 535 | return 2; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | if (chn->flags & CF_SHUTW) { |
| 540 | /* If we have found no LF and the buffer is shut, then |
| 541 | * the resulting string is made of the concatenation of |
| 542 | * the pending blocks (1 or 2). |
| 543 | */ |
| 544 | return retcode; |
| 545 | } |
| 546 | |
| 547 | /* No LF yet and not shut yet */ |
| 548 | return 0; |
| 549 | } |
| 550 | |
Willy Tarreau | 4d893d4 | 2018-07-12 15:43:32 +0200 | [diff] [blame] | 551 | /* Inserts <str> followed by "\r\n" at position <pos> relative to channel <c>'s |
| 552 | * input head. The <len> argument informs about the length of string <str> so |
| 553 | * that we don't have to measure it. <str> must be a valid pointer and must not |
| 554 | * include the trailing "\r\n". |
| 555 | * |
| 556 | * The number of bytes added is returned on success. 0 is returned on failure. |
| 557 | */ |
| 558 | int ci_insert_line2(struct channel *c, int pos, const char *str, int len) |
| 559 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 560 | struct buffer *b = &c->buf; |
Willy Tarreau | 4d893d4 | 2018-07-12 15:43:32 +0200 | [diff] [blame] | 561 | char *dst = c_ptr(c, pos); |
| 562 | int delta; |
| 563 | |
| 564 | delta = len + 2; |
| 565 | |
Olivier Houchard | 363c745 | 2018-09-26 15:09:58 +0200 | [diff] [blame] | 566 | if (__b_tail(b) + delta >= b_wrap(b)) |
Willy Tarreau | 4d893d4 | 2018-07-12 15:43:32 +0200 | [diff] [blame] | 567 | return 0; /* no space left */ |
| 568 | |
| 569 | if (b_data(b) && |
| 570 | b_tail(b) + delta > b_head(b) && |
| 571 | b_head(b) >= b_tail(b)) |
| 572 | return 0; /* no space left before wrapping data */ |
| 573 | |
| 574 | /* first, protect the end of the buffer */ |
| 575 | memmove(dst + delta, dst, b_tail(b) - dst); |
| 576 | |
| 577 | /* now, copy str over dst */ |
| 578 | memcpy(dst, str, len); |
| 579 | dst[len] = '\r'; |
| 580 | dst[len + 1] = '\n'; |
| 581 | |
| 582 | b_add(b, delta); |
| 583 | return delta; |
| 584 | } |
| 585 | |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 586 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 587 | * Local variables: |
| 588 | * c-indent-level: 8 |
| 589 | * c-basic-offset: 8 |
| 590 | * End: |
| 591 | */ |