Skip to content

Commit

Permalink
Fixed string tokenization functions and implemented unexpected EOF su…
Browse files Browse the repository at this point in the history
…pport in SSL client
  • Loading branch information
kala13x committed Apr 30, 2023
1 parent e87284a commit 5d1e315
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"files.associations": {
"random": "c"
"random": "c",
"memory_resource": "c"
}
}
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# https://github.com/kala13x/smake #
####################################

CFLAGS = -g -O2 -Wall -D_XUTILS_DEBUG -D_XUTILS_USE_SSL -I./media -I./src
CFLAGS = -g -O2 -Wall -D_XUTILS_DEBUG -D_XUTILS_USE_SSL -I./src -I./media
LIBS = -lpthread -lssl -lcrypto
NAME = libxutils.a
ODIR = ./obj
Expand Down Expand Up @@ -43,7 +43,7 @@ OBJS = addr.$(OBJ) \
OBJECTS = $(patsubst %,$(ODIR)/%,$(OBJS))
INSTALL_INC = /usr/local/include/xutils
INSTALL_BIN = /usr/local/lib
VPATH = ./media:./src
VPATH = ./src:./media

.c.$(OBJ):
@test -d $(ODIR) || mkdir -p $(ODIR)
Expand All @@ -57,8 +57,8 @@ install:
@test -d $(INSTALL_BIN) || mkdir -p $(INSTALL_BIN)
@install -m 0755 $(ODIR)/$(NAME) $(INSTALL_BIN)/
@test -d $(INSTALL_INC) || mkdir -p $(INSTALL_INC)
@cp -r ./media/*.h $(INSTALL_INC)/
@cp -r ./src/*.h $(INSTALL_INC)/
@cp -r ./media/*.h $(INSTALL_INC)/

.PHONY: clean
clean:
Expand Down
1 change: 1 addition & 0 deletions src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,7 @@ xhttp_status_t XHTTP_ReadContent(xhttp_t *pHttp, xsock_t *pSock)
if (nBytes <= 0)
{
if ((!pHttp->nContentLength && !XHTTP_GetBodySize(pHttp)) ||
(pHttp->nContentLength == XHTTP_GetBodySize(pHttp)) ||
XSock_Status(pSock) == XSOCK_EOF) return XHTTP_COMPLETE;

return XHTTP_StatusCb(pHttp, XHTTP_ERRREAD);
Expand Down
4 changes: 4 additions & 0 deletions src/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,10 @@ XSOCKET XSock_InitSSLClient(xsock_t *pSock)
SSL_set_connect_state(pSSL);
SSL_set_fd(pSSL, pSock->nFD);

long nOpts = SSL_get_options(pSSL);
nOpts |= SSL_OP_IGNORE_UNEXPECTED_EOF;
SSL_set_options(pSSL, nOpts);

if (SSL_connect(pSSL) < 0)
{
if (pSSL != NULL) SSL_free(pSSL);
Expand Down
20 changes: 10 additions & 10 deletions src/xstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ int xstrntokn(char *pDst, size_t nSize, const char *pStr, size_t nLen, size_t nP
{
if (pDst != NULL) pDst[0] = XSTR_NUL;
if (nPosit >= nLen) return XSTDERR;
int nDlmtLen = (int)strlen(pDlmt);
size_t nDlmtLen = xstrused(pDlmt) ? strlen(pDlmt) : 0;

int nOffset = xstrsrc(&pStr[nPosit], pDlmt);
if (nOffset < 0)
Expand All @@ -630,18 +630,18 @@ int xstrntokn(char *pDst, size_t nSize, const char *pStr, size_t nLen, size_t nP
else if (!nOffset)
{
if (pDst != NULL) pDst[0] = '\0';
return (int)nPosit + nDlmtLen;
return (int)(nPosit + nDlmtLen);
}

xstrncpys(pDst, nSize, &pStr[nPosit], nOffset);
return (int)nPosit + nOffset + nDlmtLen;
return (int)(nPosit + nOffset + nDlmtLen);
}

int xstrntok(char *pDst, size_t nSize, const char *pStr, size_t nPosit, const char *pDlmt)
{
if (pDst != NULL) pDst[0] = XSTR_NUL;
if (nPosit >= strlen(pStr)) return XSTDERR;
int nDlmtLen = (int)strlen(pDlmt);
size_t nDlmtLen = xstrused(pDlmt) ? strlen(pDlmt) : 0;

int nOffset = xstrsrc(&pStr[nPosit], pDlmt);
if (nOffset < 0)
Expand All @@ -652,29 +652,29 @@ int xstrntok(char *pDst, size_t nSize, const char *pStr, size_t nPosit, const ch
else if (!nOffset)
{
pDst[0] = '\0';
return (int)nPosit + nDlmtLen;
return (int)(nPosit + nDlmtLen);
}

xstrncpys(pDst, nSize, &pStr[nPosit], nOffset);
return (int)nPosit + nOffset + nDlmtLen;
return (int)(nPosit + nOffset + nDlmtLen);
}

size_t xstrncuts(char *pDst, size_t nSize, const char *pSrc, const char *pStart, const char *pEnd)
{
pDst[0] = XSTR_NUL;
if (pStart == NULL)
{
if (pEnd == NULL) return 0;
if (pEnd == NULL) return XSTDNON;
int nStatus = xstrntok(pDst, nSize, pSrc, 0, pEnd);
return (nStatus >= 0) ? strnlen(pDst, nSize - 1) : 0;
return (nStatus >= 0) ? strnlen(pDst, nSize - 1) : XSTDNON;
}

int nPosit = xstrsrc(pSrc, pStart);
if (nPosit < 0) return 0;
if (nPosit < 0) return XSTDNON;
nPosit += (int)strlen(pStart);

int nStatus = xstrntok(pDst, nSize, pSrc, nPosit, pEnd);
return (nStatus >= 0) ? strnlen(pDst, nSize - 1) : 0;
return (nStatus >= 0) ? strnlen(pDst, nSize - 1) : XSTDNON;
}

char* xstrcut(char *pData, const char *pStart, const char *pEnd)
Expand Down
2 changes: 1 addition & 1 deletion src/xver.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#define XUTILS_VERSION_MAX 2
#define XUTILS_VERSION_MIN 4
#define XUTILS_BUILD_NUMBER 56
#define XUTILS_BUILD_NUMBER 57

#ifdef __cplusplus
extern "C" {
Expand Down

0 comments on commit 5d1e315

Please sign in to comment.