]> git.sur5r.net Git - minitube/blob - lib/http/src/cachedhttp.h
New upstream version 3.5
[minitube] / lib / http / src / cachedhttp.h
1 #ifndef CACHEDHTTP_H
2 #define CACHEDHTTP_H
3
4 #include "http.h"
5
6 class LocalCache;
7
8 class CachedHttp : public Http {
9 public:
10     CachedHttp(Http &http = Http::instance(), const char *name = "http");
11     void setMaxSeconds(uint seconds);
12     void setMaxSize(uint maxSize);
13     void setCachePostRequests(bool value) { cachePostRequests = value; }
14     void setIgnoreHostname(bool value) { ignoreHostname = value; }
15     HttpReply *request(const HttpRequest &req);
16
17 private:
18     QByteArray requestHash(const HttpRequest &req);
19
20     Http &http;
21     LocalCache *cache;
22     bool cachePostRequests;
23     bool ignoreHostname = false;
24 };
25
26 class CachedHttpReply : public HttpReply {
27     Q_OBJECT
28
29 public:
30     CachedHttpReply(const QByteArray &body, const HttpRequest &req);
31     QUrl url() const { return req.url; }
32     int statusCode() const { return 200; }
33     QByteArray body() const;
34
35 private slots:
36     void emitSignals();
37
38 private:
39     const QByteArray bytes;
40     const HttpRequest req;
41 };
42
43 class WrappedHttpReply : public HttpReply {
44     Q_OBJECT
45
46 public:
47     WrappedHttpReply(LocalCache *cache, const QByteArray &key, HttpReply *httpReply);
48     QUrl url() const { return httpReply->url(); }
49     int statusCode() const { return httpReply->statusCode(); }
50     QByteArray body() const { return httpReply->body(); }
51
52 private slots:
53     void originFinished(const HttpReply &reply);
54
55 private:
56     LocalCache *cache;
57     QByteArray key;
58     HttpReply *httpReply;
59 };
60
61 #endif // CACHEDHTTP_H