fd_set rfds;
char *pch = NULL;
int fdflags;
+ int IsNonBlock;
struct timeval tv;
if (buf->BufUsed > 0) {
IncreaseBuf(buf, 1, -1);
fdflags = fcntl(*fd, F_GETFL);
- if ((fdflags & O_NONBLOCK) == O_NONBLOCK)
- return -1;
+ IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
while ((nSuccessLess < timeout) && (pch == NULL)) {
- tv.tv_sec = selectresolution;
- tv.tv_usec = 0;
-
- FD_ZERO(&rfds);
- FD_SET(*fd, &rfds);
- if (select(*fd + 1, NULL, &rfds, NULL, &tv) == -1) {
- *Error = strerror(errno);
- close (*fd);
- *fd = -1;
- return -1;
- }
- if (FD_ISSET(*fd, &rfds)) {
- rlen = read(*fd,
- &buf->buf[buf->BufUsed],
- buf->BufSize - buf->BufUsed - 1);
- if (rlen < 1) {
+ if (IsNonBlock){
+ tv.tv_sec = selectresolution;
+ tv.tv_usec = 0;
+
+ FD_ZERO(&rfds);
+ FD_SET(*fd, &rfds);
+ if (select(*fd + 1, NULL, &rfds, NULL, &tv) == -1) {
*Error = strerror(errno);
- close(*fd);
+ close (*fd);
*fd = -1;
return -1;
}
- else if (rlen > 0) {
- nSuccessLess = 0;
- buf->BufUsed += rlen;
- buf->buf[buf->BufUsed] = '\0';
- if (buf->BufUsed + 10 > buf->BufSize) {
- IncreaseBuf(buf, 1, -1);
- }
- pch = strchr(buf->buf, '\n');
- continue;
+ }
+ if (IsNonBlock && ! FD_ISSET(*fd, &rfds)) {
+ nSuccessLess ++;
+ continue;
+ }
+ rlen = read(*fd,
+ &buf->buf[buf->BufUsed],
+ buf->BufSize - buf->BufUsed - 1);
+ if (rlen < 1) {
+ *Error = strerror(errno);
+ close(*fd);
+ *fd = -1;
+ return -1;
+ }
+ else if (rlen > 0) {
+ nSuccessLess = 0;
+ buf->BufUsed += rlen;
+ buf->buf[buf->BufUsed] = '\0';
+ if (buf->BufUsed + 10 > buf->BufSize) {
+ IncreaseBuf(buf, 1, -1);
}
+ pch = strchr(buf->buf, '\n');
+ continue;
}
- nSuccessLess ++;
+
}
if (pch != NULL) {
rlen = 0;
}
-static const char *ErrRBLF_WrongFDFlags="StrBufTCP_read_buffered_line_fast: don't work with fdflags & O_NONBLOCK";
static const char *ErrRBLF_SelectFailed="StrBufTCP_read_buffered_line_fast: Select failed without reason";
static const char *ErrRBLF_NotEnoughSentFromServer="StrBufTCP_read_buffered_line_fast: No complete line was sent from peer";
/**
fd_set rfds;
const char *pch = NULL;
int fdflags;
+ int IsNonBlock;
struct timeval tv;
pos = *Pos;
}
fdflags = fcntl(*fd, F_GETFL);
- if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
- *Error = ErrRBLF_WrongFDFlags;
- return -1;
- }
+ IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
pch = NULL;
while ((nSuccessLess < timeout) && (pch == NULL)) {
- tv.tv_sec = selectresolution;
- tv.tv_usec = 0;
+ if (IsNonBlock)
+ {
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
- FD_ZERO(&rfds);
- FD_SET(*fd, &rfds);
- if (select(*fd + 1, NULL, &rfds, NULL, &tv) == -1) {
- *Error = strerror(errno);
- close (*fd);
- *fd = -1;
- if (*Error == NULL)
- *Error = ErrRBLF_SelectFailed;
- return -1;
- }
- if (FD_ISSET(*fd, &rfds) != 0) {
- rlen = read(*fd,
- &IOBuf->buf[IOBuf->BufUsed],
- IOBuf->BufSize - IOBuf->BufUsed - 1);
- if (rlen < 1) {
+ FD_ZERO(&rfds);
+ FD_SET(*fd, &rfds);
+ if (select((*fd) + 1, &rfds, NULL, NULL, &tv) == -1) {
*Error = strerror(errno);
- close(*fd);
+ close (*fd);
*fd = -1;
+ if (*Error == NULL)
+ *Error = ErrRBLF_SelectFailed;
return -1;
}
- else if (rlen > 0) {
- nSuccessLess = 0;
- IOBuf->BufUsed += rlen;
- IOBuf->buf[IOBuf->BufUsed] = '\0';
- if (IOBuf->BufUsed + 10 > IOBuf->BufSize) {
- IncreaseBuf(IOBuf, 1, -1);
- }
-
- pche = IOBuf->buf + IOBuf->BufUsed;
- pch = IOBuf->buf;
- while ((pch < pche) && (*pch != '\n'))
- pch ++;
- if ((pch >= pche) || (*pch == '\0'))
- pch = NULL;
+ if (! FD_ISSET(*fd, &rfds) != 0) {
+ nSuccessLess ++;
continue;
}
}
- nSuccessLess ++;
+ rlen = read(*fd,
+ &IOBuf->buf[IOBuf->BufUsed],
+ IOBuf->BufSize - IOBuf->BufUsed - 1);
+ if (rlen < 1) {
+ *Error = strerror(errno);
+ close(*fd);
+ *fd = -1;
+ return -1;
+ }
+ else if (rlen > 0) {
+ nSuccessLess = 0;
+ IOBuf->BufUsed += rlen;
+ IOBuf->buf[IOBuf->BufUsed] = '\0';
+ if (IOBuf->BufUsed + 10 > IOBuf->BufSize) {
+ IncreaseBuf(IOBuf, 1, -1);
+ }
+
+ pche = IOBuf->buf + IOBuf->BufUsed;
+ pch = IOBuf->buf;
+ while ((pch < pche) && (*pch != '\n'))
+ pch ++;
+ if ((pch >= pche) || (*pch == '\0'))
+ pch = NULL;
+ continue;
+ }
}
if (pch != NULL) {
pos = IOBuf->buf;
*/
int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
{
- fd_set wset;
- int fdflags;
+ int fdflags;
int len, rlen, slen;
+ int nSuccessLess;
int nRead = 0;
char *ptr;
-
+ int IsNonBlock;
+ struct timeval tv;
+ fd_set rfds;
if ((Buf == NULL) || (*fd == -1))
return -1;
if (!append)
slen = len = Buf->BufUsed;
fdflags = fcntl(*fd, F_GETFL);
-
+ IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
+ nSuccessLess = 0;
while (nRead < nBytes) {
- if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
- FD_ZERO(&wset);
- FD_SET(*fd, &wset);
- if (select(*fd + 1, NULL, &wset, NULL, NULL) == -1) {
+ if (IsNonBlock)
+ {
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+
+ FD_ZERO(&rfds);
+ FD_SET(*fd, &rfds);
+ if (select(*fd + 1, &rfds, NULL, NULL, &tv) == -1) {
*Error = strerror(errno);
- return -1;
- }
- }
+ close (*fd);
+ *fd = -1;
+ if (*Error == NULL)
+ *Error = ErrRBLF_SelectFailed;
+ return -1;
+ }
+ if (! FD_ISSET(*fd, &rfds) != 0) {
+ nSuccessLess ++;
+ continue;
+ }
+ }
if ((rlen = read(*fd,
ptr,
const char *pos;
int nSelects = 0;
int SelRes;
- fd_set wset;
- int fdflags;
+ int fdflags;
int len = 0;
int rlen, slen;
int nRead = 0;
int nAlreadyRead = 0;
+ int IsNonBlock;
char *ptr;
+ fd_set rfds;
const char *pch;
+ struct timeval tv;
+ int nSuccessLess;
if ((Blob == NULL) || (*fd == -1) || (IOBuf == NULL) || (Pos == NULL))
return -1;
slen = len = Blob->BufUsed;
fdflags = fcntl(*fd, F_GETFL);
+ IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
SelRes = 1;
nBytes -= nRead;
nRead = 0;
while (nRead < nBytes) {
- if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
- FD_ZERO(&wset);
- FD_SET(*fd, &wset);
- SelRes = select(*fd + 1, NULL, &wset, NULL, NULL);
- }
- if (SelRes == -1) {
- *Error = strerror(errno);
- return -1;
- }
- else if (SelRes) {
- nSelects = 0;
- rlen = read(*fd,
- ptr,
- nBytes - nRead);
- if (rlen == -1) {
- close(*fd);
- *fd = -1;
+ if (IsNonBlock)
+ {
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+
+ FD_ZERO(&rfds);
+ FD_SET(*fd, &rfds);
+ if (select(*fd + 1, &rfds, NULL, NULL, &tv) == -1) {
*Error = strerror(errno);
- return rlen;
+ close (*fd);
+ *fd = -1;
+ if (*Error == NULL)
+ *Error = ErrRBLF_SelectFailed;
+ return -1;
+ }
+ if (! FD_ISSET(*fd, &rfds) != 0) {
+ nSuccessLess ++;
+ continue;
}
}
- else {
- nSelects ++;
+ nSuccessLess = 0;
+ rlen = read(*fd,
+ ptr,
+ nBytes - nRead);
+ if (rlen == -1) {
+ close(*fd);
+ *fd = -1;
+ *Error = strerror(errno);
+ return rlen;
+ }
+ else if (rlen == 0){
+ nSuccessLess ++;
if ((check == NNN_TERM) &&
(nRead > 5) &&
(strncmp(IOBuf->buf + IOBuf->BufUsed - 5, "\n000\n", 5) == 0))
return -1;
}
}
- if (rlen > 0) {
+ else if (rlen > 0) {
nRead += rlen;
ptr += rlen;
IOBuf->BufUsed += rlen;
#include <pthread.h>
#include <signal.h>
#include <sys/utsname.h>
+#include <string.h>
typedef void testfunc(int Sock);
if ((msock == -1)||(time_to_die))
{/* ok, we're going down. */
- exit(0);
+ return;
}
if (ssock < 0 ) continue;
StrBuf *ReadBuffer;
StrBuf *Line;
const char *Pos = NULL;
- const char *err;
+ const char *err = NULL;
int i;
ReadBuffer = NewStrBuf();
timeout,
selres,
&err);
+ TestRevalidateStrBuf(Line);
if (err != NULL)
printf("%s", err);
- CU_ASSERT_PTR_NOT_NULL(err);
+ CU_ASSERT_PTR_NULL(err);
CU_ASSERT_NOT_EQUAL(sock, -1);
if (sock == -1)
break;
time_to_die = 1;
}
-
static void SimpleLinebufferTest(void)
{
msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
worker_entry(SimpleLineBufTestFunc);
+ close (msock);
+}
+
+
+static void SimpleBlobTestFunc(int sock)
+{
+ StrBuf *ReadBuffer;
+ StrBuf *Blob;
+ const char *Pos = NULL;
+ const char *err = NULL;
+
+ ReadBuffer = NewStrBuf();
+ Blob = NewStrBuf();
+
+ StrBufReadBLOBBuffered(Blob,
+ ReadBuffer,
+ &Pos,
+ &sock,
+ 0,
+ blobsize,
+ 0,
+ &err);
+ TestRevalidateStrBuf(Blob);
+ if (err != NULL)
+ printf("%s", err);
+ CU_ASSERT(blobsize == StrLength(Blob));
+ CU_ASSERT_PTR_NULL(err);
+ CU_ASSERT_NOT_EQUAL(sock, -1);
+ if (sock == -1)
+ printf("BLOB: >%s<\n", ChrPtr(Blob));
+
+ FreeStrBuf(&ReadBuffer);
+ FreeStrBuf(&Blob);
+ time_to_die = 1;
}
+
+static void SimpleHttpPostTestFunc(int sock)
+{
+ StrBuf *ReadBuffer;
+ StrBuf *Blob;
+ StrBuf *Line;
+ const char *Pos = NULL;
+ const char *err = NULL;
+ int blobsize = 0;
+ int i;
+ const char *pch;
+
+ ReadBuffer = NewStrBuf();
+ Blob = NewStrBuf();
+ Line = NewStrBuf();
+
+ for (i = 0; (i == 0) || (StrLength(Line) != 0); i++) {
+ StrBufTCP_read_buffered_line_fast(Line,
+ ReadBuffer,
+ &Pos,
+ &sock,
+ timeout,
+ selres,
+ &err);
+ TestRevalidateStrBuf(Line);
+ if (err != NULL)
+ printf("%s", err);
+ CU_ASSERT_PTR_NULL(err);
+ CU_ASSERT_NOT_EQUAL(sock, -1);
+ if (sock == -1)
+ break;
+ printf("LINE: >%s<\n", ChrPtr(Line));
+ pch = strstr(ChrPtr(Line), "Content-Length");
+ if (pch != NULL) {
+ blobsize = atol(ChrPtr(Line) +
+ sizeof("Content-Length:"));
+
+ }
+ FlushStrBuf(Line);
+ }
+
+ StrBufReadBLOBBuffered(Blob,
+ ReadBuffer,
+ &Pos,
+ &sock,
+ 0,
+ blobsize,
+ 0,
+ &err);
+ TestRevalidateStrBuf(Blob);
+ if (err != NULL)
+ printf("%s", err);
+ CU_ASSERT(blobsize != 0);
+ CU_ASSERT(blobsize == StrLength(Blob));
+ CU_ASSERT_PTR_NULL(err);
+ CU_ASSERT_NOT_EQUAL(sock, -1);
+ if (sock == -1)
+ printf("BLOB: >%s<\n", ChrPtr(Blob));
+
+ FreeStrBuf(&ReadBuffer);
+ FreeStrBuf(&Blob);
+ FreeStrBuf(&Line);
+ time_to_die = 1;
+}
+
+
+static void SimpleBLOBbufferTest(void)
+{
+ msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
+
+ worker_entry(SimpleBlobTestFunc);
+ close (msock);
+}
+
+static void SimpleMixedLineBlob(void)
+{
+ msock = ig_tcp_server(ip_addr, listen_port, LISTEN_QUEUE_LENGTH);
+
+ worker_entry(SimpleHttpPostTestFunc);
+ close (msock);
+}
+
+
+
+
+
/*
Some samples from the original...
CU_ASSERT_EQUAL(10, 10);
CU_pTest pTest = NULL;
pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
- pTest = CU_add_test(pGroup, "testSimpleLinebufferTest", SimpleLinebufferTest);
-
+ if (n_Lines_to_read > 0)
+ pTest = CU_add_test(pGroup, "testSimpleLinebufferTest", SimpleLinebufferTest);
+ else if (blobsize > 0)
+ pTest = CU_add_test(pGroup, "testSimpleBLOBbufferTest", SimpleBLOBbufferTest);
+ else
+ pTest = CU_add_test(pGroup,"testSimpleMixedLineBlob", SimpleMixedLineBlob);
}
safestrncpy(ip_addr, optarg, sizeof ip_addr);
break;
case 'n':
+ // do linetest?
n_Lines_to_read = atoi(optarg);
break;
case 'b':
+ // or blobtest?
blobsize = atoi(optarg);
+ // else run the simple http test
break;
case 't':
timeout = atoi(optarg);