Index: buffer.c
===================================================================
RCS file: /cvsroot/levent/libevent/buffer.c,v
retrieving revision 1.17
diff -u -r1.17 buffer.c
--- buffer.c	22 Jan 2006 05:06:14 -0000	1.17
+++ buffer.c	13 Feb 2006 01:47:12 -0000
@@ -340,8 +340,21 @@
 #endif
 
 #ifdef FIONREAD
-	if (ioctl(fd, FIONREAD, &n) == -1 || n == 0)
+	if (ioctl(fd, FIONREAD, &n) == -1 || n == 0) {
 		n = EVBUFFER_MAX_READ;
+	} else if (n > EVBUFFER_MAX_READ && n > howmuch) {
+		/*
+		 * It's possible that a lot of data is available for
+		 * reading.  We do not want to exhaust resources
+		 * before the reader has a chance to do something
+		 * about it.  If the reader does not tell us how much
+		 * data we should read, we artifically limit it.
+		 */
+		if (n > buf->totallen << 2)
+			n = buf->totallen << 2;
+		if (n < EVBUFFER_MAX_READ)
+			n = EVBUFFER_MAX_READ;
+	}
 #endif	
 	if (howmuch < 0 || howmuch > n)
 		howmuch = n;
Index: evbuffer.c
===================================================================
RCS file: /cvsroot/levent/libevent/evbuffer.c,v
retrieving revision 1.11
diff -u -r1.11 evbuffer.c
--- evbuffer.c	3 Feb 2006 19:26:06 -0000	1.11
+++ evbuffer.c	13 Feb 2006 01:47:12 -0000
@@ -92,13 +92,21 @@
 	int res = 0;
 	short what = EVBUFFER_READ;
 	size_t len;
+	int howmuch = -1;
 
 	if (event == EV_TIMEOUT) {
 		what |= EVBUFFER_TIMEOUT;
 		goto error;
 	}
 
-	res = evbuffer_read(bufev->input, fd, -1);
+	/*
+	 * If we have a high watermark configured then we don't want to
+	 * read more data than would make us reach the watermark.
+	 */
+	if (bufev->wm_read.high != 0)
+		howmuch = bufev->wm_read.high;
+
+	res = evbuffer_read(bufev->input, fd, howmuch);
 	if (res == -1) {
 		if (errno == EAGAIN || errno == EINTR)
 			goto reschedule;
