blob: 0a2da34984cc94e99c7fa74d17f22c6be0753106 [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,
Willy Tarreaufe4b1f92011-11-28 13:40:49 +0100106 * excluding the reserved space, which is preserved. 0 may be returned if
107 * the reserved space was already reached or used.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100108 */
109static inline int buffer_total_space_res(const struct buffer *buf)
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100110{
Willy Tarreaufe4b1f92011-11-28 13:40:49 +0100111 int len = buffer_max_len(buf) - buf->l;
112 return len < 0 ? 0 : len;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100113}
114
115/* Returns the number of contiguous bytes between <start> and <start>+<count>,
116 * and enforces a limit on buf->data + buf->size. <start> must be within the
117 * buffer.
118 */
119static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count)
120{
121 if (count > buf->data - start + buf->size)
122 count = buf->data - start + buf->size;
123 return count;
124}
125
126/* Return the amount of bytes that can be written into the buffer at once,
127 * including reserved space which may be overwritten. This version is optimized
128 * to reduce the amount of operations but it's not easy to understand as it is.
129 * Drawing a buffer with wrapping data on a paper helps a lot.
130 */
131static inline int buffer_contig_space(struct buffer *buf)
132{
133 int space_from_end = buf->l - (buf->r - buf->data);
134 if (space_from_end < 0) /* data does not wrap */
135 space_from_end = buf->r - buf->data;
136 return buf->size - space_from_end;
137}
138
139/* Return the amount of bytes that can be written into the buffer at once,
140 * excluding reserved space, which is preserved. Same comment as above for
141 * the optimization leading to hardly understandable code.
142 */
143static inline int buffer_contig_space_res(struct buffer *buf)
144{
145 /* Proceed differently if the buffer is full, partially used or empty.
146 * The hard situation is when it's partially used and either data or
147 * reserved space wraps at the end.
148 */
149 int res = buffer_reserved(buf);
150 int spare = buf->size - res;
151
152 if (buf->l >= spare)
153 spare = 0;
154 else if (buf->l) {
155 spare = buf->w - res - buf->r;
156 if (spare <= 0)
157 spare += buf->size;
158 spare = buffer_contig_area(buf, buf->r, spare);
159 }
160 return spare;
161}
162
163/* Same as above, but lets the caller pass the pre-computed value of
164 * buffer_reserved() in <res> if it already knows it, to save some
165 * computations.
166 */
167static inline int buffer_contig_space_with_res(struct buffer *buf, int res)
168{
169 /* Proceed differently if the buffer is full, partially used or empty.
170 * The hard situation is when it's partially used and either data or
171 * reserved space wraps at the end.
172 */
173 int spare = buf->size - res;
174
175 if (buf->l >= spare)
176 spare = 0;
177 else if (buf->l) {
178 spare = buf->w - res - buf->r;
179 if (spare <= 0)
180 spare += buf->size;
181 spare = buffer_contig_area(buf, buf->r, spare);
182 }
183 return spare;
184}
185
186/* Normalizes a pointer which is supposed to be relative to the beginning of a
187 * buffer, so that wrapping is correctly handled. The intent is to use this
188 * when increasing a pointer. Note that the wrapping test is only performed
189 * once, so the original pointer must be between ->data and ->data+2*size - 1,
190 * otherwise an invalid pointer might be returned.
191 */
192static inline char *buffer_pointer(const struct buffer *buf, char *ptr)
193{
194 if (ptr - buf->size >= buf->data)
195 ptr -= buf->size;
196 return ptr;
197}
198
199/* Returns the distance between two pointers, taking into account the ability
200 * to wrap around the buffer's end.
201 */
202static inline int buffer_count(const struct buffer *buf, char *from, char *to)
203{
204 int count = to - from;
205 if (count < 0)
206 count += buf->size;
207 return count;
208}
209
210/* returns the amount of pending bytes in the buffer. It is the amount of bytes
211 * that is not scheduled to be sent.
212 */
213static inline int buffer_pending(const struct buffer *buf)
214{
215 return buf->l - buf->send_max;
216}
217
218/* Returns the size of the working area which the caller knows ends at <end>.
219 * If <end> equals buf->r (modulo size), then it means that the free area which
220 * follows is part of the working area. Otherwise, the working area stops at
221 * <end>. It always starts at buf->w+send_max. The work area includes the
222 * reserved area.
223 */
224static inline int buffer_work_area(const struct buffer *buf, char *end)
225{
226 end = buffer_pointer(buf, end);
227 if (end == buf->r) /* pointer exactly at end, lets push forwards */
228 end = buf->w;
229 return buffer_count(buf, buffer_pointer(buf, buf->w + buf->send_max), end);
230}
231
232/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
233static inline int buffer_almost_full(const struct buffer *buf)
234{
235 if (buffer_total_space(buf) < buf->size / 4)
236 return 1;
237 return 0;
238}
239
240/*
241 * Return the max amount of bytes that can be read from the buffer at once.
242 * Note that this may be lower than the actual buffer length when the data
243 * wrap after the end, so it's preferable to call this function again after
244 * reading. Also note that this function respects the send_max limit.
245 */
246static inline int buffer_contig_data(struct buffer *buf)
247{
248 int ret;
249
250 if (!buf->send_max || !buf->l)
251 return 0;
252
253 if (buf->r > buf->w)
254 ret = buf->r - buf->w;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100255 else
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100256 ret = buf->data + buf->size - buf->w;
257
258 /* limit the amount of outgoing data if required */
259 if (ret > buf->send_max)
260 ret = buf->send_max;
261
262 return ret;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100263}
264
Willy Tarreau74b08c92010-09-08 17:04:31 +0200265/* Returns true if the buffer's input is already closed */
266static inline int buffer_input_closed(struct buffer *buf)
267{
268 return ((buf->flags & BF_SHUTR) != 0);
269}
270
271/* Returns true if the buffer's output is already closed */
272static inline int buffer_output_closed(struct buffer *buf)
273{
274 return ((buf->flags & BF_SHUTW) != 0);
275}
276
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200277/* Check buffer timeouts, and set the corresponding flags. The
278 * likely/unlikely have been optimized for fastest normal path.
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100279 * The read/write timeouts are not set if there was activity on the buffer.
280 * That way, we don't have to update the timeout on every I/O. Note that the
281 * analyser timeout is always checked.
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200282 */
283static inline void buffer_check_timeouts(struct buffer *b)
284{
Willy Tarreau86491c32008-12-14 09:04:47 +0100285 if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200286 unlikely(tick_is_expired(b->rex, now_ms)))
287 b->flags |= BF_READ_TIMEOUT;
288
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100289 if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200290 unlikely(tick_is_expired(b->wex, now_ms)))
291 b->flags |= BF_WRITE_TIMEOUT;
292
293 if (likely(!(b->flags & BF_ANA_TIMEOUT)) &&
294 unlikely(tick_is_expired(b->analyse_exp, now_ms)))
295 b->flags |= BF_ANA_TIMEOUT;
296}
297
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100298/* Schedule all remaining buffer data to be sent. send_max is not touched if it
299 * already covers those data. That permits doing a flush even after a forward,
300 * although not recommended.
301 */
302static inline void buffer_flush(struct buffer *buf)
303{
304 if (buf->send_max < buf->l)
305 buf->send_max = buf->l;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200306 if (buf->send_max)
307 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100308}
309
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100310/* Erase any content from buffer <buf> and adjusts flags accordingly. Note
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100311 * that any spliced data is not affected since we may not have any access to
312 * it.
Willy Tarreaue393fe22008-08-16 22:18:07 +0200313 */
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100314static inline void buffer_erase(struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200315{
Willy Tarreauf890dc92008-12-13 21:12:26 +0100316 buf->send_max = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100317 buf->to_forward = 0;
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100318 buf->r = buf->lr = buf->w = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319 buf->l = 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200320 buf->flags &= ~(BF_FULL | BF_OUT_EMPTY);
321 if (!buf->pipe)
322 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323}
324
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200325/* Cut the "tail" of the buffer, which means strip it to the length of unsent
326 * data only, and kill any remaining unsent data. Any scheduled forwarding is
327 * stopped. This is mainly to be used to send error messages after existing
328 * data.
329 */
330static inline void buffer_cut_tail(struct buffer *buf)
331{
332 if (!buf->send_max)
333 return buffer_erase(buf);
334
335 buf->to_forward = 0;
336 if (buf->l == buf->send_max)
337 return;
338
339 buf->l = buf->send_max;
340 buf->r = buf->w + buf->l;
341 if (buf->r >= buf->data + buf->size)
342 buf->r -= buf->size;
343 buf->lr = buf->r;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200344 buf->flags &= ~BF_FULL;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100345 if (buf->l >= buffer_max_len(buf))
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200346 buf->flags |= BF_FULL;
347}
348
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100349/* Cut the <n> next unsent bytes of the buffer. The caller must ensure that <n>
350 * is smaller than the actual buffer's length. This is mainly used to remove
351 * empty lines at the beginning of a request or a response.
352 */
353static inline void buffer_ignore(struct buffer *buf, int n)
354{
355 buf->l -= n;
356 buf->w += n;
357 if (buf->w >= buf->data + buf->size)
358 buf->w -= buf->size;
359 buf->flags &= ~BF_FULL;
360 if (buf->l >= buffer_max_len(buf))
361 buf->flags |= BF_FULL;
362}
363
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200364/* marks the buffer as "shutdown" ASAP for reads */
365static inline void buffer_shutr_now(struct buffer *buf)
366{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100367 buf->flags |= BF_SHUTR_NOW;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200368}
369
370/* marks the buffer as "shutdown" ASAP for writes */
371static inline void buffer_shutw_now(struct buffer *buf)
372{
373 buf->flags |= BF_SHUTW_NOW;
374}
375
376/* marks the buffer as "shutdown" ASAP in both directions */
377static inline void buffer_abort(struct buffer *buf)
378{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100379 buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW;
Willy Tarreaue4599762010-03-21 23:25:09 +0100380 buf->flags &= ~BF_AUTO_CONNECT;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200381}
382
Willy Tarreau01bf8672008-12-07 18:03:29 +0100383/* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack
384 * flag is set, and the function called once. The function is responsible for
385 * clearing the hijack bit. It is possible that the function clears the flag
386 * during this first call.
387 */
388static inline void buffer_install_hijacker(struct session *s,
389 struct buffer *b,
390 void (*func)(struct session *, struct buffer *))
Willy Tarreau72b179a2008-08-28 16:01:32 +0200391{
Willy Tarreau01bf8672008-12-07 18:03:29 +0100392 b->hijacker = func;
393 b->flags |= BF_HIJACK;
394 func(s, b);
Willy Tarreau72b179a2008-08-28 16:01:32 +0200395}
396
Willy Tarreau01bf8672008-12-07 18:03:29 +0100397/* Releases the buffer from hijacking mode. Often used by the hijack function */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200398static inline void buffer_stop_hijack(struct buffer *buf)
399{
400 buf->flags &= ~BF_HIJACK;
401}
402
Willy Tarreau520d95e2009-09-19 21:04:57 +0200403/* allow the consumer to try to establish a new connection. */
404static inline void buffer_auto_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200405{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200406 buf->flags |= BF_AUTO_CONNECT;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200407}
408
Willy Tarreau520d95e2009-09-19 21:04:57 +0200409/* prevent the consumer from trying to establish a new connection, and also
410 * disable auto shutdown forwarding.
411 */
412static inline void buffer_dont_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200413{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200414 buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200415}
416
Willy Tarreau520d95e2009-09-19 21:04:57 +0200417/* allow the producer to forward shutdown requests */
418static inline void buffer_auto_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100419{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200420 buf->flags |= BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100421}
422
Willy Tarreau520d95e2009-09-19 21:04:57 +0200423/* prevent the producer from forwarding shutdown requests */
424static inline void buffer_dont_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100425{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200426 buf->flags &= ~BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100427}
428
Willy Tarreau90deb182010-01-07 00:20:41 +0100429/* allow the producer to read / poll the input */
430static inline void buffer_auto_read(struct buffer *buf)
431{
432 buf->flags &= ~BF_DONT_READ;
433}
434
435/* prevent the producer from read / poll the input */
436static inline void buffer_dont_read(struct buffer *buf)
437{
438 buf->flags |= BF_DONT_READ;
439}
440
Willy Tarreaubaaee002006-06-26 02:48:02 +0200441/*
442 * Tries to realign the given buffer, and returns how many bytes can be written
443 * there at once without overwriting anything.
444 */
445static inline int buffer_realign(struct buffer *buf)
446{
447 if (buf->l == 0) {
448 /* let's realign the buffer to optimize I/O */
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100449 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450 }
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100451 return buffer_contig_space(buf);
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200452}
453
454/*
455 * Advance the buffer's read pointer by <len> bytes. This is useful when data
456 * have been read directly from the buffer. It is illegal to call this function
457 * with <len> causing a wrapping at the end of the buffer. It's the caller's
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200458 * responsibility to ensure that <len> is never larger than buf->send_max.
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200459 */
460static inline void buffer_skip(struct buffer *buf, int len)
461{
462 buf->w += len;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200463 if (buf->w >= buf->data + buf->size)
464 buf->w -= buf->size; /* wrap around the buffer */
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200465
466 buf->l -= len;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200467 if (!buf->l)
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200468 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200469
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100470 if (buf->l < buffer_max_len(buf))
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200471 buf->flags &= ~BF_FULL;
472
473 buf->send_max -= len;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200474 if (!buf->send_max && !buf->pipe)
475 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200476
477 /* notify that some data was written to the SI from the buffer */
478 buf->flags |= BF_WRITE_PARTIAL;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200479}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200480
Willy Tarreauaeac3192009-08-31 08:09:57 +0200481/* writes the chunk <chunk> to buffer <buf>. Returns -1 in case of success,
482 * -2 if it is larger than the buffer size, or the number of bytes available
483 * otherwise. If the chunk has been written, its size is automatically reset
484 * to zero. The send limit is automatically adjusted with the amount of data
485 * written.
486 */
487static inline int buffer_write_chunk(struct buffer *buf, struct chunk *chunk)
488{
489 int ret;
490
491 ret = buffer_write(buf, chunk->str, chunk->len);
492 if (ret == -1)
493 chunk->len = 0;
494 return ret;
495}
496
Willy Tarreau74b08c92010-09-08 17:04:31 +0200497/* Tries to copy chunk <chunk> into buffer <buf> after length controls.
498 * The send_max and to_forward pointers are updated. If the buffer's input is
499 * closed, -2 is returned. If the block is too large for this buffer, -3 is
500 * returned. If there is not enough room left in the buffer, -1 is returned.
501 * Otherwise the number of bytes copied is returned (0 being a valid number).
502 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
503 * transferred. The chunk's length is updated with the number of bytes sent.
Willy Tarreauaeac3192009-08-31 08:09:57 +0200504 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200505static inline int buffer_put_chunk(struct buffer *buf, struct chunk *chunk)
Willy Tarreauaeac3192009-08-31 08:09:57 +0200506{
507 int ret;
508
Willy Tarreau74b08c92010-09-08 17:04:31 +0200509 ret = buffer_put_block(buf, chunk->str, chunk->len);
510 if (ret > 0)
511 chunk->len -= ret;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200512 return ret;
513}
514
Willy Tarreau74b08c92010-09-08 17:04:31 +0200515/* Tries to copy string <str> at once into buffer <buf> after length controls.
516 * The send_max and to_forward pointers are updated. If the buffer's input is
517 * closed, -2 is returned. If the block is too large for this buffer, -3 is
518 * returned. If there is not enough room left in the buffer, -1 is returned.
519 * Otherwise the number of bytes copied is returned (0 being a valid number).
520 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
521 * transferred.
522 */
523static inline int buffer_put_string(struct buffer *buf, const char *str)
524{
525 return buffer_put_block(buf, str, strlen(str));
526}
527
528/*
529 * Return one char from the buffer. If the buffer is empty and closed, return -2.
530 * If the buffer is just empty, return -1. The buffer's pointer is not advanced,
531 * it's up to the caller to call buffer_skip(buf, 1) when it has consumed the char.
532 * Also note that this function respects the send_max limit.
533 */
534static inline int buffer_get_char(struct buffer *buf)
535{
536 /* closed or empty + imminent close = -2; empty = -1 */
537 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
538 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
539 return -2;
540 return -1;
541 }
542 return *buf->w;
543}
544
545
546/* DEPRECATED, just provided for compatibility, use buffer_put_chunk() instead !!!
547 * returns >= 0 if the buffer is too small to hold the message, -1 if the
548 * transfer was OK, -2 in case of failure.
549 */
550static inline int buffer_feed_chunk(struct buffer *buf, struct chunk *msg)
551{
552 int ret = buffer_put_chunk(buf, msg);
553 if (ret >= 0) /* transfer OK */
554 return -1;
555 if (ret == -1) /* missing room */
556 return 1;
557 /* failure */
558 return -2;
559}
560
561/* DEPRECATED, just provided for compatibility, use buffer_put_string() instead !!!
562 * returns >= 0 if the buffer is too small to hold the message, -1 if the
563 * transfer was OK, -2 in case of failure.
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200564 */
565static inline int buffer_feed(struct buffer *buf, const char *str)
566{
Willy Tarreau74b08c92010-09-08 17:04:31 +0200567 int ret = buffer_put_string(buf, str);
568 if (ret >= 0) /* transfer OK */
569 return -1;
570 if (ret == -1) /* missing room */
571 return 1;
572 /* failure */
573 return -2;
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200574}
575
Willy Tarreau74b08c92010-09-08 17:04:31 +0200576/*
577 *
578 * Functions below are used to manage chunks
579 *
580 */
581
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200582static inline void chunk_init(struct chunk *chk, char *str, size_t size) {
583 chk->str = str;
584 chk->len = 0;
585 chk->size = size;
586}
587
588/* report 0 in case of error, 1 if OK. */
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200589static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200590
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200591 if (size && len > size)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200592 return 0;
593
594 chk->str = str;
595 chk->len = len;
596 chk->size = size;
597
598 return 1;
599}
600
601static inline void chunk_initstr(struct chunk *chk, char *str) {
602 chk->str = str;
603 chk->len = strlen(str);
604 chk->size = 0; /* mark it read-only */
605}
606
607static inline int chunk_strcpy(struct chunk *chk, const char *str) {
608 size_t len;
609
610 len = strlen(str);
611
612 if (unlikely(len > chk->size))
613 return 0;
614
615 chk->len = len;
616 memcpy(chk->str, str, len);
617
618 return 1;
619}
620
621int chunk_printf(struct chunk *chk, const char *fmt, ...)
622 __attribute__ ((format(printf, 2, 3)));
623
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200624int chunk_htmlencode(struct chunk *dst, struct chunk *src);
625int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
626
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200627static inline void chunk_reset(struct chunk *chk) {
628 chk->str = NULL;
629 chk->len = -1;
630 chk->size = 0;
631}
632
633static inline void chunk_destroy(struct chunk *chk) {
634
635 if (!chk->size)
636 return;
637
638 if (chk->str)
639 free(chk->str);
640
641 chunk_reset(chk);
642}
643
Willy Tarreau0f772532006-12-23 20:51:41 +0100644/*
645 * frees the destination chunk if already allocated, allocates a new string,
646 * and copies the source into it. The pointer to the destination string is
647 * returned, or NULL if the allocation fails or if any pointer is NULL..
648 */
649static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
650 if (!dst || !src || !src->str)
651 return NULL;
652 if (dst->str)
653 free(dst->str);
654 dst->len = src->len;
655 dst->str = (char *)malloc(dst->len);
656 memcpy(dst->str, src->str, dst->len);
657 return dst->str;
658}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200659
660#endif /* _PROTO_BUFFERS_H */
661
662/*
663 * Local variables:
664 * c-indent-level: 8
665 * c-basic-offset: 8
666 * End:
667 */