annotate src/cache.c @ 2048:5060d415a85a

clickable menu items (even those introducing submenus) MUST have callbacks I clicked on the "Panel size" item itself instead of any of the options in its submenu, and: Segfault!
author corvid <corvid@lavabit.com>
date Thu, 26 May 2011 02:51:18 +0000
parents 4779a3d891a1
children cb8fdc69103d
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 * File: cache.c
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3 *
35
d9e7b35430de Updated copyright lines
jcid
parents: 19
diff changeset
4 * Copyright 2000-2007 Jorge Arellano Cid <jcid@dillo.org>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 * the Free Software Foundation; either version 3 of the License, or
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9 * (at your option) any later version.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 * Dillo's cache module
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 #include <ctype.h> /* for tolower */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 #include <sys/types.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19 #include <stdlib.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22 #include "msg.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23 #include "IO/Url.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 #include "IO/IO.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25 #include "web.hh"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26 #include "dicache.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 #include "nav.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28 #include "cookies.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29 #include "misc.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30 #include "capi.h"
43
3d485d0e4a6a Added an experimental gzip decoder!
jcid
parents: 35
diff changeset
31 #include "decode.h"
470
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
32 #include "auth.h"
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34 #include "timeout.hh"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35 #include "uicmd.hh"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
36
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 #define NULLKey 0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38
360
1354085ccbfe - Removed the remaining DEBUG_MSG in src/
jcid
parents: 336
diff changeset
39 /* Maximum initial size for the automatically-growing data buffer */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 #define MAX_INIT_BUF 1024*1024
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 /* Maximum filesize for a URL, before offering a download */
285
6aa09f56686c - Incremented the HUGE_FILESIZE threshold to 15 MB
jcid
parents: 260
diff changeset
42 #define HUGE_FILESIZE 15*1024*1024
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 * Local data types
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 typedef struct {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 const DilloUrl *Url; /* Cached Url. Url is used as a primary Key */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 char *TypeDet; /* MIME type string (detected from data) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 char *TypeHdr; /* MIME type string as from the HTTP Header */
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
52 char *TypeMeta; /* MIME type string from META HTTP-EQUIV */
949
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
53 char *TypeNorm; /* MIME type string normalized */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 Dstr *Header; /* HTTP header */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 const DilloUrl *Location; /* New URI for redirects */
470
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
56 Dlist *Auth; /* Authentication fields */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 Dstr *Data; /* Pointer to raw data */
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
58 Dstr *UTF8Data; /* Data after charset translation */
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
59 int DataRefcount; /* Reference count */
63
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
60 Decode *TransferDecoder; /* Transfer decoder (e.g., chunked) */
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
61 Decode *ContentDecoder; /* Data decoder (e.g., gzip) */
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
62 Decode *CharsetDecoder; /* Translates text to UTF-8 encoding */
47
1b7243d62913 Polishing the gzip patch.
jcid
parents: 43
diff changeset
63 int ExpectedSize; /* Goal size of the HTTP transfer (0 if unknown)*/
1b7243d62913 Polishing the gzip patch.
jcid
parents: 43
diff changeset
64 int TransferSize; /* Actual length of the HTTP transfer */
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
65 uint_t Flags; /* See Flag Defines in cache.h */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66 } CacheEntry_t;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 * Local data
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 /* A sorted list for cached data. Holds pointers to CacheEntry_t structs */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 static Dlist *CachedURLs;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 /* A list for cache clients.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 * Although implemented as a list, we'll call it ClientQueue --Jcid */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 static Dlist *ClientQueue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 /* A list for delayed clients (it holds weak pointers to cache entries,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 * which are used to make deferred calls to Cache_process_queue) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 static Dlist *DelayedQueue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 static uint_t DelayedQueueIdleId = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 * Forward declarations
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 */
1251
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
88 static CacheEntry_t *Cache_process_queue(CacheEntry_t *entry);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 static void Cache_delayed_process_queue(CacheEntry_t *entry);
470
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
90 static void Cache_auth_entry(CacheEntry_t *entry, BrowserWindow *bw);
1427
92d1c9c5f462 functions needn't be publicly visible
corvid <corvid@lavabit.com>
parents: 1418
diff changeset
91 static void Cache_entry_inject(const DilloUrl *Url, Dstr *data_ds);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 * Determine if two cache entries are equal (used by CachedURLs)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96 static int Cache_entry_cmp(const void *v1, const void *v2)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 const CacheEntry_t *d1 = v1, *d2 = v2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 return a_Url_cmp(d1->Url, d2->Url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 * Determine if two cache entries are equal, using a URL as key.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 static int Cache_entry_by_url_cmp(const void *v1, const void *v2)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 const DilloUrl *u1 = ((CacheEntry_t*)v1)->Url;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 const DilloUrl *u2 = v2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 return a_Url_cmp(u1, u2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 * Initialize dicache data
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 void a_Cache_init(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 ClientQueue = dList_new(32);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120 DelayedQueue = dList_new(32);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 CachedURLs = dList_new(256);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 /* inject the splash screen in the cache */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 {
334
619177c88430 - Forbid dpi GET and POST from non dpi-generated urls.
jcid
parents: 332
diff changeset
125 DilloUrl *url = a_Url_new("about:splash", NULL);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 Dstr *ds = dStr_new(AboutSplash);
1427
92d1c9c5f462 functions needn't be publicly visible
corvid <corvid@lavabit.com>
parents: 1418
diff changeset
127 Cache_entry_inject(url, ds);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 dStr_free(ds, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 a_Url_free(url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 /* Client operations ------------------------------------------------------ */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136 * Add a client to ClientQueue.
50
22a3dedcb1a0 - s/camp/field/ s/CAMP/FIELD/
jcid
parents: 47
diff changeset
137 * - Every client-field is just a reference (except 'Web').
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 * - Return a unique number for identifying the client.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140 static int Cache_client_enqueue(const DilloUrl *Url, DilloWeb *Web,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 CA_Callback_t Callback, void *CbData)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142 {
1081
5037653f64a4 Inlined Cache_client_make_key()
Jeremy Henty <onepoint@starurchin.org>
parents: 1077
diff changeset
143 static int ClientKey = 0; /* Provide a primary key for each client */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 CacheClient_t *NewClient;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145
1081
5037653f64a4 Inlined Cache_client_make_key()
Jeremy Henty <onepoint@starurchin.org>
parents: 1077
diff changeset
146 if (++ClientKey <= 0)
5037653f64a4 Inlined Cache_client_make_key()
Jeremy Henty <onepoint@starurchin.org>
parents: 1077
diff changeset
147 ClientKey = 1;
5037653f64a4 Inlined Cache_client_make_key()
Jeremy Henty <onepoint@starurchin.org>
parents: 1077
diff changeset
148
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 NewClient = dNew(CacheClient_t, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 NewClient->Key = ClientKey;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 NewClient->Url = Url;
710
43e46533be39 imported patch dicache-cleanup5
Jorge Arellano Cid <jcid@dillo.org>
parents: 470
diff changeset
152 NewClient->Version = 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153 NewClient->Buf = NULL;
1295
a8db028b337f Added a missing initialization value (NewClient->BufSize)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1284
diff changeset
154 NewClient->BufSize = 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 NewClient->Callback = Callback;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 NewClient->CbData = CbData;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157 NewClient->Web = Web;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 dList_append(ClientQueue, NewClient);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161 return ClientKey;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165 * Compare function for searching a Client by its key
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
166 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
167 static int Cache_client_by_key_cmp(const void *client, const void *key)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
168 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
169 return ((CacheClient_t *)client)->Key - VOIDP2INT(key);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
170 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
171
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
172 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
173 * Remove a client from the queue
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
174 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
175 static void Cache_client_dequeue(CacheClient_t *Client, int Key)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
176 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
177 if (!Client) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
178 Client = dList_find_custom(ClientQueue, INT2VOIDP(Key),
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
179 Cache_client_by_key_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
180 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
181 if (Client) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
182 dList_remove(ClientQueue, Client);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
183 a_Web_free(Client->Web);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
184 dFree(Client);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
185 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
186 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
187
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
189 /* Entry operations ------------------------------------------------------- */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
190
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
191 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
192 * Set safe values for a new cache entry
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
193 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
194 static void Cache_entry_init(CacheEntry_t *NewEntry, const DilloUrl *Url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
195 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
196 NewEntry->Url = a_Url_dup(Url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
197 NewEntry->TypeDet = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
198 NewEntry->TypeHdr = NULL;
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
199 NewEntry->TypeMeta = NULL;
949
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
200 NewEntry->TypeNorm = NULL;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
201 NewEntry->Header = dStr_new("");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
202 NewEntry->Location = NULL;
470
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
203 NewEntry->Auth = NULL;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
204 NewEntry->Data = dStr_sized_new(8*1024);
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
205 NewEntry->UTF8Data = NULL;
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
206 NewEntry->DataRefcount = 0;
63
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
207 NewEntry->TransferDecoder = NULL;
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
208 NewEntry->ContentDecoder = NULL;
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
209 NewEntry->CharsetDecoder = NULL;
47
1b7243d62913 Polishing the gzip patch.
jcid
parents: 43
diff changeset
210 NewEntry->ExpectedSize = 0;
1b7243d62913 Polishing the gzip patch.
jcid
parents: 43
diff changeset
211 NewEntry->TransferSize = 0;
156
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
212 NewEntry->Flags = CA_IsEmpty;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
213 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
214
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
215 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
216 * Get the data structure for a cached URL (using 'Url' as the search key)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
217 * If 'Url' isn't cached, return NULL
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
218 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
219 static CacheEntry_t *Cache_entry_search(const DilloUrl *Url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
220 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
221 return dList_find_sorted(CachedURLs, Url, Cache_entry_by_url_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
222 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
223
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
224 /*
156
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
225 * Given a URL, find its cache entry, following redirections.
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
226 */
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
227 static CacheEntry_t *Cache_entry_search_with_redirect(const DilloUrl *Url)
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
228 {
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
229 int i;
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
230 CacheEntry_t *entry;
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
231
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
232 for (i = 0; (entry = Cache_entry_search(Url)); ++i) {
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
233
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
234 /* Test for a redirection loop */
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
235 if (entry->Flags & CA_RedirectLoop || i == 3) {
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
236 _MSG_WARN("Redirect loop for URL: >%s<\n", URL_STR_(Url));
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
237 break;
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
238 }
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
239 /* Test for a working redirection */
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
240 if (entry && entry->Flags & CA_Redirect && entry->Location) {
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
241 Url = entry->Location;
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
242 } else
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
243 break;
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
244 }
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
245 return entry;
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
246 }
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
247
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
248 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
249 * Allocate and set a new entry in the cache list
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
250 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
251 static CacheEntry_t *Cache_entry_add(const DilloUrl *Url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
252 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
253 CacheEntry_t *old_entry, *new_entry;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
254
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
255 if ((old_entry = Cache_entry_search(Url))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
256 MSG_WARN("Cache_entry_add, leaking an entry.\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
257 dList_remove(CachedURLs, old_entry);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
258 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
259
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
260 new_entry = dNew(CacheEntry_t, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
261 Cache_entry_init(new_entry, Url); /* Set safe values */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
262 dList_insert_sorted(CachedURLs, new_entry, Cache_entry_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
263 return new_entry;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
264 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
265
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
266 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
267 * Inject full page content directly into the cache.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
268 * Used for "about:splash". May be used for "about:cache" too.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
269 */
1427
92d1c9c5f462 functions needn't be publicly visible
corvid <corvid@lavabit.com>
parents: 1418
diff changeset
270 static void Cache_entry_inject(const DilloUrl *Url, Dstr *data_ds)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
271 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
272 CacheEntry_t *entry;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
273
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
274 if (!(entry = Cache_entry_search(Url)))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
275 entry = Cache_entry_add(Url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
276 entry->Flags |= CA_GotData + CA_GotHeader + CA_GotLength + CA_InternalUrl;
156
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
277 if (data_ds->len)
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
278 entry->Flags &= ~CA_IsEmpty;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
279 dStr_truncate(entry->Data, 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
280 dStr_append_l(entry->Data, data_ds->str, data_ds->len);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
281 dStr_fit(entry->Data);
47
1b7243d62913 Polishing the gzip patch.
jcid
parents: 43
diff changeset
282 entry->ExpectedSize = entry->TransferSize = entry->Data->len;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
283 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
284
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
285 /*
470
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
286 * Free Authentication fields.
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
287 */
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
288 static void Cache_auth_free(Dlist *auth)
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
289 {
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
290 int i;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
291 void *auth_field;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
292 for (i = 0; (auth_field = dList_nth_data(auth, i)); ++i)
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
293 dFree(auth_field);
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
294 dList_free(auth);
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
295 }
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
296
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
297 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
298 * Free the components of a CacheEntry_t struct.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
299 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
300 static void Cache_entry_free(CacheEntry_t *entry)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
301 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
302 a_Url_free((DilloUrl *)entry->Url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
303 dFree(entry->TypeDet);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
304 dFree(entry->TypeHdr);
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
305 dFree(entry->TypeMeta);
949
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
306 dFree(entry->TypeNorm);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
307 dStr_free(entry->Header, TRUE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
308 a_Url_free((DilloUrl *)entry->Location);
470
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
309 Cache_auth_free(entry->Auth);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
310 dStr_free(entry->Data, 1);
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
311 dStr_free(entry->UTF8Data, 1);
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
312 if (entry->CharsetDecoder)
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
313 a_Decode_free(entry->CharsetDecoder);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
314 dFree(entry);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
315 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
316
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
317 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
318 * Remove an entry, from the cache.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
319 * All the entry clients are removed too! (it may stop rendering of this
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
320 * same resource on other windows, but nothing more).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
321 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 50
diff changeset
322 static void Cache_entry_remove(CacheEntry_t *entry, DilloUrl *url)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
323 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
324 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
325 CacheClient_t *Client;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
326
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
327 if (!entry && !(entry = Cache_entry_search(url)))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
328 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
329 if (entry->Flags & CA_InternalUrl)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
330 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
331
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
332 /* remove all clients for this entry */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
333 for (i = 0; (Client = dList_nth_data(ClientQueue, i)); ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
334 if (Client->Url == entry->Url) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
335 a_Cache_stop_client(Client->Key);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
336 --i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
337 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
338 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
339
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
340 /* remove from DelayedQueue */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
341 dList_remove(DelayedQueue, entry);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
342
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
343 /* remove from dicache */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
344 a_Dicache_invalidate_entry(entry->Url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
345
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
346 /* remove from cache */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
347 dList_remove(CachedURLs, entry);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
348 Cache_entry_free(entry);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
349 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
350
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
351 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
352 * Wrapper for capi.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
353 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
354 void a_Cache_entry_remove_by_url(DilloUrl *url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
355 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
356 Cache_entry_remove(NULL, url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
357 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
358
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
359 /* Misc. operations ------------------------------------------------------- */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
360
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
361 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
362 * Try finding the url in the cache. If it hits, send the cache contents
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
363 * from there. If it misses, set up a new connection.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
364 *
1069
315b76a89789 Comment fix for a_Cache_open_url()
Jeremy Henty <onepoint@starurchin.org>
parents: 1068
diff changeset
365 * - 'Web' is an auxiliary data structure with misc. parameters.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
366 * - 'Call' is the callback that receives the data
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
367 * - 'CbData' is custom data passed to 'Call'
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
368 * Note: 'Call' and/or 'CbData' can be NULL, in that case they get set
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
369 * later by a_Web_dispatch_by_type, based on content/type and 'Web' data.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
370 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
371 * Return value: A primary key for identifying the client,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
372 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
373 int a_Cache_open_url(void *web, CA_Callback_t Call, void *CbData)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
374 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
375 int ClientKey;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
376 CacheEntry_t *entry;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
377 DilloWeb *Web = web;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
378 DilloUrl *Url = Web->url;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
379
210
2f05ce84df01 + Fixed a bug in the code that remembers the scrolling position.
jcid
parents: 178
diff changeset
380 if (URL_FLAGS(Url) & URL_E2EQuery) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
381 /* remove current entry */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
382 Cache_entry_remove(NULL, Url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
383 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
384
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
385 if ((entry = Cache_entry_search(Url))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
386 /* URL is cached: feed our client with cached data */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
387 ClientKey = Cache_client_enqueue(entry->Url, Web, Call, CbData);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
388 Cache_delayed_process_queue(entry);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
389
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
390 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
391 /* URL not cached: create an entry, send our client to the queue,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
392 * and open a new connection */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
393 entry = Cache_entry_add(Url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
394 ClientKey = Cache_client_enqueue(entry->Url, Web, Call, CbData);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
395 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
396
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
397 return ClientKey;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
398 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
399
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
400 /*
156
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
401 * Get cache entry status
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
402 */
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
403 uint_t a_Cache_get_flags(const DilloUrl *url)
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
404 {
1077
17688e07a8b7 separate a_Capi_get_flags and a_Capi_get_flags_with_redirection
corvid, Jorge Arellano Cid
parents: 1069
diff changeset
405 CacheEntry_t *entry = Cache_entry_search(url);
17688e07a8b7 separate a_Capi_get_flags and a_Capi_get_flags_with_redirection
corvid, Jorge Arellano Cid
parents: 1069
diff changeset
406 return (entry ? entry->Flags : 0);
17688e07a8b7 separate a_Capi_get_flags and a_Capi_get_flags_with_redirection
corvid, Jorge Arellano Cid
parents: 1069
diff changeset
407 }
17688e07a8b7 separate a_Capi_get_flags and a_Capi_get_flags_with_redirection
corvid, Jorge Arellano Cid
parents: 1069
diff changeset
408
17688e07a8b7 separate a_Capi_get_flags and a_Capi_get_flags_with_redirection
corvid, Jorge Arellano Cid
parents: 1069
diff changeset
409 /*
17688e07a8b7 separate a_Capi_get_flags and a_Capi_get_flags_with_redirection
corvid, Jorge Arellano Cid
parents: 1069
diff changeset
410 * Get cache entry status (following redirections).
17688e07a8b7 separate a_Capi_get_flags and a_Capi_get_flags_with_redirection
corvid, Jorge Arellano Cid
parents: 1069
diff changeset
411 */
17688e07a8b7 separate a_Capi_get_flags and a_Capi_get_flags_with_redirection
corvid, Jorge Arellano Cid
parents: 1069
diff changeset
412 uint_t a_Cache_get_flags_with_redirection(const DilloUrl *url)
17688e07a8b7 separate a_Capi_get_flags and a_Capi_get_flags_with_redirection
corvid, Jorge Arellano Cid
parents: 1069
diff changeset
413 {
156
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
414 CacheEntry_t *entry = Cache_entry_search_with_redirect(url);
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
415 return (entry ? entry->Flags : 0);
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
416 }
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
417
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
418 /*
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
419 * Reference the cache data.
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
420 */
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
421 static void Cache_ref_data(CacheEntry_t *entry)
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
422 {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
423 if (entry) {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
424 entry->DataRefcount++;
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
425 _MSG("DataRefcount++: %d\n", entry->DataRefcount);
897
cdb32eeb8322 Fix a race condition with charset, and the case of charset after CSS in HEAD
Jorge Arellano Cid <jcid@dillo.org>
parents: 889
diff changeset
426 if (entry->CharsetDecoder &&
cdb32eeb8322 Fix a race condition with charset, and the case of charset after CSS in HEAD
Jorge Arellano Cid <jcid@dillo.org>
parents: 889
diff changeset
427 (!entry->UTF8Data || entry->DataRefcount == 1)) {
736
48029b8a5478 Free UTF8Data before changing its value.
Jorge Arellano Cid <jcid@dillo.org>
parents: 710
diff changeset
428 dStr_free(entry->UTF8Data, 1);
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
429 entry->UTF8Data = a_Decode_process(entry->CharsetDecoder,
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
430 entry->Data->str,
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
431 entry->Data->len);
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
432 }
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
433 }
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
434 }
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
435
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
436 /*
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
437 * Unreference the cache data.
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
438 */
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
439 static void Cache_unref_data(CacheEntry_t *entry)
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
440 {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
441 if (entry) {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
442 entry->DataRefcount--;
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
443 _MSG("DataRefcount--: %d\n", entry->DataRefcount);
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
444
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
445 if (entry->CharsetDecoder) {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
446 if (entry->DataRefcount == 0) {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
447 dStr_free(entry->UTF8Data, 1);
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
448 entry->UTF8Data = NULL;
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
449 } else if (entry->DataRefcount < 0) {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
450 MSG_ERR("Cache_unref_data: negative refcount\n");
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
451 entry->DataRefcount = 0;
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
452 }
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
453 }
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
454 }
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
455 }
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
456
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
457 /*
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
458 * Get current content type.
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
459 */
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
460 static const char *Cache_current_content_type(CacheEntry_t *entry)
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
461 {
949
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
462 return entry->TypeNorm ? entry->TypeNorm : entry->TypeMeta ? entry->TypeMeta
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
463 : entry->TypeHdr ? entry->TypeHdr : entry->TypeDet;
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
464 }
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
465
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
466 /*
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
467 * Get current Content-Type for cache entry found by URL.
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
468 */
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
469 const char *a_Cache_get_content_type(const DilloUrl *url)
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
470 {
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
471 CacheEntry_t *entry = Cache_entry_search_with_redirect(url);
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
472
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
473 return (entry) ? Cache_current_content_type(entry) : NULL;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
474 }
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
475
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
476 /*
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
477 * Get pointer to entry's data.
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
478 */
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
479 static Dstr *Cache_data(CacheEntry_t *entry)
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
480 {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
481 return entry->UTF8Data ? entry->UTF8Data : entry->Data;
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
482 }
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
483
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
484 /*
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
485 * Change Content-Type for cache entry found by url.
814
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
486 * from = { "http" | "meta" }
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
487 * Return new content type.
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
488 */
814
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
489 const char *a_Cache_set_content_type(const DilloUrl *url, const char *ctype,
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
490 const char *from)
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
491 {
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
492 const char *curr;
949
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
493 char *major, *minor, *charset;
1366
a22491563134 don't follow redirection when setting Content-Type
corvid <corvid@lavabit.com>
parents: 1365
diff changeset
494 CacheEntry_t *entry = Cache_entry_search(url);
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
495
740
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
496 dReturn_val_if_fail (entry != NULL, NULL);
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
497
808
fd4d0a17c1a2 Fixed a memory leak in gzip decoder.
Jorge Arellano Cid <jcid@dillo.org>
parents: 802
diff changeset
498 _MSG("a_Cache_set_content_type {%s} {%s}\n", ctype, URL_STR(url));
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
499
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
500 curr = Cache_current_content_type(entry);
814
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
501 if (entry->TypeMeta || (*from == 'h' && entry->TypeHdr) ) {
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
502 /* Type is already been set. Do nothing.
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
503 * BTW, META overrides TypeHdr */
802
d29cdb5b842e Fixed charset decoding given in HTTP and META. (bugs and race condition)
Jorge Arellano Cid <jcid@dillo.org>
parents: 768
diff changeset
504 } else {
814
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
505 if (*from == 'h') {
802
d29cdb5b842e Fixed charset decoding given in HTTP and META. (bugs and race condition)
Jorge Arellano Cid <jcid@dillo.org>
parents: 768
diff changeset
506 /* Content-Type from HTTP header */
d29cdb5b842e Fixed charset decoding given in HTTP and META. (bugs and race condition)
Jorge Arellano Cid <jcid@dillo.org>
parents: 768
diff changeset
507 entry->TypeHdr = dStrdup(ctype);
d29cdb5b842e Fixed charset decoding given in HTTP and META. (bugs and race condition)
Jorge Arellano Cid <jcid@dillo.org>
parents: 768
diff changeset
508 } else {
d29cdb5b842e Fixed charset decoding given in HTTP and META. (bugs and race condition)
Jorge Arellano Cid <jcid@dillo.org>
parents: 768
diff changeset
509 /* Content-Type from META */
d29cdb5b842e Fixed charset decoding given in HTTP and META. (bugs and race condition)
Jorge Arellano Cid <jcid@dillo.org>
parents: 768
diff changeset
510 entry->TypeMeta = dStrdup(ctype);
d29cdb5b842e Fixed charset decoding given in HTTP and META. (bugs and race condition)
Jorge Arellano Cid <jcid@dillo.org>
parents: 768
diff changeset
511 }
d29cdb5b842e Fixed charset decoding given in HTTP and META. (bugs and race condition)
Jorge Arellano Cid <jcid@dillo.org>
parents: 768
diff changeset
512 if (a_Misc_content_type_cmp(curr, ctype)) {
d29cdb5b842e Fixed charset decoding given in HTTP and META. (bugs and race condition)
Jorge Arellano Cid <jcid@dillo.org>
parents: 768
diff changeset
513 /* ctype gives one different from current */
949
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
514 a_Misc_parse_content_type(ctype, &major, &minor, &charset);
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
515 if (*from == 'm' && charset &&
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
516 ((!major || !*major) && (!minor || !*minor))) {
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
517 /* META only gives charset; use detected MIME type too */
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
518 entry->TypeNorm = dStrconcat(entry->TypeDet, ctype, NULL);
1770
4779a3d891a1 trim some spaces
corvid <corvid@lavabit.com>
parents: 1707
diff changeset
519 } else if (*from == 'm' &&
1707
e44231f9c6ca Allow viewing XHTML served as such
Jorge Arellano Cid <jcid@dillo.org>
parents: 1700
diff changeset
520 !dStrncasecmp(ctype, "text/xhtml", 10)) {
e44231f9c6ca Allow viewing XHTML served as such
Jorge Arellano Cid <jcid@dillo.org>
parents: 1700
diff changeset
521 /* WORKAROUND: doxygen uses "text/xhtml" in META */
e44231f9c6ca Allow viewing XHTML served as such
Jorge Arellano Cid <jcid@dillo.org>
parents: 1700
diff changeset
522 entry->TypeNorm = dStrdup(entry->TypeDet);
949
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
523 }
814
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
524 if (charset) {
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
525 if (entry->CharsetDecoder)
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
526 a_Decode_free(entry->CharsetDecoder);
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
527 entry->CharsetDecoder = a_Decode_charset_init(charset);
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
528 curr = Cache_current_content_type(entry);
897
cdb32eeb8322 Fix a race condition with charset, and the case of charset after CSS in HEAD
Jorge Arellano Cid <jcid@dillo.org>
parents: 889
diff changeset
529
814
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
530 /* Invalidate UTF8Data */
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
531 dStr_free(entry->UTF8Data, 1);
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
532 entry->UTF8Data = NULL;
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
533 }
957
bd699ba9a469 Fixed a memory leak introduced by 691053e40394
Jorge Arellano Cid <jcid@dillo.org>
parents: 949
diff changeset
534 dFree(major); dFree(minor); dFree(charset);
802
d29cdb5b842e Fixed charset decoding given in HTTP and META. (bugs and race condition)
Jorge Arellano Cid <jcid@dillo.org>
parents: 768
diff changeset
535 }
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
536 }
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
537 return curr;
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
538 }
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
539
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 248
diff changeset
540 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
541 * Get the pointer to the URL document, and its size, from the cache entry.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
542 * Return: 1 cached, 0 not cached.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
543 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
544 int a_Cache_get_buf(const DilloUrl *Url, char **PBuf, int *BufSize)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
545 {
156
14f50d7607ed - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
jcid
parents: 155
diff changeset
546 CacheEntry_t *entry = Cache_entry_search_with_redirect(Url);
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
547 if (entry) {
430
b296fd9d8752 - Allowed compilation with older machines by removing a few C99isms.
jcid
parents: 429
diff changeset
548 Dstr *data;
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
549 Cache_ref_data(entry);
430
b296fd9d8752 - Allowed compilation with older machines by removing a few C99isms.
jcid
parents: 429
diff changeset
550 data = Cache_data(entry);
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
551 *PBuf = data->str;
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
552 *BufSize = data->len;
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
553 } else {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
554 *PBuf = NULL;
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
555 *BufSize = 0;
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
556 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
557 return (entry ? 1 : 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
558 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
559
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
560 /*
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
561 * Unreference the data buffer when no longer using it.
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
562 */
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
563 void a_Cache_unref_buf(const DilloUrl *Url)
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
564 {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
565 Cache_unref_data(Cache_entry_search_with_redirect(Url));
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
566 }
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
567
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
568
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
569 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
570 * Extract a single field from the header, allocating and storing the value
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
571 * in 'field'. ('fieldname' must not include the trailing ':')
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
572 * Return a new string with the field-content if found (NULL on error)
101
31607a8aa31a - Added support for multi-line header fields.
jcid
parents: 99
diff changeset
573 * (This function expects a '\r'-stripped header, with one-line header fields)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
574 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
575 static char *Cache_parse_field(const char *header, const char *fieldname)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
576 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
577 char *field;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
578 uint_t i, j;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
579
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
580 for (i = 0; header[i]; i++) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
581 /* Search fieldname */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
582 for (j = 0; fieldname[j]; j++)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
583 if (tolower(fieldname[j]) != tolower(header[i + j]))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
584 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
585 if (fieldname[j]) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
586 /* skip to next line */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
587 for ( i += j; header[i] != '\n'; i++);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
588 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
589 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
590
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
591 i += j;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
592 if (header[i] == ':') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
593 /* Field found! */
101
31607a8aa31a - Added support for multi-line header fields.
jcid
parents: 99
diff changeset
594 while (header[++i] == ' ' || header[i] == '\t');
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
595 for (j = 0; header[i + j] != '\n'; j++);
1585
0fc655a36dcb trim trailing whitespace from HTTP header fields
corvid <corvid@lavabit.com>
parents: 1502
diff changeset
596 while (j && (header[i + j - 1] == ' ' || header[i + j - 1] == '\t'))
0fc655a36dcb trim trailing whitespace from HTTP header fields
corvid <corvid@lavabit.com>
parents: 1502
diff changeset
597 j--;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
598 field = dStrndup(header + i, j);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
599 return field;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
600 }
1586
283f88e30d8b more robust http header field parsing
corvid <corvid@lavabit.com>
parents: 1585
diff changeset
601 while (header[i] != '\n') i++;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
602 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
603 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
604 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
605
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
606 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
607 * Extract multiple fields from the header.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
608 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
609 static Dlist *Cache_parse_multiple_fields(const char *header,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
610 const char *fieldname)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
611 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
612 uint_t i, j;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
613 Dlist *fields = dList_new(8);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
614 char *field;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
615
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
616 for (i = 0; header[i]; i++) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
617 /* Search fieldname */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
618 for (j = 0; fieldname[j]; j++)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
619 if (tolower(fieldname[j]) != tolower(header[i + j]))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
620 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
621 if (fieldname[j]) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
622 /* skip to next line */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
623 for (i += j; header[i] != '\n'; i++);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
624 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
625 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
626
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
627 i += j;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
628 if (header[i] == ':') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
629 /* Field found! */
101
31607a8aa31a - Added support for multi-line header fields.
jcid
parents: 99
diff changeset
630 while (header[++i] == ' ' || header[i] == '\t');
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
631 for (j = 0; header[i + j] != '\n'; j++);
1585
0fc655a36dcb trim trailing whitespace from HTTP header fields
corvid <corvid@lavabit.com>
parents: 1502
diff changeset
632 while (j && (header[i + j - 1] == ' ' || header[i + j - 1] == '\t'))
0fc655a36dcb trim trailing whitespace from HTTP header fields
corvid <corvid@lavabit.com>
parents: 1502
diff changeset
633 j--;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
634 field = dStrndup(header + i, j);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
635 dList_append(fields, field);
1586
283f88e30d8b more robust http header field parsing
corvid <corvid@lavabit.com>
parents: 1585
diff changeset
636 } else {
283f88e30d8b more robust http header field parsing
corvid <corvid@lavabit.com>
parents: 1585
diff changeset
637 while (header[i] != '\n') i++;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
638 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
639 }
119
9c5d7d561ba1 - Fixed the return value of Cache_parse_multiple_field.
jcid
parents: 101
diff changeset
640
9c5d7d561ba1 - Fixed the return value of Cache_parse_multiple_field.
jcid
parents: 101
diff changeset
641 if (dList_length(fields) == 0) {
9c5d7d561ba1 - Fixed the return value of Cache_parse_multiple_field.
jcid
parents: 101
diff changeset
642 dList_free(fields);
9c5d7d561ba1 - Fixed the return value of Cache_parse_multiple_field.
jcid
parents: 101
diff changeset
643 fields = NULL;
9c5d7d561ba1 - Fixed the return value of Cache_parse_multiple_field.
jcid
parents: 101
diff changeset
644 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
645 return fields;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
646 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
647
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
648 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
649 * Scan, allocate, and set things according to header info.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
650 * (This function needs the whole header to work)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
651 */
65
712cc962366f Made a cleanup in cache's parse header code.
jcid
parents: 64
diff changeset
652 static void Cache_parse_header(CacheEntry_t *entry)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
653 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
654 char *header = entry->Header->str;
740
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
655 char *Length, *Type, *location_str, *encoding;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
656 #ifndef DISABLE_COOKIES
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
657 Dlist *Cookies;
99
aef0d0b7a1cf - Added a MSG_HTTP for HTTP/1.1's warning headers.
jcid
parents: 84
diff changeset
658 #endif
aef0d0b7a1cf - Added a MSG_HTTP for HTTP/1.1's warning headers.
jcid
parents: 84
diff changeset
659 Dlist *warnings;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
660 void *data;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
661 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
662
768
eeb44d8bf3a0 Make now-unused debug messages silent.
Jorge Arellano Cid <jcid@dillo.org>
parents: 740
diff changeset
663 _MSG("Cache_parse_header\n");
740
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
664
65
712cc962366f Made a cleanup in cache's parse header code.
jcid
parents: 64
diff changeset
665 if (entry->Header->len > 12) {
63
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
666 if (header[9] == '1' && header[10] == '0' && header[11] == '0') {
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
667 /* 100: Continue. The "real" header has not come yet. */
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
668 MSG("An actual 100 Continue header!\n");
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
669 entry->Flags &= ~CA_GotHeader;
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
670 dStr_free(entry->Header, 1);
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
671 entry->Header = dStr_new("");
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
672 return;
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
673 }
57
jcid
parents: 51
diff changeset
674 if (header[9] == '3' && header[10] == '0') {
jcid
parents: 51
diff changeset
675 /* 30x: URL redirection */
889
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
676 if ((location_str = Cache_parse_field(header, "Location"))) {
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
677 DilloUrl *location_url;
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
678
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
679 entry->Flags |= CA_Redirect;
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
680 if (header[11] == '1')
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
681 entry->Flags |= CA_ForceRedirect; /* 301 Moved Permanently */
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
682 else if (header[11] == '2')
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
683 entry->Flags |= CA_TempRedirect; /* 302 Temporary Redirect */
930
b277eed3119c whitespace cleanup: 's/ +$//g'
Jorge Arellano Cid <jcid@dillo.org>
parents: 897
diff changeset
684
889
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
685 location_url = a_Url_new(location_str, URL_STR_(entry->Url));
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
686 if (URL_FLAGS(location_url) & (URL_Post + URL_Get) &&
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
687 dStrcasecmp(URL_SCHEME(location_url), "dpi") == 0 &&
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
688 dStrcasecmp(URL_SCHEME(entry->Url), "dpi") != 0) {
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
689 /* Forbid dpi GET and POST from non dpi-generated urls */
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
690 MSG("Redirection Denied! '%s' -> '%s'\n",
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
691 URL_STR(entry->Url), URL_STR(location_url));
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
692 a_Url_free(location_url);
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
693 } else {
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
694 entry->Location = location_url;
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
695 }
bb1589dbe792 Fixed a SEGFAULT bug on redirections without Location
corvid <corvid@lavabit.com>
parents: 881
diff changeset
696 dFree(location_str);
334
619177c88430 - Forbid dpi GET and POST from non dpi-generated urls.
jcid
parents: 332
diff changeset
697 }
470
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
698 } else if (strncmp(header + 9, "401", 3) == 0) {
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
699 entry->Auth =
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
700 Cache_parse_multiple_fields(header, "WWW-Authenticate");
57
jcid
parents: 51
diff changeset
701 } else if (strncmp(header + 9, "404", 3) == 0) {
jcid
parents: 51
diff changeset
702 entry->Flags |= CA_NotFound;
jcid
parents: 51
diff changeset
703 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
704 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
705
99
aef0d0b7a1cf - Added a MSG_HTTP for HTTP/1.1's warning headers.
jcid
parents: 84
diff changeset
706 if ((warnings = Cache_parse_multiple_fields(header, "Warning"))) {
aef0d0b7a1cf - Added a MSG_HTTP for HTTP/1.1's warning headers.
jcid
parents: 84
diff changeset
707 for (i = 0; (data = dList_nth_data(warnings, i)); ++i) {
aef0d0b7a1cf - Added a MSG_HTTP for HTTP/1.1's warning headers.
jcid
parents: 84
diff changeset
708 MSG_HTTP("%s\n", (char *)data);
aef0d0b7a1cf - Added a MSG_HTTP for HTTP/1.1's warning headers.
jcid
parents: 84
diff changeset
709 dFree(data);
aef0d0b7a1cf - Added a MSG_HTTP for HTTP/1.1's warning headers.
jcid
parents: 84
diff changeset
710 }
aef0d0b7a1cf - Added a MSG_HTTP for HTTP/1.1's warning headers.
jcid
parents: 84
diff changeset
711 dList_free(warnings);
aef0d0b7a1cf - Added a MSG_HTTP for HTTP/1.1's warning headers.
jcid
parents: 84
diff changeset
712 }
aef0d0b7a1cf - Added a MSG_HTTP for HTTP/1.1's warning headers.
jcid
parents: 84
diff changeset
713
1365
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
714 /*
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
715 * Get Transfer-Encoding and initialize decoder
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
716 */
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
717 encoding = Cache_parse_field(header, "Transfer-Encoding");
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
718 entry->TransferDecoder = a_Decode_transfer_init(encoding);
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
719
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
720
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
721 if ((Length = Cache_parse_field(header, "Content-Length")) != NULL) {
1365
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
722 if (encoding) {
63
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
723 /*
1365
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
724 * If Transfer-Encoding is present, Content-Length must be ignored.
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
725 * If the Transfer-Encoding is non-identity, it is an error.
63
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
726 */
1365
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
727 if (dStrcasecmp(encoding, "identity"))
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
728 MSG_HTTP("Content-Length and non-identity Transfer-Encoding "
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
729 "headers both present.\n");
63
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
730 } else {
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
731 entry->Flags |= CA_GotLength;
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
732 entry->ExpectedSize = MAX(strtol(Length, NULL, 10), 0);
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
733 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
734 dFree(Length);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
735 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
736
1365
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
737 dFree(encoding); /* free Transfer-Encoding */
ed22cc4c630e only complain about Content-Length for non-identity Transfer-Encoding
corvid <corvid@lavabit.com>
parents: 1363
diff changeset
738
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
739 #ifndef DISABLE_COOKIES
1502
0064b6fc4566 don't look for Set-Cookie2 header
corvid <corvid@lavabit.com>
parents: 1501
diff changeset
740 if ((Cookies = Cache_parse_multiple_fields(header, "Set-Cookie"))) {
1501
6fd1c55d8022 cookies use server date to interpret Expires attr
corvid <corvid@lavabit.com>
parents: 1427
diff changeset
741 char *server_date = Cache_parse_field(header, "Date");
6fd1c55d8022 cookies use server date to interpret Expires attr
corvid <corvid@lavabit.com>
parents: 1427
diff changeset
742
6fd1c55d8022 cookies use server date to interpret Expires attr
corvid <corvid@lavabit.com>
parents: 1427
diff changeset
743 a_Cookies_set(Cookies, entry->Url, server_date);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
744 for (i = 0; (data = dList_nth_data(Cookies, i)); ++i)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
745 dFree(data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
746 dList_free(Cookies);
1501
6fd1c55d8022 cookies use server date to interpret Expires attr
corvid <corvid@lavabit.com>
parents: 1427
diff changeset
747 dFree(server_date);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
748 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
749 #endif /* !DISABLE_COOKIES */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
750
43
3d485d0e4a6a Added an experimental gzip decoder!
jcid
parents: 35
diff changeset
751 /*
3d485d0e4a6a Added an experimental gzip decoder!
jcid
parents: 35
diff changeset
752 * Get Content-Encoding and initialize decoder
3d485d0e4a6a Added an experimental gzip decoder!
jcid
parents: 35
diff changeset
753 */
3d485d0e4a6a Added an experimental gzip decoder!
jcid
parents: 35
diff changeset
754 encoding = Cache_parse_field(header, "Content-Encoding");
63
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
755 entry->ContentDecoder = a_Decode_content_init(encoding);
43
3d485d0e4a6a Added an experimental gzip decoder!
jcid
parents: 35
diff changeset
756 dFree(encoding);
3d485d0e4a6a Added an experimental gzip decoder!
jcid
parents: 35
diff changeset
757
47
1b7243d62913 Polishing the gzip patch.
jcid
parents: 43
diff changeset
758 if (entry->ExpectedSize > 0) {
1b7243d62913 Polishing the gzip patch.
jcid
parents: 43
diff changeset
759 if (entry->ExpectedSize > HUGE_FILESIZE) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
760 entry->Flags |= CA_HugeFile;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
761 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
762 /* Avoid some reallocs. With MAX_INIT_BUF we avoid a SEGFAULT
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
763 * with huge files (e.g. iso files).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
764 * Note: the buffer grows automatically. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
765 dStr_free(entry->Data, 1);
157
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 156
diff changeset
766 entry->Data = dStr_sized_new(MIN(entry->ExpectedSize, MAX_INIT_BUF));
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
767 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
768
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
769 /* Get Content-Type */
1363
4be25ad9693b only require Content-Type HTTP header if there was, or should have been, a body
corvid <corvid@lavabit.com>
parents: 1316
diff changeset
770 if ((Type = Cache_parse_field(header, "Content-Type"))) {
740
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
771 /* This HTTP Content-Type is not trusted. It's checked against real data
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
772 * in Cache_process_queue(); only then CA_GotContentType becomes true. */
814
3e06388a0bc3 Cleanup and logic change in a_Cache_set_content_type().
Jorge Arellano Cid <jcid@dillo.org>
parents: 812
diff changeset
773 a_Cache_set_content_type(entry->Url, Type, "http");
768
eeb44d8bf3a0 Make now-unused debug messages silent.
Jorge Arellano Cid <jcid@dillo.org>
parents: 740
diff changeset
774 _MSG("TypeHdr {%s} {%s}\n", Type, URL_STR(entry->Url));
eeb44d8bf3a0 Make now-unused debug messages silent.
Jorge Arellano Cid <jcid@dillo.org>
parents: 740
diff changeset
775 _MSG("TypeMeta {%s}\n", entry->TypeMeta);
802
d29cdb5b842e Fixed charset decoding given in HTTP and META. (bugs and race condition)
Jorge Arellano Cid <jcid@dillo.org>
parents: 768
diff changeset
776 dFree(Type);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
777 }
802
d29cdb5b842e Fixed charset decoding given in HTTP and META. (bugs and race condition)
Jorge Arellano Cid <jcid@dillo.org>
parents: 768
diff changeset
778 Cache_ref_data(entry);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
779 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
780
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
781 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
782 * Consume bytes until the whole header is got (up to a "\r\n\r\n" sequence)
101
31607a8aa31a - Added support for multi-line header fields.
jcid
parents: 99
diff changeset
783 * (Also unfold multi-line fields and strip '\r' chars from header)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
784 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
785 static int Cache_get_header(CacheEntry_t *entry,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
786 const char *buf, size_t buf_size)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
787 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
788 size_t N, i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
789 Dstr *hdr = entry->Header;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
790
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
791 /* Header finishes when N = 2 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
792 N = (hdr->len && hdr->str[hdr->len - 1] == '\n');
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
793 for (i = 0; i < buf_size && N < 2; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
794 if (buf[i] == '\r' || !buf[i])
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
795 continue;
101
31607a8aa31a - Added support for multi-line header fields.
jcid
parents: 99
diff changeset
796 if (N == 1 && (buf[i] == ' ' || buf[i] == '\t')) {
31607a8aa31a - Added support for multi-line header fields.
jcid
parents: 99
diff changeset
797 /* unfold multiple-line header */
768
eeb44d8bf3a0 Make now-unused debug messages silent.
Jorge Arellano Cid <jcid@dillo.org>
parents: 740
diff changeset
798 _MSG("Multiple-line header!\n");
101
31607a8aa31a - Added support for multi-line header fields.
jcid
parents: 99
diff changeset
799 dStr_erase(hdr, hdr->len - 1, 1);
31607a8aa31a - Added support for multi-line header fields.
jcid
parents: 99
diff changeset
800 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
801 N = (buf[i] == '\n') ? N + 1 : 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
802 dStr_append_c(hdr, buf[i]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
803 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
804
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
805 if (N == 2) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
806 /* Got whole header */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
807 _MSG("Header [buf_size=%d]\n%s", i, hdr->str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
808 entry->Flags |= CA_GotHeader;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
809 dStr_fit(hdr);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
810 /* Return number of header bytes in 'buf' [1 based] */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
811 return i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
812 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
813 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
814 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
815
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
816 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
817 * Receive new data, update the reception buffer (for next read), update the
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
818 * cache, and service the client queue.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
819 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
820 * This function gets called whenever the IO has new data.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
821 * 'Op' is the operation to perform
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
822 * 'VPtr' is a (void) pointer to the IO control structure
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
823 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
824 void a_Cache_process_dbuf(int Op, const char *buf, size_t buf_size,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
825 const DilloUrl *Url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
826 {
740
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
827 int offset, len;
248
e5feb6331e6a - Made an input preserving decoder implementation.
jcid
parents: 242
diff changeset
828 const char *str;
740
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
829 Dstr *dstr1, *dstr2, *dstr3;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
830 CacheEntry_t *entry = Cache_entry_search(Url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
831
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
832 /* Assert a valid entry (not aborted) */
43
3d485d0e4a6a Added an experimental gzip decoder!
jcid
parents: 35
diff changeset
833 dReturn_if_fail (entry != NULL);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
834
768
eeb44d8bf3a0 Make now-unused debug messages silent.
Jorge Arellano Cid <jcid@dillo.org>
parents: 740
diff changeset
835 _MSG("__a_Cache_process_dbuf__\n");
740
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
836
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
837 if (Op == IORead) {
930
b277eed3119c whitespace cleanup: 's/ +$//g'
Jorge Arellano Cid <jcid@dillo.org>
parents: 897
diff changeset
838 /*
740
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
839 * Cache_get_header() will set CA_GotHeader if it has a full header, and
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
840 * Cache_parse_header() will unset it if the header ends being
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
841 * merely an informational response from the server (i.e., 100 Continue)
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
842 */
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
843 for (offset = 0; !(entry->Flags & CA_GotHeader) &&
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
844 (len = Cache_get_header(entry, buf + offset, buf_size - offset));
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
845 Cache_parse_header(entry) ) {
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
846 offset += len;
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
847 }
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
848
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
849 if (entry->Flags & CA_GotHeader) {
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
850 str = buf + offset;
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
851 len = buf_size - offset;
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
852 entry->TransferSize += len;
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
853 dstr1 = dstr2 = dstr3 = NULL;
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
854
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
855 /* Decode arrived data (<= 3 stages) */
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
856 if (entry->TransferDecoder) {
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
857 dstr1 = a_Decode_process(entry->TransferDecoder, str, len);
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
858 str = dstr1->str;
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
859 len = dstr1->len;
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
860 }
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
861 if (entry->ContentDecoder) {
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
862 dstr2 = a_Decode_process(entry->ContentDecoder, str, len);
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
863 str = dstr2->str;
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
864 len = dstr2->len;
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
865 }
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
866 dStr_append_l(entry->Data, str, len);
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
867 if (entry->CharsetDecoder && entry->UTF8Data) {
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
868 dstr3 = a_Decode_process(entry->CharsetDecoder, str, len);
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
869 dStr_append_l(entry->UTF8Data, dstr3->str, dstr3->len);
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
870 }
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
871 dStr_free(dstr1, 1);
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
872 dStr_free(dstr2, 1);
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
873 dStr_free(dstr3, 1);
930
b277eed3119c whitespace cleanup: 's/ +$//g'
Jorge Arellano Cid <jcid@dillo.org>
parents: 897
diff changeset
874
740
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
875 if (entry->Data->len)
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
876 entry->Flags &= ~CA_IsEmpty;
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
877
1251
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
878 entry = Cache_process_queue(entry);
740
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
879 }
2d9adb33f587 Cleanup of cache.c WRT charset decoders.
Jorge Arellano Cid <jcid@dillo.org>
parents: 736
diff changeset
880 } else if (Op == IOClose) {
1363
4be25ad9693b only require Content-Type HTTP header if there was, or should have been, a body
corvid <corvid@lavabit.com>
parents: 1316
diff changeset
881 if ((entry->ExpectedSize || entry->TransferSize) &&
4be25ad9693b only require Content-Type HTTP header if there was, or should have been, a body
corvid <corvid@lavabit.com>
parents: 1316
diff changeset
882 entry->TypeHdr == NULL) {
4be25ad9693b only require Content-Type HTTP header if there was, or should have been, a body
corvid <corvid@lavabit.com>
parents: 1316
diff changeset
883 MSG_HTTP("Message with a body lacked Content-Type header.\n");
4be25ad9693b only require Content-Type HTTP header if there was, or should have been, a body
corvid <corvid@lavabit.com>
parents: 1316
diff changeset
884 }
47
1b7243d62913 Polishing the gzip patch.
jcid
parents: 43
diff changeset
885 if ((entry->Flags & CA_GotLength) &&
1b7243d62913 Polishing the gzip patch.
jcid
parents: 43
diff changeset
886 (entry->ExpectedSize != entry->TransferSize)) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
887 MSG_HTTP("Content-Length does NOT match message body,\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
888 " at: %s\n", URL_STR_(entry->Url));
47
1b7243d62913 Polishing the gzip patch.
jcid
parents: 43
diff changeset
889 MSG("entry->ExpectedSize = %d, entry->TransferSize = %d\n",
1b7243d62913 Polishing the gzip patch.
jcid
parents: 43
diff changeset
890 entry->ExpectedSize, entry->TransferSize);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
891 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
892 entry->Flags |= CA_GotData;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
893 entry->Flags &= ~CA_Stopped; /* it may catch up! */
63
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
894 if (entry->TransferDecoder) {
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
895 a_Decode_free(entry->TransferDecoder);
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
896 entry->TransferDecoder = NULL;
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
897 }
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
898 if (entry->ContentDecoder) {
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
899 a_Decode_free(entry->ContentDecoder);
878924230e09 Added HTTP-1.1's chunked transfer support!
jcid
parents: 62
diff changeset
900 entry->ContentDecoder = NULL;
43
3d485d0e4a6a Added an experimental gzip decoder!
jcid
parents: 35
diff changeset
901 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
902 dStr_fit(entry->Data); /* fit buffer size! */
1251
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
903
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
904 if ((entry = Cache_process_queue(entry))) {
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
905 if (entry->Flags & CA_GotHeader) {
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
906 Cache_unref_data(entry);
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
907 }
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
908 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
909 } else if (Op == IOAbort) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
910 /* unused */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
911 MSG("a_Cache_process_dbuf Op = IOAbort; not implemented!\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
912 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
913 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
914
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
915 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
916 * Process redirections (HTTP 30x answers)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
917 * (This is a work in progress --not finished yet)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
918 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
919 static int Cache_redirect(CacheEntry_t *entry, int Flags, BrowserWindow *bw)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
920 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
921 DilloUrl *NewUrl;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
922
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
923 _MSG(" Cache_redirect: redirect_level = %d\n", bw->redirect_level);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
924
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
925 /* if there's a redirect loop, stop now */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
926 if (bw->redirect_level >= 5)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
927 entry->Flags |= CA_RedirectLoop;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
928
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
929 if (entry->Flags & CA_RedirectLoop) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
930 a_UIcmd_set_msg(bw, "ERROR: redirect loop for: %s", URL_STR_(entry->Url));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
931 bw->redirect_level = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
932 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
933 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
934
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
935 if ((entry->Flags & CA_Redirect && entry->Location) &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
936 (entry->Flags & CA_ForceRedirect || entry->Flags & CA_TempRedirect ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
937 !entry->Data->len || entry->Data->len < 1024)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
938
1700
e8f48dbdea2a consistent screaming quotes
Jeremy Henty <onepoint@starurchin.org>
parents: 1666
diff changeset
939 _MSG(">>>> Redirect from: %s\n to %s <<<<\n",
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
940 URL_STR_(entry->Url), URL_STR_(entry->Location));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
941 _MSG("%s", entry->Header->str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
942
1666
6beaa7ebc4a8 back out cef38874b78f
corvid <corvid@lavabit.com>
parents: 1651
diff changeset
943 if (Flags & WEB_RootUrl) {
6beaa7ebc4a8 back out cef38874b78f
corvid <corvid@lavabit.com>
parents: 1651
diff changeset
944 /* Redirection of the main page */
334
619177c88430 - Forbid dpi GET and POST from non dpi-generated urls.
jcid
parents: 332
diff changeset
945 NewUrl = a_Url_new(URL_STR_(entry->Location), URL_STR_(entry->Url));
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
946 if (entry->Flags & CA_TempRedirect)
210
2f05ce84df01 + Fixed a bug in the code that remembers the scrolling position.
jcid
parents: 178
diff changeset
947 a_Url_set_flags(NewUrl, URL_FLAGS(NewUrl) | URL_E2EQuery);
1644
2650456b8199 filter automatic requests
corvid <corvid@lavabit.com>
parents: 1621
diff changeset
948 a_Nav_push(bw, NewUrl, entry->Url);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
949 a_Url_free(NewUrl);
1666
6beaa7ebc4a8 back out cef38874b78f
corvid <corvid@lavabit.com>
parents: 1651
diff changeset
950 } else {
6beaa7ebc4a8 back out cef38874b78f
corvid <corvid@lavabit.com>
parents: 1651
diff changeset
951 /* Sub entity redirection (most probably an image) */
6beaa7ebc4a8 back out cef38874b78f
corvid <corvid@lavabit.com>
parents: 1651
diff changeset
952 if (!entry->Data->len) {
1700
e8f48dbdea2a consistent screaming quotes
Jeremy Henty <onepoint@starurchin.org>
parents: 1666
diff changeset
953 _MSG(">>>> Image redirection without entity-content <<<<\n");
1666
6beaa7ebc4a8 back out cef38874b78f
corvid <corvid@lavabit.com>
parents: 1651
diff changeset
954 } else {
1700
e8f48dbdea2a consistent screaming quotes
Jeremy Henty <onepoint@starurchin.org>
parents: 1666
diff changeset
955 _MSG(">>>> Image redirection with entity-content <<<<\n");
1666
6beaa7ebc4a8 back out cef38874b78f
corvid <corvid@lavabit.com>
parents: 1651
diff changeset
956 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
957 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
958 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
959 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
960 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
961
470
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
962 typedef struct {
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
963 Dlist *auth;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
964 DilloUrl *url;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
965 BrowserWindow *bw;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
966 } CacheAuthData_t;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
967
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
968 /*
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
969 * Ask for user/password and reload the page.
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
970 */
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
971 static void Cache_auth_callback(void *vdata)
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
972 {
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
973 CacheAuthData_t *data = (CacheAuthData_t *)vdata;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
974 if (a_Auth_do_auth(data->auth, data->url))
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
975 a_Nav_reload(data->bw);
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
976 Cache_auth_free(data->auth);
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
977 a_Url_free(data->url);
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
978 dFree(data);
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
979 Cache_auth_entry(NULL, NULL);
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
980 a_Timeout_remove();
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
981 }
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
982
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
983 /*
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
984 * Set a timeout function to ask for user/password.
930
b277eed3119c whitespace cleanup: 's/ +$//g'
Jorge Arellano Cid <jcid@dillo.org>
parents: 897
diff changeset
985 */
470
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
986 static void Cache_auth_entry(CacheEntry_t *entry, BrowserWindow *bw)
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
987 {
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
988 static int busy = 0;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
989 CacheAuthData_t *data;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
990
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
991 if (!entry) {
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
992 busy = 0;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
993 } else if (busy) {
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
994 MSG_WARN("Cache_auth_entry: caught busy!\n");
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
995 } else if (entry->Auth) {
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
996 busy = 1;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
997 data = dNew(CacheAuthData_t, 1);
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
998 data->auth = entry->Auth;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
999 data->url = a_Url_dup(entry->Url);
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
1000 data->bw = bw;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
1001 entry->Auth = NULL;
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
1002 a_Timeout_add(0.0, Cache_auth_callback, data);
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
1003 }
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
1004 }
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
1005
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1006 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1007 * Check whether a URL scheme is downloadable.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1008 * Return: 1 enabled, 0 disabled.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1009 */
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1366
diff changeset
1010 int a_Cache_download_enabled(const DilloUrl *url)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1011 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1012 if (!dStrcasecmp(URL_SCHEME(url), "http") ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1013 !dStrcasecmp(URL_SCHEME(url), "https") ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1014 !dStrcasecmp(URL_SCHEME(url), "ftp"))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1015 return 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1016 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1017 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1018
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1019 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1020 * Don't process data any further, but let the cache fill the entry.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1021 * (Currently used to handle WEB_RootUrl redirects,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1022 * and to ignore image redirects --Jcid)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1023 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1024 static void Cache_null_client(int Op, CacheClient_t *Client)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1025 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1026 DilloWeb *Web = Client->Web;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1027
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1028 /* make the stop button insensitive when done */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1029 if (Op == CA_Close) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1030 if (Web->flags & WEB_RootUrl) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1031 /* Remove this client from our active list */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1032 a_Bw_close_client(Web->bw, Client->Key);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1033 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1034 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1035
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1036 /* else ignore */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1037
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1038 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1039 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1040
1316
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1041 typedef struct {
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1042 BrowserWindow *bw;
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1043 DilloUrl *url;
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1044 } Cache_savelink_t;
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1045
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1366
diff changeset
1046 /*
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1366
diff changeset
1047 * Save link from behind a timeout so that Cache_process_queue() can
1316
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1048 * get on with its work.
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1049 */
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1050 static void Cache_savelink_cb(void *vdata)
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1051 {
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1052 Cache_savelink_t *data = (Cache_savelink_t*) vdata;
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1053
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1054 a_UIcmd_save_link(data->bw, data->url);
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1055 a_Url_free(data->url);
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1056 dFree(data);
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1057 }
b7e9339b0d82 use callback when offering download from Cache_process_queue
corvid <corvid@lavabit.com>
parents: 1295
diff changeset
1058
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1059 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1060 * Update cache clients for a single cache-entry
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1061 * Tasks:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1062 * - Set the client function (if not already set)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1063 * - Look if new data is available and pass it to client functions
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1064 * - Remove clients when done
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1065 * - Call redirect handler
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1066 *
1251
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
1067 * Return: Cache entry, which may be NULL if it has been removed.
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
1068 *
368
2242da885677 - s/todo:/TODO:/g
jcid
parents: 360
diff changeset
1069 * TODO: Implement CA_Abort Op in client callback
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1070 */
1251
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
1071 static CacheEntry_t *Cache_process_queue(CacheEntry_t *entry)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1072 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1073 uint_t i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1074 int st;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1075 const char *Type;
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
1076 Dstr *data;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1077 CacheClient_t *Client;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1078 DilloWeb *ClientWeb;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1079 BrowserWindow *Client_bw = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1080 static bool_t Busy = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1081 bool_t AbortEntry = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1082 bool_t OfferDownload = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1083 bool_t TypeMismatch = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1084
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1085 if (Busy)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1086 MSG_ERR("FATAL!: >>>> Cache_process_queue Caught busy!!! <<<<\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1087 if (!(entry->Flags & CA_GotHeader))
1251
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
1088 return entry;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1089 if (!(entry->Flags & CA_GotContentType)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1090 st = a_Misc_get_content_type_from_data(
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1091 entry->Data->str, entry->Data->len, &Type);
255
4c11a0da59e4 - more cleanups in html.cc.
jcid
parents: 250
diff changeset
1092 _MSG("Cache: detected Content-Type '%s'\n", Type);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1093 if (st == 0 || entry->Flags & CA_GotData) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1094 if (a_Misc_content_type_check(entry->TypeHdr, Type) < 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1095 MSG_HTTP("Content-Type '%s' doesn't match the real data.\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1096 entry->TypeHdr);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1097 TypeMismatch = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1098 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1099 entry->TypeDet = dStrdup(Type);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1100 entry->Flags |= CA_GotContentType;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1101 } else
1251
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
1102 return entry; /* i.e., wait for more data */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1103 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1104
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1105 Busy = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1106 for (i = 0; (Client = dList_nth_data(ClientQueue, i)); ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1107 if (Client->Url == entry->Url) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1108 ClientWeb = Client->Web; /* It was a (void*) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1109 Client_bw = ClientWeb->bw; /* 'bw' in a local var */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1110
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1111 if (ClientWeb->flags & WEB_RootUrl) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1112 if (!(entry->Flags & CA_MsgErased)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1113 /* clear the "expecting for reply..." message */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1114 a_UIcmd_set_msg(Client_bw, "");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1115 entry->Flags |= CA_MsgErased;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1116 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1117 if (TypeMismatch) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1118 a_UIcmd_set_msg(Client_bw,"HTTP warning: Content-Type '%s' "
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1119 "doesn't match the real data.", entry->TypeHdr);
429
6d09e72a4dd0 - Enabled OfferDownload for type mismatched root URLs.
jcid
parents: 368
diff changeset
1120 OfferDownload = TRUE;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1121 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1122 if (entry->Flags & CA_Redirect) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1123 if (!Client->Callback) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1124 Client->Callback = Cache_null_client;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1125 Client_bw->redirect_level++;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1126 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1127 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1128 Client_bw->redirect_level = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1129 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1130 if (entry->Flags & CA_HugeFile) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1131 a_UIcmd_set_msg(Client_bw,"Huge file! (%dMB)",
47
1b7243d62913 Polishing the gzip patch.
jcid
parents: 43
diff changeset
1132 entry->ExpectedSize / (1024*1024));
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1133 AbortEntry = OfferDownload = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1134 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1135 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1136 /* For non root URLs, ignore redirections and 404 answers */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1137 if (entry->Flags & CA_Redirect || entry->Flags & CA_NotFound)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1138 Client->Callback = Cache_null_client;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1139 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1140
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1141 /* Set the client function */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1142 if (!Client->Callback) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1143 Client->Callback = Cache_null_client;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1144 if (TypeMismatch) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1145 AbortEntry = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1146 } else {
949
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
1147 const char *curr_type = Cache_current_content_type(entry);
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
1148 st = a_Web_dispatch_by_type(curr_type, ClientWeb,
329
c0f8ecf018e1 - Added a console message for non viewable content.
jcid
parents: 285
diff changeset
1149 &Client->Callback, &Client->CbData);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1150 if (st == -1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1151 /* MIME type is not viewable */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1152 if (ClientWeb->flags & WEB_RootUrl) {
949
691053e40394 Fixed handling of META's content-type with no MIME type (e.g. only charset).
Jorge Arellano Cid <jcid@dillo.org>
parents: 930
diff changeset
1153 MSG("Content-Type '%s' not viewable.\n", curr_type);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1154 /* prepare a download offer... */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1155 AbortEntry = OfferDownload = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1156 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1157 /* TODO: Resource Type not handled.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1158 * Not aborted to avoid multiple connections on the same
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1159 * resource. A better idea is to abort the connection and
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1160 * to keep a failed-resource flag in the cache entry. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1161 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1162 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1163 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1164 if (AbortEntry) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1165 a_Bw_remove_client(Client_bw, Client->Key);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1166 Cache_client_dequeue(Client, NULLKey);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1167 --i; /* Keep the index value in the next iteration */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1168 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1169 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1170 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1171
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1172 /* Send data to our client */
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
1173 if (ClientWeb->flags & WEB_Download) {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
1174 /* for download, always provide original data, not translated */
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
1175 data = entry->Data;
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
1176 } else {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
1177 data = Cache_data(entry);
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
1178 }
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
1179 if ((Client->BufSize = data->len) > 0) {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
1180 Client->Buf = data->str;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1181 (Client->Callback)(CA_Send, Client);
811
693e90600bc1 Changed a wrong a_UIcmd_set_page_prog() parameter
Jorge Arellano Cid <jcid@dillo.org>
parents: 808
diff changeset
1182 if (ClientWeb->flags & WEB_RootUrl) {
693e90600bc1 Changed a wrong a_UIcmd_set_page_prog() parameter
Jorge Arellano Cid <jcid@dillo.org>
parents: 808
diff changeset
1183 /* show size of page received */
693e90600bc1 Changed a wrong a_UIcmd_set_page_prog() parameter
Jorge Arellano Cid <jcid@dillo.org>
parents: 808
diff changeset
1184 a_UIcmd_set_page_prog(Client_bw, entry->Data->len, 1);
693e90600bc1 Changed a wrong a_UIcmd_set_page_prog() parameter
Jorge Arellano Cid <jcid@dillo.org>
parents: 808
diff changeset
1185 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1186 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1187
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1188 /* Remove client when done */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1189 if (entry->Flags & CA_GotData) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1190 /* Copy flags to a local var */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1191 int flags = ClientWeb->flags;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1192 /* We finished sending data, let the client know */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1193 (Client->Callback)(CA_Close, Client);
336
3dcac1f10b96 - Extended Page size meter to include images.
jcid
parents: 334
diff changeset
1194 if (ClientWeb->flags & WEB_RootUrl)
3dcac1f10b96 - Extended Page size meter to include images.
jcid
parents: 334
diff changeset
1195 a_UIcmd_set_page_prog(Client_bw, 0, 0);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1196 Cache_client_dequeue(Client, NULLKey);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1197 --i; /* Keep the index value in the next iteration */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1198
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1199 /* within CA_GotData, we assert just one redirect call */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1200 if (entry->Flags & CA_Redirect)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1201 Cache_redirect(entry, flags, Client_bw);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1202 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1203 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1204 } /* for */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1205
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1206 if (AbortEntry) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1207 /* Abort the entry, remove it from cache, and maybe offer download. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1208 DilloUrl *url = a_Url_dup(entry->Url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1209 a_Capi_conn_abort_by_url(url);
1251
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
1210 entry = NULL;
1418
d63ca4d23a48 Minor fix for 3f1501ae35c4 (only remove cache entries upon OfferDownload)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1401
diff changeset
1211 if (OfferDownload) {
d63ca4d23a48 Minor fix for 3f1501ae35c4 (only remove cache entries upon OfferDownload)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1401
diff changeset
1212 /* Remove entry when 'conn' is already done */
d63ca4d23a48 Minor fix for 3f1501ae35c4 (only remove cache entries upon OfferDownload)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1401
diff changeset
1213 Cache_entry_remove(NULL, url);
d63ca4d23a48 Minor fix for 3f1501ae35c4 (only remove cache entries upon OfferDownload)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1401
diff changeset
1214 if (a_Cache_download_enabled(url)) {
d63ca4d23a48 Minor fix for 3f1501ae35c4 (only remove cache entries upon OfferDownload)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1401
diff changeset
1215 Cache_savelink_t *data = dNew(Cache_savelink_t, 1);
d63ca4d23a48 Minor fix for 3f1501ae35c4 (only remove cache entries upon OfferDownload)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1401
diff changeset
1216 data->bw = Client_bw;
d63ca4d23a48 Minor fix for 3f1501ae35c4 (only remove cache entries upon OfferDownload)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1401
diff changeset
1217 data->url = a_Url_dup(url);
d63ca4d23a48 Minor fix for 3f1501ae35c4 (only remove cache entries upon OfferDownload)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1401
diff changeset
1218 a_Timeout_add(0.0, Cache_savelink_cb, data);
d63ca4d23a48 Minor fix for 3f1501ae35c4 (only remove cache entries upon OfferDownload)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1401
diff changeset
1219 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1220 }
1418
d63ca4d23a48 Minor fix for 3f1501ae35c4 (only remove cache entries upon OfferDownload)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1401
diff changeset
1221 a_Url_free(url);
470
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
1222 } else if (entry->Auth && (entry->Flags & CA_GotData)) {
b01e80ee7c88 Added basic authentication!
Jorge Arellano Cid <jcid@dillo.org>
parents: 430
diff changeset
1223 Cache_auth_entry(entry, Client_bw);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1224 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1225
155
fb8da086d7da + s/there're/there are/g
jcid
parents: 153
diff changeset
1226 /* Trigger cleanup when there are no cache clients */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1227 if (dList_length(ClientQueue) == 0) {
19
145bd87089e7 Implemented a new scheme of scroll-position remembering. This is one per
jcid
parents: 0
diff changeset
1228 _MSG(" a_Dicache_cleanup()\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1229 a_Dicache_cleanup();
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1230 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1231
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1232 Busy = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1233 _MSG("QueueSize ====> %d\n", dList_length(ClientQueue));
1251
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
1234 return entry;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1235 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1236
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1237 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1238 * Callback function for Cache_delayed_process_queue.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1239 */
881
8568a970ae65 Avoid a compiler warning in cache.c
Jorge Arellano Cid <jcid@dillo.org>
parents: 814
diff changeset
1240 static void Cache_delayed_process_queue_callback()
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1241 {
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
1242 CacheEntry_t *entry;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1243
260
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
1244 while ((entry = (CacheEntry_t *)dList_nth_data(DelayedQueue, 0))) {
81fc5011ea56 - Moved charset decoding into cache.
jcid
parents: 255
diff changeset
1245 Cache_ref_data(entry);
1251
7e945d8a5f29 fix segfault in a_Cache_process_dbuf()
corvid <corvid@lavabit.com>
parents: 1186
diff changeset
1246 if ((entry = Cache_process_queue(entry))) {
332
29a00c6df094 - Fixed a crash bug with repush and non-viewable content.
jcid
parents: 329
diff changeset
1247 Cache_unref_data(entry);
29a00c6df094 - Fixed a crash bug with repush and non-viewable content.
jcid
parents: 329
diff changeset
1248 dList_remove(DelayedQueue, entry);
29a00c6df094 - Fixed a crash bug with repush and non-viewable content.
jcid
parents: 329
diff changeset
1249 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1250 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1251 DelayedQueueIdleId = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1252 a_Timeout_remove();
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1253 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1254
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1255 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1256 * Set a call to Cache_process_queue from the main cycle.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1257 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1258 static void Cache_delayed_process_queue(CacheEntry_t *entry)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1259 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1260 /* there's no need to repeat entries in the queue */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1261 if (!dList_find(DelayedQueue, entry))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1262 dList_append(DelayedQueue, entry);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1263
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1264 if (DelayedQueueIdleId == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1265 _MSG(" Setting timeout callback\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1266 a_Timeout_add(0.0, Cache_delayed_process_queue_callback, NULL);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1267 DelayedQueueIdleId = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1268 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1269 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1270
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1271 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1272 * Last Client for this entry?
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1273 * Return: Client if true, NULL otherwise
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1274 * (cache.c has only one call to a capi function. This avoids a second one)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1275 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1276 CacheClient_t *a_Cache_client_get_if_unique(int Key)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1277 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1278 int i, n = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1279 CacheClient_t *Client, *iClient;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1280
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1281 if ((Client = dList_find_custom(ClientQueue, INT2VOIDP(Key),
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1282 Cache_client_by_key_cmp))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1283 for (i = 0; (iClient = dList_nth_data(ClientQueue, i)); ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1284 if (iClient->Url == Client->Url) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1285 ++n;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1286 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1287 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1288 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1289 return (n == 1) ? Client : NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1290 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1291
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1292 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1293 * Remove a client from the client queue
368
2242da885677 - s/todo:/TODO:/g
jcid
parents: 360
diff changeset
1294 * TODO: notify the dicache and upper layers
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1295 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1296 void a_Cache_stop_client(int Key)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1297 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1298 CacheClient_t *Client;
812
6a5bff4e66b7 Bug fix: Made a_Cache_stop_client() also remove from the delayed queue.
Jorge Arellano Cid <jcid@dillo.org>
parents: 811
diff changeset
1299 CacheEntry_t *entry;
710
43e46533be39 imported patch dicache-cleanup5
Jorge Arellano Cid <jcid@dillo.org>
parents: 470
diff changeset
1300 DICacheEntry *DicEntry;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1301
812
6a5bff4e66b7 Bug fix: Made a_Cache_stop_client() also remove from the delayed queue.
Jorge Arellano Cid <jcid@dillo.org>
parents: 811
diff changeset
1302 /* The client can be in both queues at the same time */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1303 if ((Client = dList_find_custom(ClientQueue, INT2VOIDP(Key),
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1304 Cache_client_by_key_cmp))) {
812
6a5bff4e66b7 Bug fix: Made a_Cache_stop_client() also remove from the delayed queue.
Jorge Arellano Cid <jcid@dillo.org>
parents: 811
diff changeset
1305 /* Dicache */
6a5bff4e66b7 Bug fix: Made a_Cache_stop_client() also remove from the delayed queue.
Jorge Arellano Cid <jcid@dillo.org>
parents: 811
diff changeset
1306 if ((DicEntry = a_Dicache_get_entry(Client->Url, Client->Version)))
710
43e46533be39 imported patch dicache-cleanup5
Jorge Arellano Cid <jcid@dillo.org>
parents: 470
diff changeset
1307 a_Dicache_unref(Client->Url, Client->Version);
812
6a5bff4e66b7 Bug fix: Made a_Cache_stop_client() also remove from the delayed queue.
Jorge Arellano Cid <jcid@dillo.org>
parents: 811
diff changeset
1308
6a5bff4e66b7 Bug fix: Made a_Cache_stop_client() also remove from the delayed queue.
Jorge Arellano Cid <jcid@dillo.org>
parents: 811
diff changeset
1309 /* DelayedQueue */
6a5bff4e66b7 Bug fix: Made a_Cache_stop_client() also remove from the delayed queue.
Jorge Arellano Cid <jcid@dillo.org>
parents: 811
diff changeset
1310 if ((entry = Cache_entry_search(Client->Url)))
6a5bff4e66b7 Bug fix: Made a_Cache_stop_client() also remove from the delayed queue.
Jorge Arellano Cid <jcid@dillo.org>
parents: 811
diff changeset
1311 dList_remove(DelayedQueue, entry);
6a5bff4e66b7 Bug fix: Made a_Cache_stop_client() also remove from the delayed queue.
Jorge Arellano Cid <jcid@dillo.org>
parents: 811
diff changeset
1312
6a5bff4e66b7 Bug fix: Made a_Cache_stop_client() also remove from the delayed queue.
Jorge Arellano Cid <jcid@dillo.org>
parents: 811
diff changeset
1313 /* Main queue */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1314 Cache_client_dequeue(Client, NULLKey);
812
6a5bff4e66b7 Bug fix: Made a_Cache_stop_client() also remove from the delayed queue.
Jorge Arellano Cid <jcid@dillo.org>
parents: 811
diff changeset
1315
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1316 } else {
153
f175ca0f22be - Typo correction.
jcid
parents: 119
diff changeset
1317 _MSG("WARNING: Cache_stop_client, nonexistent client\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1318 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1319 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1320
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1321
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1322 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1323 * Memory deallocator (only called at exit time)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1324 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1325 void a_Cache_freeall(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1326 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1327 CacheClient_t *Client;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1328 void *data;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1329
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1330 /* free the client queue */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1331 while ((Client = dList_nth_data(ClientQueue, 0)))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1332 Cache_client_dequeue(Client, NULLKey);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1333
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1334 /* Remove every cache entry */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1335 while ((data = dList_nth_data(CachedURLs, 0))) {
1621
c1e218f916fa some remove_fast at freeall() time
corvid <corvid@lavabit.com>
parents: 1587
diff changeset
1336 dList_remove_fast(CachedURLs, data);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1337 Cache_entry_free(data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1338 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1339 /* Remove the cache list */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1340 dList_free(CachedURLs);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1341 }