Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed sorted list management and cookie parsing. #1

Merged
merged 2 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 81 additions & 84 deletions Include/ArrayList.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,89 +72,86 @@ class TArrayList : public virtual CBaseMemObj

virtual BOOL SortedInsert(_In_ TType elem)
{
SIZE_T nIndex, nMin, nMax;
SIZE_T nIndex;

nMin = 1; //shifted by one to avoid problems with negative indexes
nMax = nCount; //if count == 0, loop will not enter
while (nMin <= nMax)
if (SetSize(nCount + 1) == FALSE)
return FALSE;
nIndex = nCount;
while (nIndex > 0 && elem < lpItems[nIndex - 1])
{
nIndex = nMin + (nMax - nMin) / 2;
if (elem == lpItems[nIndex - 1])
{
nMin = nIndex;
break;
}
if (elem < lpItems[nIndex - 1])
nMax = nIndex - 1;
else
nMin = nIndex + 1;
lpItems[nIndex] = lpItems[nIndex - 1];
nIndex--;
}
return InsertElementAt(elem, nMin - 1);
lpItems[nIndex] = elem;
nCount++;
return TRUE;
};

template<class _Comparator>
BOOL SortedInsert(_In_ TType elem, _In_ _Comparator lpCompareFunc, _In_opt_ LPVOID lpContext = NULL,
_In_opt_ BOOL bDontInsertDuplicates = FALSE, _Out_opt_ LPBOOL lpbAlreadyOnList = NULL)
{
SIZE_T nIndex, nMin, nMax;
int res;
SIZE_T nIndex;

if (lpbAlreadyOnList != NULL)
*lpbAlreadyOnList = FALSE;
if (lpCompareFunc == NULL)
return FALSE;
nMin = 1; //shifted by one to avoid problems with negative indexes
nMax = nCount; //if count == 0, loop will not enter
while (nMin <= nMax)

if (SetSize(nCount + 1) == FALSE)
return FALSE;
nIndex = nCount;
while (nIndex > 0)
{
nIndex = nMin + (nMax - nMin) / 2;
res = lpCompareFunc(lpContext, &elem, &lpItems[nIndex - 1]);
if (res == 0)
int comp = lpCompareFunc(lpContext, &elem, &lpItems[nIndex - 1]);
if (comp == 0)
{
if (bDontInsertDuplicates != FALSE)
{
if (lpbAlreadyOnList != NULL)
*lpbAlreadyOnList = TRUE;
return TRUE;
}
nMin = nIndex;
break;
}
if (res < 0)
nMax = nIndex - 1;
else
nMin = nIndex + 1;
if (comp >= 0)
break;
lpItems[nIndex] = lpItems[nIndex - 1];
nIndex--;
}
return InsertElementAt(elem, nMin - 1);
lpItems[nIndex] = elem;
nCount++;
return TRUE;
};

template<class _Comparator, class _KeyType>
TType* BinarySearchPtr(_In_ _KeyType lpKey, _In_ _Comparator lpSearchFunc, _In_opt_ LPVOID lpContext = NULL)
TType* BinarySearchPtr(_In_ _KeyType _key, _In_ _Comparator lpSearchFunc, _In_opt_ LPVOID lpContext = NULL)
{
SIZE_T nIndex = BinarySearch(lpKey, lpSearchFunc, lpContext);
SIZE_T nIndex = BinarySearch(_key, lpSearchFunc, lpContext);
return (nIndex != (SIZE_T)-1) ? lpItems + nIndex : NULL;
};

template<class _Comparator, class _KeyType>
SIZE_T BinarySearch(_In_ _KeyType lpKey, _In_ _Comparator lpSearchFunc, _In_opt_ LPVOID lpContext = NULL)
SIZE_T BinarySearch(_In_ _KeyType _key, _In_ _Comparator lpSearchFunc, _In_opt_ LPVOID lpContext = NULL)
{
SIZE_T nMid, nMin, nMax;
int res;
SIZE_T nBase, n;

if (lpKey == NULL || lpSearchFunc == NULL)
if (lpSearchFunc == NULL)
return (SIZE_T)-1;
nMin = 1; //shifted by one to avoid problems with negative indexes
nMax = nCount; //if count == 0, loop will not enter
while (nMin <= nMax)
{
nMid = nMin + (nMax - nMin) / 2;
res = lpSearchFunc(lpContext, lpKey, &lpItems[nMid - 1]);
if (res == 0)
return nMid - 1;
if (res < 0)
nMax = nMid - 1;
else
nMin = nMid + 1;
nBase = 0;
n = nCount;
while (n > 0)
{
SIZE_T nMid = nBase + (n >> 1);

int comp = lpSearchFunc(lpContext, _key, &lpItems[nMid]);
if (comp == 0)
return nMid;
if (comp > 0)
{
nBase = nMid + 1;
n--;
}
n >>= 1;
}
return (SIZE_T)-1;
};
Expand Down Expand Up @@ -484,67 +481,67 @@ class TArrayList4Structs : public virtual CBaseMemObj
BOOL SortedInsert(_In_ TType *lpElem, _In_ _Comparator lpCompareFunc, _In_opt_ LPVOID lpContext = NULL,
_In_opt_ BOOL bDontInsertDuplicates = FALSE, _Out_opt_ LPBOOL lpbAlreadyOnList = NULL)
{
SIZE_T nIndex, nMin, nMax;
int res;
SIZE_T nIndex;

if (lpbAlreadyOnList != NULL)
*lpbAlreadyOnList = FALSE;
if (lpElem == NULL && lpCompareFunc == NULL)
return FALSE;
if (nCount == 0)
return InsertElementAt(lpElem);
nMin = 1; //shifted by one to avoid problems with negative indexes
nMax = nCount;
while (nMin <= nMax)
{
nIndex = (nMin + nMax) / 2;
res = lpCompareFunc(lpContext, lpElem, &lpItems[nIndex - 1]);
if (res == 0)

if (SetSize(nCount + 1) == FALSE)
return FALSE;
nIndex = nCount;
while (nIndex > 0)
{
int comp = lpCompareFunc(lpContext, lpElem, &lpItems[nIndex - 1]);
if (comp == 0)
{
if (bDontInsertDuplicates != FALSE)
{
if (lpbAlreadyOnList != NULL)
*lpbAlreadyOnList = TRUE;
return TRUE;
}
nMin = nIndex;
break;
}
if (res < 0)
nMax = nIndex - 1;
else
nMin = nIndex + 1;
if (comp >= 0)
break;
MxMemCopy(&lpItems[nIndex], &lpItems[nIndex - 1], sizeof(TType));
nIndex--;
}
return InsertElementAt(lpElem, nMin - 1);
MxMemCopy(&lpItems[nIndex], lpElem, sizeof(TType));
nCount++;
return TRUE;
};

template<class _Comparator, class _KeyType>
TType* BinarySearchPtr(_In_ _KeyType lpKey, _In_ _Comparator lpSearchFunc, _In_opt_ LPVOID lpContext = NULL)
TType* BinarySearchPtr(_In_ _KeyType _key, _In_ _Comparator lpSearchFunc, _In_opt_ LPVOID lpContext = NULL)
{
SIZE_T nIndex = BinarySearch(lpKey, lpSearchFunc, lpContext);
SIZE_T nIndex = BinarySearch(_key, lpSearchFunc, lpContext);
return (nIndex != (SIZE_T)-1) ? lpItems + nIndex : NULL;
};

template<class _Comparator, class _KeyType>
SIZE_T BinarySearch(_In_ _KeyType lpKey, _In_ _Comparator lpCompareFunc, _In_opt_ LPVOID lpContext = NULL)
SIZE_T BinarySearch(_In_ _KeyType _key, _In_ _Comparator lpSearchFunc, _In_opt_ LPVOID lpContext = NULL)
{
SIZE_T nMid, nMin, nMax;
int res;
SIZE_T nBase, n;

if (lpKey == NULL || lpCompareFunc == NULL)
if (_key == NULL || lpSearchFunc == NULL)
return (SIZE_T)-1;
nMin = 1; //shifted by one to avoid problems with negative indexes
nMax = nCount; //if count == 0, loop will not enter
while (nMin <= nMax)
{
nMid = nMin + (nMax - nMin) / 2;
res = lpCompareFunc(lpContext, lpKey, &lpItems[nMid - 1]);
if (res == 0)
return nMid - 1;
if (res < 0)
nMax = nMid - 1;
else
nMin = nMid + 1;
nBase = 0;
n = nCount;
while (n > 0)
{
SIZE_T nMid = nBase + (n >> 1);

int comp = lpSearchFunc(lpContext, _key, &lpItems[nMid]);
if (comp == 0)
return nMid;
if (comp > 0)
{
nBase = nMid + 1;
n--;
}
n >>= 1;
}
return (SIZE_T)-1;
};
Expand Down
29 changes: 21 additions & 8 deletions Include/Comm/IpcCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -597,20 +597,14 @@ class MX_NOVTABLE CIpc : public virtual CBaseMemObj, public CLoggable
protected:
friend class CLayer;

class MX_NOVTABLE CConnectionBase : public virtual TRefCounted<CBaseMemObj>,
public TRedBlackTreeNode<CConnectionBase,SIZE_T>
class MX_NOVTABLE CConnectionBase : public virtual TRefCounted<CBaseMemObj>, public TRedBlackTreeNode<CConnectionBase>
{
MX_DISABLE_COPY_CONSTRUCTOR(CConnectionBase);
protected:
CConnectionBase(_In_ CIpc *lpIpc, _In_ CIpc::eConnectionClass nClass);
public:
virtual ~CConnectionBase();

SIZE_T GetNodeKey() const
{
return (SIZE_T)this;
};

virtual VOID ShutdownLink(_In_ BOOL bAbortive);

HRESULT SendMsg(_In_ LPCVOID lpData, _In_ SIZE_T nDataSize, _In_opt_ CLayer *lpToLayer);
Expand Down Expand Up @@ -661,6 +655,25 @@ class MX_NOVTABLE CIpc : public virtual CBaseMemObj, public CLoggable
VOID UpdateStats(_In_ BOOL bRead, _In_ DWORD dwBytesTransferred);
VOID GetStats(_In_ BOOL bRead, _Out_ PULONGLONG lpullBytesTransferred, _Out_opt_ float *lpnThroughputKbps);

protected:
static int InsertCompareFunc(_In_ LPVOID lpContext, _In_ CConnectionBase *lpConn1, _In_ CConnectionBase *lpConn2)
{
if ((SIZE_T)lpConn1 < (SIZE_T)lpConn2)
return -1;
if ((SIZE_T)lpConn1 > (SIZE_T)lpConn2)
return 1;
return 0;
};

static int SearchCompareFunc(_In_ LPVOID lpContext, _In_ SIZE_T key, _In_ CConnectionBase *lpConn)
{
if (key < (SIZE_T)lpConn)
return -1;
if (key > (SIZE_T)lpConn)
return 1;
return 0;
};

protected:
friend class CIpc;

Expand Down Expand Up @@ -743,7 +756,7 @@ class MX_NOVTABLE CIpc : public virtual CBaseMemObj, public CLoggable
OnEngineErrorCallback cEngineErrorCallback;
struct {
LONG volatile nRwMutex;
TRedBlackTree<CConnectionBase,SIZE_T> cTree;
TRedBlackTree<CConnectionBase> cTree;
} sConnections;
CPacketList cFreePacketsList32768;
CPacketList cFreePacketsList4096;
Expand Down
9 changes: 9 additions & 0 deletions Include/Http/HttpCookie.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ namespace MX {

class CHttpCookie : public virtual CBaseMemObj
{
public:
typedef enum {
SameSiteNone, SameSiteLax, SameSiteStrict
} eSameSite;

public:
CHttpCookie();
CHttpCookie(_In_ const CHttpCookie& cSrc) throw(...);
Expand Down Expand Up @@ -85,6 +90,9 @@ class CHttpCookie : public virtual CBaseMemObj
VOID SetHttpOnlyFlag(_In_ BOOL bIsHttpOnly);
BOOL GetHttpOnlyFlag() const;

HRESULT SetSameSite(_In_ eSameSite nSameSite);
eSameSite GetSameSite() const;

HRESULT ToString(_Inout_ CStringA& cStrDestA, _In_ BOOL bAddAttributes=TRUE);

HRESULT DoesDomainMatch(_In_z_ LPCSTR szDomainToMatchA);
Expand All @@ -101,6 +109,7 @@ class CHttpCookie : public virtual CBaseMemObj
CStringA cStrDomainA;
CStringA cStrPathA;
CDateTime cExpiresDt;
eSameSite nSameSite;
};

//-----------------------------------------------------------
Expand Down
14 changes: 7 additions & 7 deletions Include/Http/HttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,24 +384,24 @@ class CHttpServer : public virtual CThread, public CLoggable, private CCriticalS
HRESULT OnDownloadStarted(_Out_ LPHANDLE lphFile, _In_z_ LPCWSTR szFileNameW, _In_ LPVOID lpUserParam);

private:
class CRequestLimiter : public TRedBlackTreeNode<CRequestLimiter, PSOCKADDR_INET>
class CRequestLimiter : public virtual CBaseMemObj, public TRedBlackTreeNode<CRequestLimiter>
{
public:
CRequestLimiter(_In_ PSOCKADDR_INET lpAddr) : TRedBlackTreeNode<CRequestLimiter, PSOCKADDR_INET>()
CRequestLimiter(_In_ PSOCKADDR_INET lpAddr) : CBaseMemObj(), TRedBlackTreeNode<CRequestLimiter>()
{
MxMemCopy(&sAddr, lpAddr, sizeof(SOCKADDR_INET));
_InterlockedExchange(&nCount, 1);
return;
};

virtual PSOCKADDR_INET GetNodeKey() const
static int InsertCompareFunc(_In_ LPVOID lpContext, _In_ CRequestLimiter *lpLim1, _In_ CRequestLimiter *lpLim2)
{
return &(const_cast<CRequestLimiter*>(this)->sAddr);
return ::MxMemCompare(&(lpLim1->sAddr), &(lpLim2->sAddr), sizeof(SOCKADDR_INET));
};

virtual int CompareKeys(_In_ PSOCKADDR_INET key) const
static int SearchCompareFunc(_In_ LPVOID lpContext, _In_ PSOCKADDR_INET key, _In_ CRequestLimiter *lpLim)
{
return ::MxMemCompare(key, GetNodeKey(), sizeof(SOCKADDR_INET));
return ::MxMemCompare(key, &(lpLim->sAddr), sizeof(SOCKADDR_INET));
};

public:
Expand Down Expand Up @@ -446,7 +446,7 @@ class CHttpServer : public virtual CThread, public CLoggable, private CCriticalS

struct {
LONG volatile nRwMutex;
TRedBlackTree<CRequestLimiter, PSOCKADDR_INET> cTree;
TRedBlackTree<CRequestLimiter> cTree;
} sRequestLimiter;
};

Expand Down
Loading