-/* $Id$
- *
+/*
* Copyright (c) 1987-2009 by the citadel.org team
*
* This program is free software; you can redistribute it and/or modify
return (0);
}
-int client_readline_sslbuffer(StrBuf *Target, StrBuf *Buffer, int timeout)
+int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int timeout)
{
- int ntries = 0;
- const char *pch, *pchs;
- int rlen, len, retval = 0;
CitContext *CCC = CC;
+ const char *pos = NULL;
+ const char *pLF;
+ int len, rlen, retlen;
+ int nSuccessLess = 0;
+ const char *pch = NULL;
+
+ retlen = 0;
+ if ((Line == NULL) ||
+ (Pos == NULL) ||
+ (IOBuf == NULL))
+ {
+ if (Pos != NULL)
+ *Pos = NULL;
+// *Error = ErrRBLF_PreConditionFailed;
+ return -1;
+ }
- if (StrLength(Target) > 0) {
- pchs = ChrPtr(Buffer);
- pch = strchr(pchs, '\n');
- if (pch != NULL) {
- rlen = 0;
- len = pch - pchs;
- if (len > 0 && (*(pch - 1) == '\r') )
- rlen ++;
- StrBufSub(Target, Buffer, 0, len - rlen);
- StrBufCutLeft(Buffer, len + 1);
- return len - rlen;
+ pos = *Pos;
+ if ((StrLength(IOBuf) > 0) &&
+ (pos != NULL) &&
+ (pos < ChrPtr(IOBuf) + StrLength(IOBuf)))
+ {
+ pch = pos;
+ pch = strchr(pch, '\n');
+
+ if (pch == NULL) {
+ StrBufAppendBufPlain(Line, pos,
+ StrLength(IOBuf) - (pos - ChrPtr(IOBuf)), 0);
+ FlushStrBuf(IOBuf);
+ pos = *Pos = NULL;
+ }
+ else {
+ int n = 0;
+ if ((pch > ChrPtr(IOBuf)) &&
+ (*(pch - 1) == '\r')) {
+ n = 1;
+ }
+ StrBufAppendBufPlain(Line, pos,
+ (pch - pos - n), 0);
+
+ if (StrLength(IOBuf) <= (pch - ChrPtr(IOBuf) + 1)) {
+ FlushStrBuf(IOBuf);
+ pos = *Pos = NULL;
+ }
+ else
+ *Pos = pch + 1;
+ return StrLength(Line);
}
}
-
- while ((retval == 0) && (CCC->ssl != NULL)) {
- pch = NULL;
- pchs = ChrPtr(Buffer);
- if (*pchs != '\0')
- pch = strchr(pchs, '\n');
- if (pch == NULL) {
- retval = client_read_sslbuffer(Buffer, timeout);
- pchs = ChrPtr(Buffer);
- pch = strchr(pchs, '\n');
+
+ pLF = NULL;
+ while ((nSuccessLess < timeout) &&
+ (pLF == NULL) &&
+ (CCC->ssl != NULL)) {
+
+ rlen = client_read_sslbuffer(IOBuf, timeout);
+ if (rlen < 1) {
+// *Error = strerror(errno);
+// close(*fd);
+// *fd = -1;
+ return -1;
}
- if (retval == 0) {
- sleep(1);
- ntries ++;
+ else if (rlen > 0) {
+ pLF = strchr(ChrPtr(IOBuf), '\n');
}
- if (ntries > 10)
- return 0;
}
- if ((retval > 0) && (pch != NULL)) {
- rlen = 0;
- len = pch - pchs;
- if (len > 0 && (*(pch - 1) == '\r') )
- rlen ++;
- StrBufSub(Target, Buffer, 0, len - rlen);
- StrBufCutLeft(Buffer, len + 1);
- return len - rlen;
-
+ *Pos = NULL;
+ if (pLF != NULL) {
+ pos = ChrPtr(IOBuf);
+ len = pLF - pos;
+ if (len > 0 && (*(pLF - 1) == '\r') )
+ len --;
+ StrBufAppendBufPlain(Line, pos, len, 0);
+ if (pLF + 1 >= ChrPtr(IOBuf) + StrLength(IOBuf))
+ {
+ FlushStrBuf(IOBuf);
+ }
+ else
+ *Pos = pLF + 1;
+ return StrLength(Line);
}
- else
- return -1;
+// *Error = ErrRBLF_NotEnoughSentFromServer;
+ return -1;
}
int client_read_sslblob(StrBuf *Target, long bytes, int timeout)
{
- long bufremain;
long baselen;
- int retval;
+ long RemainRead;
+ int retval = 0;
CitContext *CCC = CC;
baselen = StrLength(Target);
- if (CCC->Pos == NULL)
- CCC->Pos = ChrPtr(CCC->ReadBuf);
- bufremain = StrLength(CCC->ReadBuf) -
- (CCC->Pos - ChrPtr(CCC->ReadBuf));
+ if (StrLength(CCC->RecvBuf.Buf) > 0)
+ {
+ long RemainLen;
+ long TotalLen;
+ const char *pchs;
+
+ if (CCC->RecvBuf.ReadWritePointer == NULL)
+ CCC->RecvBuf.ReadWritePointer = ChrPtr(CCC->RecvBuf.Buf);
+ pchs = ChrPtr(CCC->RecvBuf.Buf);
+ TotalLen = StrLength(CCC->RecvBuf.Buf);
+ RemainLen = TotalLen - (pchs - CCC->RecvBuf.ReadWritePointer);
+ if (RemainLen > bytes)
+ RemainLen = bytes;
+ if (RemainLen > 0)
+ {
+ StrBufAppendBufPlain(Target,
+ CCC->RecvBuf.ReadWritePointer,
+ RemainLen, 0);
+ CCC->RecvBuf.ReadWritePointer += RemainLen;
+ }
+ if ((ChrPtr(CCC->RecvBuf.Buf) + StrLength(CCC->RecvBuf.Buf)) <= CCC->RecvBuf.ReadWritePointer)
+ {
+ CCC->RecvBuf.ReadWritePointer = NULL;
+ FlushStrBuf(CCC->RecvBuf.Buf);
+ }
+ }
+
+ if (StrLength(Target) >= bytes + baselen)
+ return 1;
- if (bytes < bufremain)
- bufremain = bytes;
- StrBufAppendBufPlain(Target, CCC->Pos, bufremain, 0);
- StrBufCutLeft(CCC->ReadBuf, bufremain);
- CCC->Pos = NULL;
+ CCC->RecvBuf.ReadWritePointer = NULL;
- if (bytes > bufremain)
+ while ((StrLength(Target) < bytes + baselen) &&
+ (retval >= 0))
{
- while ((StrLength(CCC->ReadBuf) + StrLength(Target) < bytes + baselen) &&
- (retval >= 0))
- retval = client_read_sslbuffer(CCC->ReadBuf, timeout);
+ retval = client_read_sslbuffer(CCC->RecvBuf.Buf, timeout);
if (retval >= 0) {
- StrBufAppendBuf(Target, CCC->ReadBuf, 0); /* todo: Buf > bytes? */
- return 1;
+ RemainRead = bytes - (StrLength (Target) - baselen);
+ if (RemainRead < StrLength(CCC->RecvBuf.Buf))
+ {
+ StrBufAppendBufPlain(
+ Target,
+ ChrPtr(CCC->RecvBuf.Buf),
+ RemainRead, 0);
+ CCC->RecvBuf.ReadWritePointer = ChrPtr(CCC->RecvBuf.Buf) + RemainRead;
+ break;
+ }
+ StrBufAppendBuf(Target, CCC->RecvBuf.Buf, 0); /* todo: Buf > bytes? */
+ FlushStrBuf(CCC->RecvBuf.Buf);
}
- else {
+ else
+ {
+ FlushStrBuf(CCC->RecvBuf.Buf);
return -1;
+
}
}
- else
- return 1;
+ return 1;
}