2014-10-13 18:08:16 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2011 John Mark Bell <jmb@netsurf-browser.org>
|
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2010-06-29 10:05:29 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
#include "content/fetch.h"
|
|
|
|
#include "content/llcache.h"
|
|
|
|
#include "utils/ring.h"
|
2011-10-15 01:29:13 +04:00
|
|
|
#include "utils/nsurl.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "utils/url.h"
|
2015-02-26 18:58:40 +03:00
|
|
|
#include "utils/corestrings.h"
|
2011-10-15 01:29:13 +04:00
|
|
|
#include "utils/utils.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Things that we'd reasonably expect to have to implement *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
bool verbose_log;
|
|
|
|
|
|
|
|
/* utils/utils.h */
|
|
|
|
void die(const char * const error)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\n", error);
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* utils/utils.h */
|
|
|
|
void warn_user(const char *warning, const char *detail)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s %s\n", warning, detail);
|
|
|
|
}
|
|
|
|
|
2010-06-29 10:05:29 +04:00
|
|
|
/* utils/utils.h */
|
|
|
|
char *filename_from_path(char *path)
|
|
|
|
{
|
|
|
|
char *leafname;
|
|
|
|
|
|
|
|
leafname = strrchr(path, '/');
|
|
|
|
if (!leafname)
|
|
|
|
leafname = path;
|
|
|
|
else
|
|
|
|
leafname += 1;
|
|
|
|
|
|
|
|
return strdup(leafname);
|
|
|
|
}
|
|
|
|
|
2011-10-15 01:29:13 +04:00
|
|
|
/* utils/schedule.h */
|
|
|
|
void schedule(int t, schedule_callback_fn cb, void *pw)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* utils/schedule.h */
|
|
|
|
void schedule_remove(schedule_callback_fn cb, void *pw)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* content/fetch.h */
|
|
|
|
const char *fetch_filetype(const char *unix_path)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* content/fetch.h */
|
|
|
|
char *fetch_mimetype(const char *ro_path)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-29 10:05:29 +04:00
|
|
|
/* utils/url.h */
|
|
|
|
char *path_to_url(const char *path)
|
|
|
|
{
|
|
|
|
int urllen = strlen(path) + FILE_SCHEME_PREFIX_LEN + 1;
|
|
|
|
char *url = malloc(urllen);
|
|
|
|
|
|
|
|
if (url == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*path == '/') {
|
|
|
|
path++; /* file: paths are already absolute */
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, path);
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* utils/url.h */
|
|
|
|
char *url_to_path(const char *url)
|
|
|
|
{
|
|
|
|
char *url_path = curl_unescape(url, 0);
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
/* return the absolute path including leading / */
|
|
|
|
path = strdup(url_path + (FILE_SCHEME_PREFIX_LEN - 1));
|
|
|
|
curl_free(url_path);
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* Things that are absolutely not reasonable, and should disappear *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-09-03 01:41:04 +04:00
|
|
|
#include "desktop/cookie_manager.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "desktop/tree.h"
|
|
|
|
|
2013-09-03 01:41:04 +04:00
|
|
|
/* desktop/cookie_manager.h -- used by urldb
|
2010-03-28 16:56:39 +04:00
|
|
|
*
|
|
|
|
* URLdb should have a cookies update event + handler registration
|
|
|
|
*/
|
2013-09-03 01:41:04 +04:00
|
|
|
bool cookie_manager_add(const struct cookie_data *data)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-03 01:41:04 +04:00
|
|
|
/* desktop/cookie_manager.h -- used by urldb
|
2011-01-12 11:40:08 +03:00
|
|
|
*
|
|
|
|
* URLdb should have a cookies removal handler registration
|
|
|
|
*/
|
2013-09-03 01:41:04 +04:00
|
|
|
void cookie_manager_remove(const struct cookie_data *data)
|
2011-01-12 11:40:08 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* image/bitmap.h -- used by urldb
|
|
|
|
*
|
|
|
|
* URLdb shouldn't care about bitmaps.
|
|
|
|
* This is because the legacy RO thumbnail stuff was hacked in and must die.
|
|
|
|
*/
|
|
|
|
void bitmap_destroy(void *bitmap)
|
|
|
|
{
|
|
|
|
}
|
2013-04-08 20:50:55 +04:00
|
|
|
/* image/image.h -- used by urldb
|
|
|
|
*
|
|
|
|
* URLdb shouldn't care about bitmaps.
|
|
|
|
* This is because the legacy RO thumbnail stuff was hacked in and must die.
|
|
|
|
*/
|
|
|
|
bool image_bitmap_plot(struct bitmap *bitmap, struct content_redraw_data *data,
|
|
|
|
const struct rect *clip, const struct redraw_context *ctx)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2011-01-12 11:40:08 +03:00
|
|
|
/* content/fetchers/fetch_file.h -- used by fetcher core
|
2010-03-28 16:56:39 +04:00
|
|
|
*
|
2011-01-12 11:40:08 +03:00
|
|
|
* Simpler to stub this than haul in all the file fetcher's dependencies
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
2011-01-12 11:40:08 +03:00
|
|
|
void fetch_file_register(void)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-10-15 01:29:13 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* test: protocol handler *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
typedef struct test_context {
|
|
|
|
struct fetch *parent;
|
|
|
|
|
|
|
|
bool aborted;
|
|
|
|
bool locked;
|
|
|
|
|
|
|
|
struct test_context *r_prev;
|
|
|
|
struct test_context *r_next;
|
|
|
|
} test_context;
|
|
|
|
|
|
|
|
static test_context *ring;
|
|
|
|
|
2011-10-15 01:29:13 +04:00
|
|
|
bool test_initialise(lwc_string *scheme)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-08 20:50:55 +04:00
|
|
|
bool test_can_fetch(const nsurl *url)
|
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-15 01:29:13 +04:00
|
|
|
void test_finalise(lwc_string *scheme)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
}
|
|
|
|
|
2013-04-08 20:50:55 +04:00
|
|
|
void *test_setup_fetch(struct fetch *parent, nsurl *url, bool only_2xx,
|
|
|
|
bool downgrade_tls, const char *post_urlenc,
|
|
|
|
const struct fetch_multipart_data *post_multipart,
|
2010-03-28 16:56:39 +04:00
|
|
|
const char **headers)
|
|
|
|
{
|
|
|
|
test_context *ctx = calloc(1, sizeof(test_context));
|
|
|
|
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ctx->parent = parent;
|
|
|
|
|
|
|
|
RING_INSERT(ring, ctx);
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool test_start_fetch(void *handle)
|
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_abort_fetch(void *handle)
|
|
|
|
{
|
|
|
|
test_context *ctx = handle;
|
|
|
|
|
|
|
|
ctx->aborted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_free_fetch(void *handle)
|
|
|
|
{
|
|
|
|
test_context *ctx = handle;
|
|
|
|
|
|
|
|
RING_REMOVE(ring, ctx);
|
|
|
|
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_process(test_context *ctx)
|
|
|
|
{
|
|
|
|
/** \todo Implement */
|
|
|
|
}
|
|
|
|
|
2011-10-15 01:29:13 +04:00
|
|
|
void test_poll(lwc_string *scheme)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
test_context *ctx, *next;
|
|
|
|
|
|
|
|
if (ring == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ctx = ring;
|
|
|
|
do {
|
|
|
|
next = ctx->r_next;
|
|
|
|
|
|
|
|
if (ctx->locked)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ctx->aborted == false) {
|
|
|
|
test_process(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch_remove_from_queues(ctx->parent);
|
|
|
|
fetch_free(ctx->parent);
|
|
|
|
} while ((ctx = next) != ring && ring != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* The actual test code *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
nserror query_handler(const llcache_query *query, void *pw,
|
|
|
|
llcache_query_response cb, void *cbpw)
|
|
|
|
{
|
|
|
|
/* I'm too lazy to actually implement this. It should queue the query,
|
|
|
|
* then deliver the response from main(). */
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-29 10:05:29 +04:00
|
|
|
nserror event_handler(llcache_handle *handle,
|
2010-03-28 16:56:39 +04:00
|
|
|
const llcache_event *event, void *pw)
|
|
|
|
{
|
|
|
|
static char *event_names[] = {
|
|
|
|
"HAD_HEADERS", "HAD_DATA", "DONE", "ERROR", "PROGRESS"
|
|
|
|
};
|
|
|
|
bool *done = pw;
|
|
|
|
|
|
|
|
if (event->type != LLCACHE_EVENT_PROGRESS)
|
|
|
|
fprintf(stdout, "%p : %s\n", handle, event_names[event->type]);
|
|
|
|
|
|
|
|
/* Inform main() that the fetch completed */
|
|
|
|
if (event->type == LLCACHE_EVENT_DONE)
|
|
|
|
*done = true;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
nserror error;
|
|
|
|
llcache_handle *handle;
|
|
|
|
llcache_handle *handle2;
|
2011-10-15 01:29:13 +04:00
|
|
|
lwc_string *scheme;
|
|
|
|
nsurl *url;
|
2010-03-28 16:56:39 +04:00
|
|
|
bool done = false;
|
|
|
|
|
|
|
|
/* Initialise subsystems */
|
|
|
|
fetch_init();
|
2011-10-15 01:29:13 +04:00
|
|
|
|
|
|
|
if (lwc_intern_string("test", SLEN("test"), &scheme) != lwc_error_ok) {
|
|
|
|
fprintf(stderr, "Failed to intern \"test\"\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-04-08 20:50:55 +04:00
|
|
|
fetch_add_fetcher(scheme, test_initialise, test_can_fetch,
|
|
|
|
test_setup_fetch, test_start_fetch, test_abort_fetch,
|
|
|
|
test_free_fetch, test_poll, test_finalise);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
/* Initialise low-level cache */
|
2011-10-15 01:29:13 +04:00
|
|
|
error = llcache_initialise(query_handler, NULL, 1024 * 1024);
|
2010-03-28 16:56:39 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
fprintf(stderr, "llcache_initialise: %d\n", error);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-10-15 01:29:13 +04:00
|
|
|
if (nsurl_create("http://www.netsurf-browser.org", &url) != NSERROR_OK) {
|
|
|
|
fprintf(stderr, "Failed creating url\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Retrieve an URL from the low-level cache (may trigger fetch) */
|
2011-10-15 01:29:13 +04:00
|
|
|
error = llcache_handle_retrieve(url,
|
2010-03-28 16:56:39 +04:00
|
|
|
LLCACHE_RETRIEVE_VERIFIABLE, NULL, NULL,
|
|
|
|
event_handler, &done, &handle);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
fprintf(stderr, "llcache_handle_retrieve: %d\n", error);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Poll relevant components */
|
|
|
|
while (done == false) {
|
|
|
|
llcache_poll();
|
|
|
|
}
|
|
|
|
|
|
|
|
done = false;
|
2011-10-15 01:29:13 +04:00
|
|
|
error = llcache_handle_retrieve(url,
|
2010-03-28 16:56:39 +04:00
|
|
|
LLCACHE_RETRIEVE_VERIFIABLE, NULL, NULL,
|
|
|
|
event_handler, &done, &handle2);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
fprintf(stderr, "llcache_handle_retrieve: %d\n", error);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (done == false) {
|
|
|
|
llcache_poll();
|
|
|
|
}
|
|
|
|
|
2010-06-29 10:05:29 +04:00
|
|
|
fprintf(stdout, "%p, %p -> %d\n", handle, handle2,
|
|
|
|
llcache_handle_references_same_object(handle, handle2));
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
/* Cleanup */
|
|
|
|
llcache_handle_release(handle2);
|
|
|
|
llcache_handle_release(handle);
|
|
|
|
|
|
|
|
fetch_quit();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|