bug 991: fix crash when the file is already cached
[elinks.git] / src / cache / cache.h
blobed68d1fee846c011c2fefd2a524b33088c490668
1 #ifndef EL__CACHE_CACHE_H
2 #define EL__CACHE_CACHE_H
4 #include "main/object.h"
5 #include "util/lists.h"
6 #include "util/time.h"
8 struct listbox_item;
9 struct uri;
11 /* This enum describes the level of caching of certain cache entry. That is,
12 * under what conditions shall it be reloaded, if ever. The one with lowest
13 * value is most agressively cached, however the cache is most reluctant to
14 * cache the one with highest value. */
15 /* TODO: Invert order to be more intuitive. But be careful, many tests rely on
16 * current order. --Miciah,Zas */
17 enum cache_mode {
18 CACHE_MODE_INCREMENT = -1,
19 CACHE_MODE_ALWAYS,
20 CACHE_MODE_NORMAL,
21 CACHE_MODE_CHECK_IF_MODIFIED,
22 CACHE_MODE_FORCE_RELOAD,
23 CACHE_MODE_NEVER,
26 struct cache_entry {
27 OBJECT_HEAD(struct cache_entry);
29 /* Items in this list are ALLOCATED IN A NON-STANDARD WAY! Thus if you
30 * are gonna mess with them (you shouldn't), you need to use the
31 * mmap suite. */
32 LIST_OF(struct fragment) frag;
34 struct uri *uri; /* Identifier for the cached data */
35 struct uri *proxy_uri; /* Proxy identifier or same as @uri */
36 struct uri *redirect; /* Location we were redirected to */
38 unsigned char *head; /* The protocol header */
39 unsigned char *content_type; /* MIME type: <type> "/" <subtype> */
40 unsigned char *last_modified; /* Latest modification date */
41 unsigned char *etag; /* ETag value from the HTTP header */
42 unsigned char *ssl_info; /* SSL ciphers used during transfer */
43 unsigned char *encoding_info; /* Encoding used during transfer */
45 unsigned int id; /* Change each time entry is modified. */
47 time_t seconds; /* Access time. Used by 'If-Modified-Since' */
49 off_t length; /* The expected and complete size */
50 off_t data_size; /* The actual size of all fragments */
52 struct listbox_item *box_item; /* Dialog data for cache manager */
54 timeval_T max_age; /* Expiration time */
56 unsigned int expire:1; /* Whether to honour max_age */
57 unsigned int preformatted:1; /* Has content been preformatted? */
58 unsigned int redirect_get:1; /* Follow redirect using get method? */
59 unsigned int incomplete:1; /* Has all data been downloaded? */
60 unsigned int valid:1; /* Is cache entry usable? */
62 /* This is a mark for internal workings of garbage_collection(), whether
63 * the cache_entry should be busted or not. You are not likely to see
64 * an entry with this set to 1 in wild nature ;-). */
65 unsigned int gc_target:1; /* The GC touch of death */
66 unsigned int cgi:1; /* Is a CGI output? */
68 enum cache_mode cache_mode; /* Reload condition */
71 struct fragment {
72 LIST_HEAD(struct fragment);
74 off_t offset;
75 off_t length;
76 off_t real_length;
77 unsigned char data[1]; /* Must be last */
81 /* Searches the cache for an entry matching the URI. Returns NULL if no one
82 * matches. */
83 struct cache_entry *find_in_cache(struct uri *uri);
85 /* Searches the cache for a matching entry else a new one is added. Returns
86 * NULL if allocation fails. */
87 struct cache_entry *get_cache_entry(struct uri *uri);
89 /* Searches the cache for a matching entry and checks if it is still valid and
90 * usable. Returns NULL if the @cache_mode suggests to reload it again. */
91 struct cache_entry *get_validated_cache_entry(struct uri *uri, enum cache_mode cache_mode);
93 /* Checks if a dangling cache entry pointer is still valid. */
94 int cache_entry_is_valid(struct cache_entry *cached);
96 /* Follow all redirects and return the resulting cache entry or NULL if there
97 * are missing redirects. */
98 struct cache_entry *follow_cached_redirects(struct cache_entry *cached);
100 /* Works like find_in_cache(), but will follow cached redirects using
101 * follow_cached_redirects(). */
102 struct cache_entry *get_redirected_cache_entry(struct uri *uri);
104 /* Add a fragment to the @cached object at the given @offset containing @length
105 * bytes from the @data pointer. */
106 /* Returns -1 upon error,
107 * 1 if cache entry was enlarged,
108 * 0 if only old data were overwritten. */
109 int add_fragment(struct cache_entry *cached, off_t offset,
110 const unsigned char *data, ssize_t length);
112 /* Defragments the cache entry and returns the resulting fragment containing the
113 * complete source of all currently downloaded fragments. Returns NULL if
114 * validation of the fragments fails. */
115 struct fragment *get_cache_fragment(struct cache_entry *cached);
117 /* Should be called when creation of a new cache has been completed. Most
118 * importantly, it will updates cached->incomplete. */
119 void normalize_cache_entry(struct cache_entry *cached, off_t length);
121 void free_entry_to(struct cache_entry *cached, off_t offset);
122 void delete_entry_content(struct cache_entry *cached);
123 void delete_cache_entry(struct cache_entry *cached);
125 /* Sets up the cache entry to redirect to a new location
126 * @location decides where to redirect to by resolving it relative to the
127 * entry's URI.
128 * @get controls the method should be used when handling the redirect.
129 * @incomplete will become the new value of the incomplete member if it
130 * is >= 0.
131 * Returns the URI being redirected to or NULL if allocation failed.
133 struct uri *
134 redirect_cache(struct cache_entry *cached, unsigned char *location,
135 int get, int incomplete);
137 /* The garbage collector trigger. If @whole is zero, remove unused cache
138 * entries which are bigger than the cache size limit set by user. For @zero
139 * being one, remove all unused cache entries. */
140 void garbage_collection(int whole);
142 /* Used by the resource and memory info dialogs for getting information about
143 * the cache. */
144 unsigned longlong get_cache_size(void);
145 int get_cache_entry_count(void);
146 int get_cache_entry_used_count(void);
147 int get_cache_entry_loading_count(void);
149 #endif