blob: af470baa95431cce3c71a464ae27c0b871dcd7a1 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau7c3c5412009-12-13 15:53:05 +01002 * include/proto/buffers.h
3 * Buffer management definitions, macros and inline functions.
4 *
Willy Tarreaub97f1992010-02-25 23:54:31 +01005 * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
Willy Tarreau7c3c5412009-12-13 15:53:05 +01006 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#ifndef _PROTO_BUFFERS_H
23#define _PROTO_BUFFERS_H
24
Willy Tarreau7341d942007-05-13 19:56:02 +020025#include <stdio.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010026#include <stdlib.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020027#include <string.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010028
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020029#include <common/config.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020030#include <common/memory.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020031#include <common/ticks.h>
Willy Tarreaufa645582007-06-03 15:59:52 +020032#include <common/time.h>
33
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <types/buffers.h>
Willy Tarreau7c3c5412009-12-13 15:53:05 +010035#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
Willy Tarreau7341d942007-05-13 19:56:02 +020037extern struct pool_head *pool2_buffer;
38
39/* perform minimal intializations, report 0 in case of error, 1 if OK. */
40int init_buffer();
41
Willy Tarreau74b08c92010-09-08 17:04:31 +020042/* SI-to-buffer functions : buffer_{get,put}_{char,block,string,chunk} */
43int buffer_write(struct buffer *buf, const char *msg, int len);
44int buffer_put_block(struct buffer *buf, const char *str, int len);
45int buffer_put_char(struct buffer *buf, char c);
46int buffer_get_line(struct buffer *buf, char *str, int len);
47int buffer_get_block(struct buffer *buf, char *blk, int len, int offset);
48int buffer_replace(struct buffer *b, char *pos, char *end, const char *str);
49int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
50int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
51void buffer_dump(FILE *o, struct buffer *b, int from, int to);
52void buffer_bounce_realign(struct buffer *buf);
Willy Tarreau0bc34932011-03-28 16:25:58 +020053unsigned long long buffer_forward(struct buffer *buf, unsigned long long bytes);
Willy Tarreau74b08c92010-09-08 17:04:31 +020054
Willy Tarreau7c3c5412009-12-13 15:53:05 +010055/* Initialize all fields in the buffer. The BF_OUT_EMPTY flags is set. */
Willy Tarreau54469402006-07-29 16:59:06 +020056static inline void buffer_init(struct buffer *buf)
57{
Willy Tarreauf890dc92008-12-13 21:12:26 +010058 buf->send_max = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +010059 buf->to_forward = 0;
Willy Tarreaue393fe22008-08-16 22:18:07 +020060 buf->l = buf->total = 0;
Willy Tarreau3eba98a2009-01-25 13:56:13 +010061 buf->pipe = NULL;
Willy Tarreau2df28e82008-08-17 15:20:19 +020062 buf->analysers = 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +020063 buf->cons = NULL;
Willy Tarreauba0b63d2009-09-20 08:09:44 +020064 buf->flags = BF_OUT_EMPTY;
Willy Tarreau2df28e82008-08-17 15:20:19 +020065 buf->r = buf->lr = buf->w = buf->data;
Willy Tarreau54469402006-07-29 16:59:06 +020066}
67
Willy Tarreau7c3c5412009-12-13 15:53:05 +010068/* Return the max number of bytes the buffer can contain so that once all the
69 * pending bytes are forwarded, the buffer still has global.tune.maxrewrite
70 * bytes free. The result sits between buf->size - maxrewrite and buf->size.
71 */
72static inline int buffer_max_len(struct buffer *buf)
73{
74 if (buf->to_forward == BUF_INFINITE_FORWARD ||
75 buf->to_forward + buf->send_max >= global.tune.maxrewrite)
76 return buf->size;
77 else
78 return buf->size - global.tune.maxrewrite + buf->to_forward + buf->send_max;
79}
80
Willy Tarreau74b08c92010-09-08 17:04:31 +020081/* Returns true if the buffer's input is already closed */
82static inline int buffer_input_closed(struct buffer *buf)
83{
84 return ((buf->flags & BF_SHUTR) != 0);
85}
86
87/* Returns true if the buffer's output is already closed */
88static inline int buffer_output_closed(struct buffer *buf)
89{
90 return ((buf->flags & BF_SHUTW) != 0);
91}
92
Willy Tarreau2eb52f02008-09-04 09:14:08 +020093/* Check buffer timeouts, and set the corresponding flags. The
94 * likely/unlikely have been optimized for fastest normal path.
Willy Tarreaudd80c6f2008-12-13 22:25:59 +010095 * The read/write timeouts are not set if there was activity on the buffer.
96 * That way, we don't have to update the timeout on every I/O. Note that the
97 * analyser timeout is always checked.
Willy Tarreau2eb52f02008-09-04 09:14:08 +020098 */
99static inline void buffer_check_timeouts(struct buffer *b)
100{
Willy Tarreau86491c32008-12-14 09:04:47 +0100101 if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200102 unlikely(tick_is_expired(b->rex, now_ms)))
103 b->flags |= BF_READ_TIMEOUT;
104
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100105 if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200106 unlikely(tick_is_expired(b->wex, now_ms)))
107 b->flags |= BF_WRITE_TIMEOUT;
108
109 if (likely(!(b->flags & BF_ANA_TIMEOUT)) &&
110 unlikely(tick_is_expired(b->analyse_exp, now_ms)))
111 b->flags |= BF_ANA_TIMEOUT;
112}
113
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100114/* Schedule all remaining buffer data to be sent. send_max is not touched if it
115 * already covers those data. That permits doing a flush even after a forward,
116 * although not recommended.
117 */
118static inline void buffer_flush(struct buffer *buf)
119{
120 if (buf->send_max < buf->l)
121 buf->send_max = buf->l;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200122 if (buf->send_max)
123 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100124}
125
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100126/* Erase any content from buffer <buf> and adjusts flags accordingly. Note
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100127 * that any spliced data is not affected since we may not have any access to
128 * it.
Willy Tarreaue393fe22008-08-16 22:18:07 +0200129 */
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100130static inline void buffer_erase(struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131{
Willy Tarreauf890dc92008-12-13 21:12:26 +0100132 buf->send_max = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100133 buf->to_forward = 0;
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100134 buf->r = buf->lr = buf->w = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200135 buf->l = 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200136 buf->flags &= ~(BF_FULL | BF_OUT_EMPTY);
137 if (!buf->pipe)
138 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139}
140
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200141/* Cut the "tail" of the buffer, which means strip it to the length of unsent
142 * data only, and kill any remaining unsent data. Any scheduled forwarding is
143 * stopped. This is mainly to be used to send error messages after existing
144 * data.
145 */
146static inline void buffer_cut_tail(struct buffer *buf)
147{
148 if (!buf->send_max)
149 return buffer_erase(buf);
150
151 buf->to_forward = 0;
152 if (buf->l == buf->send_max)
153 return;
154
155 buf->l = buf->send_max;
156 buf->r = buf->w + buf->l;
157 if (buf->r >= buf->data + buf->size)
158 buf->r -= buf->size;
159 buf->lr = buf->r;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200160 buf->flags &= ~BF_FULL;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100161 if (buf->l >= buffer_max_len(buf))
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200162 buf->flags |= BF_FULL;
163}
164
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100165/* Cut the <n> next unsent bytes of the buffer. The caller must ensure that <n>
166 * is smaller than the actual buffer's length. This is mainly used to remove
167 * empty lines at the beginning of a request or a response.
168 */
169static inline void buffer_ignore(struct buffer *buf, int n)
170{
171 buf->l -= n;
172 buf->w += n;
173 if (buf->w >= buf->data + buf->size)
174 buf->w -= buf->size;
175 buf->flags &= ~BF_FULL;
176 if (buf->l >= buffer_max_len(buf))
177 buf->flags |= BF_FULL;
178}
179
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200180/* marks the buffer as "shutdown" ASAP for reads */
181static inline void buffer_shutr_now(struct buffer *buf)
182{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100183 buf->flags |= BF_SHUTR_NOW;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200184}
185
186/* marks the buffer as "shutdown" ASAP for writes */
187static inline void buffer_shutw_now(struct buffer *buf)
188{
189 buf->flags |= BF_SHUTW_NOW;
190}
191
192/* marks the buffer as "shutdown" ASAP in both directions */
193static inline void buffer_abort(struct buffer *buf)
194{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100195 buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW;
Willy Tarreaue4599762010-03-21 23:25:09 +0100196 buf->flags &= ~BF_AUTO_CONNECT;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200197}
198
Willy Tarreau01bf8672008-12-07 18:03:29 +0100199/* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack
200 * flag is set, and the function called once. The function is responsible for
201 * clearing the hijack bit. It is possible that the function clears the flag
202 * during this first call.
203 */
204static inline void buffer_install_hijacker(struct session *s,
205 struct buffer *b,
206 void (*func)(struct session *, struct buffer *))
Willy Tarreau72b179a2008-08-28 16:01:32 +0200207{
Willy Tarreau01bf8672008-12-07 18:03:29 +0100208 b->hijacker = func;
209 b->flags |= BF_HIJACK;
210 func(s, b);
Willy Tarreau72b179a2008-08-28 16:01:32 +0200211}
212
Willy Tarreau01bf8672008-12-07 18:03:29 +0100213/* Releases the buffer from hijacking mode. Often used by the hijack function */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200214static inline void buffer_stop_hijack(struct buffer *buf)
215{
216 buf->flags &= ~BF_HIJACK;
217}
218
Willy Tarreau520d95e2009-09-19 21:04:57 +0200219/* allow the consumer to try to establish a new connection. */
220static inline void buffer_auto_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200221{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200222 buf->flags |= BF_AUTO_CONNECT;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200223}
224
Willy Tarreau520d95e2009-09-19 21:04:57 +0200225/* prevent the consumer from trying to establish a new connection, and also
226 * disable auto shutdown forwarding.
227 */
228static inline void buffer_dont_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200229{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200230 buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200231}
232
Willy Tarreau520d95e2009-09-19 21:04:57 +0200233/* allow the producer to forward shutdown requests */
234static inline void buffer_auto_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100235{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200236 buf->flags |= BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100237}
238
Willy Tarreau520d95e2009-09-19 21:04:57 +0200239/* prevent the producer from forwarding shutdown requests */
240static inline void buffer_dont_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100241{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200242 buf->flags &= ~BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100243}
244
Willy Tarreau90deb182010-01-07 00:20:41 +0100245/* allow the producer to read / poll the input */
246static inline void buffer_auto_read(struct buffer *buf)
247{
248 buf->flags &= ~BF_DONT_READ;
249}
250
251/* prevent the producer from read / poll the input */
252static inline void buffer_dont_read(struct buffer *buf)
253{
254 buf->flags |= BF_DONT_READ;
255}
256
Willy Tarreaubaaee002006-06-26 02:48:02 +0200257/* returns the maximum number of bytes writable at once in this buffer */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200258static inline int buffer_max(const struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200259{
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200260 if (buf->l == buf->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200261 return 0;
262 else if (buf->r >= buf->w)
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200263 return buf->data + buf->size - buf->r;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200264 else
265 return buf->w - buf->r;
266}
267
Willy Tarreaubaaee002006-06-26 02:48:02 +0200268/*
269 * Tries to realign the given buffer, and returns how many bytes can be written
270 * there at once without overwriting anything.
271 */
272static inline int buffer_realign(struct buffer *buf)
273{
274 if (buf->l == 0) {
275 /* let's realign the buffer to optimize I/O */
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100276 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200277 }
278 return buffer_max(buf);
279}
280
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200281/*
Willy Tarreau591fedc2010-08-10 15:28:21 +0200282 * Return the maximum amount of bytes that can be written into the buffer in
Willy Tarreau74b08c92010-09-08 17:04:31 +0200283 * one call to buffer_put_*().
Willy Tarreau591fedc2010-08-10 15:28:21 +0200284 */
285static inline int buffer_free_space(struct buffer *buf)
286{
287 return buffer_max_len(buf) - buf->l;
288}
289
290/*
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200291 * Return the max amount of bytes that can be stuffed into the buffer at once.
292 * Note that this may be lower than the actual buffer size when the free space
293 * wraps after the end, so it's preferable to call this function again after
294 * writing. Also note that this function respects max_len.
295 */
296static inline int buffer_contig_space(struct buffer *buf)
297{
298 int ret;
299
300 if (buf->l == 0) {
301 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100302 ret = buffer_max_len(buf);
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200303 }
304 else if (buf->r > buf->w) {
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100305 ret = buf->data + buffer_max_len(buf) - buf->r;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200306 }
307 else {
308 ret = buf->w - buf->r;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100309 if (ret > buffer_max_len(buf))
310 ret = buffer_max_len(buf);
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200311 }
312 return ret;
313}
314
Willy Tarreau591fedc2010-08-10 15:28:21 +0200315/*
316 * Same as above but the caller may pass the value of buffer_max_len(buf) if it
317 * knows it, thus avoiding some calculations.
318 */
319static inline int buffer_contig_space_with_len(struct buffer *buf, int maxlen)
320{
321 int ret;
322
323 if (buf->l == 0) {
324 buf->r = buf->w = buf->lr = buf->data;
325 ret = maxlen;
326 }
327 else if (buf->r > buf->w) {
328 ret = buf->data + maxlen - buf->r;
329 }
330 else {
331 ret = buf->w - buf->r;
332 if (ret > maxlen)
333 ret = maxlen;
334 }
335 return ret;
336}
337
Willy Tarreau4e33d862009-10-11 23:35:10 +0200338/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
339static inline int buffer_almost_full(struct buffer *buf)
340{
Willy Tarreau591fedc2010-08-10 15:28:21 +0200341 if (buffer_free_space(buf) < buf->size / 4)
Willy Tarreau4e33d862009-10-11 23:35:10 +0200342 return 1;
343 return 0;
344}
345
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200346/*
347 * Return the max amount of bytes that can be read from the buffer at once.
348 * Note that this may be lower than the actual buffer length when the data
349 * wrap after the end, so it's preferable to call this function again after
350 * reading. Also note that this function respects the send_max limit.
351 */
352static inline int buffer_contig_data(struct buffer *buf)
353{
354 int ret;
355
356 if (!buf->send_max || !buf->l)
357 return 0;
358
359 if (buf->r > buf->w)
360 ret = buf->r - buf->w;
361 else
362 ret = buf->data + buf->size - buf->w;
363
364 /* limit the amount of outgoing data if required */
365 if (ret > buf->send_max)
366 ret = buf->send_max;
367
368 return ret;
369}
370
371/*
372 * Advance the buffer's read pointer by <len> bytes. This is useful when data
373 * have been read directly from the buffer. It is illegal to call this function
374 * with <len> causing a wrapping at the end of the buffer. It's the caller's
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200375 * responsibility to ensure that <len> is never larger than buf->send_max.
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200376 */
377static inline void buffer_skip(struct buffer *buf, int len)
378{
379 buf->w += len;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200380 if (buf->w >= buf->data + buf->size)
381 buf->w -= buf->size; /* wrap around the buffer */
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200382
383 buf->l -= len;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200384 if (!buf->l)
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200385 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200386
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100387 if (buf->l < buffer_max_len(buf))
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200388 buf->flags &= ~BF_FULL;
389
390 buf->send_max -= len;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200391 if (!buf->send_max && !buf->pipe)
392 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200393
394 /* notify that some data was written to the SI from the buffer */
395 buf->flags |= BF_WRITE_PARTIAL;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200396}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200397
Willy Tarreauaeac3192009-08-31 08:09:57 +0200398/* writes the chunk <chunk> to buffer <buf>. Returns -1 in case of success,
399 * -2 if it is larger than the buffer size, or the number of bytes available
400 * otherwise. If the chunk has been written, its size is automatically reset
401 * to zero. The send limit is automatically adjusted with the amount of data
402 * written.
403 */
404static inline int buffer_write_chunk(struct buffer *buf, struct chunk *chunk)
405{
406 int ret;
407
408 ret = buffer_write(buf, chunk->str, chunk->len);
409 if (ret == -1)
410 chunk->len = 0;
411 return ret;
412}
413
Willy Tarreau74b08c92010-09-08 17:04:31 +0200414/* Tries to copy chunk <chunk> into buffer <buf> after length controls.
415 * The send_max and to_forward pointers are updated. If the buffer's input is
416 * closed, -2 is returned. If the block is too large for this buffer, -3 is
417 * returned. If there is not enough room left in the buffer, -1 is returned.
418 * Otherwise the number of bytes copied is returned (0 being a valid number).
419 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
420 * transferred. The chunk's length is updated with the number of bytes sent.
Willy Tarreauaeac3192009-08-31 08:09:57 +0200421 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200422static inline int buffer_put_chunk(struct buffer *buf, struct chunk *chunk)
Willy Tarreauaeac3192009-08-31 08:09:57 +0200423{
424 int ret;
425
Willy Tarreau74b08c92010-09-08 17:04:31 +0200426 ret = buffer_put_block(buf, chunk->str, chunk->len);
427 if (ret > 0)
428 chunk->len -= ret;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200429 return ret;
430}
431
Willy Tarreau74b08c92010-09-08 17:04:31 +0200432/* Tries to copy string <str> at once into buffer <buf> after length controls.
433 * The send_max and to_forward pointers are updated. If the buffer's input is
434 * closed, -2 is returned. If the block is too large for this buffer, -3 is
435 * returned. If there is not enough room left in the buffer, -1 is returned.
436 * Otherwise the number of bytes copied is returned (0 being a valid number).
437 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
438 * transferred.
439 */
440static inline int buffer_put_string(struct buffer *buf, const char *str)
441{
442 return buffer_put_block(buf, str, strlen(str));
443}
444
445/*
446 * Return one char from the buffer. If the buffer is empty and closed, return -2.
447 * If the buffer is just empty, return -1. The buffer's pointer is not advanced,
448 * it's up to the caller to call buffer_skip(buf, 1) when it has consumed the char.
449 * Also note that this function respects the send_max limit.
450 */
451static inline int buffer_get_char(struct buffer *buf)
452{
453 /* closed or empty + imminent close = -2; empty = -1 */
454 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
455 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
456 return -2;
457 return -1;
458 }
459 return *buf->w;
460}
461
462
463/* DEPRECATED, just provided for compatibility, use buffer_put_chunk() instead !!!
464 * returns >= 0 if the buffer is too small to hold the message, -1 if the
465 * transfer was OK, -2 in case of failure.
466 */
467static inline int buffer_feed_chunk(struct buffer *buf, struct chunk *msg)
468{
469 int ret = buffer_put_chunk(buf, msg);
470 if (ret >= 0) /* transfer OK */
471 return -1;
472 if (ret == -1) /* missing room */
473 return 1;
474 /* failure */
475 return -2;
476}
477
478/* DEPRECATED, just provided for compatibility, use buffer_put_string() instead !!!
479 * returns >= 0 if the buffer is too small to hold the message, -1 if the
480 * transfer was OK, -2 in case of failure.
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200481 */
482static inline int buffer_feed(struct buffer *buf, const char *str)
483{
Willy Tarreau74b08c92010-09-08 17:04:31 +0200484 int ret = buffer_put_string(buf, str);
485 if (ret >= 0) /* transfer OK */
486 return -1;
487 if (ret == -1) /* missing room */
488 return 1;
489 /* failure */
490 return -2;
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200491}
492
Willy Tarreau74b08c92010-09-08 17:04:31 +0200493/*
494 *
495 * Functions below are used to manage chunks
496 *
497 */
498
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200499static inline void chunk_init(struct chunk *chk, char *str, size_t size) {
500 chk->str = str;
501 chk->len = 0;
502 chk->size = size;
503}
504
505/* report 0 in case of error, 1 if OK. */
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200506static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200507
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200508 if (size && len > size)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200509 return 0;
510
511 chk->str = str;
512 chk->len = len;
513 chk->size = size;
514
515 return 1;
516}
517
518static inline void chunk_initstr(struct chunk *chk, char *str) {
519 chk->str = str;
520 chk->len = strlen(str);
521 chk->size = 0; /* mark it read-only */
522}
523
524static inline int chunk_strcpy(struct chunk *chk, const char *str) {
525 size_t len;
526
527 len = strlen(str);
528
529 if (unlikely(len > chk->size))
530 return 0;
531
532 chk->len = len;
533 memcpy(chk->str, str, len);
534
535 return 1;
536}
537
538int chunk_printf(struct chunk *chk, const char *fmt, ...)
539 __attribute__ ((format(printf, 2, 3)));
540
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200541int chunk_htmlencode(struct chunk *dst, struct chunk *src);
542int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
543
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200544static inline void chunk_reset(struct chunk *chk) {
545 chk->str = NULL;
546 chk->len = -1;
547 chk->size = 0;
548}
549
550static inline void chunk_destroy(struct chunk *chk) {
551
552 if (!chk->size)
553 return;
554
555 if (chk->str)
556 free(chk->str);
557
558 chunk_reset(chk);
559}
560
Willy Tarreau0f772532006-12-23 20:51:41 +0100561/*
562 * frees the destination chunk if already allocated, allocates a new string,
563 * and copies the source into it. The pointer to the destination string is
564 * returned, or NULL if the allocation fails or if any pointer is NULL..
565 */
566static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
567 if (!dst || !src || !src->str)
568 return NULL;
569 if (dst->str)
570 free(dst->str);
571 dst->len = src->len;
572 dst->str = (char *)malloc(dst->len);
573 memcpy(dst->str, src->str, dst->len);
574 return dst->str;
575}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200576
577#endif /* _PROTO_BUFFERS_H */
578
579/*
580 * Local variables:
581 * c-indent-level: 8
582 * c-basic-offset: 8
583 * End:
584 */