[MEDIUM] buffers: add BF_EMPTY and BF_FULL to remove dependency on req/rep->l

It is not always convenient to run checks on req->l in functions to
check if a buffer is empty or full. Now the stream_sock functions
set flags BF_EMPTY and BF_FULL according to the buffer contents. Of
course, functions which touch the buffer contents adjust the flags
too.
diff --git a/include/proto/buffers.h b/include/proto/buffers.h
index 4090ead..27bf2ea 100644
--- a/include/proto/buffers.h
+++ b/include/proto/buffers.h
@@ -40,12 +40,15 @@
 
 /* Initializes all fields in the buffer. The ->rlim field is initialized last
  * so that the compiler can optimize it away if changed immediately after the
- * call to this function.
+ * call to this function. By default, it is set to the full size of the buffer.
+ * The BF_EMPTY flags is set.
  */
 static inline void buffer_init(struct buffer *buf)
 {
-	buf->l = buf->total = buf->flags = 0;
-	buf->rlim = buf->r = buf->lr = buf->w = buf->data;
+	buf->l = buf->total = 0;
+	buf->r = buf->lr = buf->w = buf->data;
+	buf->flags = BF_EMPTY;
+	buf->rlim = buf->data + BUFSIZE;
 }
 
 /* returns 1 if the buffer is empty, 0 otherwise */
@@ -59,11 +62,16 @@
 	return buf->l == BUFSIZE;
 }
 
-/* flushes any content from buffer <buf> */
+/* flushes any content from buffer <buf> and adjusts flags
+ * accordingly.
+ */
 static inline void buffer_flush(struct buffer *buf)
 {
 	buf->r = buf->lr = buf->w = buf->data;
 	buf->l = 0;
+	buf->flags |= BF_EMPTY | BF_FULL;
+	if (buf->rlim)
+		buf->flags &= ~BF_FULL;
 }
 
 /* marks the buffer as "shutdown" for reads and cancels the timeout */
@@ -91,6 +99,17 @@
 		return buf->w - buf->r;
 }
 
+/* sets the buffer read limit to <size> bytes, and adjusts the FULL
+ * flag accordingly.
+ */
+static inline void buffer_set_rlim(struct buffer *buf, int size)
+{
+	buf->rlim = buf->data + size;
+	if (buf->l < size)
+		buf->flags &= ~BF_FULL;
+	else
+		buf->flags |= BF_FULL;
+}
 
 /*
  * Tries to realign the given buffer, and returns how many bytes can be written
diff --git a/include/types/buffers.h b/include/types/buffers.h
index be93693..0c3bb37 100644
--- a/include/types/buffers.h
+++ b/include/types/buffers.h
@@ -26,8 +26,13 @@
 #include <common/memory.h>
 
 /* The BF_* macros designate Buffer Flags, which may be ORed in the bit field
- * member 'flags' in struct buffer.
+ * member 'flags' in struct buffer. Some of them are persistent (BF_SHUT*),
+ * some of them (BF_EMPTY,BF_FULL) may only be set by the low-level read/write
+ * functions as well as those who change the buffer's read limit.
  */
+#define BF_EMPTY                1  /* buffer is empty */
+#define BF_FULL                 2  /* buffer cannot accept any more data (l >= rlim-data) */
+
 #define BF_SHUTR                4  /* producer has already shut down */
 #define BF_SHUTW                8  /* consumer has already shut down */
 
diff --git a/src/buffers.c b/src/buffers.c
index fa6b8a9..f7e672d 100644
--- a/src/buffers.c
+++ b/src/buffers.c
@@ -49,6 +49,12 @@
 	if (buf->r == buf->data + BUFSIZE)
 		buf->r = buf->data;
 
+	buf->flags &= ~(BF_EMPTY|BF_FULL);
+	if (buf->l == 0)
+		buf->flags |= BF_EMPTY;
+	if (buf->l >= buf->rlim - buf->data)
+		buf->flags |= BF_FULL;
+
 	return -1;
 }
 
@@ -74,8 +80,14 @@
 	buf->total += chunk->len;
 	if (buf->r == buf->data + BUFSIZE)
 		buf->r = buf->data;
-	chunk->len = 0;
 
+	buf->flags &= ~(BF_EMPTY|BF_FULL);
+	if (buf->l == 0)
+		buf->flags |= BF_EMPTY;
+	if (buf->l >= buf->rlim - buf->data)
+		buf->flags |= BF_FULL;
+
+	chunk->len = 0;
 	return -1;
 }
 
@@ -110,6 +122,12 @@
 	if (b->lr > pos) b->lr += delta;
 	b->l += delta;
 
+	b->flags &= ~(BF_EMPTY|BF_FULL);
+	if (b->l == 0)
+		b->flags |= BF_EMPTY;
+	if (b->l >= b->rlim - b->data)
+		b->flags |= BF_FULL;
+
 	return delta;
 }
 
@@ -145,6 +163,12 @@
 	if (b->lr > pos) b->lr += delta;
 	b->l += delta;
 
+	b->flags &= ~(BF_EMPTY|BF_FULL);
+	if (b->l == 0)
+		b->flags |= BF_EMPTY;
+	if (b->l >= b->rlim - b->data)
+		b->flags |= BF_FULL;
+
 	return delta;
 }
 
@@ -183,6 +207,12 @@
 	if (b->lr > pos) b->lr += delta;
 	b->l += delta;
 
+	b->flags &= ~(BF_EMPTY|BF_FULL);
+	if (b->l == 0)
+		b->flags |= BF_EMPTY;
+	if (b->l >= b->rlim - b->data)
+		b->flags |= BF_FULL;
+
 	return delta;
 }
 
diff --git a/src/client.c b/src/client.c
index 7135d71..67c6dc7 100644
--- a/src/client.c
+++ b/src/client.c
@@ -333,7 +333,6 @@
 			goto out_fail_req; /* no memory */
 
 		buffer_init(s->req);
-		s->req->rlim += BUFSIZE;
 		if (p->mode == PR_MODE_HTTP) /* reserve some space for header rewriting */
 			s->req->rlim -= MAXREWRITE;
 
diff --git a/src/proto_http.c b/src/proto_http.c
index acbe5dd..294ab37 100644
--- a/src/proto_http.c
+++ b/src/proto_http.c
@@ -1743,7 +1743,7 @@
 			 *    later, so the session will never terminate. We
 			 *    must terminate it now.
 			 */
-			if (unlikely(req->l >= req->rlim - req->data)) {
+			if (unlikely(req->flags & BF_FULL)) {
 				/* FIXME: check if URI is set and return Status
 				 * 414 Request URI too long instead.
 				 */
@@ -2331,7 +2331,7 @@
                  */
 		if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
 		     t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
-		     t->be->url_param_post_limit != 0 && req->l < BUFSIZE &&
+		     t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
 		     memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
 			/* are there enough bytes here? total == l || r || rlim ?
 			 * len is unsigned, but eoh is int,
@@ -2408,7 +2408,7 @@
 		 * could. Let's switch to the DATA state.                    *
 		 ************************************************************/
 
-		req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
+		buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
 		t->logs.tv_request = now;
 
 		/* When a connection is tarpitted, we use the tarpit timeout,
@@ -2418,7 +2418,7 @@
 		 * FIXME: this part should be moved elsewhere (eg: on the server side)
 		 */
 		if (txn->flags & TX_CLTARPIT) {
-			t->req->l = 0;
+			buffer_flush(t->req);
 			/* flush the request so that we can drop the connection early
 			 * if the client closes first.
 			 */
@@ -2502,8 +2502,7 @@
 		 * buffer closed).
 		 */
 		if (req->l - body >= limit ||             /* enough bytes! */
-		    req->l >= req->rlim - req->data ||    /* full */
-		    req->flags & (BF_READ_ERROR | BF_READ_NULL | BF_READ_TIMEOUT)) {
+		    req->flags & (BF_FULL | BF_READ_ERROR | BF_READ_NULL | BF_READ_TIMEOUT)) {
 			/* The situation will not evolve, so let's give up on the analysis. */
 			t->logs.tv_request = now;  /* update the request timer to reflect full request */
 			t->analysis &= ~AN_REQ_HTTP_BODY;
@@ -2542,9 +2541,9 @@
 		 * For the parsing, we use a 28 states FSM.
 		 *
 		 * Here is the information we currently have :
-		 *   rep->data + req->som  = beginning of response
-		 *   rep->data + req->eoh  = end of processed headers / start of current one
-		 *   rep->data + req->eol  = end of current header or line (LF or CRLF)
+		 *   rep->data + rep->som  = beginning of response
+		 *   rep->data + rep->eoh  = end of processed headers / start of current one
+		 *   rep->data + rep->eol  = end of current header or line (LF or CRLF)
 		 *   rep->lr = first non-visited byte
 		 *   rep->r  = end of data
 		 */
@@ -2644,7 +2643,7 @@
 				return 1;
 			}
 			/* too large response does not fit in buffer. */
-			else if (rep->l >= rep->rlim - rep->data) {
+			else if (rep->flags & BF_FULL) {
 				goto hdr_response_bad;
 			}
 			/* read timeout : return a 504 to the client. */
@@ -2948,7 +2947,7 @@
 		 * could. Let's switch to the DATA state.                    *
 		 ************************************************************/
 
-		rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
+		buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
 		t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
 
 #ifdef CONFIG_HAP_TCPSPLICE
@@ -3049,7 +3048,7 @@
 		 * allowed to forward the data.
 		 */
 		else if (!(rep->flags & BF_SHUTW) &&   /* already done */
-			 rep->l == 0 && rep->flags & BF_MAY_FORWARD &&
+			 rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
 			 rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)) {
 			buffer_shutw(rep);
 			if (!(req->flags & BF_SHUTR)) {
@@ -3129,7 +3128,7 @@
 
 		/* manage read timeout */
 		if (!(req->flags & BF_SHUTR)) {
-			if (req->l >= req->rlim - req->data) {
+			if (req->flags & BF_FULL) {
 				/* no room to read more data */
 				if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
 					/* stop reading until we get some space */
@@ -3148,7 +3147,7 @@
 			 */
 			if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
 				produce_content(t);
-				if (rep->l == 0) {
+				if (rep->flags & BF_EMPTY) {
 					buffer_shutw(rep);
 					fd_delete(t->cli_fd);
 					t->cli_state = CL_STCLOSE;
@@ -3158,7 +3157,7 @@
 			}
 
 			/* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
-			if ((rep->l == 0) || !(rep->flags & BF_MAY_FORWARD)) {
+			if ((rep->flags & BF_EMPTY) || !(rep->flags & BF_MAY_FORWARD)) {
 				if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
 					/* stop writing */
 					rep->wex = TICK_ETERNITY;
@@ -3227,7 +3226,7 @@
 	if (t->srv_state == SV_STIDLE) {
 		if ((rep->flags & BF_SHUTW) ||
 			 ((req->flags & BF_SHUTR) &&
-			  (req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
+			  (req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
 			req->cex = TICK_ETERNITY;
 			if (t->pend_pos)
 				t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
@@ -3359,7 +3358,7 @@
 	else if (t->srv_state == SV_STCONN) { /* connection in progress */
 		if ((rep->flags & BF_SHUTW) ||
 		    ((req->flags & BF_SHUTR) &&
-		     ((req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
+		     ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
 		      t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
 			req->cex = TICK_ETERNITY;
 			if (!(t->flags & SN_CONN_TAR)) {
@@ -3458,10 +3457,10 @@
 		else { /* no error or write 0 */
 			t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
 
-			if (req->l == 0) /* nothing to write */ {
+			if (req->flags & BF_EMPTY) {
 				EV_FD_CLR(t->srv_fd, DIR_WR);
 				req->wex = TICK_ETERNITY;
-			} else  /* need the right to write */ {
+			} else {
 				EV_FD_SET(t->srv_fd, DIR_WR);
 				req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
 				if (tick_isset(req->wex)) {
@@ -3475,7 +3474,7 @@
 				EV_FD_SET(t->srv_fd, DIR_RD);
 				rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
 				t->srv_state = SV_STDATA;
-				rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
+				buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
 
 				/* if the user wants to log as soon as possible, without counting
 				   bytes from the server, then this is the right moment. */
@@ -3493,7 +3492,7 @@
 			else {
 				t->srv_state = SV_STDATA;
 				t->analysis |= AN_RTR_HTTP_HDR;
-				rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
+				buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
 				t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
 				/* reset hdr_idx which was already initialized by the request.
 				 * right now, the http parser does it.
@@ -3558,7 +3557,7 @@
 		 * coming from the server.
 		 */
 		else if (!(req->flags & BF_SHUTW) && /* not already done */
-			 req->l == 0 && req->flags & BF_MAY_FORWARD &&
+			 req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD &&
 			 (req->flags & BF_SHUTR ||
 			  (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) {
 			buffer_shutw(req);
@@ -3642,7 +3641,7 @@
 
 		/* manage read timeout */
 		if (!(rep->flags & BF_SHUTR)) {
-			if (rep->l >= rep->rlim - rep->data) {
+			if (rep->flags & BF_FULL) {
 				if (EV_FD_COND_C(t->srv_fd, DIR_RD))
 					rep->rex = TICK_ETERNITY;
 			} else {
@@ -3653,7 +3652,7 @@
 
 		/* manage write timeout */
 		if (!(req->flags & BF_SHUTW)) {
-			if (req->l == 0 || !(req->flags & BF_MAY_FORWARD)) {
+			if (req->flags & BF_EMPTY || !(req->flags & BF_MAY_FORWARD)) {
 				/* stop writing */
 				if (EV_FD_COND_C(t->srv_fd, DIR_WR))
 					req->wex = TICK_ETERNITY;
@@ -5101,7 +5100,7 @@
 	EV_FD_CLR(t->cli_fd, DIR_RD);
 	buffer_shutr(t->req);
 	buffer_shutr(t->rep);
-	t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
+	buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */
 	t->logs.tv_request = now;
 	t->data_source = DATA_SRC_STATS;
 	t->data_state  = DATA_ST_INIT;
diff --git a/src/proto_uxst.c b/src/proto_uxst.c
index d74bcb4..443586f 100644
--- a/src/proto_uxst.c
+++ b/src/proto_uxst.c
@@ -483,8 +483,6 @@
 
 		buffer_init(s->req);
 		buffer_init(s->rep);
-		s->req->rlim += BUFSIZE;
-		s->rep->rlim += BUFSIZE;
 
 		fd_insert(cfd);
 		fdtab[cfd].owner = t;
@@ -582,7 +580,7 @@
 			return 1;
 		}	
 		/* last server read and buffer empty */
-		else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
+		else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->flags & BF_EMPTY)) {
 			EV_FD_CLR(t->cli_fd, DIR_WR);
 			buffer_shutw(rep);
 			shutdown(t->cli_fd, SHUT_WR);
@@ -635,7 +633,7 @@
 			return 1;
 		}
 
-		if (req->l >= req->rlim - req->data) {
+		if (req->flags & BF_FULL) {
 			/* no room to read more data */
 			if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
 				/* stop reading until we get some space */
@@ -657,7 +655,7 @@
 			}
 		}
 
-		if ((rep->l == 0) ||
+		if ((rep->flags & BF_EMPTY) ||
 		    ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
 			if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
 				/* stop writing */
@@ -694,7 +692,7 @@
 			}
 			return 1;
 		}
-		else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
+		else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->flags & BF_EMPTY)) {
 			buffer_shutw(rep);
 			fd_delete(t->cli_fd);
 			t->cli_state = CL_STCLOSE;
@@ -717,7 +715,7 @@
 			return 1;
 		}
 
-		if (rep->l == 0) {
+		if (rep->flags & BF_EMPTY) {
 			if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
 				/* stop writing */
 				rep->wex = TICK_ETERNITY;
@@ -770,7 +768,7 @@
 			}
 			return 1;
 		}
-		else if (req->l >= req->rlim - req->data) {
+		else if (req->flags & BF_FULL) {
 			/* no room to read more data */
 
 			/* FIXME-20050705: is it possible for a client to maintain a session
@@ -821,7 +819,7 @@
 	if (s == SV_STIDLE) {
 		if (c == CL_STCLOSE || c == CL_STSHUTW ||
 			 (c == CL_STSHUTR &&
-			  (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
+			  (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
 			tv_eternity(&req->cex);
 			if (t->pend_pos)
 				t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
@@ -870,7 +868,7 @@
 	else if (s == SV_STCONN) { /* connection in progress */
 		if (c == CL_STCLOSE || c == CL_STSHUTW ||
 		    (c == CL_STSHUTR &&
-		     ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
+		     ((t->req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
 		      t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
 			tv_eternity(&req->cex);
 			fd_delete(t->srv_fd);
@@ -941,7 +939,7 @@
 			t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
 
 			//fprintf(stderr,"3: c=%d, s=%d\n", c, s);
-			if (req->l == 0) /* nothing to write */ {
+			if (req->flags & BF_EMPTY) /* nothing to write */ {
 				EV_FD_CLR(t->srv_fd, DIR_WR);
 				tv_eternity(&req->wex);
 			} else  /* need the right to write */ {
@@ -962,7 +960,7 @@
 			t->srv_state = SV_STDATA;
 			if (t->srv)
 				t->srv->cum_sess++;
-			rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
+			buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
 
 			/* if the user wants to log as soon as possible, without counting
 			   bytes from the server, then this is the right moment. */
@@ -1007,7 +1005,7 @@
 			return 1;
 		}
 		/* end of client read and no more data to send */
-		else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
+		else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->flags & BF_EMPTY)) {
 			EV_FD_CLR(t->srv_fd, DIR_WR);
 			buffer_shutw(req);
 			shutdown(t->srv_fd, SHUT_WR);
@@ -1048,7 +1046,7 @@
 		}
 
 		/* recompute request time-outs */
-		if (req->l == 0) {
+		if (req->flags & BF_EMPTY) {
 			if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
 				/* stop writing */
 				tv_eternity(&req->wex);
@@ -1106,7 +1104,7 @@
 
 			return 1;
 		}
-		else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
+		else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->flags & BF_EMPTY)) {
 			//EV_FD_CLR(t->srv_fd, DIR_WR);
 			buffer_shutw(req);
 			fd_delete(t->srv_fd);
@@ -1142,7 +1140,7 @@
 
 			return 1;
 		}
-		else if (req->l == 0) {
+		else if (req->flags & BF_EMPTY) {
 			if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
 				/* stop writing */
 				tv_eternity(&req->wex);
@@ -1263,8 +1261,8 @@
 				continue;
 			}
 			if (s->cli_state == CL_STSHUTR ||
-			    (s->req->l >= s->req->rlim - s->req->data)) {
-				if (s->req->l == 0) {
+			    (s->req->flags & BF_FULL)) {
+				if (s->req->flags & BF_EMPTY) {
 					s->srv_state = SV_STCLOSE;
 					fsm_resync |= 1;
 					continue;
@@ -1275,7 +1273,7 @@
 				 */
 				memcpy(s->rep->data, s->req->data, sizeof(s->rep->data));
 				s->rep->l = s->req->l;
-				s->rep->rlim = s->rep->data + BUFSIZE;
+				buffer_set_rlim(s->rep, BUFSIZE);
 				s->rep->w = s->rep->data;
 				s->rep->lr = s->rep->r = s->rep->data + s->rep->l;
 
diff --git a/src/senddata.c b/src/senddata.c
index 33fe548..6bc5915 100644
--- a/src/senddata.c
+++ b/src/senddata.c
@@ -51,13 +51,13 @@
 	EV_FD_CLR(s->cli_fd, DIR_RD);
 	EV_FD_SET(s->cli_fd, DIR_WR);
 	buffer_shutr(s->req);
+	buffer_flush(s->req);
 	s->rep->wex = tick_add_ifset(now_ms, s->rep->wto);
 	s->rep->flags |= BF_MAY_FORWARD;
 	s->cli_state = CL_STSHUTR; // FIXME: still used by unix sockets
 	buffer_flush(s->rep);
 	if (msg && msg->len)
 		buffer_write(s->rep, msg->str, msg->len);
-	s->req->l = 0;
 }
 
 
@@ -69,10 +69,10 @@
  */
 void client_return(struct session *s, const struct chunk *msg)
 {
+	buffer_flush(s->req);
 	buffer_flush(s->rep);
 	if (msg && msg->len)
 		buffer_write(s->rep, msg->str, msg->len);
-	s->req->l = 0;
 }
 
 /*
diff --git a/src/stream_sock.c b/src/stream_sock.c
index 3e3e0b9..b35b9fe 100644
--- a/src/stream_sock.c
+++ b/src/stream_sock.c
@@ -84,6 +84,7 @@
 			/* Not anymore room to store data. This should theorically
 			 * never happen, but better safe than sorry !
 			 */
+			b->flags |= BF_FULL;
 			EV_FD_CLR(fd, DIR_RD);
 			b->rex = TICK_ETERNITY;
 			goto out_wakeup;
@@ -111,6 +112,7 @@
 			b->l += ret;
 			cur_read += ret;
 			b->flags |= BF_PARTIAL_READ;
+			b->flags &= ~BF_EMPTY;
 	
 			if (b->r == b->data + BUFSIZE) {
 				b->r = b->data; /* wrap around the buffer */
@@ -118,7 +120,7 @@
 
 			b->total += ret;
 
-			if (b->l == b->rlim - b->data) {
+			if (b->l >= b->rlim - b->data) {
 				/* The buffer is now full, there's no point in going through
 				 * the loop again.
 				 */
@@ -151,6 +153,7 @@
 					b->xfer_large = 0;
 				}
 
+				b->flags |= BF_FULL;
 				EV_FD_CLR(fd, DIR_RD);
 				b->rex = TICK_ETERNITY;
 				goto out_wakeup;
@@ -335,12 +338,16 @@
 			b->w += ret;
 	    
 			b->flags |= BF_PARTIAL_WRITE;
+
+			if (b->l < b->rlim - b->data)
+				b->flags &= ~BF_FULL;
 	    
 			if (b->w == b->data + BUFSIZE) {
 				b->w = b->data; /* wrap around the buffer */
 			}
 
 			if (!b->l) {
+				b->flags |= BF_EMPTY;
 				EV_FD_CLR(fd, DIR_WR);
 				b->wex = TICK_ETERNITY;
 				goto out_wakeup;