blob: 2388b15856c7d621453eac1cc4811ac98bf3fdbc [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);
53
Willy Tarreau7c3c5412009-12-13 15:53:05 +010054/* Initialize all fields in the buffer. The BF_OUT_EMPTY flags is set. */
Willy Tarreau54469402006-07-29 16:59:06 +020055static inline void buffer_init(struct buffer *buf)
56{
Willy Tarreauf890dc92008-12-13 21:12:26 +010057 buf->send_max = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +010058 buf->to_forward = 0;
Willy Tarreaue393fe22008-08-16 22:18:07 +020059 buf->l = buf->total = 0;
Willy Tarreau3eba98a2009-01-25 13:56:13 +010060 buf->pipe = NULL;
Willy Tarreau2df28e82008-08-17 15:20:19 +020061 buf->analysers = 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +020062 buf->cons = NULL;
Willy Tarreauba0b63d2009-09-20 08:09:44 +020063 buf->flags = BF_OUT_EMPTY;
Willy Tarreau2df28e82008-08-17 15:20:19 +020064 buf->r = buf->lr = buf->w = buf->data;
Willy Tarreau54469402006-07-29 16:59:06 +020065}
66
Willy Tarreau7c3c5412009-12-13 15:53:05 +010067/* Return the max number of bytes the buffer can contain so that once all the
68 * pending bytes are forwarded, the buffer still has global.tune.maxrewrite
69 * bytes free. The result sits between buf->size - maxrewrite and buf->size.
70 */
71static inline int buffer_max_len(struct buffer *buf)
72{
73 if (buf->to_forward == BUF_INFINITE_FORWARD ||
74 buf->to_forward + buf->send_max >= global.tune.maxrewrite)
75 return buf->size;
76 else
77 return buf->size - global.tune.maxrewrite + buf->to_forward + buf->send_max;
78}
79
Willy Tarreau74b08c92010-09-08 17:04:31 +020080/* Returns true if the buffer's input is already closed */
81static inline int buffer_input_closed(struct buffer *buf)
82{
83 return ((buf->flags & BF_SHUTR) != 0);
84}
85
86/* Returns true if the buffer's output is already closed */
87static inline int buffer_output_closed(struct buffer *buf)
88{
89 return ((buf->flags & BF_SHUTW) != 0);
90}
91
Willy Tarreau2eb52f02008-09-04 09:14:08 +020092/* Check buffer timeouts, and set the corresponding flags. The
93 * likely/unlikely have been optimized for fastest normal path.
Willy Tarreaudd80c6f2008-12-13 22:25:59 +010094 * The read/write timeouts are not set if there was activity on the buffer.
95 * That way, we don't have to update the timeout on every I/O. Note that the
96 * analyser timeout is always checked.
Willy Tarreau2eb52f02008-09-04 09:14:08 +020097 */
98static inline void buffer_check_timeouts(struct buffer *b)
99{
Willy Tarreau86491c32008-12-14 09:04:47 +0100100 if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200101 unlikely(tick_is_expired(b->rex, now_ms)))
102 b->flags |= BF_READ_TIMEOUT;
103
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100104 if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200105 unlikely(tick_is_expired(b->wex, now_ms)))
106 b->flags |= BF_WRITE_TIMEOUT;
107
108 if (likely(!(b->flags & BF_ANA_TIMEOUT)) &&
109 unlikely(tick_is_expired(b->analyse_exp, now_ms)))
110 b->flags |= BF_ANA_TIMEOUT;
111}
112
Willy Tarreaud8ee85a2011-03-28 16:06:28 +0200113/* Schedule up to <bytes> more bytes to be forwarded by the buffer without notifying
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100114 * the task. Any pending data in the buffer is scheduled to be sent as well,
115 * in the limit of the number of bytes to forward. This must be the only method
Willy Tarreaud8ee85a2011-03-28 16:06:28 +0200116 * to use to schedule bytes to be sent. If the requested number is too large, it
117 * is automatically adjusted. The number of bytes taken into account is returned.
118 * Directly touching ->to_forward will cause lockups when send_max goes down to
119 * zero if nobody is ready to push the remaining data.
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100120 */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +0200121static inline unsigned long long buffer_forward(struct buffer *buf, unsigned long long bytes)
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100122{
Willy Tarreaud8ee85a2011-03-28 16:06:28 +0200123 unsigned int data_left;
124 unsigned int new_forward;
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100125
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200126 if (!bytes)
Willy Tarreaud8ee85a2011-03-28 16:06:28 +0200127 return 0;
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100128 data_left = buf->l - buf->send_max;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +0200129 if (bytes <= (unsigned long long)data_left) {
Willy Tarreau91aa5772009-09-15 20:32:30 +0200130 buf->send_max += bytes;
Willy Tarreau2d028db2009-09-20 22:56:25 +0200131 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +0200132 return bytes;
Willy Tarreau91aa5772009-09-15 20:32:30 +0200133 }
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100134
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100135 buf->send_max += data_left;
Willy Tarreau2d028db2009-09-20 22:56:25 +0200136 if (buf->send_max)
137 buf->flags &= ~BF_OUT_EMPTY;
138
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100139 if (buf->l < buffer_max_len(buf))
140 buf->flags &= ~BF_FULL;
141 else
142 buf->flags |= BF_FULL;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +0200143
144 if (likely(bytes == BUF_INFINITE_FORWARD)) {
145 buf->to_forward = bytes;
146 return bytes;
147 }
148
149 /* Note: the case below is the only case where we may return
150 * a byte count that does not fit into a 32-bit number.
151 */
152 if (likely(buf->to_forward == BUF_INFINITE_FORWARD))
153 return bytes;
154
155 new_forward = buf->to_forward + bytes - data_left;
156 bytes = data_left; /* at least those bytes were scheduled */
157
158 if (new_forward <= buf->to_forward) {
159 /* integer overflow detected, let's assume no more than 2G at once */
160 new_forward = MID_RANGE(new_forward);
161 }
162
163 if (new_forward > buf->to_forward) {
164 bytes += new_forward - buf->to_forward;
165 buf->to_forward = new_forward;
166 }
167 return bytes;
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100168}
169
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100170/* Schedule all remaining buffer data to be sent. send_max is not touched if it
171 * already covers those data. That permits doing a flush even after a forward,
172 * although not recommended.
173 */
174static inline void buffer_flush(struct buffer *buf)
175{
176 if (buf->send_max < buf->l)
177 buf->send_max = buf->l;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200178 if (buf->send_max)
179 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100180}
181
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100182/* Erase any content from buffer <buf> and adjusts flags accordingly. Note
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100183 * that any spliced data is not affected since we may not have any access to
184 * it.
Willy Tarreaue393fe22008-08-16 22:18:07 +0200185 */
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100186static inline void buffer_erase(struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187{
Willy Tarreauf890dc92008-12-13 21:12:26 +0100188 buf->send_max = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100189 buf->to_forward = 0;
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100190 buf->r = buf->lr = buf->w = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191 buf->l = 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200192 buf->flags &= ~(BF_FULL | BF_OUT_EMPTY);
193 if (!buf->pipe)
194 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195}
196
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200197/* Cut the "tail" of the buffer, which means strip it to the length of unsent
198 * data only, and kill any remaining unsent data. Any scheduled forwarding is
199 * stopped. This is mainly to be used to send error messages after existing
200 * data.
201 */
202static inline void buffer_cut_tail(struct buffer *buf)
203{
204 if (!buf->send_max)
205 return buffer_erase(buf);
206
207 buf->to_forward = 0;
208 if (buf->l == buf->send_max)
209 return;
210
211 buf->l = buf->send_max;
212 buf->r = buf->w + buf->l;
213 if (buf->r >= buf->data + buf->size)
214 buf->r -= buf->size;
215 buf->lr = buf->r;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200216 buf->flags &= ~BF_FULL;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100217 if (buf->l >= buffer_max_len(buf))
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200218 buf->flags |= BF_FULL;
219}
220
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100221/* Cut the <n> next unsent bytes of the buffer. The caller must ensure that <n>
222 * is smaller than the actual buffer's length. This is mainly used to remove
223 * empty lines at the beginning of a request or a response.
224 */
225static inline void buffer_ignore(struct buffer *buf, int n)
226{
227 buf->l -= n;
228 buf->w += n;
229 if (buf->w >= buf->data + buf->size)
230 buf->w -= buf->size;
231 buf->flags &= ~BF_FULL;
232 if (buf->l >= buffer_max_len(buf))
233 buf->flags |= BF_FULL;
234}
235
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200236/* marks the buffer as "shutdown" ASAP for reads */
237static inline void buffer_shutr_now(struct buffer *buf)
238{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100239 buf->flags |= BF_SHUTR_NOW;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200240}
241
242/* marks the buffer as "shutdown" ASAP for writes */
243static inline void buffer_shutw_now(struct buffer *buf)
244{
245 buf->flags |= BF_SHUTW_NOW;
246}
247
248/* marks the buffer as "shutdown" ASAP in both directions */
249static inline void buffer_abort(struct buffer *buf)
250{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100251 buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW;
Willy Tarreaue4599762010-03-21 23:25:09 +0100252 buf->flags &= ~BF_AUTO_CONNECT;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200253}
254
Willy Tarreau01bf8672008-12-07 18:03:29 +0100255/* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack
256 * flag is set, and the function called once. The function is responsible for
257 * clearing the hijack bit. It is possible that the function clears the flag
258 * during this first call.
259 */
260static inline void buffer_install_hijacker(struct session *s,
261 struct buffer *b,
262 void (*func)(struct session *, struct buffer *))
Willy Tarreau72b179a2008-08-28 16:01:32 +0200263{
Willy Tarreau01bf8672008-12-07 18:03:29 +0100264 b->hijacker = func;
265 b->flags |= BF_HIJACK;
266 func(s, b);
Willy Tarreau72b179a2008-08-28 16:01:32 +0200267}
268
Willy Tarreau01bf8672008-12-07 18:03:29 +0100269/* Releases the buffer from hijacking mode. Often used by the hijack function */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200270static inline void buffer_stop_hijack(struct buffer *buf)
271{
272 buf->flags &= ~BF_HIJACK;
273}
274
Willy Tarreau520d95e2009-09-19 21:04:57 +0200275/* allow the consumer to try to establish a new connection. */
276static inline void buffer_auto_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200277{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200278 buf->flags |= BF_AUTO_CONNECT;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200279}
280
Willy Tarreau520d95e2009-09-19 21:04:57 +0200281/* prevent the consumer from trying to establish a new connection, and also
282 * disable auto shutdown forwarding.
283 */
284static inline void buffer_dont_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200285{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200286 buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200287}
288
Willy Tarreau520d95e2009-09-19 21:04:57 +0200289/* allow the producer to forward shutdown requests */
290static inline void buffer_auto_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100291{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200292 buf->flags |= BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100293}
294
Willy Tarreau520d95e2009-09-19 21:04:57 +0200295/* prevent the producer from forwarding shutdown requests */
296static inline void buffer_dont_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100297{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200298 buf->flags &= ~BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100299}
300
Willy Tarreau90deb182010-01-07 00:20:41 +0100301/* allow the producer to read / poll the input */
302static inline void buffer_auto_read(struct buffer *buf)
303{
304 buf->flags &= ~BF_DONT_READ;
305}
306
307/* prevent the producer from read / poll the input */
308static inline void buffer_dont_read(struct buffer *buf)
309{
310 buf->flags |= BF_DONT_READ;
311}
312
Willy Tarreaubaaee002006-06-26 02:48:02 +0200313/* returns the maximum number of bytes writable at once in this buffer */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200314static inline int buffer_max(const struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200315{
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200316 if (buf->l == buf->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200317 return 0;
318 else if (buf->r >= buf->w)
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200319 return buf->data + buf->size - buf->r;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320 else
321 return buf->w - buf->r;
322}
323
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324/*
325 * Tries to realign the given buffer, and returns how many bytes can be written
326 * there at once without overwriting anything.
327 */
328static inline int buffer_realign(struct buffer *buf)
329{
330 if (buf->l == 0) {
331 /* let's realign the buffer to optimize I/O */
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100332 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333 }
334 return buffer_max(buf);
335}
336
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200337/*
Willy Tarreau591fedc2010-08-10 15:28:21 +0200338 * Return the maximum amount of bytes that can be written into the buffer in
Willy Tarreau74b08c92010-09-08 17:04:31 +0200339 * one call to buffer_put_*().
Willy Tarreau591fedc2010-08-10 15:28:21 +0200340 */
341static inline int buffer_free_space(struct buffer *buf)
342{
343 return buffer_max_len(buf) - buf->l;
344}
345
346/*
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200347 * Return the max amount of bytes that can be stuffed into the buffer at once.
348 * Note that this may be lower than the actual buffer size when the free space
349 * wraps after the end, so it's preferable to call this function again after
350 * writing. Also note that this function respects max_len.
351 */
352static inline int buffer_contig_space(struct buffer *buf)
353{
354 int ret;
355
356 if (buf->l == 0) {
357 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100358 ret = buffer_max_len(buf);
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200359 }
360 else if (buf->r > buf->w) {
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100361 ret = buf->data + buffer_max_len(buf) - buf->r;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200362 }
363 else {
364 ret = buf->w - buf->r;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100365 if (ret > buffer_max_len(buf))
366 ret = buffer_max_len(buf);
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200367 }
368 return ret;
369}
370
Willy Tarreau591fedc2010-08-10 15:28:21 +0200371/*
372 * Same as above but the caller may pass the value of buffer_max_len(buf) if it
373 * knows it, thus avoiding some calculations.
374 */
375static inline int buffer_contig_space_with_len(struct buffer *buf, int maxlen)
376{
377 int ret;
378
379 if (buf->l == 0) {
380 buf->r = buf->w = buf->lr = buf->data;
381 ret = maxlen;
382 }
383 else if (buf->r > buf->w) {
384 ret = buf->data + maxlen - buf->r;
385 }
386 else {
387 ret = buf->w - buf->r;
388 if (ret > maxlen)
389 ret = maxlen;
390 }
391 return ret;
392}
393
Willy Tarreau4e33d862009-10-11 23:35:10 +0200394/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
395static inline int buffer_almost_full(struct buffer *buf)
396{
Willy Tarreau591fedc2010-08-10 15:28:21 +0200397 if (buffer_free_space(buf) < buf->size / 4)
Willy Tarreau4e33d862009-10-11 23:35:10 +0200398 return 1;
399 return 0;
400}
401
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200402/*
403 * Return the max amount of bytes that can be read from the buffer at once.
404 * Note that this may be lower than the actual buffer length when the data
405 * wrap after the end, so it's preferable to call this function again after
406 * reading. Also note that this function respects the send_max limit.
407 */
408static inline int buffer_contig_data(struct buffer *buf)
409{
410 int ret;
411
412 if (!buf->send_max || !buf->l)
413 return 0;
414
415 if (buf->r > buf->w)
416 ret = buf->r - buf->w;
417 else
418 ret = buf->data + buf->size - buf->w;
419
420 /* limit the amount of outgoing data if required */
421 if (ret > buf->send_max)
422 ret = buf->send_max;
423
424 return ret;
425}
426
427/*
428 * Advance the buffer's read pointer by <len> bytes. This is useful when data
429 * have been read directly from the buffer. It is illegal to call this function
430 * with <len> causing a wrapping at the end of the buffer. It's the caller's
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200431 * responsibility to ensure that <len> is never larger than buf->send_max.
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200432 */
433static inline void buffer_skip(struct buffer *buf, int len)
434{
435 buf->w += len;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200436 if (buf->w >= buf->data + buf->size)
437 buf->w -= buf->size; /* wrap around the buffer */
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200438
439 buf->l -= len;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200440 if (!buf->l)
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200441 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200442
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100443 if (buf->l < buffer_max_len(buf))
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200444 buf->flags &= ~BF_FULL;
445
446 buf->send_max -= len;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200447 if (!buf->send_max && !buf->pipe)
448 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200449
450 /* notify that some data was written to the SI from the buffer */
451 buf->flags |= BF_WRITE_PARTIAL;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200452}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200453
Willy Tarreauaeac3192009-08-31 08:09:57 +0200454/* writes the chunk <chunk> to buffer <buf>. Returns -1 in case of success,
455 * -2 if it is larger than the buffer size, or the number of bytes available
456 * otherwise. If the chunk has been written, its size is automatically reset
457 * to zero. The send limit is automatically adjusted with the amount of data
458 * written.
459 */
460static inline int buffer_write_chunk(struct buffer *buf, struct chunk *chunk)
461{
462 int ret;
463
464 ret = buffer_write(buf, chunk->str, chunk->len);
465 if (ret == -1)
466 chunk->len = 0;
467 return ret;
468}
469
Willy Tarreau74b08c92010-09-08 17:04:31 +0200470/* Tries to copy chunk <chunk> into buffer <buf> after length controls.
471 * The send_max and to_forward pointers are updated. If the buffer's input is
472 * closed, -2 is returned. If the block is too large for this buffer, -3 is
473 * returned. If there is not enough room left in the buffer, -1 is returned.
474 * Otherwise the number of bytes copied is returned (0 being a valid number).
475 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
476 * transferred. The chunk's length is updated with the number of bytes sent.
Willy Tarreauaeac3192009-08-31 08:09:57 +0200477 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200478static inline int buffer_put_chunk(struct buffer *buf, struct chunk *chunk)
Willy Tarreauaeac3192009-08-31 08:09:57 +0200479{
480 int ret;
481
Willy Tarreau74b08c92010-09-08 17:04:31 +0200482 ret = buffer_put_block(buf, chunk->str, chunk->len);
483 if (ret > 0)
484 chunk->len -= ret;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200485 return ret;
486}
487
Willy Tarreau74b08c92010-09-08 17:04:31 +0200488/* Tries to copy string <str> at once into buffer <buf> after length controls.
489 * The send_max and to_forward pointers are updated. If the buffer's input is
490 * closed, -2 is returned. If the block is too large for this buffer, -3 is
491 * returned. If there is not enough room left in the buffer, -1 is returned.
492 * Otherwise the number of bytes copied is returned (0 being a valid number).
493 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
494 * transferred.
495 */
496static inline int buffer_put_string(struct buffer *buf, const char *str)
497{
498 return buffer_put_block(buf, str, strlen(str));
499}
500
501/*
502 * Return one char from the buffer. If the buffer is empty and closed, return -2.
503 * If the buffer is just empty, return -1. The buffer's pointer is not advanced,
504 * it's up to the caller to call buffer_skip(buf, 1) when it has consumed the char.
505 * Also note that this function respects the send_max limit.
506 */
507static inline int buffer_get_char(struct buffer *buf)
508{
509 /* closed or empty + imminent close = -2; empty = -1 */
510 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
511 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
512 return -2;
513 return -1;
514 }
515 return *buf->w;
516}
517
518
519/* DEPRECATED, just provided for compatibility, use buffer_put_chunk() instead !!!
520 * returns >= 0 if the buffer is too small to hold the message, -1 if the
521 * transfer was OK, -2 in case of failure.
522 */
523static inline int buffer_feed_chunk(struct buffer *buf, struct chunk *msg)
524{
525 int ret = buffer_put_chunk(buf, msg);
526 if (ret >= 0) /* transfer OK */
527 return -1;
528 if (ret == -1) /* missing room */
529 return 1;
530 /* failure */
531 return -2;
532}
533
534/* DEPRECATED, just provided for compatibility, use buffer_put_string() instead !!!
535 * returns >= 0 if the buffer is too small to hold the message, -1 if the
536 * transfer was OK, -2 in case of failure.
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200537 */
538static inline int buffer_feed(struct buffer *buf, const char *str)
539{
Willy Tarreau74b08c92010-09-08 17:04:31 +0200540 int ret = buffer_put_string(buf, str);
541 if (ret >= 0) /* transfer OK */
542 return -1;
543 if (ret == -1) /* missing room */
544 return 1;
545 /* failure */
546 return -2;
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200547}
548
Willy Tarreau74b08c92010-09-08 17:04:31 +0200549/*
550 *
551 * Functions below are used to manage chunks
552 *
553 */
554
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200555static inline void chunk_init(struct chunk *chk, char *str, size_t size) {
556 chk->str = str;
557 chk->len = 0;
558 chk->size = size;
559}
560
561/* report 0 in case of error, 1 if OK. */
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200562static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200563
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200564 if (size && len > size)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200565 return 0;
566
567 chk->str = str;
568 chk->len = len;
569 chk->size = size;
570
571 return 1;
572}
573
574static inline void chunk_initstr(struct chunk *chk, char *str) {
575 chk->str = str;
576 chk->len = strlen(str);
577 chk->size = 0; /* mark it read-only */
578}
579
580static inline int chunk_strcpy(struct chunk *chk, const char *str) {
581 size_t len;
582
583 len = strlen(str);
584
585 if (unlikely(len > chk->size))
586 return 0;
587
588 chk->len = len;
589 memcpy(chk->str, str, len);
590
591 return 1;
592}
593
594int chunk_printf(struct chunk *chk, const char *fmt, ...)
595 __attribute__ ((format(printf, 2, 3)));
596
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200597int chunk_htmlencode(struct chunk *dst, struct chunk *src);
598int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
599
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200600static inline void chunk_reset(struct chunk *chk) {
601 chk->str = NULL;
602 chk->len = -1;
603 chk->size = 0;
604}
605
606static inline void chunk_destroy(struct chunk *chk) {
607
608 if (!chk->size)
609 return;
610
611 if (chk->str)
612 free(chk->str);
613
614 chunk_reset(chk);
615}
616
Willy Tarreau0f772532006-12-23 20:51:41 +0100617/*
618 * frees the destination chunk if already allocated, allocates a new string,
619 * and copies the source into it. The pointer to the destination string is
620 * returned, or NULL if the allocation fails or if any pointer is NULL..
621 */
622static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
623 if (!dst || !src || !src->str)
624 return NULL;
625 if (dst->str)
626 free(dst->str);
627 dst->len = src->len;
628 dst->str = (char *)malloc(dst->len);
629 memcpy(dst->str, src->str, dst->len);
630 return dst->str;
631}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632
633#endif /* _PROTO_BUFFERS_H */
634
635/*
636 * Local variables:
637 * c-indent-level: 8
638 * c-basic-offset: 8
639 * End:
640 */