From bf803fb1ccc8e4b0ff141c2de990012ddb8cc633 Mon Sep 17 00:00:00 2001 From: Urban Wallasch Date: Fri, 25 Oct 2019 23:51:04 +0200 Subject: [PATCH] * Simplified and fixed rcv_request(). --- telehttpd.c | 52 ++++++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/telehttpd.c b/telehttpd.c index 7be9f97..845d91a 100644 --- a/telehttpd.c +++ b/telehttpd.c @@ -223,40 +223,28 @@ static int rcv_request(int sock, char *buf, size_t size, int timeout) { int res = -1; size_t total = 0; ssize_t bread = 0; - struct pollfd pe; - - pe.fd = sock; - do { - pe.events = POLLIN; - pe.revents = 0; - res = poll(&pe, 1, timeout); - if ( 0 < res && pe.revents == POLLIN ) { - errno = 0; - bread = read_tm(sock, buf + total, size - total, 1000); - if ( 0 > bread ) { - switch (errno) { - case EAGAIN: - case EINTR: - break; - default: - EPRINT( "read: %s\n", strerror(errno) ); - res = -1; - break; - } - } - else if ( 0 == bread && 0 == errno ) { - EPRINT( "read: premature EOF\n" ); - res = -1; - } - else { - total += bread; - if ( strstr(buf, "\r\n\r\n" ) ) - res = 0; - } + --size; + + while ( total < size && !force_quit ) { + bread = read_tm( sock, buf + total, size - total, timeout ); + if ( 0 > bread ) { + if ( errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK ) + continue; + EPRINT( "read: %s\n", strerror(errno) ); + break; + } + else if ( 0 == bread ) { + EPRINT( "read: premature EOF\n" ); + break; + } + total += bread; + buf[total] = 0; + if ( strstr( buf, "\r\n\r\n" ) ) { + res = 0; + break; } timeout = 200; - } while ( 1 == res && pe.revents == POLLIN && !force_quit ); - buf[total] = 0; + } return res; } -- 2.30.2