1 | #pragma once |
2 | |
3 | #include <mbgl/util/chrono.hpp> |
4 | #include <mbgl/util/optional.hpp> |
5 | |
6 | #include <string> |
7 | #include <memory> |
8 | |
9 | namespace mbgl { |
10 | |
11 | class Response { |
12 | public: |
13 | Response() = default; |
14 | Response(const Response&); |
15 | Response& operator=(const Response&); |
16 | |
17 | public: |
18 | class Error; |
19 | // When this object is empty, the response was successful. |
20 | std::unique_ptr<const Error> error; |
21 | |
22 | // This is set to true for 204 Not Modified responses, and, for backward compatibility, |
23 | // for 404 Not Found responses for tiles. |
24 | bool noContent = false; |
25 | |
26 | // This is set to true for 304 Not Modified responses. |
27 | bool notModified = false; |
28 | |
29 | // This is set to true when the server requested that no expired resources be used by |
30 | // specifying "Cache-Control: must-revalidate". |
31 | bool mustRevalidate = false; |
32 | |
33 | // The actual data of the response. Present only for non-error, non-notModified responses. |
34 | std::shared_ptr<const std::string> data; |
35 | |
36 | optional<Timestamp> modified; |
37 | optional<Timestamp> expires; |
38 | optional<std::string> etag; |
39 | |
40 | bool isFresh() const { |
41 | return expires ? *expires > util::now() : !error; |
42 | } |
43 | |
44 | // Indicates whether we are allowed to use this response according to HTTP caching rules. |
45 | // It may or may not be stale. |
46 | bool isUsable() const { |
47 | return !mustRevalidate || (expires && *expires > util::now()); |
48 | } |
49 | }; |
50 | |
51 | class Response::Error { |
52 | public: |
53 | enum class Reason : uint8_t { |
54 | Success = 1, |
55 | NotFound = 2, |
56 | Server = 3, |
57 | Connection = 4, |
58 | RateLimit = 5, |
59 | Other = 6, |
60 | } reason = Reason::Other; |
61 | |
62 | // An error message from the request handler, e.g. a server message or a system message |
63 | // informing the user about the reason for the failure. |
64 | std::string message; |
65 | |
66 | optional<Timestamp> retryAfter; |
67 | |
68 | public: |
69 | Error(Reason, std::string = "" , optional<Timestamp> = {}); |
70 | }; |
71 | |
72 | std::ostream& operator<<(std::ostream&, Response::Error::Reason); |
73 | |
74 | } // namespace mbgl |
75 | |