blob: f9b5aa46bb5c3ab38658357a0a28755483006b4f [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Buffer management functions.
3 *
Willy Tarreau9bcc91e2009-10-10 18:01:44 +02004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 Oledzkiba8d7d32009-10-10 21:06:03 +020013#include <ctype.h>
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010014#include <stdarg.h>
15#include <stdio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <string.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020017
18#include <common/config.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020019#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020#include <proto/buffers.h>
Willy Tarreau27a674e2009-08-17 07:23:33 +020021#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022
Willy Tarreau7341d942007-05-13 19:56:02 +020023struct pool_head *pool2_buffer;
24
25
26/* perform minimal intializations, report 0 in case of error, 1 if OK. */
27int init_buffer()
28{
Willy Tarreau27a674e2009-08-17 07:23:33 +020029 pool2_buffer = create_pool("buffer", sizeof(struct buffer) + global.tune.bufsize, MEM_F_SHARED);
Willy Tarreau7341d942007-05-13 19:56:02 +020030 return pool2_buffer != NULL;
31}
32
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020034/* writes <len> bytes from message <msg> to buffer <buf>. Returns -1 in case of
Willy Tarreau078e2942009-08-18 07:19:39 +020035 * success, -2 if the message is larger than the buffer size, or the number of
36 * bytes available otherwise. The send limit is automatically adjusted with the
37 * amount of data written. FIXME-20060521: handle unaligned data.
Willy Tarreaubaaee002006-06-26 02:48:02 +020038 */
39int buffer_write(struct buffer *buf, const char *msg, int len)
40{
41 int max;
42
Willy Tarreauaeac3192009-08-31 08:09:57 +020043 if (len == 0)
44 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020045
Willy Tarreau078e2942009-08-18 07:19:39 +020046 if (len > buf->size) {
47 /* we can't write this chunk and will never be able to, because
48 * it is larger than the buffer. This must be reported as an
49 * error. Then we return -2 so that writers that don't care can
50 * ignore it and go on, and others can check for this value.
51 */
52 return -2;
53 }
54
Willy Tarreauaeac3192009-08-31 08:09:57 +020055 max = buffer_realign(buf);
56
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 if (len > max)
58 return max;
59
60 memcpy(buf->r, msg, len);
61 buf->l += len;
Willy Tarreauf890dc92008-12-13 21:12:26 +010062 buf->send_max += len;
Willy Tarreaubaaee002006-06-26 02:48:02 +020063 buf->r += len;
Willy Tarreau35d66b02007-01-02 00:28:21 +010064 buf->total += len;
Willy Tarreaua07a34e2009-08-16 23:27:46 +020065 if (buf->r == buf->data + buf->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +020066 buf->r = buf->data;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020067
Willy Tarreauba0b63d2009-09-20 08:09:44 +020068 buf->flags &= ~(BF_OUT_EMPTY|BF_FULL);
Willy Tarreau7c3c5412009-12-13 15:53:05 +010069 if (buf->l >= buffer_max_len(buf))
Willy Tarreaue393fe22008-08-16 22:18:07 +020070 buf->flags |= BF_FULL;
71
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020072 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020073}
74
Willy Tarreauaeac3192009-08-31 08:09:57 +020075/* Try to write string <str> into buffer <buf> after length controls. This
76 * is the equivalent of buffer_write() except that to_forward and send_max
77 * are updated and that max_len is respected. Returns -1 in case of success,
Willy Tarreau078e2942009-08-18 07:19:39 +020078 * -2 if it is larger than the buffer size, or the number of bytes available
Willy Tarreauaeac3192009-08-31 08:09:57 +020079 * otherwise. The send limit is automatically adjusted with the amount of data
Willy Tarreau078e2942009-08-18 07:19:39 +020080 * written.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010081 */
Willy Tarreau9bcc91e2009-10-10 18:01:44 +020082int buffer_feed2(struct buffer *buf, const char *str, int len)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010083{
84 int max;
85
Willy Tarreauaeac3192009-08-31 08:09:57 +020086 if (len == 0)
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020087 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010088
Willy Tarreau7c3c5412009-12-13 15:53:05 +010089 if (len > buffer_max_len(buf)) {
Willy Tarreau078e2942009-08-18 07:19:39 +020090 /* we can't write this chunk and will never be able to, because
Willy Tarreauaeac3192009-08-31 08:09:57 +020091 * it is larger than the buffer's current max size.
Willy Tarreau078e2942009-08-18 07:19:39 +020092 */
93 return -2;
94 }
95
Willy Tarreauaeac3192009-08-31 08:09:57 +020096 max = buffer_contig_space(buf);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010097
Willy Tarreauaeac3192009-08-31 08:09:57 +020098 if (len > max)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010099 return max;
100
Willy Tarreauaeac3192009-08-31 08:09:57 +0200101 memcpy(buf->r, str, len);
102 buf->l += len;
103 buf->r += len;
104 buf->total += len;
Willy Tarreau31971e52009-09-20 12:07:52 +0200105 if (buf->to_forward) {
106 unsigned long fwd = len;
107 if (buf->to_forward != BUF_INFINITE_FORWARD) {
108 if (fwd > buf->to_forward)
109 fwd = buf->to_forward;
110 buf->to_forward -= fwd;
111 }
112 buf->send_max += fwd;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200113 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200114 }
115
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200116 if (buf->r == buf->data + buf->size)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100117 buf->r = buf->data;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200118
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200119 buf->flags &= ~BF_FULL;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100120 if (buf->l >= buffer_max_len(buf))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200121 buf->flags |= BF_FULL;
122
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200123 /* notify that some data was read from the SI into the buffer */
124 buf->flags |= BF_READ_PARTIAL;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200125 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100126}
127
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200128/* Get one text line out of a buffer from a stream interface.
129 * Return values :
130 * >0 : number of bytes read. Includes the \n if present before len or end.
131 * =0 : no '\n' before end found. <buf> is undefined.
132 * <0 : no more bytes readable + shutdown set.
133 * The buffer status is not changed. The caller must call buffer_skip() to
134 * update it. The '\n' is waited for as long as neither the buffer nor the
135 * output are full. If either of them is full, the string may be returned
136 * as is, without the '\n'.
137 */
138int buffer_si_peekline(struct buffer *buf, char *str, int len)
139{
140 int ret, max;
141 char *p;
142
143 ret = 0;
144 max = len;
145 if (!buf->send_max) {
146 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
147 ret = -1;
148 goto out;
149 }
150
151 p = buf->w;
152
153 if (max > buf->send_max) {
154 max = buf->send_max;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200155 str[max-1] = 0;
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200156 }
157 while (max) {
158 *str++ = *p;
159 ret++;
160 max--;
161
162 if (*p == '\n')
163 break;
164 p++;
165 if (p == buf->data + buf->size)
166 p = buf->data;
167 }
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200168 if (ret > 0 && ret < len && ret < buf->send_max &&
169 *(str-1) != '\n' &&
170 !(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200171 ret = 0;
172 out:
173 if (max)
174 *str = 0;
175 return ret;
176}
177
Willy Tarreaubaaee002006-06-26 02:48:02 +0200178/*
179 * this function writes the string <str> at position <pos> which must be in buffer <b>,
180 * and moves <end> just after the end of <str>.
181 * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift.
182 * the shift value (positive or negative) is returned.
183 * If there's no space left, the move is not done.
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200184 * The function does not adjust ->send_max nor BF_OUT_EMPTY because it does not
185 * make sense to use it on data scheduled to be sent.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200186 *
187 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100188int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189{
190 int delta;
191 int len;
192
193 len = strlen(str);
194 delta = len - (end - pos);
195
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200196 if (delta + b->r >= b->data + b->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197 return 0; /* no space left */
198
199 /* first, protect the end of the buffer */
Willy Tarreau019fd5b2009-12-28 18:37:54 +0100200 memmove(end + delta, end, b->r - end);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200201
202 /* now, copy str over pos */
203 memcpy(pos, str,len);
204
205 /* we only move data after the displaced zone */
206 if (b->r > pos) b->r += delta;
207 if (b->w > pos) b->w += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200208 if (b->lr > pos) b->lr += delta;
209 b->l += delta;
210
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200211 b->flags &= ~BF_FULL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200212 if (b->l == 0)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200213 b->r = b->w = b->lr = b->data;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100214 if (b->l >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200215 b->flags |= BF_FULL;
216
Willy Tarreaubaaee002006-06-26 02:48:02 +0200217 return delta;
218}
219
220/*
221 * same except that the string length is given, which allows str to be NULL if
Willy Tarreauf890dc92008-12-13 21:12:26 +0100222 * len is 0. The send limit is *not* adjusted.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200223 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100224int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200225{
226 int delta;
227
228 delta = len - (end - pos);
229
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200230 if (delta + b->r >= b->data + b->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231 return 0; /* no space left */
232
233 if (b->data + b->l < end) {
234 /* The data has been stolen, we could have crashed.
235 * Maybe we should abort() ? */
236 return 0;
237 }
238
239 /* first, protect the end of the buffer */
Willy Tarreau019fd5b2009-12-28 18:37:54 +0100240 memmove(end + delta, end, b->r - end);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200241
242 /* now, copy str over pos */
243 if (len)
244 memcpy(pos, str, len);
245
246 /* we only move data after the displaced zone */
247 if (b->r > pos) b->r += delta;
248 if (b->w > pos) b->w += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200249 if (b->lr > pos) b->lr += delta;
250 b->l += delta;
251
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200252 b->flags &= ~BF_FULL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200253 if (b->l == 0)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200254 b->r = b->w = b->lr = b->data;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100255 if (b->l >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200256 b->flags |= BF_FULL;
257
Willy Tarreaubaaee002006-06-26 02:48:02 +0200258 return delta;
259}
260
261
262/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100263 * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
264 * argument informs about the length of string <str> so that we don't have to
265 * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
266 * is only opened for len+2 bytes but nothing is copied in. It may be useful in
Willy Tarreauf890dc92008-12-13 21:12:26 +0100267 * some circumstances. The send limit is *not* adjusted.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100268 *
269 * The number of bytes added is returned on success. 0 is returned on failure.
270 */
271int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
272{
273 int delta;
274
275 delta = len + 2;
276
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200277 if (delta + b->r >= b->data + b->size)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100278 return 0; /* no space left */
279
280 /* first, protect the end of the buffer */
Willy Tarreau019fd5b2009-12-28 18:37:54 +0100281 memmove(pos + delta, pos, b->r - pos);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100282
283 /* now, copy str over pos */
284 if (len && str) {
285 memcpy(pos, str, len);
286 pos[len] = '\r';
287 pos[len + 1] = '\n';
288 }
289
290 /* we only move data after the displaced zone */
291 if (b->r > pos) b->r += delta;
292 if (b->w > pos) b->w += delta;
293 if (b->lr > pos) b->lr += delta;
294 b->l += delta;
295
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200296 b->flags &= ~BF_FULL;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100297 if (b->l >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200298 b->flags |= BF_FULL;
299
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100300 return delta;
301}
302
303
304/*
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100305 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200306 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100307 * the new chunk size.
308 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200309int chunk_printf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100310{
311 va_list argp;
Willy Tarreaudceaa082007-07-25 14:38:45 +0200312 int ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100313
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200314 if (!chk->str || !chk->size)
315 return 0;
316
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100317 va_start(argp, fmt);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200318 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
319 if (ret >= chk->size - chk->len)
Willy Tarreaudceaa082007-07-25 14:38:45 +0200320 /* do not copy anything in case of truncation */
321 chk->str[chk->len] = 0;
322 else
323 chk->len += ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100324 va_end(argp);
325 return chk->len;
326}
327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100328/*
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200329 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
330 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
331 * If the chk->len is over, nothing is added. Returns the new chunk size.
332 */
333int chunk_htmlencode(struct chunk *dst, struct chunk *src) {
334
335 int i, l;
336 int olen, free;
337 char c;
338
339 olen = dst->len;
340
341 for (i = 0; i < src->len; i++) {
342 free = dst->size - dst->len;
343
344 if (!free) {
345 dst->len = olen;
346 return dst->len;
347 }
348
349 c = src->str[i];
350
351 if (!isascii(c) || !isprint(c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
352 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
353
354 if (free < l) {
355 dst->len = olen;
356 return dst->len;
357 }
358
359 dst->len += l;
360 } else {
361 dst->str[dst->len] = c;
362 dst->len++;
363 }
364 }
365
366 return dst->len;
367}
368
369/*
370 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
371 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
372 * If the chk->len is over, nothing is added. Returns the new chunk size.
373 */
374int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) {
375 int i, l;
376 int olen, free;
377 char c;
378
379 olen = dst->len;
380
381 for (i = 0; i < src->len; i++) {
382 free = dst->size - dst->len;
383
384 if (!free) {
385 dst->len = olen;
386 return dst->len;
387 }
388
389 c = src->str[i];
390
391 if (!isascii(c) || !isprint(c) || c == '<' || c == '>' || c == qc) {
392 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
393
394 if (free < l) {
395 dst->len = olen;
396 return dst->len;
397 }
398
399 dst->len += l;
400 } else {
401 dst->str[dst->len] = c;
402 dst->len++;
403 }
404 }
405
406 return dst->len;
407}
408
409/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100410 * Dumps part or all of a buffer.
411 */
412void buffer_dump(FILE *o, struct buffer *b, int from, int to)
413{
414 fprintf(o, "Dumping buffer %p\n", b);
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100415 fprintf(o, " data=%p l=%d r=%p w=%p lr=%p\n",
416 b->data, b->l, b->r, b->w, b->lr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100417
418 if (!to || to > b->l)
419 to = b->l;
420
421 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
422 for (; from < to; from++) {
423 if ((from & 15) == 0)
424 fprintf(o, " %04x: ", from);
425 fprintf(o, "%02x ", b->data[from]);
426 if ((from & 15) == 7)
427 fprintf(o, "- ");
428 else if (((from & 15) == 15) && (from != to-1))
429 fprintf(o, "\n");
430 }
431 fprintf(o, "\n--\n");
432}
433
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100434
435/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200436 * Local variables:
437 * c-indent-level: 8
438 * c-basic-offset: 8
439 * End:
440 */