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 | |
| 18 | #include <common/config.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 19 | #include <common/buffer.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 21 | #include <proto/channel.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 23 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 24 | /* 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 Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 26 | * 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 Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 28 | * 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 Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 31 | */ |
Willy Tarreau | 55a6906 | 2012-10-26 00:21:52 +0200 | [diff] [blame] | 32 | unsigned long long __channel_forward(struct channel *chn, unsigned long long bytes) |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 33 | { |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 34 | unsigned int budget; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 35 | unsigned int forwarded; |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 37 | /* This is more of a safety measure as it's not supposed to happen in |
| 38 | * regular code paths. |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 39 | */ |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 40 | if (unlikely(chn->to_forward == CHN_INFINITE_FORWARD)) { |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 41 | c_adv(chn, chn->buf->i); |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 42 | return bytes; |
| 43 | } |
| 44 | |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 45 | /* 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 Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 49 | |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 50 | /* transfer as much as we can of buf->i */ |
| 51 | forwarded = MIN(chn->buf->i, budget); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 52 | c_adv(chn, forwarded); |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 53 | budget -= forwarded; |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 54 | |
Willy Tarreau | 8bf242b | 2016-05-04 14:05:58 +0200 | [diff] [blame] | 55 | 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 Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 69 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 71 | /* 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 78 | int co_inject(struct channel *chn, const char *msg, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 79 | { |
| 80 | int max; |
| 81 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 82 | if (len == 0) |
| 83 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 85 | if (len < 0 || len > chn->buf->size) { |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 86 | /* 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 Tarreau | d5b343b | 2018-06-06 06:42:46 +0200 | [diff] [blame] | 94 | c_realign_if_empty(chn); |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 95 | max = bo_contig_space(chn->buf); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | if (len > max) |
| 97 | return max; |
| 98 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 99 | memcpy(chn->buf->p, msg, len); |
| 100 | chn->buf->o += len; |
| 101 | chn->buf->p = b_ptr(chn->buf, len); |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 102 | chn->total += len; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 103 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 106 | /* 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] | 107 | * 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] | 108 | * 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 Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 111 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 112 | int ci_putchr(struct channel *chn, char c) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 113 | { |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 114 | if (unlikely(channel_input_closed(chn))) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 115 | return -2; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 116 | |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 117 | if (!channel_may_recv(chn)) |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 118 | return -1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 119 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 120 | *bi_end(chn->buf) = c; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 121 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 122 | chn->buf->i++; |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 123 | chn->flags |= CF_READ_PARTIAL; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 124 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 125 | if (chn->to_forward >= 1) { |
| 126 | if (chn->to_forward != CHN_INFINITE_FORWARD) |
| 127 | chn->to_forward--; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 128 | c_adv(chn, 1); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 131 | chn->total++; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 132 | return 1; |
| 133 | } |
| 134 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 135 | /* 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] | 136 | * 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] | 137 | * 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 Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 141 | * transferred. |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 142 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 143 | int ci_putblk(struct channel *chn, const char *blk, int len) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 144 | { |
| 145 | int max; |
| 146 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 147 | if (unlikely(channel_input_closed(chn))) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 148 | return -2; |
| 149 | |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 150 | if (len < 0) |
| 151 | return -3; |
| 152 | |
Willy Tarreau | 3f5096d | 2015-01-14 20:21:43 +0100 | [diff] [blame] | 153 | max = channel_recv_limit(chn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 154 | if (unlikely(len > max - buffer_len(chn->buf))) { |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 155 | /* 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 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 */ |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 169 | max = bi_contig_space(chn->buf); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 170 | memcpy(bi_end(chn->buf), blk, MIN(len, max)); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 171 | if (len > max) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 172 | memcpy(chn->buf->data, blk + max, len - max); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 173 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 174 | chn->buf->i += len; |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 175 | chn->total += len; |
| 176 | if (chn->to_forward) { |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 177 | unsigned long fwd = len; |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 178 | if (chn->to_forward != CHN_INFINITE_FORWARD) { |
| 179 | if (fwd > chn->to_forward) |
| 180 | fwd = chn->to_forward; |
| 181 | chn->to_forward -= fwd; |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 182 | } |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 183 | c_adv(chn, fwd); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Willy Tarreau | fb0e920 | 2009-09-23 23:47:55 +0200 | [diff] [blame] | 186 | /* notify that some data was read from the SI into the buffer */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 187 | chn->flags |= CF_READ_PARTIAL; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 188 | return len; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Willy Tarreau | b034b25 | 2014-12-08 18:14:53 +0100 | [diff] [blame] | 191 | /* Tries to copy the whole buffer <buf> into the channel's buffer after length |
| 192 | * controls. It will only succeed if the target buffer is empty, in which case |
| 193 | * it will simply swap the buffers. The buffer not attached to the channel is |
| 194 | * returned so that the caller can store it locally. The chn->buf->o and |
| 195 | * to_forward pointers are updated. If the output buffer is a dummy buffer or |
| 196 | * if it still contains data <buf> is returned, indicating that nothing could |
| 197 | * be done. Channel flag READ_PARTIAL is updated if some data can be transferred. |
| 198 | * The chunk's length is updated with the number of bytes sent. On errors, NULL |
| 199 | * is returned. Note that only buf->i is considered. |
| 200 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 201 | struct buffer *ci_swpbuf(struct channel *chn, struct buffer *buf) |
Willy Tarreau | b034b25 | 2014-12-08 18:14:53 +0100 | [diff] [blame] | 202 | { |
| 203 | struct buffer *old; |
| 204 | |
| 205 | if (unlikely(channel_input_closed(chn))) |
| 206 | return NULL; |
| 207 | |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 208 | if (!chn->buf->size || !buffer_empty(chn->buf)) |
Willy Tarreau | b034b25 | 2014-12-08 18:14:53 +0100 | [diff] [blame] | 209 | return buf; |
Willy Tarreau | b034b25 | 2014-12-08 18:14:53 +0100 | [diff] [blame] | 210 | |
| 211 | old = chn->buf; |
| 212 | chn->buf = buf; |
| 213 | |
| 214 | if (!buf->i) |
| 215 | return old; |
| 216 | |
| 217 | chn->total += buf->i; |
| 218 | |
| 219 | if (chn->to_forward) { |
| 220 | unsigned long fwd = buf->i; |
| 221 | if (chn->to_forward != CHN_INFINITE_FORWARD) { |
| 222 | if (fwd > chn->to_forward) |
| 223 | fwd = chn->to_forward; |
| 224 | chn->to_forward -= fwd; |
| 225 | } |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 226 | c_adv(chn, fwd); |
Willy Tarreau | b034b25 | 2014-12-08 18:14:53 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* notify that some data was read from the SI into the buffer */ |
| 230 | chn->flags |= CF_READ_PARTIAL; |
| 231 | return old; |
| 232 | } |
| 233 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 234 | /* Gets one text line out of a channel's buffer from a stream interface. |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 235 | * Return values : |
| 236 | * >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] | 237 | * =0 : no '\n' before end found. <str> is left undefined. |
| 238 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 239 | * 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] | 240 | * update it. The '\n' is waited for as long as neither the buffer nor the |
| 241 | * output are full. If either of them is full, the string may be returned |
| 242 | * as is, without the '\n'. |
| 243 | */ |
Willy Tarreau | 41ab868 | 2017-10-19 14:58:40 +0200 | [diff] [blame] | 244 | int co_getline(const struct channel *chn, char *str, int len) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 245 | { |
| 246 | int ret, max; |
| 247 | char *p; |
| 248 | |
| 249 | ret = 0; |
| 250 | max = len; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 251 | |
| 252 | /* closed or empty + imminent close = -1; empty = 0 */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 253 | if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) { |
| 254 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 255 | ret = -1; |
| 256 | goto out; |
| 257 | } |
| 258 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 259 | p = bo_ptr(chn->buf); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 260 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 261 | if (max > chn->buf->o) { |
| 262 | max = chn->buf->o; |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 263 | str[max-1] = 0; |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 264 | } |
| 265 | while (max) { |
| 266 | *str++ = *p; |
| 267 | ret++; |
| 268 | max--; |
| 269 | |
| 270 | if (*p == '\n') |
| 271 | break; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 272 | p = buffer_wrap_add(chn->buf, p + 1); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 273 | } |
Willy Tarreau | 82de2b6 | 2013-12-10 18:58:23 +0100 | [diff] [blame] | 274 | if (ret > 0 && ret < len && |
Willy Tarreau | 3889fff | 2015-01-13 20:20:10 +0100 | [diff] [blame] | 275 | (ret < chn->buf->o || channel_may_recv(chn)) && |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 276 | *(str-1) != '\n' && |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 277 | !(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 278 | ret = 0; |
| 279 | out: |
| 280 | if (max) |
| 281 | *str = 0; |
| 282 | return ret; |
| 283 | } |
| 284 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 285 | /* Gets one full block of data at once from a channel's buffer, optionally from |
| 286 | * a specific offset. Return values : |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 287 | * >0 : number of bytes read, equal to requested size. |
| 288 | * =0 : not enough data available. <blk> is left undefined. |
| 289 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 290 | * 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] | 291 | * update it. |
| 292 | */ |
Willy Tarreau | 41ab868 | 2017-10-19 14:58:40 +0200 | [diff] [blame] | 293 | 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] | 294 | { |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 295 | if (chn->flags & CF_SHUTW) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 296 | return -1; |
| 297 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 298 | if (len + offset > chn->buf->o) { |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 299 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 300 | return -1; |
| 301 | return 0; |
| 302 | } |
| 303 | |
Willy Tarreau | 6b3f353 | 2017-10-19 14:57:54 +0200 | [diff] [blame] | 304 | return bo_getblk(chn->buf, blk, len, offset); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 307 | /* Gets one or two blocks of data at once from a channel's output buffer. |
| 308 | * Return values : |
| 309 | * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2. |
| 310 | * =0 : not enough data available. <blk*> are left undefined. |
| 311 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 312 | * 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] | 313 | * update it. Unused buffers are left in an undefined state. |
| 314 | */ |
Willy Tarreau | 41ab868 | 2017-10-19 14:58:40 +0200 | [diff] [blame] | 315 | int co_getblk_nc(const struct channel *chn, char **blk1, int *len1, char **blk2, int *len2) |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 316 | { |
| 317 | if (unlikely(chn->buf->o == 0)) { |
| 318 | if (chn->flags & CF_SHUTW) |
| 319 | return -1; |
| 320 | return 0; |
| 321 | } |
| 322 | |
Willy Tarreau | 6b3f353 | 2017-10-19 14:57:54 +0200 | [diff] [blame] | 323 | return bo_getblk_nc(chn->buf, blk1, len1, blk2, len2); |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /* Gets one text line out of a channel's output buffer from a stream interface. |
| 327 | * Return values : |
| 328 | * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2. |
| 329 | * =0 : not enough data available. |
| 330 | * <0 : no more bytes readable because output is shut. |
| 331 | * The '\n' is waited for as long as neither the buffer nor the output are |
| 332 | * full. If either of them is full, the string may be returned as is, without |
| 333 | * the '\n'. Unused buffers are left in an undefined state. |
| 334 | */ |
Willy Tarreau | 41ab868 | 2017-10-19 14:58:40 +0200 | [diff] [blame] | 335 | int co_getline_nc(const struct channel *chn, |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 336 | char **blk1, int *len1, |
| 337 | char **blk2, int *len2) |
| 338 | { |
| 339 | int retcode; |
| 340 | int l; |
| 341 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 342 | retcode = co_getblk_nc(chn, blk1, len1, blk2, len2); |
Tim Duesterhus | 45be38c | 2018-04-24 19:20:43 +0200 | [diff] [blame] | 343 | if (unlikely(retcode <= 0)) |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 344 | return retcode; |
| 345 | |
| 346 | for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++); |
| 347 | if (l < *len1 && (*blk1)[l] == '\n') { |
| 348 | *len1 = l + 1; |
| 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | if (retcode >= 2) { |
| 353 | for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++); |
| 354 | if (l < *len2 && (*blk2)[l] == '\n') { |
| 355 | *len2 = l + 1; |
| 356 | return 2; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if (chn->flags & CF_SHUTW) { |
| 361 | /* If we have found no LF and the buffer is shut, then |
| 362 | * the resulting string is made of the concatenation of |
| 363 | * the pending blocks (1 or 2). |
| 364 | */ |
| 365 | return retcode; |
| 366 | } |
| 367 | |
| 368 | /* No LF yet and not shut yet */ |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | /* Gets one full block of data at once from a channel's input buffer. |
| 373 | * This function can return the data slitted in one or two blocks. |
| 374 | * Return values : |
| 375 | * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2. |
| 376 | * =0 : not enough data available. |
| 377 | * <0 : no more bytes readable because input is shut. |
| 378 | */ |
Willy Tarreau | 41ab868 | 2017-10-19 14:58:40 +0200 | [diff] [blame] | 379 | int ci_getblk_nc(const struct channel *chn, |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 380 | char **blk1, int *len1, |
| 381 | char **blk2, int *len2) |
| 382 | { |
| 383 | if (unlikely(chn->buf->i == 0)) { |
| 384 | if (chn->flags & CF_SHUTR) |
| 385 | return -1; |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | if (unlikely(chn->buf->p + chn->buf->i > chn->buf->data + chn->buf->size)) { |
| 390 | *blk1 = chn->buf->p; |
| 391 | *len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
| 392 | *blk2 = chn->buf->data; |
| 393 | *len2 = chn->buf->i - *len1; |
| 394 | return 2; |
| 395 | } |
| 396 | |
| 397 | *blk1 = chn->buf->p; |
| 398 | *len1 = chn->buf->i; |
| 399 | return 1; |
| 400 | } |
| 401 | |
| 402 | /* Gets one text line out of a channel's input buffer from a stream interface. |
| 403 | * Return values : |
| 404 | * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2. |
| 405 | * =0 : not enough data available. |
| 406 | * <0 : no more bytes readable because output is shut. |
| 407 | * The '\n' is waited for as long as neither the buffer nor the input are |
| 408 | * full. If either of them is full, the string may be returned as is, without |
| 409 | * the '\n'. Unused buffers are left in an undefined state. |
| 410 | */ |
Willy Tarreau | 41ab868 | 2017-10-19 14:58:40 +0200 | [diff] [blame] | 411 | int ci_getline_nc(const struct channel *chn, |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 412 | char **blk1, int *len1, |
| 413 | char **blk2, int *len2) |
| 414 | { |
| 415 | int retcode; |
| 416 | int l; |
| 417 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 418 | retcode = ci_getblk_nc(chn, blk1, len1, blk2, len2); |
Thierry FOURNIER / OZON.IO | 500d11e | 2016-11-12 17:39:58 +0100 | [diff] [blame] | 419 | if (unlikely(retcode <= 0)) |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 420 | return retcode; |
| 421 | |
| 422 | for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++); |
| 423 | if (l < *len1 && (*blk1)[l] == '\n') { |
| 424 | *len1 = l + 1; |
| 425 | return 1; |
| 426 | } |
| 427 | |
| 428 | if (retcode >= 2) { |
| 429 | for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++); |
| 430 | if (l < *len2 && (*blk2)[l] == '\n') { |
| 431 | *len2 = l + 1; |
| 432 | return 2; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | if (chn->flags & CF_SHUTW) { |
| 437 | /* If we have found no LF and the buffer is shut, then |
| 438 | * the resulting string is made of the concatenation of |
| 439 | * the pending blocks (1 or 2). |
| 440 | */ |
| 441 | return retcode; |
| 442 | } |
| 443 | |
| 444 | /* No LF yet and not shut yet */ |
| 445 | return 0; |
| 446 | } |
| 447 | |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 448 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 449 | * Local variables: |
| 450 | * c-indent-level: 8 |
| 451 | * c-basic-offset: 8 |
| 452 | * End: |
| 453 | */ |