1 #include "cachedhttp.h"
2 #include "localcache.h"
6 QByteArray requestHash(const HttpRequest &req) {
8 QByteArray s = req.url.toEncoded() + sep + req.body + sep + QByteArray::number(req.offset);
9 if (req.operation == QNetworkAccessManager::PostOperation) {
13 return LocalCache::hash(s);
17 CachedHttpReply::CachedHttpReply(const QByteArray &body, const HttpRequest &req)
18 : bytes(body), req(req) {
19 QTimer::singleShot(0, this, SLOT(emitSignals()));
22 QByteArray CachedHttpReply::body() const {
26 void CachedHttpReply::emitSignals() {
32 WrappedHttpReply::WrappedHttpReply(LocalCache *cache, const QByteArray &key, QObject *httpReply)
33 : QObject(httpReply), cache(cache), key(key), httpReply(httpReply) {
34 connect(httpReply, SIGNAL(data(QByteArray)), SIGNAL(data(QByteArray)));
35 connect(httpReply, SIGNAL(error(QString)), SIGNAL(error(QString)));
36 connect(httpReply, SIGNAL(finished(HttpReply)), SLOT(originFinished(HttpReply)));
39 void WrappedHttpReply::originFinished(const HttpReply &reply) {
40 if (reply.isSuccessful()) cache->insert(key, reply.body());
44 CachedHttp::CachedHttp(Http &http, const char *name)
45 : http(http), cache(LocalCache::instance(name)), cachePostRequests(false) {}
47 void CachedHttp::setMaxSeconds(uint seconds) {
48 cache->setMaxSeconds(seconds);
51 void CachedHttp::setMaxSize(uint maxSize) {
52 cache->setMaxSize(maxSize);
55 QObject *CachedHttp::request(const HttpRequest &req) {
56 bool cacheable = req.operation == QNetworkAccessManager::GetOperation ||
57 (cachePostRequests && req.operation == QNetworkAccessManager::PostOperation);
59 qDebug() << "Not cacheable" << req.url;
60 return http.request(req);
62 const QByteArray key = requestHash(req);
63 const QByteArray value = cache->value(key);
64 if (!value.isNull()) {
65 qDebug() << "CachedHttp HIT" << req.url;
66 return new CachedHttpReply(value, req);
68 qDebug() << "CachedHttp MISS" << req.url.toString();
69 return new WrappedHttpReply(cache, key, http.request(req));