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