blob: b241a569fb7057775f581401caf183bdb7e0f2b6 [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 Tarreau4b517ca2011-11-25 20:33:58 +010068/*****************************************************************/
69/* These functions are used to compute various buffer area sizes */
70/*****************************************************************/
71
72/* Return the number of reserved bytes in the buffer, which ensures that once
73 * all pending data are forwarded, the buffer still has global.tune.maxrewrite
74 * bytes free. The result is between 0 and global.maxrewrite, which is itself
75 * smaller than any buf->size.
76 */
77static inline int buffer_reserved(const struct buffer *buf)
78{
79 int ret = global.tune.maxrewrite - buf->to_forward - buf->send_max;
80
81 if (buf->to_forward == BUF_INFINITE_FORWARD)
82 return 0;
83 if (ret <= 0)
84 return 0;
85 return ret;
86}
87
Willy Tarreau7c3c5412009-12-13 15:53:05 +010088/* Return the max number of bytes the buffer can contain so that once all the
89 * pending bytes are forwarded, the buffer still has global.tune.maxrewrite
90 * bytes free. The result sits between buf->size - maxrewrite and buf->size.
91 */
Willy Tarreau4b517ca2011-11-25 20:33:58 +010092static inline int buffer_max_len(const struct buffer *buf)
93{
94 return buf->size - buffer_reserved(buf);
95}
96
97/* Return the maximum amount of bytes that can be written into the buffer,
98 * including reserved space which may be overwritten.
99 */
100static inline int buffer_total_space(const struct buffer *buf)
101{
102 return buf->size - buf->l;
103}
104
105/* Return the maximum amount of bytes that can be written into the buffer,
106 * excluding the reserved space, which is preserved.
107 */
108static inline int buffer_total_space_res(const struct buffer *buf)
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100109{
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100110 return buffer_max_len(buf) - buf->l;
111}
112
113/* Returns the number of contiguous bytes between <start> and <start>+<count>,
114 * and enforces a limit on buf->data + buf->size. <start> must be within the
115 * buffer.
116 */
117static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count)
118{
119 if (count > buf->data - start + buf->size)
120 count = buf->data - start + buf->size;
121 return count;
122}
123
124/* Return the amount of bytes that can be written into the buffer at once,
125 * including reserved space which may be overwritten. This version is optimized
126 * to reduce the amount of operations but it's not easy to understand as it is.
127 * Drawing a buffer with wrapping data on a paper helps a lot.
128 */
129static inline int buffer_contig_space(struct buffer *buf)
130{
131 int space_from_end = buf->l - (buf->r - buf->data);
132 if (space_from_end < 0) /* data does not wrap */
133 space_from_end = buf->r - buf->data;
134 return buf->size - space_from_end;
135}
136
137/* Return the amount of bytes that can be written into the buffer at once,
138 * excluding reserved space, which is preserved. Same comment as above for
139 * the optimization leading to hardly understandable code.
140 */
141static inline int buffer_contig_space_res(struct buffer *buf)
142{
143 /* Proceed differently if the buffer is full, partially used or empty.
144 * The hard situation is when it's partially used and either data or
145 * reserved space wraps at the end.
146 */
147 int res = buffer_reserved(buf);
148 int spare = buf->size - res;
149
150 if (buf->l >= spare)
151 spare = 0;
152 else if (buf->l) {
153 spare = buf->w - res - buf->r;
154 if (spare <= 0)
155 spare += buf->size;
156 spare = buffer_contig_area(buf, buf->r, spare);
157 }
158 return spare;
159}
160
161/* Same as above, but lets the caller pass the pre-computed value of
162 * buffer_reserved() in <res> if it already knows it, to save some
163 * computations.
164 */
165static inline int buffer_contig_space_with_res(struct buffer *buf, int res)
166{
167 /* Proceed differently if the buffer is full, partially used or empty.
168 * The hard situation is when it's partially used and either data or
169 * reserved space wraps at the end.
170 */
171 int spare = buf->size - res;
172
173 if (buf->l >= spare)
174 spare = 0;
175 else if (buf->l) {
176 spare = buf->w - res - buf->r;
177 if (spare <= 0)
178 spare += buf->size;
179 spare = buffer_contig_area(buf, buf->r, spare);
180 }
181 return spare;
182}
183
184/* Normalizes a pointer which is supposed to be relative to the beginning of a
185 * buffer, so that wrapping is correctly handled. The intent is to use this
186 * when increasing a pointer. Note that the wrapping test is only performed
187 * once, so the original pointer must be between ->data and ->data+2*size - 1,
188 * otherwise an invalid pointer might be returned.
189 */
190static inline char *buffer_pointer(const struct buffer *buf, char *ptr)
191{
192 if (ptr - buf->size >= buf->data)
193 ptr -= buf->size;
194 return ptr;
195}
196
197/* Returns the distance between two pointers, taking into account the ability
198 * to wrap around the buffer's end.
199 */
200static inline int buffer_count(const struct buffer *buf, char *from, char *to)
201{
202 int count = to - from;
203 if (count < 0)
204 count += buf->size;
205 return count;
206}
207
208/* returns the amount of pending bytes in the buffer. It is the amount of bytes
209 * that is not scheduled to be sent.
210 */
211static inline int buffer_pending(const struct buffer *buf)
212{
213 return buf->l - buf->send_max;
214}
215
216/* Returns the size of the working area which the caller knows ends at <end>.
217 * If <end> equals buf->r (modulo size), then it means that the free area which
218 * follows is part of the working area. Otherwise, the working area stops at
219 * <end>. It always starts at buf->w+send_max. The work area includes the
220 * reserved area.
221 */
222static inline int buffer_work_area(const struct buffer *buf, char *end)
223{
224 end = buffer_pointer(buf, end);
225 if (end == buf->r) /* pointer exactly at end, lets push forwards */
226 end = buf->w;
227 return buffer_count(buf, buffer_pointer(buf, buf->w + buf->send_max), end);
228}
229
230/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
231static inline int buffer_almost_full(const struct buffer *buf)
232{
233 if (buffer_total_space(buf) < buf->size / 4)
234 return 1;
235 return 0;
236}
237
238/*
239 * Return the max amount of bytes that can be read from the buffer at once.
240 * Note that this may be lower than the actual buffer length when the data
241 * wrap after the end, so it's preferable to call this function again after
242 * reading. Also note that this function respects the send_max limit.
243 */
244static inline int buffer_contig_data(struct buffer *buf)
245{
246 int ret;
247
248 if (!buf->send_max || !buf->l)
249 return 0;
250
251 if (buf->r > buf->w)
252 ret = buf->r - buf->w;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100253 else
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100254 ret = buf->data + buf->size - buf->w;
255
256 /* limit the amount of outgoing data if required */
257 if (ret > buf->send_max)
258 ret = buf->send_max;
259
260 return ret;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100261}
262
Willy Tarreau74b08c92010-09-08 17:04:31 +0200263/* Returns true if the buffer's input is already closed */
264static inline int buffer_input_closed(struct buffer *buf)
265{
266 return ((buf->flags & BF_SHUTR) != 0);
267}
268
269/* Returns true if the buffer's output is already closed */
270static inline int buffer_output_closed(struct buffer *buf)
271{
272 return ((buf->flags & BF_SHUTW) != 0);
273}
274
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200275/* Check buffer timeouts, and set the corresponding flags. The
276 * likely/unlikely have been optimized for fastest normal path.
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100277 * The read/write timeouts are not set if there was activity on the buffer.
278 * That way, we don't have to update the timeout on every I/O. Note that the
279 * analyser timeout is always checked.
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200280 */
281static inline void buffer_check_timeouts(struct buffer *b)
282{
Willy Tarreau86491c32008-12-14 09:04:47 +0100283 if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200284 unlikely(tick_is_expired(b->rex, now_ms)))
285 b->flags |= BF_READ_TIMEOUT;
286
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100287 if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200288 unlikely(tick_is_expired(b->wex, now_ms)))
289 b->flags |= BF_WRITE_TIMEOUT;
290
291 if (likely(!(b->flags & BF_ANA_TIMEOUT)) &&
292 unlikely(tick_is_expired(b->analyse_exp, now_ms)))
293 b->flags |= BF_ANA_TIMEOUT;
294}
295
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100296/* Schedule all remaining buffer data to be sent. send_max is not touched if it
297 * already covers those data. That permits doing a flush even after a forward,
298 * although not recommended.
299 */
300static inline void buffer_flush(struct buffer *buf)
301{
302 if (buf->send_max < buf->l)
303 buf->send_max = buf->l;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200304 if (buf->send_max)
305 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100306}
307
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100308/* Erase any content from buffer <buf> and adjusts flags accordingly. Note
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100309 * that any spliced data is not affected since we may not have any access to
310 * it.
Willy Tarreaue393fe22008-08-16 22:18:07 +0200311 */
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100312static inline void buffer_erase(struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200313{
Willy Tarreauf890dc92008-12-13 21:12:26 +0100314 buf->send_max = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100315 buf->to_forward = 0;
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100316 buf->r = buf->lr = buf->w = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200317 buf->l = 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200318 buf->flags &= ~(BF_FULL | BF_OUT_EMPTY);
319 if (!buf->pipe)
320 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321}
322
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200323/* Cut the "tail" of the buffer, which means strip it to the length of unsent
324 * data only, and kill any remaining unsent data. Any scheduled forwarding is
325 * stopped. This is mainly to be used to send error messages after existing
326 * data.
327 */
328static inline void buffer_cut_tail(struct buffer *buf)
329{
330 if (!buf->send_max)
331 return buffer_erase(buf);
332
333 buf->to_forward = 0;
334 if (buf->l == buf->send_max)
335 return;
336
337 buf->l = buf->send_max;
338 buf->r = buf->w + buf->l;
339 if (buf->r >= buf->data + buf->size)
340 buf->r -= buf->size;
341 buf->lr = buf->r;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200342 buf->flags &= ~BF_FULL;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100343 if (buf->l >= buffer_max_len(buf))
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200344 buf->flags |= BF_FULL;
345}
346
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100347/* Cut the <n> next unsent bytes of the buffer. The caller must ensure that <n>
348 * is smaller than the actual buffer's length. This is mainly used to remove
349 * empty lines at the beginning of a request or a response.
350 */
351static inline void buffer_ignore(struct buffer *buf, int n)
352{
353 buf->l -= n;
354 buf->w += n;
355 if (buf->w >= buf->data + buf->size)
356 buf->w -= buf->size;
357 buf->flags &= ~BF_FULL;
358 if (buf->l >= buffer_max_len(buf))
359 buf->flags |= BF_FULL;
360}
361
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200362/* marks the buffer as "shutdown" ASAP for reads */
363static inline void buffer_shutr_now(struct buffer *buf)
364{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100365 buf->flags |= BF_SHUTR_NOW;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200366}
367
368/* marks the buffer as "shutdown" ASAP for writes */
369static inline void buffer_shutw_now(struct buffer *buf)
370{
371 buf->flags |= BF_SHUTW_NOW;
372}
373
374/* marks the buffer as "shutdown" ASAP in both directions */
375static inline void buffer_abort(struct buffer *buf)
376{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100377 buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW;
Willy Tarreaue4599762010-03-21 23:25:09 +0100378 buf->flags &= ~BF_AUTO_CONNECT;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200379}
380
Willy Tarreau01bf8672008-12-07 18:03:29 +0100381/* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack
382 * flag is set, and the function called once. The function is responsible for
383 * clearing the hijack bit. It is possible that the function clears the flag
384 * during this first call.
385 */
386static inline void buffer_install_hijacker(struct session *s,
387 struct buffer *b,
388 void (*func)(struct session *, struct buffer *))
Willy Tarreau72b179a2008-08-28 16:01:32 +0200389{
Willy Tarreau01bf8672008-12-07 18:03:29 +0100390 b->hijacker = func;
391 b->flags |= BF_HIJACK;
392 func(s, b);
Willy Tarreau72b179a2008-08-28 16:01:32 +0200393}
394
Willy Tarreau01bf8672008-12-07 18:03:29 +0100395/* Releases the buffer from hijacking mode. Often used by the hijack function */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200396static inline void buffer_stop_hijack(struct buffer *buf)
397{
398 buf->flags &= ~BF_HIJACK;
399}
400
Willy Tarreau520d95e2009-09-19 21:04:57 +0200401/* allow the consumer to try to establish a new connection. */
402static inline void buffer_auto_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200403{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200404 buf->flags |= BF_AUTO_CONNECT;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200405}
406
Willy Tarreau520d95e2009-09-19 21:04:57 +0200407/* prevent the consumer from trying to establish a new connection, and also
408 * disable auto shutdown forwarding.
409 */
410static inline void buffer_dont_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200411{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200412 buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200413}
414
Willy Tarreau520d95e2009-09-19 21:04:57 +0200415/* allow the producer to forward shutdown requests */
416static inline void buffer_auto_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100417{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200418 buf->flags |= BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100419}
420
Willy Tarreau520d95e2009-09-19 21:04:57 +0200421/* prevent the producer from forwarding shutdown requests */
422static inline void buffer_dont_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100423{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200424 buf->flags &= ~BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100425}
426
Willy Tarreau90deb182010-01-07 00:20:41 +0100427/* allow the producer to read / poll the input */
428static inline void buffer_auto_read(struct buffer *buf)
429{
430 buf->flags &= ~BF_DONT_READ;
431}
432
433/* prevent the producer from read / poll the input */
434static inline void buffer_dont_read(struct buffer *buf)
435{
436 buf->flags |= BF_DONT_READ;
437}
438
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439/*
440 * Tries to realign the given buffer, and returns how many bytes can be written
441 * there at once without overwriting anything.
442 */
443static inline int buffer_realign(struct buffer *buf)
444{
445 if (buf->l == 0) {
446 /* let's realign the buffer to optimize I/O */
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100447 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448 }
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100449 return buffer_contig_space(buf);
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200450}
451
452/*
453 * Advance the buffer's read pointer by <len> bytes. This is useful when data
454 * have been read directly from the buffer. It is illegal to call this function
455 * with <len> causing a wrapping at the end of the buffer. It's the caller's
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200456 * responsibility to ensure that <len> is never larger than buf->send_max.
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200457 */
458static inline void buffer_skip(struct buffer *buf, int len)
459{
460 buf->w += len;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200461 if (buf->w >= buf->data + buf->size)
462 buf->w -= buf->size; /* wrap around the buffer */
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200463
464 buf->l -= len;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200465 if (!buf->l)
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200466 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200467
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100468 if (buf->l < buffer_max_len(buf))
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200469 buf->flags &= ~BF_FULL;
470
471 buf->send_max -= len;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200472 if (!buf->send_max && !buf->pipe)
473 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200474
475 /* notify that some data was written to the SI from the buffer */
476 buf->flags |= BF_WRITE_PARTIAL;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200477}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200478
Willy Tarreauaeac3192009-08-31 08:09:57 +0200479/* writes the chunk <chunk> to buffer <buf>. Returns -1 in case of success,
480 * -2 if it is larger than the buffer size, or the number of bytes available
481 * otherwise. If the chunk has been written, its size is automatically reset
482 * to zero. The send limit is automatically adjusted with the amount of data
483 * written.
484 */
485static inline int buffer_write_chunk(struct buffer *buf, struct chunk *chunk)
486{
487 int ret;
488
489 ret = buffer_write(buf, chunk->str, chunk->len);
490 if (ret == -1)
491 chunk->len = 0;
492 return ret;
493}
494
Willy Tarreau74b08c92010-09-08 17:04:31 +0200495/* Tries to copy chunk <chunk> into buffer <buf> after length controls.
496 * The send_max and to_forward pointers are updated. If the buffer's input is
497 * closed, -2 is returned. If the block is too large for this buffer, -3 is
498 * returned. If there is not enough room left in the buffer, -1 is returned.
499 * Otherwise the number of bytes copied is returned (0 being a valid number).
500 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
501 * transferred. The chunk's length is updated with the number of bytes sent.
Willy Tarreauaeac3192009-08-31 08:09:57 +0200502 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200503static inline int buffer_put_chunk(struct buffer *buf, struct chunk *chunk)
Willy Tarreauaeac3192009-08-31 08:09:57 +0200504{
505 int ret;
506
Willy Tarreau74b08c92010-09-08 17:04:31 +0200507 ret = buffer_put_block(buf, chunk->str, chunk->len);
508 if (ret > 0)
509 chunk->len -= ret;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200510 return ret;
511}
512
Willy Tarreau74b08c92010-09-08 17:04:31 +0200513/* Tries to copy string <str> at once into buffer <buf> after length controls.
514 * The send_max and to_forward pointers are updated. If the buffer's input is
515 * closed, -2 is returned. If the block is too large for this buffer, -3 is
516 * returned. If there is not enough room left in the buffer, -1 is returned.
517 * Otherwise the number of bytes copied is returned (0 being a valid number).
518 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
519 * transferred.
520 */
521static inline int buffer_put_string(struct buffer *buf, const char *str)
522{
523 return buffer_put_block(buf, str, strlen(str));
524}
525
526/*
527 * Return one char from the buffer. If the buffer is empty and closed, return -2.
528 * If the buffer is just empty, return -1. The buffer's pointer is not advanced,
529 * it's up to the caller to call buffer_skip(buf, 1) when it has consumed the char.
530 * Also note that this function respects the send_max limit.
531 */
532static inline int buffer_get_char(struct buffer *buf)
533{
534 /* closed or empty + imminent close = -2; empty = -1 */
535 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
536 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
537 return -2;
538 return -1;
539 }
540 return *buf->w;
541}
542
543
544/* DEPRECATED, just provided for compatibility, use buffer_put_chunk() instead !!!
545 * returns >= 0 if the buffer is too small to hold the message, -1 if the
546 * transfer was OK, -2 in case of failure.
547 */
548static inline int buffer_feed_chunk(struct buffer *buf, struct chunk *msg)
549{
550 int ret = buffer_put_chunk(buf, msg);
551 if (ret >= 0) /* transfer OK */
552 return -1;
553 if (ret == -1) /* missing room */
554 return 1;
555 /* failure */
556 return -2;
557}
558
559/* DEPRECATED, just provided for compatibility, use buffer_put_string() instead !!!
560 * returns >= 0 if the buffer is too small to hold the message, -1 if the
561 * transfer was OK, -2 in case of failure.
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200562 */
563static inline int buffer_feed(struct buffer *buf, const char *str)
564{
Willy Tarreau74b08c92010-09-08 17:04:31 +0200565 int ret = buffer_put_string(buf, str);
566 if (ret >= 0) /* transfer OK */
567 return -1;
568 if (ret == -1) /* missing room */
569 return 1;
570 /* failure */
571 return -2;
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200572}
573
Willy Tarreau74b08c92010-09-08 17:04:31 +0200574/*
575 *
576 * Functions below are used to manage chunks
577 *
578 */
579
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200580static inline void chunk_init(struct chunk *chk, char *str, size_t size) {
581 chk->str = str;
582 chk->len = 0;
583 chk->size = size;
584}
585
586/* report 0 in case of error, 1 if OK. */
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200587static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200588
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200589 if (size && len > size)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200590 return 0;
591
592 chk->str = str;
593 chk->len = len;
594 chk->size = size;
595
596 return 1;
597}
598
599static inline void chunk_initstr(struct chunk *chk, char *str) {
600 chk->str = str;
601 chk->len = strlen(str);
602 chk->size = 0; /* mark it read-only */
603}
604
605static inline int chunk_strcpy(struct chunk *chk, const char *str) {
606 size_t len;
607
608 len = strlen(str);
609
610 if (unlikely(len > chk->size))
611 return 0;
612
613 chk->len = len;
614 memcpy(chk->str, str, len);
615
616 return 1;
617}
618
619int chunk_printf(struct chunk *chk, const char *fmt, ...)
620 __attribute__ ((format(printf, 2, 3)));
621
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200622int chunk_htmlencode(struct chunk *dst, struct chunk *src);
623int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
624
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200625static inline void chunk_reset(struct chunk *chk) {
626 chk->str = NULL;
627 chk->len = -1;
628 chk->size = 0;
629}
630
631static inline void chunk_destroy(struct chunk *chk) {
632
633 if (!chk->size)
634 return;
635
636 if (chk->str)
637 free(chk->str);
638
639 chunk_reset(chk);
640}
641
Willy Tarreau0f772532006-12-23 20:51:41 +0100642/*
643 * frees the destination chunk if already allocated, allocates a new string,
644 * and copies the source into it. The pointer to the destination string is
645 * returned, or NULL if the allocation fails or if any pointer is NULL..
646 */
647static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
648 if (!dst || !src || !src->str)
649 return NULL;
650 if (dst->str)
651 free(dst->str);
652 dst->len = src->len;
653 dst->str = (char *)malloc(dst->len);
654 memcpy(dst->str, src->str, dst->len);
655 return dst->str;
656}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657
658#endif /* _PROTO_BUFFERS_H */
659
660/*
661 * Local variables:
662 * c-indent-level: 8
663 * c-basic-offset: 8
664 * End:
665 */