changeset 35335:85003f7fa621

stream ftp: readline: Always try to read complete lines If there is not enough space in the provided line buffer just skip the remaining bytes until reaching EOL. Usually we are only interested in the first 5 characters and for everything else the (on-stack) response buffer should still be big enough.
author al
date Tue, 20 Nov 2012 22:18:25 +0000
parents 3397976a029b
children 9c334690f765
files stream/stream_ftp.c
diffstat 1 files changed, 17 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/stream/stream_ftp.c	Tue Nov 20 22:16:29 2012 +0000
+++ b/stream/stream_ftp.c	Tue Nov 20 22:18:25 2012 +0000
@@ -105,6 +105,11 @@
 /*
  * read a line of text
  *
+ * If the line is too long to fit in the buffer, provided via parameters
+ * buf and max, the remaining characters are skipped. So the next call to
+ * this function is synchronized to the start of the following response
+ * line.
+ *
  * The parameter buf will always be initialized as long as max is bigger
  * then 1. If nothing is read it will contain an empty string.
  *
@@ -144,8 +149,18 @@
 	}
       }
       if (max == 1) {
-	*buf = '\0';
-	break;
+        char *q = memchr(ctl->cget, '\n', ctl->cavail);
+
+        if (q) { // found EOL: update state and return
+          ++q;
+          ctl->cavail -= q - ctl->cget;
+          ctl->cget = q;
+
+          break;
+        }
+
+        // receive more data to find end of current line
+        ctl->cget = ctl->cput;
       }
       if (ctl->cput == ctl->cget) {
 	ctl->cput = ctl->cget = ctl->buf;