summaryrefslogtreecommitdiff
path: root/content/fetchcache.c
blob: 34105c71d14891ed8d0096cf9cacd2a660694a90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
 * $Id: fetchcache.c,v 1.8 2003/04/17 21:35:02 bursa Exp $
 */

#include <assert.h>
#include <string.h>
#include "netsurf/content/cache.h"
#include "netsurf/content/fetchcache.h"
#include "netsurf/content/fetch.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/utils.h"


struct fetchcache {
	char *url;
	void (*callback)(fetchcache_msg msg, struct content *c, void *p, const char *error);
	void *p;
	struct fetch *f;
	struct content *c;
	unsigned long width, height;
	unsigned long size;
	content_type allowed;
	struct fetchcache *next;
	struct fetchcache *prev;
	struct fetchcache *next_request;
	int active;
};

static struct fetchcache *fetchcache_list = 0;

static void fetchcache_free(struct fetchcache *fc);
static void fetchcache_callback(fetchcache_msg msg, void *p, char *data, unsigned long size);
static void status_callback(void *p, const char *status);


void fetchcache(const char *url, char *referer,
		void (*callback)(fetchcache_msg msg, struct content *c, void *p, const char *error),
		void *p, unsigned long width, unsigned long height, content_type allowed)
{
	struct content *c;
	struct fetchcache *fc, *fc_url;

	c = cache_get(url);
	if (c != 0) {
		/* check type is allowed */
		if ((1 << c->type) & allowed) {
			callback(FETCHCACHE_STATUS, c, p, "Found in cache");
			content_revive(c, width, height);
			callback(FETCHCACHE_OK, c, p, 0);
		} else {
			callback(FETCHCACHE_BADTYPE, 0, p, "");
			cache_free(c);
		}
		return;
	}

	callback(FETCHCACHE_STATUS, c, p, "Starting fetch");
	fc = xcalloc(1, sizeof(struct fetchcache));
	fc->url = xstrdup(url);
	fc->callback = callback;
	fc->p = p;
	fc->c = 0;
	fc->width = width;
	fc->height = height;
	fc->size = 0;
	fc->allowed = allowed;
	fc->next = 0;
	fc->prev = 0;
	fc->next_request = 0;
	fc->active = 1;

	/* check if we're already fetching this url */
	for (fc_url = fetchcache_list;
			fc_url != 0 && strcmp(fc_url->url, url) != 0;
			fc_url = fc_url->next)
		;
	if (fc_url != 0) {
		/* already fetching: add ourselves to list of requestors */
		LOG(("already fetching"));
		fc->next_request = fc_url->next_request;
		fc_url->next_request = fc;
	
	} else {
		/* not fetching yet */
		if (fetchcache_list != 0)
			fetchcache_list->prev = fc;
		fc->next = fetchcache_list;
		fetchcache_list = fc;
		fc->f = fetch_start(fc->url, referer, fetchcache_callback, fc);
	}
}


void fetchcache_free(struct fetchcache *fc)
{
	free(fc->url);
	free(fc);
	if (fc->prev == 0)
		fetchcache_list = fc->next;
	else
		fc->prev->next = fc->next;
	if (fc->next != 0)
		fc->next->prev = fc->prev;
}


void fetchcache_callback(fetch_msg msg, void *p, char *data, unsigned long size)
{
	struct fetchcache *fc = p, *fc_url;
	content_type type;
	char *mime_type;
	char *semic;
	char status[40];
	int active = 0;

	switch (msg) {
		case FETCH_TYPE:
			mime_type = strdup(data);
			if ((semic = strchr(mime_type, ';')) != 0)
				*semic = 0;	/* remove "; charset=..." */
			type = content_lookup(mime_type);
			LOG(("FETCH_TYPE, type %u", type));

			/* check if each request allows this type */
			for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request) {
				if (!fc_url->active)
					continue;
				if ((1 << type) & fc_url->allowed) {
					active++;
				} else {
					fc_url->active = 0;
					fc_url->callback(FETCHCACHE_BADTYPE, 0,
							fc_url->p, mime_type);
				}
			}
			if (active != 0) {
				/* someone is still interested */
				fc->c = content_create(type, fc->url);
				fc->c->status_callback = status_callback;
				fc->c->status_p = fc;
			} else {
				/* no request allows the type */
				fetch_abort(fc->f);
				for (; fc != 0; fc = fc_url) {
					fc_url = fc->next_request;
					fetchcache_free(fc);
				}
			}

			free(mime_type);
			break;

		case FETCH_DATA:
			LOG(("FETCH_DATA"));
			assert(fc->c != 0);
			fc->size += size;
			sprintf(status, "Received %lu bytes", fc->size);
			for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request)
				if (fc_url->active)
					fc_url->callback(FETCHCACHE_STATUS, fc->c,
							fc_url->p, status);
			content_process_data(fc->c, data, size);
			break;

		case FETCH_FINISHED:
			LOG(("FETCH_FINISHED"));
			assert(fc->c != 0);
			sprintf(status, "Converting %lu bytes", fc->size);
			for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request)
				if (fc_url->active)
					fc_url->callback(FETCHCACHE_STATUS, fc->c,
							fc_url->p, status);

			if (content_convert(fc->c, fc->width, fc->height) == 0) {
				cache_put(fc->c);
				for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request)
					if (fc_url->active)
						fc_url->callback(FETCHCACHE_OK, cache_get(fc->url),
								fc_url->p, 0);
				cache_free(fc->c);
			} else {
				content_destroy(fc->c);
				for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request)
					if (fc_url->active)
						fc_url->callback(FETCHCACHE_ERROR, 0,
								fc_url->p, "Conversion failed");
			}
			for (; fc != 0; fc = fc_url) {
				fc_url = fc->next_request;
				fetchcache_free(fc);
			}
			break;

		case FETCH_ERROR:
			LOG(("FETCH_ERROR, '%s'", data));
			if (fc->c != 0)
				content_destroy(fc->c);
			for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request)
				if (fc_url->active)
					fc->callback(FETCHCACHE_ERROR, 0, fc_url->p, data);
			for (; fc != 0; fc = fc_url) {
				fc_url = fc->next_request;
				fetchcache_free(fc);
			}
			break;

		default:
			assert(0);
	}
}


void status_callback(void *p, const char *status)
{
	struct fetchcache *fc = p, *fc_url;
	for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request)
		if (fc_url->active)
			fc_url->callback(FETCHCACHE_STATUS, fc->c, fc_url->p, status);
}


#ifdef TEST

#include <unistd.h>

void callback(fetchcache_msg msg, struct content *c, void *p, char *error)
{
	switch (msg) {
		case FETCHCACHE_OK:
			LOG(("FETCHCACHE_OK, url '%s'", p));
			break;
		case FETCHCACHE_BADTYPE:
			LOG(("FETCHCACHE_BADTYPE, url '%s'", p));
			break;
		case FETCHCACHE_ERROR:
			LOG(("FETCHCACHE_ERROR, url '%s', error '%s'", p, error));
			break;
		default:
			assert(0);
	}
}

char *test[] = {"http://www.google.co.uk/", "http://www.ox.ac.uk/", "blah://blah/"};

int main(void)
{
	int i;

	cache_init();
	fetch_init();

	for (i = 0; i != sizeof(test) / sizeof(test[0]); i++)
		fetchcache(test[i], 0, callback, test[i], 800, 0);
	for (i = 0; i != 5; i++) {
		fetch_poll();
		sleep(1);
	}
	for (i = 0; i != sizeof(test) / sizeof(test[0]); i++)
		fetchcache(test[i], 0, callback, test[i], 800, 0);
	for (i = 0; i != 20; i++) {
		fetch_poll();
		sleep(1);
	}
	return 0;
}

#endif