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 |
| 26 | * sent as well, in the limit of the number of bytes to forward. This must be |
| 27 | * the only method to use to schedule bytes to be forwarded. If the requested |
| 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 | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 34 | unsigned int new_forward; |
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 | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 37 | forwarded = chn->buf->i; |
| 38 | b_adv(chn->buf, chn->buf->i); |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 39 | |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 40 | /* Note: the case below is the only case where we may return |
| 41 | * a byte count that does not fit into a 32-bit number. |
| 42 | */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 43 | if (likely(chn->to_forward == CHN_INFINITE_FORWARD)) |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 44 | return bytes; |
| 45 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 46 | if (likely(bytes == CHN_INFINITE_FORWARD)) { |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 47 | chn->to_forward = bytes; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 48 | return bytes; |
| 49 | } |
| 50 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 51 | new_forward = chn->to_forward + bytes - forwarded; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 52 | bytes = forwarded; /* at least those bytes were scheduled */ |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 54 | if (new_forward <= chn->to_forward) { |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 55 | /* integer overflow detected, let's assume no more than 2G at once */ |
| 56 | new_forward = MID_RANGE(new_forward); |
| 57 | } |
| 58 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 59 | if (new_forward > chn->to_forward) { |
| 60 | bytes += new_forward - chn->to_forward; |
| 61 | chn->to_forward = new_forward; |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 62 | } |
| 63 | return bytes; |
| 64 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 66 | /* writes <len> bytes from message <msg> to the channel's buffer. Returns -1 in |
| 67 | * case of success, -2 if the message is larger than the buffer size, or the |
| 68 | * number of bytes available otherwise. The send limit is automatically |
| 69 | * adjusted to the amount of data written. FIXME-20060521: handle unaligned |
| 70 | * data. Note: this function appends data to the buffer's output and possibly |
| 71 | * overwrites any pending input data which are assumed not to exist. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 72 | */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 73 | int bo_inject(struct channel *chn, const char *msg, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | { |
| 75 | int max; |
| 76 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 77 | if (len == 0) |
| 78 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 79 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 80 | if (len > chn->buf->size) { |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 81 | /* we can't write this chunk and will never be able to, because |
| 82 | * it is larger than the buffer. This must be reported as an |
| 83 | * error. Then we return -2 so that writers that don't care can |
| 84 | * ignore it and go on, and others can check for this value. |
| 85 | */ |
| 86 | return -2; |
| 87 | } |
| 88 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 89 | max = buffer_realign(chn->buf); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 90 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | if (len > max) |
| 92 | return max; |
| 93 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 94 | memcpy(chn->buf->p, msg, len); |
| 95 | chn->buf->o += len; |
| 96 | chn->buf->p = b_ptr(chn->buf, len); |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 97 | chn->total += len; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 98 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 101 | /* 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] | 102 | * 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] | 103 | * input is closed, -2 is returned. If there is not enough room left in the |
| 104 | * buffer, -1 is returned. Otherwise the number of bytes copied is returned |
| 105 | * (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] | 106 | */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 107 | int bi_putchr(struct channel *chn, char c) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 108 | { |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 109 | if (unlikely(channel_input_closed(chn))) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 110 | return -2; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 111 | |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 112 | if (!channel_may_recv(chn)) |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 113 | return -1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 114 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 115 | *bi_end(chn->buf) = c; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 116 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 117 | chn->buf->i++; |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 118 | chn->flags |= CF_READ_PARTIAL; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 119 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 120 | if (chn->to_forward >= 1) { |
| 121 | if (chn->to_forward != CHN_INFINITE_FORWARD) |
| 122 | chn->to_forward--; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 123 | b_adv(chn->buf, 1); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 126 | chn->total++; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 127 | return 1; |
| 128 | } |
| 129 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 130 | /* 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] | 131 | * 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] | 132 | * input is closed, -2 is returned. If the block is too large for this buffer, |
| 133 | * -3 is returned. If there is not enough room left in the buffer, -1 is |
| 134 | * returned. Otherwise the number of bytes copied is returned (0 being a valid |
| 135 | * number). Channel flag READ_PARTIAL is updated if some data can be |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 136 | * transferred. |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 137 | */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 138 | int bi_putblk(struct channel *chn, const char *blk, int len) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 139 | { |
| 140 | int max; |
| 141 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 142 | if (unlikely(channel_input_closed(chn))) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 143 | return -2; |
| 144 | |
Willy Tarreau | 3f5096d | 2015-01-14 20:21:43 +0100 | [diff] [blame] | 145 | max = channel_recv_limit(chn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 146 | if (unlikely(len > max - buffer_len(chn->buf))) { |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 147 | /* we can't write this chunk right now because the buffer is |
| 148 | * almost full or because the block is too large. Return the |
| 149 | * available space or -2 if impossible. |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 150 | */ |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 151 | if (len > max) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 152 | return -3; |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 153 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 154 | return -1; |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 155 | } |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 156 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 157 | if (unlikely(len == 0)) |
| 158 | return 0; |
| 159 | |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 160 | /* OK so the data fits in the buffer in one or two blocks */ |
Willy Tarreau | 285ff0f | 2014-04-24 17:02:57 +0200 | [diff] [blame] | 161 | max = buffer_contig_space(chn->buf); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 162 | memcpy(bi_end(chn->buf), blk, MIN(len, max)); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 163 | if (len > max) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 164 | memcpy(chn->buf->data, blk + max, len - max); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 165 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 166 | chn->buf->i += len; |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 167 | chn->total += len; |
| 168 | if (chn->to_forward) { |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 169 | unsigned long fwd = len; |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 170 | if (chn->to_forward != CHN_INFINITE_FORWARD) { |
| 171 | if (fwd > chn->to_forward) |
| 172 | fwd = chn->to_forward; |
| 173 | chn->to_forward -= fwd; |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 174 | } |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 175 | b_adv(chn->buf, fwd); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Willy Tarreau | fb0e920 | 2009-09-23 23:47:55 +0200 | [diff] [blame] | 178 | /* notify that some data was read from the SI into the buffer */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 179 | chn->flags |= CF_READ_PARTIAL; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 180 | return len; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Willy Tarreau | b034b25 | 2014-12-08 18:14:53 +0100 | [diff] [blame] | 183 | /* Tries to copy the whole buffer <buf> into the channel's buffer after length |
| 184 | * controls. It will only succeed if the target buffer is empty, in which case |
| 185 | * it will simply swap the buffers. The buffer not attached to the channel is |
| 186 | * returned so that the caller can store it locally. The chn->buf->o and |
| 187 | * to_forward pointers are updated. If the output buffer is a dummy buffer or |
| 188 | * if it still contains data <buf> is returned, indicating that nothing could |
| 189 | * be done. Channel flag READ_PARTIAL is updated if some data can be transferred. |
| 190 | * The chunk's length is updated with the number of bytes sent. On errors, NULL |
| 191 | * is returned. Note that only buf->i is considered. |
| 192 | */ |
| 193 | struct buffer *bi_swpbuf(struct channel *chn, struct buffer *buf) |
| 194 | { |
| 195 | struct buffer *old; |
| 196 | |
| 197 | if (unlikely(channel_input_closed(chn))) |
| 198 | return NULL; |
| 199 | |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 200 | if (!chn->buf->size || !buffer_empty(chn->buf)) |
Willy Tarreau | b034b25 | 2014-12-08 18:14:53 +0100 | [diff] [blame] | 201 | return buf; |
Willy Tarreau | b034b25 | 2014-12-08 18:14:53 +0100 | [diff] [blame] | 202 | |
| 203 | old = chn->buf; |
| 204 | chn->buf = buf; |
| 205 | |
| 206 | if (!buf->i) |
| 207 | return old; |
| 208 | |
| 209 | chn->total += buf->i; |
| 210 | |
| 211 | if (chn->to_forward) { |
| 212 | unsigned long fwd = buf->i; |
| 213 | if (chn->to_forward != CHN_INFINITE_FORWARD) { |
| 214 | if (fwd > chn->to_forward) |
| 215 | fwd = chn->to_forward; |
| 216 | chn->to_forward -= fwd; |
| 217 | } |
| 218 | b_adv(chn->buf, fwd); |
| 219 | } |
| 220 | |
| 221 | /* notify that some data was read from the SI into the buffer */ |
| 222 | chn->flags |= CF_READ_PARTIAL; |
| 223 | return old; |
| 224 | } |
| 225 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 226 | /* 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] | 227 | * Return values : |
| 228 | * >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] | 229 | * =0 : no '\n' before end found. <str> is left undefined. |
| 230 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 231 | * The channel status is not changed. The caller must call bo_skip() to |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 232 | * update it. The '\n' is waited for as long as neither the buffer nor the |
| 233 | * output are full. If either of them is full, the string may be returned |
| 234 | * as is, without the '\n'. |
| 235 | */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 236 | int bo_getline(struct channel *chn, char *str, int len) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 237 | { |
| 238 | int ret, max; |
| 239 | char *p; |
| 240 | |
| 241 | ret = 0; |
| 242 | max = len; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 243 | |
| 244 | /* closed or empty + imminent close = -1; empty = 0 */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 245 | if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) { |
| 246 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 247 | ret = -1; |
| 248 | goto out; |
| 249 | } |
| 250 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 251 | p = bo_ptr(chn->buf); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 252 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 253 | if (max > chn->buf->o) { |
| 254 | max = chn->buf->o; |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 255 | str[max-1] = 0; |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 256 | } |
| 257 | while (max) { |
| 258 | *str++ = *p; |
| 259 | ret++; |
| 260 | max--; |
| 261 | |
| 262 | if (*p == '\n') |
| 263 | break; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 264 | p = buffer_wrap_add(chn->buf, p + 1); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 265 | } |
Willy Tarreau | 82de2b6 | 2013-12-10 18:58:23 +0100 | [diff] [blame] | 266 | if (ret > 0 && ret < len && |
Willy Tarreau | 3889fff | 2015-01-13 20:20:10 +0100 | [diff] [blame] | 267 | (ret < chn->buf->o || channel_may_recv(chn)) && |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 268 | *(str-1) != '\n' && |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 269 | !(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 270 | ret = 0; |
| 271 | out: |
| 272 | if (max) |
| 273 | *str = 0; |
| 274 | return ret; |
| 275 | } |
| 276 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 277 | /* Gets one full block of data at once from a channel's buffer, optionally from |
| 278 | * a specific offset. Return values : |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 279 | * >0 : number of bytes read, equal to requested size. |
| 280 | * =0 : not enough data available. <blk> is left undefined. |
| 281 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 282 | * The channel status is not changed. The caller must call bo_skip() to |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 283 | * update it. |
| 284 | */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 285 | int bo_getblk(struct channel *chn, char *blk, int len, int offset) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 286 | { |
| 287 | int firstblock; |
| 288 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 289 | if (chn->flags & CF_SHUTW) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 290 | return -1; |
| 291 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 292 | if (len + offset > chn->buf->o) { |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 293 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 294 | return -1; |
| 295 | return 0; |
| 296 | } |
| 297 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 298 | firstblock = chn->buf->data + chn->buf->size - bo_ptr(chn->buf); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 299 | if (firstblock > offset) { |
| 300 | if (firstblock >= len + offset) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 301 | memcpy(blk, bo_ptr(chn->buf) + offset, len); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 302 | return len; |
| 303 | } |
| 304 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 305 | memcpy(blk, bo_ptr(chn->buf) + offset, firstblock - offset); |
| 306 | memcpy(blk + firstblock - offset, chn->buf->data, len - firstblock + offset); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 307 | return len; |
| 308 | } |
| 309 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 310 | memcpy(blk, chn->buf->data + offset - firstblock, len); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 311 | return len; |
| 312 | } |
| 313 | |
Thierry FOURNIER | ca16b03 | 2015-02-16 19:26:48 +0100 | [diff] [blame] | 314 | /* Gets one or two blocks of data at once from a channel's output buffer. |
| 315 | * Return values : |
| 316 | * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2. |
| 317 | * =0 : not enough data available. <blk*> are left undefined. |
| 318 | * <0 : no more bytes readable because output is shut. |
| 319 | * The channel status is not changed. The caller must call bo_skip() to |
| 320 | * update it. Unused buffers are left in an undefined state. |
| 321 | */ |
| 322 | int bo_getblk_nc(struct channel *chn, char **blk1, int *len1, char **blk2, int *len2) |
| 323 | { |
| 324 | if (unlikely(chn->buf->o == 0)) { |
| 325 | if (chn->flags & CF_SHUTW) |
| 326 | return -1; |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | if (unlikely(chn->buf->p - chn->buf->o < chn->buf->data)) { |
| 331 | *blk1 = chn->buf->p - chn->buf->o + chn->buf->size; |
| 332 | *len1 = chn->buf->data + chn->buf->size - *blk1; |
| 333 | *blk2 = chn->buf->data; |
| 334 | *len2 = chn->buf->p - chn->buf->data; |
| 335 | return 2; |
| 336 | } |
| 337 | |
| 338 | *blk1 = chn->buf->p - chn->buf->o; |
| 339 | *len1 = chn->buf->o; |
| 340 | return 1; |
| 341 | } |
| 342 | |
| 343 | /* Gets one text line out of a channel's output buffer from a stream interface. |
| 344 | * Return values : |
| 345 | * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2. |
| 346 | * =0 : not enough data available. |
| 347 | * <0 : no more bytes readable because output is shut. |
| 348 | * The '\n' is waited for as long as neither the buffer nor the output are |
| 349 | * full. If either of them is full, the string may be returned as is, without |
| 350 | * the '\n'. Unused buffers are left in an undefined state. |
| 351 | */ |
| 352 | int bo_getline_nc(struct channel *chn, |
| 353 | char **blk1, int *len1, |
| 354 | char **blk2, int *len2) |
| 355 | { |
| 356 | int retcode; |
| 357 | int l; |
| 358 | |
| 359 | retcode = bo_getblk_nc(chn, blk1, len1, blk2, len2); |
| 360 | if (unlikely(retcode) <= 0) |
| 361 | return retcode; |
| 362 | |
| 363 | for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++); |
| 364 | if (l < *len1 && (*blk1)[l] == '\n') { |
| 365 | *len1 = l + 1; |
| 366 | return 1; |
| 367 | } |
| 368 | |
| 369 | if (retcode >= 2) { |
| 370 | for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++); |
| 371 | if (l < *len2 && (*blk2)[l] == '\n') { |
| 372 | *len2 = l + 1; |
| 373 | return 2; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | if (chn->flags & CF_SHUTW) { |
| 378 | /* If we have found no LF and the buffer is shut, then |
| 379 | * the resulting string is made of the concatenation of |
| 380 | * the pending blocks (1 or 2). |
| 381 | */ |
| 382 | return retcode; |
| 383 | } |
| 384 | |
| 385 | /* No LF yet and not shut yet */ |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | /* Gets one full block of data at once from a channel's input buffer. |
| 390 | * This function can return the data slitted in one or two blocks. |
| 391 | * Return values : |
| 392 | * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2. |
| 393 | * =0 : not enough data available. |
| 394 | * <0 : no more bytes readable because input is shut. |
| 395 | */ |
| 396 | int bi_getblk_nc(struct channel *chn, |
| 397 | char **blk1, int *len1, |
| 398 | char **blk2, int *len2) |
| 399 | { |
| 400 | if (unlikely(chn->buf->i == 0)) { |
| 401 | if (chn->flags & CF_SHUTR) |
| 402 | return -1; |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | if (unlikely(chn->buf->p + chn->buf->i > chn->buf->data + chn->buf->size)) { |
| 407 | *blk1 = chn->buf->p; |
| 408 | *len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
| 409 | *blk2 = chn->buf->data; |
| 410 | *len2 = chn->buf->i - *len1; |
| 411 | return 2; |
| 412 | } |
| 413 | |
| 414 | *blk1 = chn->buf->p; |
| 415 | *len1 = chn->buf->i; |
| 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | /* Gets one text line out of a channel's input buffer from a stream interface. |
| 420 | * Return values : |
| 421 | * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2. |
| 422 | * =0 : not enough data available. |
| 423 | * <0 : no more bytes readable because output is shut. |
| 424 | * The '\n' is waited for as long as neither the buffer nor the input are |
| 425 | * full. If either of them is full, the string may be returned as is, without |
| 426 | * the '\n'. Unused buffers are left in an undefined state. |
| 427 | */ |
| 428 | int bi_getline_nc(struct channel *chn, |
| 429 | char **blk1, int *len1, |
| 430 | char **blk2, int *len2) |
| 431 | { |
| 432 | int retcode; |
| 433 | int l; |
| 434 | |
| 435 | retcode = bi_getblk_nc(chn, blk1, len1, blk2, len2); |
| 436 | if (unlikely(retcode) <= 0) |
| 437 | return retcode; |
| 438 | |
| 439 | for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++); |
| 440 | if (l < *len1 && (*blk1)[l] == '\n') { |
| 441 | *len1 = l + 1; |
| 442 | return 1; |
| 443 | } |
| 444 | |
| 445 | if (retcode >= 2) { |
| 446 | for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++); |
| 447 | if (l < *len2 && (*blk2)[l] == '\n') { |
| 448 | *len2 = l + 1; |
| 449 | return 2; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | if (chn->flags & CF_SHUTW) { |
| 454 | /* If we have found no LF and the buffer is shut, then |
| 455 | * the resulting string is made of the concatenation of |
| 456 | * the pending blocks (1 or 2). |
| 457 | */ |
| 458 | return retcode; |
| 459 | } |
| 460 | |
| 461 | /* No LF yet and not shut yet */ |
| 462 | return 0; |
| 463 | } |
| 464 | |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 465 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 466 | * Local variables: |
| 467 | * c-indent-level: 8 |
| 468 | * c-basic-offset: 8 |
| 469 | * End: |
| 470 | */ |