-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathHttpCacheInternal.cpp
More file actions
188 lines (158 loc) · 6.17 KB
/
Copy pathHttpCacheInternal.cpp
File metadata and controls
188 lines (158 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright 2017 Sony Corporation
*/
#include "Poco/File.h"
#include "Poco/NumberParser.h"
#include "Poco/Timestamp.h"
#include "easyhttpcpp/common/Cache.h"
#include "easyhttpcpp/common/CacheManager.h"
#include "easyhttpcpp/common/CacheMetadata.h"
#include "easyhttpcpp/common/CoreLogger.h"
#include "easyhttpcpp/common/FileUtil.h"
#include "easyhttpcpp/common/StringUtil.h"
#include "easyhttpcpp/CacheControl.h"
#include "easyhttpcpp/HttpConstants.h"
#include "easyhttpcpp/HttpException.h"
#include "easyhttpcpp/Headers.h"
#include "easyhttpcpp/Request.h"
#include "HttpCacheInternal.h"
#include "HttpCacheMetadata.h"
#include "HttpFileCache.h"
#include "HttpInternalConstants.h"
#include "HttpUtil.h"
using easyhttpcpp::common::Cache;
using easyhttpcpp::common::CacheManager;
using easyhttpcpp::common::CacheMetadata;
using easyhttpcpp::common::FileUtil;
using easyhttpcpp::common::StringUtil;
namespace easyhttpcpp {
static const std::string Tag = "HttpCacheInternal";
HttpCacheInternal::HttpCacheInternal(const Poco::Path& path, size_t maxSize) : m_maxSize(maxSize)
{
m_cachePath = path;
m_cacheRootDir = path.absolute();
Poco::Path cacheDir(HttpInternalConstants::Caches::CacheDir);
m_cacheRootDir.append(cacheDir);
m_pFileCache = new HttpFileCache(m_cacheRootDir, maxSize);
m_pCacheManager = new CacheManager(NULL, m_pFileCache);
Poco::Path tempDir(m_cacheRootDir);
Poco::Path tempChildDir(HttpInternalConstants::Caches::TempDir);
tempDir.append(tempChildDir);
m_cacheTempDir = tempDir.toString();
}
HttpCacheInternal::~HttpCacheInternal()
{
m_pCacheManager = NULL;
m_pFileCache = NULL;
}
const Poco::Path& HttpCacheInternal::getPath() const
{
return m_cachePath;
}
void HttpCacheInternal::evictAll()
{
if (!m_pCacheManager->purge(true)) {
EASYHTTPCPP_LOG_D(Tag, "Failed to purge cache.");
throw HttpExecutionException("Failed to purge cache.");
}
Poco::FastMutex::ScopedLock lock(m_directoryMutex);
if (!FileUtil::removeDirsIfPresent(Poco::Path(m_cacheTempDir))) {
EASYHTTPCPP_LOG_D(Tag, "Failed to remove cache temporary directory. [%s]", m_cacheTempDir.c_str());
throw HttpExecutionException(StringUtil::format("Failed to remove cache temporary directory.[%s]",
m_cacheTempDir.c_str()));
}
}
size_t HttpCacheInternal::getMaxSize() const
{
return m_maxSize;
}
size_t HttpCacheInternal::getSize()
{
HttpFileCache* pFileCache = static_cast<HttpFileCache*> (m_pFileCache.get());
return pFileCache->getSize();
}
const std::string& HttpCacheInternal::getTempDirectory()
{
Poco::FastMutex::ScopedLock lock(m_directoryMutex);
Poco::File file(m_cacheTempDir);
if (!FileUtil::createDirsIfAbsent(file)) {
EASYHTTPCPP_LOG_D(Tag, "create cache temp directory failed.");
throw HttpExecutionException("can not access cache temporary directory.");
}
return m_cacheTempDir;
}
easyhttpcpp::common::CacheManager::Ptr HttpCacheInternal::getCacheManager() const
{
return m_pCacheManager;
}
HttpCacheStrategy::Ptr HttpCacheInternal::createCacheStrategy(Request::Ptr pRequest)
{
Poco::Timestamp now;
unsigned long long nowAtEpoch = static_cast<unsigned long long>(now.epochTime());
std::string key = HttpUtil::makeCacheKey(pRequest);
if (key.empty()) {
EASYHTTPCPP_LOG_D(Tag, "createCacheStrategy: can not make key.");
return new HttpCacheStrategy(pRequest, NULL, nowAtEpoch);
}
// get CacheMetadata
CacheMetadata::Ptr pCacheMetadata;
if (!m_pCacheManager->getMetadata(key, pCacheMetadata)) {
// not exist cache
EASYHTTPCPP_LOG_D(Tag, "cache not found.");
return new HttpCacheStrategy(pRequest, NULL, nowAtEpoch);
}
HttpCacheMetadata* pHttpCacheMetadata = static_cast<HttpCacheMetadata*> (pCacheMetadata.get());
// create CacheResponse
Response::Ptr pCacheResponse = createResponseFromCacheMetadata(pRequest, *pHttpCacheMetadata);
// check CacheStrategy
HttpCacheStrategy::Ptr pCacheStrategy = new HttpCacheStrategy(pRequest, pCacheResponse, nowAtEpoch);
return pCacheStrategy;
}
void HttpCacheInternal::remove(Request::Ptr pRequest)
{
std::string key = HttpUtil::makeCacheKey(Request::HttpMethodGet, pRequest->getUrl());
if (key.empty()) {
EASYHTTPCPP_LOG_D(Tag, "remove: can not make key.");
return;
}
m_pCacheManager->remove(key);
}
std::istream* HttpCacheInternal::createInputStreamFromCache(Request::Ptr pRequest)
{
std::string key = HttpUtil::makeCacheKey(pRequest);
if (key.empty()) {
EASYHTTPCPP_LOG_D(Tag, "createInputStreamFromCache: can not make key.");
return NULL;
}
std::istream* pStream = NULL;
if (m_pCacheManager->getData(key, pStream)) {
EASYHTTPCPP_LOG_D(Tag, "create response body stream from cache.");
return pStream;
} else {
EASYHTTPCPP_LOG_D(Tag, "can not create response body stream from cache.");
return NULL;
}
}
Response::Ptr HttpCacheInternal::createResponseFromCacheMetadata(Request::Ptr pRequest,
HttpCacheMetadata& httpCacheMetadata)
{
// create CacheControl from CacheMetadata
CacheControl::Ptr pCacheControl = CacheControl::createFromHeaders(httpCacheMetadata.getResponseHeaders());
// create Response
Headers::Ptr pResponseHeaders = httpCacheMetadata.getResponseHeaders();
Response::Builder builder;
builder.setCode(httpCacheMetadata.getStatusCode()).setMessage(httpCacheMetadata.getStatusMessage()).
setCacheControl(pCacheControl).setHeaders(pResponseHeaders).
setRequest(pRequest).setSentRequestSec(httpCacheMetadata.getSentRequestAtEpoch()).
setReceivedResponseSec(httpCacheMetadata.getReceivedResponseAtEpoch());
if (pResponseHeaders->has(HttpConstants::HeaderNames::ContentLength)) {
std::string contentLengthStr = pResponseHeaders->getValue(
HttpConstants::HeaderNames::ContentLength, "-1");
Poco::Int64 contentLength;
if (Poco::NumberParser::tryParse64(contentLengthStr, contentLength)) {
builder.setHasContentLength(true).setContentLength(static_cast<ssize_t> (contentLength));
}
}
return builder.build();
}
} /* namespace easyhttpcpp */