summaryrefslogtreecommitdiff
path: root/content/fetchcache.c
blob: 23316b1b62bc25c0eed229065751d3cc24b20b2d (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
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2003 James Bursa <bursa@users.sourceforge.net>
 */

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


static void fetchcache_callback(fetch_msg msg, void *p, char *data, unsigned long size);


struct content * fetchcache(const char *url0, char *referer,
		void (*callback)(content_msg msg, struct content *c, void *p1,
			void *p2, const char *error),
		void *p1, void *p2, unsigned long width, unsigned long height,
		struct object_params *object_params)
{
	struct content *c;
	char *url = xstrdup(url0);
	char *hash = strchr(url, '#');

	/* strip fragment identifier */
	if (hash != 0)
		*hash = 0;

	LOG(("url %s", url));

	c = cache_get(url);
	if (c != 0) {
		content_add_user(c, callback, p1, p2, object_params);
		return c;
	}

	c = content_create(url);
	content_add_user(c, callback, p1, p2, object_params);
	cache_put(c);
	c->fetch_size = 0;
	c->width = width;
	c->height = height;
	c->fetch = fetch_start(url, referer, fetchcache_callback, c);
	return c;
}


void fetchcache_callback(fetch_msg msg, void *p, char *data, unsigned long size)
{
	struct content *c = p;
	content_type type;
	char *mime_type;
	char *semic;
	char *url;

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

		case FETCH_DATA:
			LOG(("FETCH_DATA"));
			c->fetch_size += size;
			sprintf(c->status_message, "Received %lu bytes", c->fetch_size);
			content_broadcast(c, CONTENT_MSG_STATUS, 0);
			content_process_data(c, data, size);
			break;

		case FETCH_FINISHED:
			LOG(("FETCH_FINISHED"));
			sprintf(c->status_message, "Converting %lu bytes", c->fetch_size);
			c->fetch = 0;
			content_broadcast(c, CONTENT_MSG_STATUS, 0);
			content_convert(c, c->width, c->height);
			break;

		case FETCH_ERROR:
			LOG(("FETCH_ERROR, '%s'", data));
			c->fetch = 0;
			content_broadcast(c, CONTENT_MSG_ERROR, data);
			cache_destroy(c);
			content_destroy(c);
			break;

		case FETCH_REDIRECT:
			LOG(("FETCH_REDIRECT, '%s'", data));
			c->fetch = 0;
			/* redirect URLs must be absolute by HTTP/1.1, but many sites send
			 * relative ones: treat them as relative to requested URL */
			url = url_join(data, c->url);
			content_broadcast(c, CONTENT_MSG_REDIRECT, url);
			xfree(url);
			cache_destroy(c);
			content_destroy(c);
			break;

		default:
			assert(0);
	}
}


#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