blob: 0d395a077346079fc34c272d1844ac302f86087e [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
Willy Tarreau71730252011-11-28 16:04:29 +0100189 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100190 * otherwise an invalid pointer might be returned.
191 */
192static inline char *buffer_pointer(const struct buffer *buf, char *ptr)
193{
Willy Tarreau71730252011-11-28 16:04:29 +0100194 if (ptr < buf->data)
195 ptr += buf->size;
196 else if (ptr - buf->size >= buf->data)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100197 ptr -= buf->size;
198 return ptr;
199}
200
201/* Returns the distance between two pointers, taking into account the ability
202 * to wrap around the buffer's end.
203 */
204static inline int buffer_count(const struct buffer *buf, char *from, char *to)
205{
206 int count = to - from;
207 if (count < 0)
208 count += buf->size;
209 return count;
210}
211
212/* returns the amount of pending bytes in the buffer. It is the amount of bytes
213 * that is not scheduled to be sent.
214 */
215static inline int buffer_pending(const struct buffer *buf)
216{
217 return buf->l - buf->send_max;
218}
219
220/* Returns the size of the working area which the caller knows ends at <end>.
221 * If <end> equals buf->r (modulo size), then it means that the free area which
222 * follows is part of the working area. Otherwise, the working area stops at
223 * <end>. It always starts at buf->w+send_max. The work area includes the
224 * reserved area.
225 */
226static inline int buffer_work_area(const struct buffer *buf, char *end)
227{
228 end = buffer_pointer(buf, end);
229 if (end == buf->r) /* pointer exactly at end, lets push forwards */
230 end = buf->w;
231 return buffer_count(buf, buffer_pointer(buf, buf->w + buf->send_max), end);
232}
233
234/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
235static inline int buffer_almost_full(const struct buffer *buf)
236{
237 if (buffer_total_space(buf) < buf->size / 4)
238 return 1;
239 return 0;
240}
241
242/*
243 * Return the max amount of bytes that can be read from the buffer at once.
244 * Note that this may be lower than the actual buffer length when the data
245 * wrap after the end, so it's preferable to call this function again after
246 * reading. Also note that this function respects the send_max limit.
247 */
248static inline int buffer_contig_data(struct buffer *buf)
249{
250 int ret;
251
252 if (!buf->send_max || !buf->l)
253 return 0;
254
255 if (buf->r > buf->w)
256 ret = buf->r - buf->w;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100257 else
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100258 ret = buf->data + buf->size - buf->w;
259
260 /* limit the amount of outgoing data if required */
261 if (ret > buf->send_max)
262 ret = buf->send_max;
263
264 return ret;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100265}
266
Willy Tarreau74b08c92010-09-08 17:04:31 +0200267/* Returns true if the buffer's input is already closed */
268static inline int buffer_input_closed(struct buffer *buf)
269{
270 return ((buf->flags & BF_SHUTR) != 0);
271}
272
273/* Returns true if the buffer's output is already closed */
274static inline int buffer_output_closed(struct buffer *buf)
275{
276 return ((buf->flags & BF_SHUTW) != 0);
277}
278
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200279/* Check buffer timeouts, and set the corresponding flags. The
280 * likely/unlikely have been optimized for fastest normal path.
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100281 * The read/write timeouts are not set if there was activity on the buffer.
282 * That way, we don't have to update the timeout on every I/O. Note that the
283 * analyser timeout is always checked.
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200284 */
285static inline void buffer_check_timeouts(struct buffer *b)
286{
Willy Tarreau86491c32008-12-14 09:04:47 +0100287 if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200288 unlikely(tick_is_expired(b->rex, now_ms)))
289 b->flags |= BF_READ_TIMEOUT;
290
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100291 if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200292 unlikely(tick_is_expired(b->wex, now_ms)))
293 b->flags |= BF_WRITE_TIMEOUT;
294
295 if (likely(!(b->flags & BF_ANA_TIMEOUT)) &&
296 unlikely(tick_is_expired(b->analyse_exp, now_ms)))
297 b->flags |= BF_ANA_TIMEOUT;
298}
299
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100300/* Schedule all remaining buffer data to be sent. send_max is not touched if it
301 * already covers those data. That permits doing a flush even after a forward,
302 * although not recommended.
303 */
304static inline void buffer_flush(struct buffer *buf)
305{
306 if (buf->send_max < buf->l)
307 buf->send_max = buf->l;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200308 if (buf->send_max)
309 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100310}
311
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100312/* Erase any content from buffer <buf> and adjusts flags accordingly. Note
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100313 * that any spliced data is not affected since we may not have any access to
314 * it.
Willy Tarreaue393fe22008-08-16 22:18:07 +0200315 */
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100316static inline void buffer_erase(struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200317{
Willy Tarreauf890dc92008-12-13 21:12:26 +0100318 buf->send_max = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100319 buf->to_forward = 0;
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100320 buf->r = buf->lr = buf->w = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321 buf->l = 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200322 buf->flags &= ~(BF_FULL | BF_OUT_EMPTY);
323 if (!buf->pipe)
324 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325}
326
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200327/* Cut the "tail" of the buffer, which means strip it to the length of unsent
328 * data only, and kill any remaining unsent data. Any scheduled forwarding is
329 * stopped. This is mainly to be used to send error messages after existing
330 * data.
331 */
332static inline void buffer_cut_tail(struct buffer *buf)
333{
334 if (!buf->send_max)
335 return buffer_erase(buf);
336
337 buf->to_forward = 0;
338 if (buf->l == buf->send_max)
339 return;
340
341 buf->l = buf->send_max;
342 buf->r = buf->w + buf->l;
343 if (buf->r >= buf->data + buf->size)
344 buf->r -= buf->size;
345 buf->lr = buf->r;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200346 buf->flags &= ~BF_FULL;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100347 if (buf->l >= buffer_max_len(buf))
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200348 buf->flags |= BF_FULL;
349}
350
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100351/* Cut the <n> next unsent bytes of the buffer. The caller must ensure that <n>
352 * is smaller than the actual buffer's length. This is mainly used to remove
353 * empty lines at the beginning of a request or a response.
354 */
355static inline void buffer_ignore(struct buffer *buf, int n)
356{
357 buf->l -= n;
358 buf->w += n;
359 if (buf->w >= buf->data + buf->size)
360 buf->w -= buf->size;
361 buf->flags &= ~BF_FULL;
362 if (buf->l >= buffer_max_len(buf))
363 buf->flags |= BF_FULL;
364}
365
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200366/* marks the buffer as "shutdown" ASAP for reads */
367static inline void buffer_shutr_now(struct buffer *buf)
368{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100369 buf->flags |= BF_SHUTR_NOW;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200370}
371
372/* marks the buffer as "shutdown" ASAP for writes */
373static inline void buffer_shutw_now(struct buffer *buf)
374{
375 buf->flags |= BF_SHUTW_NOW;
376}
377
378/* marks the buffer as "shutdown" ASAP in both directions */
379static inline void buffer_abort(struct buffer *buf)
380{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100381 buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW;
Willy Tarreaue4599762010-03-21 23:25:09 +0100382 buf->flags &= ~BF_AUTO_CONNECT;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200383}
384
Willy Tarreau01bf8672008-12-07 18:03:29 +0100385/* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack
386 * flag is set, and the function called once. The function is responsible for
387 * clearing the hijack bit. It is possible that the function clears the flag
388 * during this first call.
389 */
390static inline void buffer_install_hijacker(struct session *s,
391 struct buffer *b,
392 void (*func)(struct session *, struct buffer *))
Willy Tarreau72b179a2008-08-28 16:01:32 +0200393{
Willy Tarreau01bf8672008-12-07 18:03:29 +0100394 b->hijacker = func;
395 b->flags |= BF_HIJACK;
396 func(s, b);
Willy Tarreau72b179a2008-08-28 16:01:32 +0200397}
398
Willy Tarreau01bf8672008-12-07 18:03:29 +0100399/* Releases the buffer from hijacking mode. Often used by the hijack function */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200400static inline void buffer_stop_hijack(struct buffer *buf)
401{
402 buf->flags &= ~BF_HIJACK;
403}
404
Willy Tarreau520d95e2009-09-19 21:04:57 +0200405/* allow the consumer to try to establish a new connection. */
406static inline void buffer_auto_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200407{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200408 buf->flags |= BF_AUTO_CONNECT;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200409}
410
Willy Tarreau520d95e2009-09-19 21:04:57 +0200411/* prevent the consumer from trying to establish a new connection, and also
412 * disable auto shutdown forwarding.
413 */
414static inline void buffer_dont_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200415{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200416 buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200417}
418
Willy Tarreau520d95e2009-09-19 21:04:57 +0200419/* allow the producer to forward shutdown requests */
420static inline void buffer_auto_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100421{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200422 buf->flags |= BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100423}
424
Willy Tarreau520d95e2009-09-19 21:04:57 +0200425/* prevent the producer from forwarding shutdown requests */
426static inline void buffer_dont_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100427{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200428 buf->flags &= ~BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100429}
430
Willy Tarreau90deb182010-01-07 00:20:41 +0100431/* allow the producer to read / poll the input */
432static inline void buffer_auto_read(struct buffer *buf)
433{
434 buf->flags &= ~BF_DONT_READ;
435}
436
437/* prevent the producer from read / poll the input */
438static inline void buffer_dont_read(struct buffer *buf)
439{
440 buf->flags |= BF_DONT_READ;
441}
442
Willy Tarreaubaaee002006-06-26 02:48:02 +0200443/*
444 * Tries to realign the given buffer, and returns how many bytes can be written
445 * there at once without overwriting anything.
446 */
447static inline int buffer_realign(struct buffer *buf)
448{
449 if (buf->l == 0) {
450 /* let's realign the buffer to optimize I/O */
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100451 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200452 }
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100453 return buffer_contig_space(buf);
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200454}
455
456/*
457 * Advance the buffer's read pointer by <len> bytes. This is useful when data
458 * have been read directly from the buffer. It is illegal to call this function
459 * with <len> causing a wrapping at the end of the buffer. It's the caller's
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200460 * responsibility to ensure that <len> is never larger than buf->send_max.
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200461 */
462static inline void buffer_skip(struct buffer *buf, int len)
463{
464 buf->w += len;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200465 if (buf->w >= buf->data + buf->size)
466 buf->w -= buf->size; /* wrap around the buffer */
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200467
468 buf->l -= len;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200469 if (!buf->l)
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200470 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200471
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100472 if (buf->l < buffer_max_len(buf))
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200473 buf->flags &= ~BF_FULL;
474
475 buf->send_max -= len;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200476 if (!buf->send_max && !buf->pipe)
477 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200478
479 /* notify that some data was written to the SI from the buffer */
480 buf->flags |= BF_WRITE_PARTIAL;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200481}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200482
Willy Tarreauaeac3192009-08-31 08:09:57 +0200483/* writes the chunk <chunk> to buffer <buf>. Returns -1 in case of success,
484 * -2 if it is larger than the buffer size, or the number of bytes available
485 * otherwise. If the chunk has been written, its size is automatically reset
486 * to zero. The send limit is automatically adjusted with the amount of data
487 * written.
488 */
489static inline int buffer_write_chunk(struct buffer *buf, struct chunk *chunk)
490{
491 int ret;
492
493 ret = buffer_write(buf, chunk->str, chunk->len);
494 if (ret == -1)
495 chunk->len = 0;
496 return ret;
497}
498
Willy Tarreau74b08c92010-09-08 17:04:31 +0200499/* Tries to copy chunk <chunk> into buffer <buf> after length controls.
500 * The send_max and to_forward pointers are updated. If the buffer's input is
501 * closed, -2 is returned. If the block is too large for this buffer, -3 is
502 * returned. If there is not enough room left in the buffer, -1 is returned.
503 * Otherwise the number of bytes copied is returned (0 being a valid number).
504 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
505 * transferred. The chunk's length is updated with the number of bytes sent.
Willy Tarreauaeac3192009-08-31 08:09:57 +0200506 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200507static inline int buffer_put_chunk(struct buffer *buf, struct chunk *chunk)
Willy Tarreauaeac3192009-08-31 08:09:57 +0200508{
509 int ret;
510
Willy Tarreau74b08c92010-09-08 17:04:31 +0200511 ret = buffer_put_block(buf, chunk->str, chunk->len);
512 if (ret > 0)
513 chunk->len -= ret;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200514 return ret;
515}
516
Willy Tarreau74b08c92010-09-08 17:04:31 +0200517/* Tries to copy string <str> at once into buffer <buf> after length controls.
518 * The send_max and to_forward pointers are updated. If the buffer's input is
519 * closed, -2 is returned. If the block is too large for this buffer, -3 is
520 * returned. If there is not enough room left in the buffer, -1 is returned.
521 * Otherwise the number of bytes copied is returned (0 being a valid number).
522 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
523 * transferred.
524 */
525static inline int buffer_put_string(struct buffer *buf, const char *str)
526{
527 return buffer_put_block(buf, str, strlen(str));
528}
529
530/*
531 * Return one char from the buffer. If the buffer is empty and closed, return -2.
532 * If the buffer is just empty, return -1. The buffer's pointer is not advanced,
533 * it's up to the caller to call buffer_skip(buf, 1) when it has consumed the char.
534 * Also note that this function respects the send_max limit.
535 */
536static inline int buffer_get_char(struct buffer *buf)
537{
538 /* closed or empty + imminent close = -2; empty = -1 */
539 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
540 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
541 return -2;
542 return -1;
543 }
544 return *buf->w;
545}
546
547
548/* DEPRECATED, just provided for compatibility, use buffer_put_chunk() instead !!!
549 * returns >= 0 if the buffer is too small to hold the message, -1 if the
550 * transfer was OK, -2 in case of failure.
551 */
552static inline int buffer_feed_chunk(struct buffer *buf, struct chunk *msg)
553{
554 int ret = buffer_put_chunk(buf, msg);
555 if (ret >= 0) /* transfer OK */
556 return -1;
557 if (ret == -1) /* missing room */
558 return 1;
559 /* failure */
560 return -2;
561}
562
563/* DEPRECATED, just provided for compatibility, use buffer_put_string() instead !!!
564 * returns >= 0 if the buffer is too small to hold the message, -1 if the
565 * transfer was OK, -2 in case of failure.
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200566 */
567static inline int buffer_feed(struct buffer *buf, const char *str)
568{
Willy Tarreau74b08c92010-09-08 17:04:31 +0200569 int ret = buffer_put_string(buf, str);
570 if (ret >= 0) /* transfer OK */
571 return -1;
572 if (ret == -1) /* missing room */
573 return 1;
574 /* failure */
575 return -2;
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200576}
577
Willy Tarreau74b08c92010-09-08 17:04:31 +0200578/*
579 *
580 * Functions below are used to manage chunks
581 *
582 */
583
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200584static inline void chunk_init(struct chunk *chk, char *str, size_t size) {
585 chk->str = str;
586 chk->len = 0;
587 chk->size = size;
588}
589
590/* report 0 in case of error, 1 if OK. */
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200591static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200592
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200593 if (size && len > size)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200594 return 0;
595
596 chk->str = str;
597 chk->len = len;
598 chk->size = size;
599
600 return 1;
601}
602
603static inline void chunk_initstr(struct chunk *chk, char *str) {
604 chk->str = str;
605 chk->len = strlen(str);
606 chk->size = 0; /* mark it read-only */
607}
608
609static inline int chunk_strcpy(struct chunk *chk, const char *str) {
610 size_t len;
611
612 len = strlen(str);
613
614 if (unlikely(len > chk->size))
615 return 0;
616
617 chk->len = len;
618 memcpy(chk->str, str, len);
619
620 return 1;
621}
622
623int chunk_printf(struct chunk *chk, const char *fmt, ...)
624 __attribute__ ((format(printf, 2, 3)));
625
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200626int chunk_htmlencode(struct chunk *dst, struct chunk *src);
627int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
628
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200629static inline void chunk_reset(struct chunk *chk) {
630 chk->str = NULL;
631 chk->len = -1;
632 chk->size = 0;
633}
634
635static inline void chunk_destroy(struct chunk *chk) {
636
637 if (!chk->size)
638 return;
639
640 if (chk->str)
641 free(chk->str);
642
643 chunk_reset(chk);
644}
645
Willy Tarreau0f772532006-12-23 20:51:41 +0100646/*
647 * frees the destination chunk if already allocated, allocates a new string,
648 * and copies the source into it. The pointer to the destination string is
649 * returned, or NULL if the allocation fails or if any pointer is NULL..
650 */
651static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
652 if (!dst || !src || !src->str)
653 return NULL;
654 if (dst->str)
655 free(dst->str);
656 dst->len = src->len;
657 dst->str = (char *)malloc(dst->len);
658 memcpy(dst->str, src->str, dst->len);
659 return dst->str;
660}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200661
662#endif /* _PROTO_BUFFERS_H */
663
664/*
665 * Local variables:
666 * c-indent-level: 8
667 * c-basic-offset: 8
668 * End:
669 */