diff --git a/SRC/echoping.c b/SRC/echoping.c index 4815452..3ee54b0 100644 --- a/SRC/echoping.c +++ b/SRC/echoping.c @@ -391,6 +391,8 @@ main (argc, argv) for (i = 1; i <= number; i++) { + clear_read_buffer(); + attempts++; if (!udp) { diff --git a/SRC/echoping.h b/SRC/echoping.h index 9afcfe2..7dda976 100644 --- a/SRC/echoping.h +++ b/SRC/echoping.h @@ -3,9 +3,7 @@ #define DEFLINE 256 #define MAXLINE 65535 #define UDPMAX 65535 -#ifdef HTTP #define MAXTOREAD 150000 -#endif #define MAXNUMBER 20 /* Settings you should not change */ @@ -95,6 +93,7 @@ char *sys_err_str (); int writen (); /* readline.c */ int readline (); +void clear_read_buffer (); /* util.c */ char *random_string (); void tvsub (); diff --git a/SRC/http.c b/SRC/http.c index f54f8b2..4034e3f 100644 --- a/SRC/http.c +++ b/SRC/http.c @@ -68,6 +68,7 @@ read_from_server (int fd) while (!body) { nr = readline (fd, big_recvline, MAXTOREAD, TRUE); + /* printf ("DEBUG HTTP, I got (%s)\n", big_recvline); */ /* HTTP replies should be separated by CR-LF. Unfortunately, some servers send only CR :-( */ body = ((nr == 2) || (nr == 1)); /* Empty line CR-LF seen */ @@ -91,8 +92,9 @@ read_from_server (int fd) nr = readline (fd, big_recvline, MAXTOREAD, FALSE); if ((nr < 2) && (errno == EINTR)) /* Probably a timeout */ return -1; - if (nr < 2) /* Hmm, if the body is empty, we'll get a meaningless error message */ - err_sys ("Reading HTTP body"); + if (nr < 2) /* Hmm, if the body is empty, we'll + get a meaningless error message */ + err_sys ("Reading HTTP body, may be empty"); total = total + nr; return total; /* How to do if we want only the body's size? */ } diff --git a/SRC/readline.c b/SRC/readline.c index ee35ace..e927d97 100644 --- a/SRC/readline.c +++ b/SRC/readline.c @@ -1,18 +1,19 @@ /* - * Read a line from a descriptor. Read the line one byte at a time, looking - * for the newline. We store the newline in the buffer, then follow it with - * a null (the same as fgets(3)). We return the number of characters up to, - * but not including, the null (the same as strlen(3)). If ln == 0, we treat - * newline as an ordinary charracter. + * Read a line from a descriptor. * - * Stolen from Stevens' book + * To save read() system calls, we first check the static buffer if it + * still contains data. If so, we send it back. Otherwise, we call + * read(). * - * $Id$ + * If ln == 0, we treat newline as an ordinary charracter. * - */ + * $Id$ + * */ #include "echoping.h" +char buffer[MAXTOREAD]; /* Must survive between calls to readline */ + int readline (fd, ptr, maxlen, ln) int fd; @@ -20,30 +21,71 @@ readline (fd, ptr, maxlen, ln) int maxlen; unsigned short ln; { - int n, rc; - char c; + char *origptr = ptr; + int n, i, j, rc; + char s[maxlen]; + int bufend = strlen(buffer); - for (n = 1; n < maxlen; n++) + n = 0; + + /* Use the buffer if it is still full */ + for (i = 0; i < strlen(buffer); i++) { + *ptr++ = buffer[i]; + n++; + if (buffer[i] == '\n' && ln == 1) + break; + } + if (n) { + /* printf ("DEBUG, got %d bytes from the buffer\n", i); */ + strcpy (buffer, (char *)(buffer+i+1)); + *ptr = 0; + return (n); + } + bufend = strlen (buffer); + + reading: + for (; n < maxlen;) { if (timeout_flag) return n; - if ((rc = read (fd, &c, 1)) == 1) + if ((rc = read (fd, &s, maxlen)) > 0) { - *ptr++ = c; - if (c == '\n' && ln == 1) + /* printf ("DEBUG, %d bytes asked (nl = %d) %d bytes read\n", maxlen, ln, rc); */ + parsing: + for (i = 0; i < rc; i++) { + *ptr++ = s[i]; + /* printf ("DEBUG, adding %c ", s[i]); */ + n++; + if (s[i] == '\n' && ln == 1) { + + for (j = i+1; j < rc; j++) { + buffer[bufend++] = s[j]; + } + break; + } + } + if (s[i] == '\n' && ln == 1) { break; + } } else if (rc == 0) { if (n == 1) return (0); /* EOF, no data read */ - else + else { + n++; break; /* EOF, some data was read */ + } } else return (-1); /* error */ } *ptr = 0; + /* printf ("DEBUG, Returning %d bytes (%s)\n", n, origptr); */ return (n); } + +void clear_read_buffer () { + buffer[0] = 0; +}