Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Buffer management functions. |
| 3 | * |
Willy Tarreau | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 4 | * Copyright 2000-2010 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 | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 19 | #include <common/memory.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | #include <proto/buffers.h> |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 21 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 23 | struct pool_head *pool2_buffer; |
| 24 | |
| 25 | |
| 26 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 27 | int init_buffer() |
| 28 | { |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 29 | pool2_buffer = create_pool("buffer", sizeof(struct buffer) + global.tune.bufsize, MEM_F_SHARED); |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 30 | return pool2_buffer != NULL; |
| 31 | } |
| 32 | |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 33 | /* Schedule up to <bytes> more bytes to be forwarded by the buffer without notifying |
| 34 | * the task. Any pending data in the buffer is scheduled to be sent as well, |
| 35 | * in the limit of the number of bytes to forward. This must be the only method |
| 36 | * to use to schedule bytes to be sent. If the requested number is too large, it |
| 37 | * is automatically adjusted. The number of bytes taken into account is returned. |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 38 | * Directly touching ->to_forward will cause lockups when ->o goes down to |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 39 | * zero if nobody is ready to push the remaining data. |
| 40 | */ |
| 41 | unsigned long long buffer_forward(struct buffer *buf, unsigned long long bytes) |
| 42 | { |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 43 | unsigned int new_forward; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 44 | unsigned int forwarded; |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 45 | unsigned int bytes32; |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 46 | |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 47 | bytes32 = bytes; |
| 48 | |
| 49 | /* hint: avoid comparisons on long long for the fast case, since if the |
| 50 | * length does not fit in an unsigned it, it will never be forwarded at |
| 51 | * once anyway. |
| 52 | */ |
| 53 | if (bytes <= ~0U) { |
| 54 | if (bytes32 <= buf->i) { |
| 55 | /* OK this amount of bytes might be forwarded at once */ |
| 56 | if (!bytes32) |
| 57 | return 0; |
| 58 | b_adv(buf, bytes32); |
| 59 | return bytes; |
| 60 | } |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 63 | forwarded = buf->i; |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 64 | b_adv(buf, buf->i); |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 65 | |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 66 | /* Note: the case below is the only case where we may return |
| 67 | * a byte count that does not fit into a 32-bit number. |
| 68 | */ |
| 69 | if (likely(buf->to_forward == BUF_INFINITE_FORWARD)) |
| 70 | return bytes; |
| 71 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 72 | if (likely(bytes == BUF_INFINITE_FORWARD)) { |
| 73 | buf->to_forward = bytes; |
| 74 | return bytes; |
| 75 | } |
| 76 | |
| 77 | new_forward = buf->to_forward + bytes - forwarded; |
| 78 | bytes = forwarded; /* at least those bytes were scheduled */ |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 79 | |
| 80 | if (new_forward <= buf->to_forward) { |
| 81 | /* integer overflow detected, let's assume no more than 2G at once */ |
| 82 | new_forward = MID_RANGE(new_forward); |
| 83 | } |
| 84 | |
| 85 | if (new_forward > buf->to_forward) { |
| 86 | bytes += new_forward - buf->to_forward; |
| 87 | buf->to_forward = new_forward; |
| 88 | } |
| 89 | return bytes; |
| 90 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 92 | /* writes <len> bytes from message <msg> to buffer <buf>. Returns -1 in case of |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 93 | * success, -2 if the message is larger than the buffer size, or the number of |
| 94 | * bytes available otherwise. The send limit is automatically adjusted with the |
| 95 | * amount of data written. FIXME-20060521: handle unaligned data. |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 96 | * Note: this function appends data to the buffer's output and possibly overwrites |
| 97 | * any pending input data which are assumed not to exist. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 98 | */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 99 | int bo_inject(struct buffer *buf, const char *msg, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 100 | { |
| 101 | int max; |
| 102 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 103 | if (len == 0) |
| 104 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 106 | if (len > buf->size) { |
| 107 | /* we can't write this chunk and will never be able to, because |
| 108 | * it is larger than the buffer. This must be reported as an |
| 109 | * error. Then we return -2 so that writers that don't care can |
| 110 | * ignore it and go on, and others can check for this value. |
| 111 | */ |
| 112 | return -2; |
| 113 | } |
| 114 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 115 | max = buffer_realign(buf); |
| 116 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | if (len > max) |
| 118 | return max; |
| 119 | |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 120 | memcpy(buf->p, msg, len); |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 121 | buf->o += len; |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 122 | buf->p = b_ptr(buf, len); |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 123 | buf->total += len; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 124 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 125 | buf->flags &= ~(BF_OUT_EMPTY|BF_FULL); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 126 | if (bi_full(buf)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 127 | buf->flags |= BF_FULL; |
| 128 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 129 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 132 | /* Tries to copy character <c> into buffer <buf> after length controls. The |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 133 | * ->o and to_forward pointers are updated. If the buffer's input is |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 134 | * closed, -2 is returned. If there is not enough room left in the buffer, -1 |
| 135 | * is returned. Otherwise the number of bytes copied is returned (1). Buffer |
| 136 | * flags FULL, EMPTY and READ_PARTIAL are updated if some data can be |
| 137 | * transferred. |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 138 | */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 139 | int bi_putchr(struct buffer *buf, char c) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 140 | { |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 141 | if (unlikely(buffer_input_closed(buf))) |
| 142 | return -2; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 143 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 144 | if (buf->flags & BF_FULL) |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 145 | return -1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 146 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 147 | *bi_end(buf) = c; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 148 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 149 | buf->i++; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 150 | if (bi_full(buf)) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 151 | buf->flags |= BF_FULL; |
| 152 | buf->flags |= BF_READ_PARTIAL; |
| 153 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 154 | if (buf->to_forward >= 1) { |
| 155 | if (buf->to_forward != BUF_INFINITE_FORWARD) |
| 156 | buf->to_forward--; |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 157 | buf->o++; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 158 | buf->i--; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 159 | buf->flags &= ~BF_OUT_EMPTY; |
| 160 | } |
| 161 | |
| 162 | buf->total++; |
| 163 | return 1; |
| 164 | } |
| 165 | |
| 166 | /* Tries to copy block <blk> at once into buffer <buf> after length controls. |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 167 | * The ->o and to_forward pointers are updated. If the buffer's input is |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 168 | * closed, -2 is returned. If the block is too large for this buffer, -3 is |
| 169 | * returned. If there is not enough room left in the buffer, -1 is returned. |
| 170 | * Otherwise the number of bytes copied is returned (0 being a valid number). |
| 171 | * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be |
| 172 | * transferred. |
| 173 | */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 174 | int bi_putblk(struct buffer *buf, const char *blk, int len) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 175 | { |
| 176 | int max; |
| 177 | |
| 178 | if (unlikely(buffer_input_closed(buf))) |
| 179 | return -2; |
| 180 | |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 181 | max = buffer_max_len(buf); |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 182 | if (unlikely(len > max - buffer_len(buf))) { |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 183 | /* we can't write this chunk right now because the buffer is |
| 184 | * almost full or because the block is too large. Return the |
| 185 | * available space or -2 if impossible. |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 186 | */ |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 187 | if (len > max) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 188 | return -3; |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 189 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 190 | return -1; |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 191 | } |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 192 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 193 | if (unlikely(len == 0)) |
| 194 | return 0; |
| 195 | |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 196 | /* OK so the data fits in the buffer in one or two blocks */ |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 197 | max = buffer_contig_space_with_res(buf, buf->size - max); |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 198 | memcpy(bi_end(buf), blk, MIN(len, max)); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 199 | if (len > max) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 200 | memcpy(buf->data, blk + max, len - max); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 201 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 202 | buf->i += len; |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 203 | buf->total += len; |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 204 | if (buf->to_forward) { |
| 205 | unsigned long fwd = len; |
| 206 | if (buf->to_forward != BUF_INFINITE_FORWARD) { |
| 207 | if (fwd > buf->to_forward) |
| 208 | fwd = buf->to_forward; |
| 209 | buf->to_forward -= fwd; |
| 210 | } |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 211 | b_adv(buf, fwd); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 214 | buf->flags &= ~BF_FULL; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 215 | if (bi_full(buf)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 216 | buf->flags |= BF_FULL; |
| 217 | |
Willy Tarreau | fb0e920 | 2009-09-23 23:47:55 +0200 | [diff] [blame] | 218 | /* notify that some data was read from the SI into the buffer */ |
| 219 | buf->flags |= BF_READ_PARTIAL; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 220 | return len; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 223 | /* Gets one text line out of a buffer from a stream interface. |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 224 | * Return values : |
| 225 | * >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] | 226 | * =0 : no '\n' before end found. <str> is left undefined. |
| 227 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 228 | * The buffer status is not changed. The caller must call bo_skip() to |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 229 | * update it. The '\n' is waited for as long as neither the buffer nor the |
| 230 | * output are full. If either of them is full, the string may be returned |
| 231 | * as is, without the '\n'. |
| 232 | */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 233 | int bo_getline(struct buffer *buf, char *str, int len) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 234 | { |
| 235 | int ret, max; |
| 236 | char *p; |
| 237 | |
| 238 | ret = 0; |
| 239 | max = len; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 240 | |
| 241 | /* closed or empty + imminent close = -1; empty = 0 */ |
| 242 | if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) { |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 243 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) |
| 244 | ret = -1; |
| 245 | goto out; |
| 246 | } |
| 247 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 248 | p = bo_ptr(buf); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 249 | |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 250 | if (max > buf->o) { |
| 251 | max = buf->o; |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 252 | str[max-1] = 0; |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 253 | } |
| 254 | while (max) { |
| 255 | *str++ = *p; |
| 256 | ret++; |
| 257 | max--; |
| 258 | |
| 259 | if (*p == '\n') |
| 260 | break; |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 261 | p = buffer_wrap_add(buf, p + 1); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 262 | } |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 263 | if (ret > 0 && ret < len && ret < buf->o && |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 264 | *(str-1) != '\n' && |
| 265 | !(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 266 | ret = 0; |
| 267 | out: |
| 268 | if (max) |
| 269 | *str = 0; |
| 270 | return ret; |
| 271 | } |
| 272 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 273 | /* Gets one full block of data at once from a buffer, optionally from a |
| 274 | * specific offset. Return values : |
| 275 | * >0 : number of bytes read, equal to requested size. |
| 276 | * =0 : not enough data available. <blk> is left undefined. |
| 277 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 278 | * The buffer status is not changed. The caller must call bo_skip() to |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 279 | * update it. |
| 280 | */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 281 | int bo_getblk(struct buffer *buf, char *blk, int len, int offset) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 282 | { |
| 283 | int firstblock; |
| 284 | |
| 285 | if (buf->flags & BF_SHUTW) |
| 286 | return -1; |
| 287 | |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 288 | if (len + offset > buf->o) { |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 289 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) |
| 290 | return -1; |
| 291 | return 0; |
| 292 | } |
| 293 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 294 | firstblock = buf->data + buf->size - bo_ptr(buf); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 295 | if (firstblock > offset) { |
| 296 | if (firstblock >= len + offset) { |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 297 | memcpy(blk, bo_ptr(buf) + offset, len); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 298 | return len; |
| 299 | } |
| 300 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 301 | memcpy(blk, bo_ptr(buf) + offset, firstblock - offset); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 302 | memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset); |
| 303 | return len; |
| 304 | } |
| 305 | |
| 306 | memcpy(blk, buf->data + offset - firstblock, len); |
| 307 | return len; |
| 308 | } |
| 309 | |
Willy Tarreau | 19ae56b | 2011-11-28 10:36:13 +0100 | [diff] [blame] | 310 | /* This function writes the string <str> at position <pos> which must be in |
| 311 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 312 | * <l> and <r> are updated to be valid after the shift. The shift value |
Willy Tarreau | 19ae56b | 2011-11-28 10:36:13 +0100 | [diff] [blame] | 313 | * (positive or negative) is returned. If there's no space left, the move is |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 314 | * not done. The function does not adjust ->o nor BF_OUT_EMPTY because it |
| 315 | * does not make sense to use it on data scheduled to be sent. For the same |
| 316 | * reason, it does not make sense to call this function on unparsed data, so |
| 317 | * <orig> is not updated. The string length is taken from parameter <len>. If |
| 318 | * <len> is null, the <str> pointer is allowed to be null. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 319 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 320 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 321 | { |
| 322 | int delta; |
| 323 | |
| 324 | delta = len - (end - pos); |
| 325 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 326 | if (bi_end(b) + delta >= b->data + b->size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 327 | return 0; /* no space left */ |
| 328 | |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 329 | if (buffer_not_empty(b) && |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 330 | bi_end(b) + delta > bo_ptr(b) && |
| 331 | bo_ptr(b) >= bi_end(b)) |
Willy Tarreau | bbfa793 | 2010-01-25 01:49:57 +0100 | [diff] [blame] | 332 | return 0; /* no space left before wrapping data */ |
| 333 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 334 | /* first, protect the end of the buffer */ |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 335 | memmove(end + delta, end, bi_end(b) - end); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 336 | |
| 337 | /* now, copy str over pos */ |
| 338 | if (len) |
| 339 | memcpy(pos, str, len); |
| 340 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 341 | b->i += delta; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 342 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 343 | b->flags &= ~BF_FULL; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 344 | if (buffer_len(b) == 0) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 345 | b->p = b->data; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 346 | if (bi_full(b)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 347 | b->flags |= BF_FULL; |
| 348 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 349 | return delta; |
| 350 | } |
| 351 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 352 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 353 | * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len> |
| 354 | * argument informs about the length of string <str> so that we don't have to |
| 355 | * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer |
| 356 | * is only opened for len+2 bytes but nothing is copied in. It may be useful in |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 357 | * some circumstances. The send limit is *not* adjusted. Same comments as above |
| 358 | * for the valid use cases. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 359 | * |
| 360 | * The number of bytes added is returned on success. 0 is returned on failure. |
| 361 | */ |
| 362 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len) |
| 363 | { |
| 364 | int delta; |
| 365 | |
| 366 | delta = len + 2; |
| 367 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 368 | if (bi_end(b) + delta >= b->data + b->size) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 369 | return 0; /* no space left */ |
| 370 | |
| 371 | /* first, protect the end of the buffer */ |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 372 | memmove(pos + delta, pos, bi_end(b) - pos); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 373 | |
| 374 | /* now, copy str over pos */ |
| 375 | if (len && str) { |
| 376 | memcpy(pos, str, len); |
| 377 | pos[len] = '\r'; |
| 378 | pos[len + 1] = '\n'; |
| 379 | } |
| 380 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 381 | b->i += delta; |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 382 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 383 | b->flags &= ~BF_FULL; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 384 | if (bi_full(b)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 385 | b->flags |= BF_FULL; |
| 386 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 387 | return delta; |
| 388 | } |
| 389 | |
| 390 | |
Willy Tarreau | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 391 | /* Realigns a possibly non-contiguous buffer by bouncing bytes from source to |
| 392 | * destination. It does not use any intermediate buffer and does the move in |
| 393 | * place, though it will be slower than a simple memmove() on contiguous data, |
| 394 | * so it's desirable to use it only on non-contiguous buffers. No pointers are |
| 395 | * changed, the caller is responsible for that. |
| 396 | */ |
| 397 | void buffer_bounce_realign(struct buffer *buf) |
| 398 | { |
| 399 | int advance, to_move; |
| 400 | char *from, *to; |
| 401 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 402 | from = bo_ptr(buf); |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 403 | advance = buf->data + buf->size - from; |
Willy Tarreau | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 404 | if (!advance) |
| 405 | return; |
| 406 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 407 | to_move = buffer_len(buf); |
Willy Tarreau | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 408 | while (to_move) { |
| 409 | char last, save; |
| 410 | |
| 411 | last = *from; |
| 412 | to = from + advance; |
| 413 | if (to >= buf->data + buf->size) |
| 414 | to -= buf->size; |
| 415 | |
| 416 | while (1) { |
| 417 | save = *to; |
| 418 | *to = last; |
| 419 | last = save; |
| 420 | to_move--; |
| 421 | if (!to_move) |
| 422 | break; |
| 423 | |
| 424 | /* check if we went back home after rotating a number of bytes */ |
| 425 | if (to == from) |
| 426 | break; |
| 427 | |
| 428 | /* if we ended up in the empty area, let's walk to next place. The |
| 429 | * empty area is either between buf->r and from or before from or |
| 430 | * after buf->r. |
| 431 | */ |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 432 | if (from > bi_end(buf)) { |
| 433 | if (to >= bi_end(buf) && to < from) |
Willy Tarreau | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 434 | break; |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 435 | } else if (from < bi_end(buf)) { |
| 436 | if (to < from || to >= bi_end(buf)) |
Willy Tarreau | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | |
| 440 | /* we have overwritten a byte of the original set, let's move it */ |
| 441 | to += advance; |
| 442 | if (to >= buf->data + buf->size) |
| 443 | to -= buf->size; |
| 444 | } |
| 445 | |
| 446 | from++; |
| 447 | if (from >= buf->data + buf->size) |
| 448 | from -= buf->size; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 453 | /* |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 454 | * Does an snprintf() at the end of chunk <chk>, respecting the limit of |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 455 | * at most chk->size chars. If the chk->len is over, nothing is added. Returns |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 456 | * the new chunk size. |
| 457 | */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 458 | int chunk_printf(struct chunk *chk, const char *fmt, ...) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 459 | { |
| 460 | va_list argp; |
Willy Tarreau | dceaa08 | 2007-07-25 14:38:45 +0200 | [diff] [blame] | 461 | int ret; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 462 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 463 | if (!chk->str || !chk->size) |
| 464 | return 0; |
| 465 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 466 | va_start(argp, fmt); |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 467 | ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp); |
| 468 | if (ret >= chk->size - chk->len) |
Willy Tarreau | dceaa08 | 2007-07-25 14:38:45 +0200 | [diff] [blame] | 469 | /* do not copy anything in case of truncation */ |
| 470 | chk->str[chk->len] = 0; |
| 471 | else |
| 472 | chk->len += ret; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 473 | va_end(argp); |
| 474 | return chk->len; |
| 475 | } |
| 476 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 477 | /* |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 478 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 479 | * chk->size chars. Replace non-printable or special chracters with "&#%d;". |
| 480 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 481 | */ |
| 482 | int chunk_htmlencode(struct chunk *dst, struct chunk *src) { |
| 483 | |
| 484 | int i, l; |
| 485 | int olen, free; |
| 486 | char c; |
| 487 | |
| 488 | olen = dst->len; |
| 489 | |
| 490 | for (i = 0; i < src->len; i++) { |
| 491 | free = dst->size - dst->len; |
| 492 | |
| 493 | if (!free) { |
| 494 | dst->len = olen; |
| 495 | return dst->len; |
| 496 | } |
| 497 | |
| 498 | c = src->str[i]; |
| 499 | |
Willy Tarreau | 88e0581 | 2010-03-03 00:16:00 +0100 | [diff] [blame] | 500 | if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') { |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 501 | l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c); |
| 502 | |
| 503 | if (free < l) { |
| 504 | dst->len = olen; |
| 505 | return dst->len; |
| 506 | } |
| 507 | |
| 508 | dst->len += l; |
| 509 | } else { |
| 510 | dst->str[dst->len] = c; |
| 511 | dst->len++; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | return dst->len; |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 520 | * chk->size chars. Replace non-printable or char passed in qc with "<%02X>". |
| 521 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 522 | */ |
| 523 | int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) { |
| 524 | int i, l; |
| 525 | int olen, free; |
| 526 | char c; |
| 527 | |
| 528 | olen = dst->len; |
| 529 | |
| 530 | for (i = 0; i < src->len; i++) { |
| 531 | free = dst->size - dst->len; |
| 532 | |
| 533 | if (!free) { |
| 534 | dst->len = olen; |
| 535 | return dst->len; |
| 536 | } |
| 537 | |
| 538 | c = src->str[i]; |
| 539 | |
Willy Tarreau | 88e0581 | 2010-03-03 00:16:00 +0100 | [diff] [blame] | 540 | if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) { |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 541 | l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c); |
| 542 | |
| 543 | if (free < l) { |
| 544 | dst->len = olen; |
| 545 | return dst->len; |
| 546 | } |
| 547 | |
| 548 | dst->len += l; |
| 549 | } else { |
| 550 | dst->str[dst->len] = c; |
| 551 | dst->len++; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | return dst->len; |
| 556 | } |
| 557 | |
| 558 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 559 | * Dumps part or all of a buffer. |
| 560 | */ |
| 561 | void buffer_dump(FILE *o, struct buffer *b, int from, int to) |
| 562 | { |
| 563 | fprintf(o, "Dumping buffer %p\n", b); |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 564 | fprintf(o, " data=%p o=%d i=%d p=%p\n", |
| 565 | b->data, b->o, b->i, b->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 566 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 567 | if (!to || to > buffer_len(b)) |
| 568 | to = buffer_len(b); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 569 | |
| 570 | fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to); |
| 571 | for (; from < to; from++) { |
| 572 | if ((from & 15) == 0) |
| 573 | fprintf(o, " %04x: ", from); |
| 574 | fprintf(o, "%02x ", b->data[from]); |
| 575 | if ((from & 15) == 7) |
| 576 | fprintf(o, "- "); |
| 577 | else if (((from & 15) == 15) && (from != to-1)) |
| 578 | fprintf(o, "\n"); |
| 579 | } |
| 580 | fprintf(o, "\n--\n"); |
| 581 | } |
| 582 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 583 | |
| 584 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 585 | * Local variables: |
| 586 | * c-indent-level: 8 |
| 587 | * c-basic-offset: 8 |
| 588 | * End: |
| 589 | */ |