2003-02-09 15:58:15 +03:00
|
|
|
/**
|
2003-04-10 01:57:09 +04:00
|
|
|
* $Id: fetchcache.c,v 1.7 2003/04/09 21:57:09 bursa Exp $
|
2003-02-09 15:58:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2003-02-28 14:49:13 +03:00
|
|
|
#include <string.h>
|
2003-02-09 15:58:15 +03:00
|
|
|
#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 {
|
2003-03-08 23:26:31 +03:00
|
|
|
char *url;
|
2003-02-28 14:49:13 +03:00
|
|
|
void (*callback)(fetchcache_msg msg, struct content *c, void *p, const char *error);
|
2003-02-09 15:58:15 +03:00
|
|
|
void *p;
|
|
|
|
struct fetch *f;
|
|
|
|
struct content *c;
|
|
|
|
unsigned long width, height;
|
2003-02-28 14:49:13 +03:00
|
|
|
unsigned long size;
|
2003-04-10 01:57:09 +04:00
|
|
|
content_type allowed;
|
2003-02-09 15:58:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-03-04 14:59:36 +03:00
|
|
|
static void fetchcache_free(struct fetchcache *fc);
|
|
|
|
static void fetchcache_callback(fetchcache_msg msg, void *p, char *data, unsigned long size);
|
2003-04-06 22:09:34 +04:00
|
|
|
static void status_callback(void *p, const char *status);
|
2003-02-09 15:58:15 +03:00
|
|
|
|
|
|
|
|
2003-03-04 14:59:36 +03:00
|
|
|
void fetchcache(const char *url, char *referer,
|
2003-02-28 14:49:13 +03:00
|
|
|
void (*callback)(fetchcache_msg msg, struct content *c, void *p, const char *error),
|
2003-04-10 01:57:09 +04:00
|
|
|
void *p, unsigned long width, unsigned long height, content_type allowed)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
|
|
|
struct content *c;
|
|
|
|
struct fetchcache *fc;
|
|
|
|
|
|
|
|
c = cache_get(url);
|
|
|
|
if (c != 0) {
|
2003-04-10 01:57:09 +04:00
|
|
|
/* 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);
|
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-02-28 14:49:13 +03:00
|
|
|
callback(FETCHCACHE_STATUS, c, p, "Starting fetch");
|
2003-02-09 15:58:15 +03:00
|
|
|
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;
|
2003-02-28 14:49:13 +03:00
|
|
|
fc->size = 0;
|
2003-04-10 01:57:09 +04:00
|
|
|
fc->allowed = allowed;
|
2003-03-08 23:26:31 +03:00
|
|
|
fc->f = fetch_start(fc->url, referer, fetchcache_callback, fc);
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void fetchcache_free(struct fetchcache *fc)
|
|
|
|
{
|
|
|
|
free(fc->url);
|
|
|
|
free(fc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-28 14:49:13 +03:00
|
|
|
void fetchcache_callback(fetch_msg msg, void *p, char *data, unsigned long size)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
2003-02-28 14:49:13 +03:00
|
|
|
struct fetchcache *fc = p;
|
2003-02-09 15:58:15 +03:00
|
|
|
content_type type;
|
2003-02-26 00:00:27 +03:00
|
|
|
char *mime_type;
|
|
|
|
char *semic;
|
2003-02-28 14:49:13 +03:00
|
|
|
char status[40];
|
2003-02-09 15:58:15 +03:00
|
|
|
switch (msg) {
|
|
|
|
case FETCH_TYPE:
|
2003-02-26 00:00:27 +03:00
|
|
|
mime_type = strdup(data);
|
|
|
|
if ((semic = strchr(mime_type, ';')) != 0)
|
|
|
|
*semic = 0; /* remove "; charset=..." */
|
|
|
|
type = content_lookup(mime_type);
|
2003-02-09 15:58:15 +03:00
|
|
|
LOG(("FETCH_TYPE, type %u", type));
|
2003-04-10 01:57:09 +04:00
|
|
|
if ((1 << type) & fc->allowed) {
|
2003-02-09 15:58:15 +03:00
|
|
|
fc->c = content_create(type, fc->url);
|
2003-04-06 22:09:34 +04:00
|
|
|
fc->c->status_callback = status_callback;
|
|
|
|
fc->c->status_p = fc;
|
2003-04-10 01:57:09 +04:00
|
|
|
} else {
|
|
|
|
fetch_abort(fc->f);
|
|
|
|
fc->callback(FETCHCACHE_BADTYPE, 0, fc->p, mime_type);
|
|
|
|
free(fc);
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
2003-02-28 14:49:13 +03:00
|
|
|
free(mime_type);
|
2003-02-09 15:58:15 +03:00
|
|
|
break;
|
|
|
|
case FETCH_DATA:
|
|
|
|
LOG(("FETCH_DATA"));
|
|
|
|
assert(fc->c != 0);
|
2003-02-28 14:49:13 +03:00
|
|
|
fc->size += size;
|
|
|
|
sprintf(status, "Received %lu bytes", fc->size);
|
|
|
|
fc->callback(FETCHCACHE_STATUS, fc->c, fc->p, status);
|
2003-02-09 15:58:15 +03:00
|
|
|
content_process_data(fc->c, data, size);
|
|
|
|
break;
|
|
|
|
case FETCH_FINISHED:
|
|
|
|
LOG(("FETCH_FINISHED"));
|
|
|
|
assert(fc->c != 0);
|
2003-02-28 14:49:13 +03:00
|
|
|
sprintf(status, "Converting %lu bytes", fc->size);
|
2003-03-08 23:26:31 +03:00
|
|
|
fc->callback(FETCHCACHE_STATUS, fc->c, fc->p, status);
|
2003-02-09 15:58:15 +03:00
|
|
|
if (content_convert(fc->c, fc->width, fc->height) == 0) {
|
|
|
|
cache_put(fc->c);
|
|
|
|
fc->callback(FETCHCACHE_OK, fc->c, fc->p, 0);
|
|
|
|
} else {
|
|
|
|
content_destroy(fc->c);
|
|
|
|
fc->callback(FETCHCACHE_ERROR, 0, fc->p, "Conversion failed");
|
|
|
|
}
|
2003-03-08 23:26:31 +03:00
|
|
|
fetchcache_free(fc);
|
2003-02-09 15:58:15 +03:00
|
|
|
break;
|
|
|
|
case FETCH_ERROR:
|
|
|
|
LOG(("FETCH_ERROR, '%s'", data));
|
|
|
|
if (fc->c != 0)
|
|
|
|
content_destroy(fc->c);
|
|
|
|
fc->callback(FETCHCACHE_ERROR, 0, fc->p, data);
|
2003-03-08 23:26:31 +03:00
|
|
|
fetchcache_free(fc);
|
2003-02-09 15:58:15 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-06 22:09:34 +04:00
|
|
|
void status_callback(void *p, const char *status)
|
|
|
|
{
|
|
|
|
struct fetchcache *fc = p;
|
|
|
|
fc->callback(FETCHCACHE_STATUS, fc->c, fc->p, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
#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
|