📄 cache.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: cache.cpp,v 1.6.8.3 2004/07/09 02:04:03 hubbe Exp $ * * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved. * * The contents of this file, and the files included with this file, * are subject to the current version of the RealNetworks Public * Source License (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (the "RCSL") available at * http://www.helixcommunity.org/content/rcsl, in which case the RCSL * will apply. You may also obtain the license terms directly from * RealNetworks. You may not use this file except in compliance with * the RPSL or, if you have a valid RCSL with RealNetworks applicable * to this file, the RCSL. Please see the applicable RPSL or RCSL for * the rights, obligations and limitations governing use of the * contents of the file. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * This file is part of the Helix DNA Technology. RealNetworks is the * developer of the Original Code and owns the copyrights in the * portions it created. * * This file, and the files included with this file, is distributed * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET * ENJOYMENT OR NON-INFRINGEMENT. * * Technology Compatibility Kit Test Suite(s) Location: * http://www.helixcommunity.org/content/tck * * Contributor(s): * * ***** END LICENSE BLOCK ***** */#include "hlxclib/stdio.h"#include "hlxclib/string.h"#include "hlxclib/stdlib.h"#include "hlxclib/windows.h"#include "hlxosstr.h"#include "hxtypes.h"#include "hxcom.h"#include "hxstrutl.h"#include "ihxpckts.h"#include "chunkres.h"#include "hxheap.h"#ifdef _DEBUG#undef HX_THIS_FILEstatic const char HX_THIS_FILE[] = __FILE__;#endif#include "db.h"#include "cache.h"#define LOG_FILE "C:/Temp/cache.log"#include "http_debug.h"DB* g_pCacheDbHeader = NULL;DB* g_pCacheDbData = NULL;DB* g_pCacheDbUsage = NULL;static char* GetAbbrevUrl (char* pData, UINT32 ulSize);UINT32 GetFreeMbyteCount (const char *pDirectoryName);/**************************************************************************** * * Class: CCacheEntry * * Purpose: * * This is a helper class to allow a header to be easily placed before the * data stored in a URL's cache entry. It allows the expiry time and other * out-of-band information to be stored with the data. */CCacheEntry::CCacheEntry(){ // This creates a cache object for an existing cache entry LOG ("CCacheEntry::CCacheEntry() ******* Not expected to be used *******");}CCacheEntry::~CCacheEntry(){ if (m_pCacheHeader) // Free up the dbm data buffer { delete [] m_pCacheHeader; m_pCacheHeader = NULL; } if (m_pszDbDir) // Free the filename storage delete [] m_pszDbDir;}const char* CCacheEntry::GetDbBuffer(void){ return((const char *) m_pCacheHeader);}const char*CCacheEntry::GetDataBuffer(void){ const char *pData = (const char *) m_pCacheHeader; return(pData + sizeof (CacheHeader));}BOOLCCacheEntry::IsExpired(void){ if (m_pCacheHeader == NULL) return(TRUE); // Be mean if we don't know return((m_pCacheHeader->m_ulExpiryTime < (UINT32) time(NULL)) ? TRUE : FALSE);}UINT32CCacheEntry::CreateTime(void){ if (m_pCacheHeader == NULL) return(0); return(m_pCacheHeader->m_ulCreateTime);}UINT32CCacheEntry::ExpiryTime(void){ if (m_pCacheHeader == NULL) return(0); return(m_pCacheHeader->m_ulExpiryTime);}BOOLCCacheEntry::Evergreen(void){ if (m_pCacheHeader == NULL) return(FALSE); return((m_pCacheHeader->m_ulFlags & CACHE_EVERGREEN) ? TRUE : FALSE);}UINT32CCacheEntry::GetCategory(void){ if (m_pCacheHeader == NULL) return(0); return(m_pCacheHeader->m_ulCategory);} CCacheEntry::CCacheEntry(const char* pszDbDir, UINT32 ulMaxCacheSize, const char* pszUrl) : m_pszDbDir (NULL) , m_pCacheHeader (NULL){// HX_ASSERT (g_pCacheDB == NULL); INT32 rc = 0; INT32 nMode = 0; // A zero value defines the default setting char* pszFilename = NULL; // Will reference the full filename for each DB UINT32 ulFlags = DB_CREATE; // DB_CREATE DB_NOMAP DB_THREAD DBTYPE dbtType = DB_HASH; // DB_BTREE DB_HASH DB_RECNO DB_UNKNOWN DB_INFO diDbInfo = { 0 }; m_ulMaxCacheSize = ulMaxCacheSize; pszFilename = new char [strlen(pszDbDir) + 24]; if (pszFilename) { strcpy (pszFilename, pszDbDir); /* Flawfinder: ignore */ LOGX ((szDbgTemp, " [DB] Opening database '%s'", pszDbDir)); diDbInfo.ulSize = ulMaxCacheSize; strcpy (pszFilename + strlen(pszDbDir), OS_SEPARATOR_STRING "c_header" OS_SEPARATOR_STRING "db.dat"); /* Flawfinder: ignore */ m_pDbHeader = dbopen(pszFilename, ulFlags, nMode, dbtType, &diDbInfo); if (!m_pDbHeader) { LOGX ((szDbgTemp, " [DB] Error opening databases '%s'", pszFilename)); remove (pszFilename); // remove the troublesome cache databases and try again m_pDbHeader = dbopen(pszFilename, DB_CREATE, DB_RDWR, dbtType, &diDbInfo); } strcpy (pszFilename + strlen(pszDbDir), OS_SEPARATOR_STRING "c_data" OS_SEPARATOR_STRING "db.dat"); /* Flawfinder: ignore */ m_pDbData = dbopen(pszFilename, ulFlags, nMode, dbtType, &diDbInfo); if (!m_pDbData) { LOGX ((szDbgTemp, " [DB] Error opening databases '%s'", pszFilename)); remove (pszFilename); // remove the troublesome cache databases and try again m_pDbData = dbopen(pszFilename, DB_CREATE, DB_RDWR, dbtType, &diDbInfo); } strcpy (pszFilename + strlen(pszDbDir), OS_SEPARATOR_STRING "c_usage" OS_SEPARATOR_STRING "db.dat"); /* Flawfinder: ignore */ m_pDbUsage = dbopen(pszFilename, ulFlags, nMode, dbtType, &diDbInfo); if (!m_pDbUsage) { LOGX ((szDbgTemp, " [DB] Error opening databases '%s'", pszFilename)); remove (pszFilename); // remove the troublesome cache databases and try again m_pDbUsage = dbopen(pszFilename, DB_CREATE, DB_RDWR, dbtType, &diDbInfo); } if (rc || !m_pDbHeader || !m_pDbData || !m_pDbUsage) { LOGX ((szDbgTemp, " [DB] Error creating new databases in '%s' [%d]", pszDbDir, rc)); } else { LOGX ((szDbgTemp, " [DB] Created databases in '%s' [%08X]", pszDbDir, m_pDbHeader)); g_pCacheDbHeader = m_pDbHeader; g_pCacheDbData = m_pDbData; g_pCacheDbUsage = m_pDbUsage; m_pszDbDir = new char[strlen (pszDbDir) + 1]; strcpy (m_pszDbDir, pszDbDir); /* Flawfinder: ignore */ CleanCache(0); // purge expired entries } delete [] pszFilename; }}HX_RESULTCCacheEntry::WriteCache(UINT32 nContentRead, UINT32 ulExpiryTime, char * pszUrl, const char * pszMimeType, IHXBuffer* pHttpHeaders, CChunkyRes* pChunkyRes, BOOL bMirroredServers){ char* pszAbbrevUrl = NULL; UINT32 ulActual = 0; UINT32 ulFlags = 0; LOG ("WriteCache()"); if (m_pDbHeader == NULL || m_pDbData == NULL) { return(HXR_FAIL); } // If the flag m_bMirroredServers is set, truncate the leftmost domain label if (bMirroredServers) { pszAbbrevUrl = GetAbbrevUrl (pszUrl, strlen(pszUrl)); if (pszAbbrevUrl) { pszUrl = pszAbbrevUrl; } else { LOG (" Failed to get abbreviated URL"); } } // Save the header in the cache with the URL as the key UINT32 ulHeaderSize = sizeof (CacheHeader) + pHttpHeaders->GetSize(); char * pHeader = new char[ulHeaderSize]; LOGX((szDbgTemp,"> Caching %lu + %lu bytes under the key '%s', Type is '%s'", ulHeaderSize, nContentRead, pszUrl, NULLOK(pszMimeType))); if (pHeader) { // Read directly into the buffer CacheHeader* pCacheHeader = (CacheHeader*) pHeader; memset (pCacheHeader, 0, sizeof (CacheHeader)); pCacheHeader->m_ulCreateTime = time (NULL); pCacheHeader->m_ulExpiryTime = ulExpiryTime; pCacheHeader->m_ulFlags = 0; pCacheHeader->m_ulHttpHeaderSize = pHttpHeaders->GetSize(); strncpy(pCacheHeader->m_szMimeType, pszMimeType, sizeof pCacheHeader->m_szMimeType); /* Flawfinder: ignore */ pCacheHeader->m_szMimeType[sizeof(pCacheHeader->m_szMimeType)-1] = '\0'; // Add the HTTP Header data after the Cache Header memcpy (pCacheHeader + 1, pHttpHeaders->GetBuffer(), pHttpHeaders->GetSize()); /* Flawfinder: ignore */ } DBT key = { pszUrl, strlen(pszUrl) }; DBT data = { pHeader, ulHeaderSize }; int rc = m_pDbHeader->put(m_pDbHeader, &key, &data, ulFlags); if (rc == 0) { m_bCached = TRUE; LOG(" Cache header entry written"); // Create usage info CacheUsage usage = { 0, 0, 0, 0, 0, 0 }; DBT dbtUsage = { &usage, sizeof (CacheUsage) }; UINT32 ulNow = time (NULL); usage.m_ulCreateTime = time (NULL); usage.m_ulLastUsedTime = time (NULL); usage.m_ulExpiryTime = ulExpiryTime; usage.m_ulUseCount = 1; usage.m_ulSize = nContentRead; if ((rc = m_pDbUsage->put (m_pDbUsage, &key, &dbtUsage, ulFlags)) == DB_OK) { LOG (" Put of usage info done"); LOGX((szDbgTemp, " %lu Create=%ld Last=%ld Expiry=%ld", usage.m_ulUseCount, ulNow - usage.m_ulCreateTime, ulNow - usage.m_ulLastUsedTime, usage.m_ulExpiryTime - ulNow)); } else { LOG ("*** Put of usage failed"); } } else { if (rc == DB_KEYEXIST) { LOG(" Duplicate header entry, not written"); m_bCached = TRUE; } else { LOGX((szDbgTemp, "*** Error putting header entry into cache [%lu]", rc)); } }/**********/ /**** Now write the content ****/ // Add the URL contents
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -