⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cookies.cpp

📁 著名的 helix realplayer 基于手机 symbian 系统的 播放器全套源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		if (pTmpBuf[i+1])
		{
		    pTmpBuf[i+1] = '\0';
		}

		// Back the file pointer up.
		fseek(fp, (long)((i + 1) - ulBytesRead), SEEK_CUR);
		*pBytesRead = i + 1;
		break;
	    }
	    i++;
	}
    }
    else
    {
	hr = HXR_FAILED;
    }
    
cleanup:

    return hr;
#endif /* _OPENWAVE */
}

CookieStruct*
HXCookies::CheckForPrevCookie(char * path,
			       char * hostname,
			       char * name)
{
    int		    nResult = 0;
    const char*	    pValue = NULL;
    BOOL	    bFound = FALSE;
    CookieStruct*   pCookie = NULL;
    CHXSimpleList::Iterator  i;

    if (!m_pRMCookies)
    {	
	goto cleanup;
    }

    for (i = m_pRMCookies->Begin(); i != m_pRMCookies->End(); ++i)
    {
	pCookie = (CookieStruct*) (*i);
	
        if(path && hostname			&& 
	   pCookie->pPath			&& 
	   pCookie->pHost			&& 
	   pCookie->pCookieName			&&
	   !pCookie->pCookieName->Compare(name)	&&
	   !pCookie->pPath->Compare(path)	&&	    
	   !pCookie->pHost->CompareNoCase(hostname))
	{
            bFound = TRUE;
	    break;
	}
    }

cleanup:    
    return bFound?pCookie:NULL;
}

HX_RESULT
HXCookies::OpenCookies(char* pCookieFile, BOOL bRMCookies, CHXSimpleList*& pCookiesList)
{
#ifdef _OPENWAVE
    return HXR_NOTIMPL;
#else
    HX_RESULT	    hr = HXR_OK;
    char*	    pHost = NULL;
    char*	    pPath = NULL;
    char*	    pCookieName = NULL; 
    char*	    pCookieValue = NULL;
    char*	    pIsDomain = NULL;
    char*	    pXXX = NULL;
    char*	    pExpires = NULL;
    char*	    pBuffer = new char[LINE_BUFFER_SIZE]; /* Flawfinder: ignore */
    UINT32	    ulBytesRead = 0;
    FILE*	    fp = NULL;
    CookieStruct*   pNewCookie = NULL;
 
    pCookiesList = NULL;

    if (!pBuffer)
    {
	hr = HXR_FAILED;
	goto cleanup;
    }

    pBuffer[0] = '\0';

    if (!pCookieFile)
    {
	hr = HXR_FAILED;
	goto cleanup;
    }

#ifdef _WINDOWS
    if (bRMCookies)
    {
	if (!m_pLock)
	{
#ifdef THREADS_SUPPORTED            
	    HXEvent::MakeEvent(m_pLock, "CookieFileLock", FALSE);
#else            
	    HXEvent::MakeStubEvent(m_pLock, "CookieFileLock", FALSE);
#endif            
	}
	else
	{
	    m_pLock->Wait();
	}
    }
#endif /* _WINDOWS */

    if (bRMCookies)
    {
	if(CheckCookies() == HXR_FAIL)
	{
     	    hr = HXR_FAILED;
	    goto cleanup;
	}
    }

    if (!(fp = fopen(pCookieFile, "r+b")))
    {
	hr = HXR_FAILED;
	goto cleanup;
    }

#if defined (_UNIX) && !defined(_SUN) && !defined(_HPUX) && !defined(_IRIX) && !defined(_AIX) && !defined(_OSF1)
    if (bRMCookies)
    {
	m_fileID = fileno(fp);    
	flock(m_fileID, LOCK_EX);
    }
#endif /* _UNIX */

    /* format is:
     *
     * host \t is_domain \t path \t xxx \t expires \t name \t cookie
     *
     * if this format isn't respected we move onto the next line in the file.
     *
     * is_domain is TRUE or FALSE   -- defaulting to FALSE
     * xxx is TRUE or FALSE	    -- should default to TRUE
     * expires is a time_t integer
     * cookie can have tabs
     */
    while(HXR_OK == FileReadLine(fp, &pBuffer[0], LINE_BUFFER_SIZE, &ulBytesRead))
    {
	if (*pBuffer == '#' || *pBuffer == CR || *pBuffer == LF || *pBuffer == 0)
	{
	    continue;
	}

	pHost = pBuffer;
	    
	if(!(pIsDomain = strchr(pHost, '\t')))
	{
	    continue;
	}
	*pIsDomain++ = '\0';
	if(*pIsDomain == CR || *pIsDomain == LF || *pIsDomain == 0)
	{
	    continue;
	}
	
	if(!(pPath = strchr(pIsDomain, '\t')))
	{
	    continue;
	}
	*pPath++ = '\0';
	if(*pPath == CR || *pPath == LF || *pPath == 0)
	{
	    continue;
	}

	if(!(pXXX = strchr(pPath, '\t')))
	{
	    continue;
	}
	*pXXX++ = '\0';
	if(*pXXX == CR || *pXXX == LF || *pXXX == 0)
	{
	    continue;
	}

	if(!(pExpires = strchr(pXXX, '\t')))
	{
	    continue;
	}
	*pExpires++ = '\0';
	if(*pExpires == CR || *pExpires == LF || *pExpires == 0)
	{
	    continue;
	}

	if(!(pCookieName = strchr(pExpires, '\t')))
	{
	    continue;
	}
	*pCookieName++ = '\0';
	if(*pCookieName == CR || *pCookieName == LF || *pCookieName == 0)
	{
	    continue;
	}

	if(!(pCookieValue = strchr(pCookieName, '\t')))
	{
	    continue;
	}
	*pCookieValue++ = '\0';
	if(*pCookieValue == CR || *pCookieValue == LF || *pCookieValue == 0)
	{
	    continue;
	}

	// remove the '\n' from the end of the cookie
	pCookieValue = ::StripLine(pCookieValue);

	// construct a new cookie_struct
	pNewCookie = new CookieStruct;
	if(!pNewCookie)
	{
	    hr = HXR_OUTOFMEMORY;
	    goto cleanup;
	}
	
	memset(pNewCookie, 0, sizeof(CookieStruct));

	/* copy
	 */
	pNewCookie->pCookieValue = new CHXString(pCookieValue);
	pNewCookie->pCookieName = new CHXString(pCookieName);
	pNewCookie->pPath = new CHXString(pPath);
	pNewCookie->pHost = new CHXString(pHost);

#ifdef _MACINTOSH
	pNewCookie->expires = (time_t)atoi64(pExpires);
#else
	pNewCookie->expires = atol(pExpires);
#endif

	if(!strcasecmp(pIsDomain, "TRUE"))
	{
	    pNewCookie->bIsDomain = TRUE;
	}
	else
	{
	    pNewCookie->bIsDomain = FALSE;
	}
	
	pNewCookie->bMemoryOnly = FALSE;

	if (!pCookiesList)
	{
	    pCookiesList = new CHXSimpleList();
	}

	hr = AddCookie(pNewCookie, pCookiesList);
    }

    UpdateModificationTime();

cleanup:

#if defined (_UNIX) && !defined(_SUN) && !defined(_HPUX) && !defined(_IRIX) && !defined(_AIX) && !defined(_OSF1)
    if (bRMCookies)
    {
	flock(m_fileID, LOCK_UN);
    }
#endif /* _UNIX */

    if (fp)
    {
	fclose(fp);
    }

#ifdef _WINDOWS
    if (bRMCookies && m_pLock)
    {
	m_pLock->SignalEvent();
    }
#endif /* _WINDOWS */

    delete [] pBuffer;
    
    return hr;
#endif /* _OPENWAVE */
}

HX_RESULT	
HXCookies::SaveCookies(void)
{
#ifdef _OPENWAVE
    return HXR_NOTIMPL;
#else
    HX_RESULT	    hr = HXR_OK;
    FILE*	    fp = NULL;
    INT32	    len = 0;
    char	    date_string[36] = {0}; /* Flawfinder: ignore */
    time_t	    cur_date = time(NULL);
    CookieStruct*   pTempCookie = NULL;
    CHXSimpleList::Iterator  i;

    if (!m_pRMCookies || !m_pRMCookiesPath)
    {
	goto cleanup;
    }

#ifdef _WINDOWS
    if (!m_pLock)
    {
#ifdef THREADS_SUPPORTED        
	HXEvent::MakeEvent(m_pLock, "CookieFileLock", FALSE);
#else        
	HXEvent::MakeStubEvent(m_pLock, "CookieFileLock", FALSE);
#endif
    }
    else
    {
	m_pLock->Wait();
    }
#endif /* _WINDOWS */

    if (!(fp = fopen(m_pRMCookiesPath, "w")))
    {
	hr = HXR_FAILED;
	goto cleanup;
    }

#ifdef _UNIX
    //Make the permisions on the cookies file User read/write only.
    if( chmod( m_pRMCookiesPath, S_IRUSR | S_IWUSR ) != 0 )
    {
        HX_ASSERT( "Can't change permision on cookies file." == NULL );
    }
    
#endif    

#if defined (_UNIX) && !defined(_SUN) && !defined(_HPUX) && !defined(_IRIX) && !defined(_AIX) && !defined(_OSF1)
    m_fileID = fileno(fp);    
    flock(m_fileID, LOCK_EX);
#endif /* _UNIX */

    fwrite(RM_COOKIE_CAPTION, sizeof(char), strlen(RM_COOKIE_CAPTION), fp);

    for (i = m_pRMCookies->Begin(); i != m_pRMCookies->End(); ++i)
    {
	pTempCookie = (CookieStruct*) (*i);

	/* format shall be:
 	 *
	 * host \t is_domain \t path \t secure \t expires \t name \t cookie
	 *
	 * is_domain is TRUE or FALSE
	 * secure is TRUE or FALSE  
	 * expires is a time_t integer
	 * cookie can have tabs
	 */
	if(pTempCookie->expires < cur_date)
	{
	    continue;  /* don't write entry if cookie has expired 
			* or has no expiration date
			*/
	}
		
	len = fwrite((const char*)*(pTempCookie->pHost), sizeof(char), pTempCookie->pHost->GetLength(), fp);
	if (len < 0)
	{
	    hr = HXR_FAILED;
	    goto cleanup;
	}
	fwrite("\t", sizeof(char), 1, fp);

	if(pTempCookie->bIsDomain)
	{
	    fwrite("TRUE", sizeof(char), 4, fp);
	}
	else
	{
	    fwrite("FALSE", sizeof(char), 5, fp);
	}	    
	fwrite("\t", sizeof(char), 1, fp);

	fwrite((const char*)*(pTempCookie->pPath), sizeof(char), pTempCookie->pPath->GetLength(), fp);
	fwrite("\t", sizeof(char), 1, fp);

	fwrite("FALSE", sizeof(char), 5, fp);
	fwrite("\t", sizeof(char), 1, fp);

	sprintf(date_string, "%lu", pTempCookie->expires); /* Flawfinder: ignore */
	fwrite(date_string, sizeof(char), strlen(date_string), fp);
	fwrite("\t", sizeof(char), 1, fp);

	fwrite((const char*)*(pTempCookie->pCookieName), sizeof(char), 
		pTempCookie->pCookieName->GetLength(), fp);
	fwrite("\t", sizeof(char), 1, fp);

	fwrite((const char*)*(pTempCookie->pCookieValue), sizeof(char), 
		pTempCookie->pCookieValue->GetLength(), fp);
	fwrite(LINEBREAK, sizeof(char), LINEBREAK_LEN, fp);

	//once saved to disk, make sure the memory only flag is false
	pTempCookie->bMemoryOnly = FALSE;
    }

    UpdateModificationTime();    
    m_bSaveCookies = FALSE;

cleanup:

#if defined (_UNIX) && !defined(_SUN) && !defined(_HPUX) && !defined(_IRIX) && !defined(_AIX) && !defined(_OSF1)
    flock(m_fileID, LOCK_UN);
#endif /* _UNIX */

    if (fp)
    {
	fclose(fp);
    }

#ifdef _WINDOWS
    if (m_pLock)
    {
	m_pLock->SignalEvent();
    }
#endif /* _WINDOWS */

    SecureCookies();

    return(hr);
#endif /* _OPENWAVE */
}

HX_RESULT
HXCookies::AddCookie(CookieStruct* pCookie, CHXSimpleList*& pCookiesList)
{
    HX_RESULT	    hr = HXR_OK;
    BOOL	    bShorterPathFound = FALSE;
    CookieStruct*   pTempCookie = NULL;
    LISTPOSITION    position;

    if (!pCookie || !pCookiesList)
    {
	hr = HXR_FAILED;
	goto cleanup;
    }

    /* add it to the list so that it is before any strings of
     * smaller length
     */
    position = pCookiesList->GetHeadPosition();
    while (position != NULL)
    {
	pTempCookie = (CookieStruct*) pCookiesList->GetNext(position);

	if (strlen(*pCookie->pPath) > strlen(*pTempCookie->pPath))
	{
	    // Remember that we found a shorter path
	    bShorterPathFound = TRUE;

	    // If the position is null, then event was the first
	    // item in the list, and we need to do some fancy footwork...
	    if (!position)
	    {
		POSITION theTail = pCookiesList->GetTailPosition();
		pCookiesList->InsertBefore(theTail,(void*) pCookie);
	    }
	    // otherwise, roll after one...
	    else
	    {
		pCookiesList->GetPrev(position);

		if (!position)
		{
		    pCookiesList->AddHead((void*) pCookie);
		}
		else
		{
		    pCookiesList->InsertBefore(position,(void*) pCookie);
		}
	    }

	    break;
	}
    }

    // If we didn't find an earlier packet, then we should insert at
    // the head of the list...
    if (!bShorterPathFound)
    {
	pCookiesList->AddTail((void*) pCookie);
    }

cleanup:

    return hr;
}

BOOL
HXCookies::WasCookieAdded(CHXSimpleList* pCookiesList, CookieStruct* pCookie)
{
    BOOL bResult = FALSE;
    CookieStruct*  pTempCookie = NULL;
    CHXSimpleList::Iterator  i;

    if (!pCookiesList || !pCookie)
    {
	goto cleanup;
    }

    for (i = pCookiesList->Begin(); i != pCookiesList->End(); ++i)
    {
	pTempCookie = (CookieStruct*) (*i);
	
	if (pTempCookie->pCookieName && pCookie->pCookieName 
		&& *(pTempCookie->pCookieName) == *(pCookie->pCookieName) 
		&& pTempCookie->pHost && pCookie->pHost)
	{
	    if(DoesDomainMatch(*pTempCookie->pHost, *pCookie->pHost))
	    {
		bResult = TRUE;
		break;
	    }
	}
    }

cleanup:

    return bResult;
}


BOOL HXCookies::DoesDomainMatch(const char* szDomain, const char* szDomainToParse)
{
    BOOL bMatches = FALSE;
    CHXString cHostCopy;
    CHXString cDomainCopy;
    CHXString cHostRight;

    if(!szDomain || !szDomainToParse || !strlen(szDomain) || !strlen(szDomainToParse))
    {
	goto cleanup;
    }

    cHostCopy = szDomainToParse;
    cDomainCopy = szDomain;
    cDomainCopy.MakeLower();

    // Now we compare the domain (from the cookie itself) with
    // the rightmost characters of the host. For instance,
    // a domain of ".bar.com" would match with a host (passed in)
    // of "foo.bar.com", "www.bar.com", etc. but would NOT match
    // a host of "bar.com".
    cHostRight = cHostCopy.Right(cDomainCopy.GetLength());
    if (cHostRight != cDomainCopy)
    {
        // no match
        goto cleanup;
    }
    else if (cDomainCopy.GetAt(0) != '.' && cHostCopy.GetLength() > cDomainCopy.GetLength() &&

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -