2003-02-09 15:58:15 +03:00
|
|
|
/**
|
2003-06-26 15:41:26 +04:00
|
|
|
* $Id: fetchcache.c,v 1.11 2003/06/26 11:41:26 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"
|
2003-06-17 23:24:21 +04:00
|
|
|
#include "netsurf/content/content.h"
|
2003-02-09 15:58:15 +03:00
|
|
|
#include "netsurf/content/fetchcache.h"
|
|
|
|
#include "netsurf/content/fetch.h"
|
|
|
|
#include "netsurf/utils/log.h"
|
|
|
|
#include "netsurf/utils/utils.h"
|
|
|
|
|
|
|
|
|
2003-06-17 23:24:21 +04:00
|
|
|
static void fetchcache_callback(fetch_msg msg, void *p, char *data, unsigned long size);
|
2003-02-09 15:58:15 +03:00
|
|
|
|
|
|
|
|
2003-06-17 23:24:21 +04:00
|
|
|
struct content * fetchcache(const char *url, 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)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
|
|
|
struct content *c;
|
2003-06-17 23:24:21 +04:00
|
|
|
|
|
|
|
LOG(("url %s", url));
|
2003-02-09 15:58:15 +03:00
|
|
|
|
|
|
|
c = cache_get(url);
|
|
|
|
if (c != 0) {
|
2003-06-17 23:24:21 +04:00
|
|
|
content_add_user(c, callback, p1, p2);
|
|
|
|
return c;
|
2003-04-18 01:35:02 +04:00
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2003-06-17 23:24:21 +04:00
|
|
|
c = content_create(url);
|
|
|
|
content_add_user(c, callback, p1, p2);
|
|
|
|
cache_put(c);
|
|
|
|
c->fetch_size = 0;
|
|
|
|
c->width = width;
|
|
|
|
c->height = height;
|
|
|
|
c->fetch = fetch_start(url, referer, fetchcache_callback, c);
|
|
|
|
return c;
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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-06-17 23:24:21 +04:00
|
|
|
struct content *c = 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-04-18 01:35:02 +04:00
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
switch (msg) {
|
|
|
|
case FETCH_TYPE:
|
2003-04-25 12:03:15 +04:00
|
|
|
mime_type = xstrdup(data);
|
2003-02-26 00:00:27 +03:00
|
|
|
if ((semic = strchr(mime_type, ';')) != 0)
|
|
|
|
*semic = 0; /* remove "; charset=..." */
|
|
|
|
type = content_lookup(mime_type);
|
2003-02-28 14:49:13 +03:00
|
|
|
free(mime_type);
|
2003-06-17 23:24:21 +04:00
|
|
|
LOG(("FETCH_TYPE, type %u", type));
|
|
|
|
content_set_type(c, type);
|
2003-02-09 15:58:15 +03:00
|
|
|
break;
|
2003-04-18 01:35:02 +04:00
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
case FETCH_DATA:
|
|
|
|
LOG(("FETCH_DATA"));
|
2003-06-17 23:24:21 +04:00
|
|
|
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);
|
2003-02-09 15:58:15 +03:00
|
|
|
break;
|
2003-04-18 01:35:02 +04:00
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
case FETCH_FINISHED:
|
|
|
|
LOG(("FETCH_FINISHED"));
|
2003-06-17 23:24:21 +04:00
|
|
|
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);
|
2003-02-09 15:58:15 +03:00
|
|
|
break;
|
2003-04-18 01:35:02 +04:00
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
case FETCH_ERROR:
|
|
|
|
LOG(("FETCH_ERROR, '%s'", data));
|
2003-06-17 23:24:21 +04:00
|
|
|
c->fetch = 0;
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, data);
|
|
|
|
cache_destroy(c);
|
|
|
|
content_destroy(c);
|
2003-02-09 15:58:15 +03:00
|
|
|
break;
|
2003-04-18 01:35:02 +04:00
|
|
|
|
2003-06-26 15:41:26 +04:00
|
|
|
case FETCH_REDIRECT:
|
|
|
|
LOG(("FETCH_REDIRECT, '%s'", data));
|
|
|
|
c->fetch = 0;
|
|
|
|
content_broadcast(c, CONTENT_MSG_REDIRECT, data);
|
|
|
|
cache_destroy(c);
|
|
|
|
content_destroy(c);
|
|
|
|
break;
|
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
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
|