2011-02-24 01:27:55 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Vincent Sanders <vince@netsurf-browser.org>
|
|
|
|
*
|
|
|
|
* This file is part of NetSurf.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* resource: URL handling. Based on the data fetcher by Rob Kendrick */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2011-09-27 01:07:19 +04:00
|
|
|
#include <libwapcaplet/libwapcaplet.h>
|
|
|
|
|
2011-02-24 01:27:55 +03:00
|
|
|
#include "utils/config.h"
|
2014-06-19 21:27:24 +04:00
|
|
|
#include "utils/errors.h"
|
2014-01-25 00:08:41 +04:00
|
|
|
#include "utils/corestrings.h"
|
2013-05-26 01:46:27 +04:00
|
|
|
#include "utils/nsoption.h"
|
2011-02-24 01:27:55 +03:00
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/messages.h"
|
|
|
|
#include "utils/utils.h"
|
|
|
|
#include "utils/ring.h"
|
2014-06-19 21:27:24 +04:00
|
|
|
#include "desktop/gui_factory.h"
|
|
|
|
|
|
|
|
#include "content/fetch.h"
|
|
|
|
#include "content/fetchers.h"
|
|
|
|
#include "content/fetchers/resource.h"
|
|
|
|
#include "content/urldb.h"
|
2011-02-24 01:27:55 +03:00
|
|
|
|
|
|
|
struct fetch_resource_context;
|
|
|
|
|
|
|
|
typedef bool (*fetch_resource_handler)(struct fetch_resource_context *);
|
|
|
|
|
|
|
|
/** Context for an resource fetch */
|
|
|
|
struct fetch_resource_context {
|
|
|
|
struct fetch_resource_context *r_next, *r_prev;
|
|
|
|
|
|
|
|
struct fetch *fetchh; /**< Handle for this fetch */
|
|
|
|
|
|
|
|
bool aborted; /**< Flag indicating fetch has been aborted */
|
|
|
|
bool locked; /**< Flag indicating entry is already entered */
|
|
|
|
|
2011-10-05 12:32:52 +04:00
|
|
|
nsurl *url;
|
|
|
|
nsurl *redirect_url; /**< The url the fetch redirects to */
|
2011-02-24 01:27:55 +03:00
|
|
|
|
|
|
|
fetch_resource_handler handler;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct fetch_resource_context *ring = NULL;
|
|
|
|
|
2011-10-05 12:32:52 +04:00
|
|
|
/** Valid resource paths */
|
|
|
|
static const char *fetch_resource_paths[] = {
|
|
|
|
"adblock.css",
|
|
|
|
"default.css",
|
|
|
|
"internal.css",
|
|
|
|
"quirks.css",
|
2012-03-06 21:44:24 +04:00
|
|
|
"user.css",
|
2011-10-05 12:32:52 +04:00
|
|
|
"credits.html",
|
|
|
|
"licence.html",
|
2012-07-21 16:12:51 +04:00
|
|
|
"welcome.html",
|
2011-10-08 01:03:45 +04:00
|
|
|
"favicon.ico",
|
2014-05-25 03:57:48 +04:00
|
|
|
"default.ico",
|
2013-05-31 13:00:59 +04:00
|
|
|
"netsurf.png",
|
2013-08-12 19:34:14 +04:00
|
|
|
"icons/arrow-l.png",
|
2013-05-31 13:00:59 +04:00
|
|
|
"icons/content.png",
|
|
|
|
"icons/directory.png",
|
2013-11-08 22:53:34 +04:00
|
|
|
"icons/directory2.png",
|
2013-11-21 02:03:07 +04:00
|
|
|
"icons/hotlist-add.png",
|
|
|
|
"icons/hotlist-rmv.png",
|
2013-05-31 13:00:59 +04:00
|
|
|
"icons/search.png"
|
2011-10-05 12:32:52 +04:00
|
|
|
};
|
|
|
|
static struct fetch_resource_map_entry {
|
|
|
|
lwc_string *path;
|
|
|
|
nsurl *url;
|
|
|
|
} fetch_resource_map[NOF_ELEMENTS(fetch_resource_paths)];
|
|
|
|
|
2011-10-08 02:51:31 +04:00
|
|
|
static uint32_t fetch_resource_path_count;
|
|
|
|
|
2011-02-24 01:27:55 +03:00
|
|
|
/** issue fetch callbacks with locking */
|
2011-11-09 01:51:42 +04:00
|
|
|
static inline bool fetch_resource_send_callback(const fetch_msg *msg,
|
|
|
|
struct fetch_resource_context *ctx)
|
2011-02-24 01:27:55 +03:00
|
|
|
{
|
|
|
|
ctx->locked = true;
|
2011-11-09 01:51:42 +04:00
|
|
|
fetch_send_callback(msg, ctx->fetchh);
|
2011-02-24 01:27:55 +03:00
|
|
|
ctx->locked = false;
|
|
|
|
|
|
|
|
return ctx->aborted;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool fetch_resource_send_header(struct fetch_resource_context *ctx,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
2011-11-09 01:51:42 +04:00
|
|
|
fetch_msg msg;
|
2011-02-24 01:27:55 +03:00
|
|
|
char header[64];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
vsnprintf(header, sizeof header, fmt, ap);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.type = FETCH_HEADER;
|
|
|
|
msg.data.header_or_data.buf = (const uint8_t *) header;
|
|
|
|
msg.data.header_or_data.len = strlen(header);
|
|
|
|
fetch_resource_send_callback(&msg, ctx);
|
2011-02-24 01:27:55 +03:00
|
|
|
|
|
|
|
return ctx->aborted;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool fetch_resource_redirect_handler(struct fetch_resource_context *ctx)
|
|
|
|
{
|
2011-11-09 01:51:42 +04:00
|
|
|
fetch_msg msg;
|
|
|
|
|
2011-02-24 01:27:55 +03:00
|
|
|
/* content is going to return redirect */
|
|
|
|
fetch_set_http_code(ctx->fetchh, 302);
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.type = FETCH_REDIRECT;
|
|
|
|
msg.data.redirect = nsurl_access(ctx->redirect_url);
|
|
|
|
fetch_resource_send_callback(&msg, ctx);
|
2011-02-24 01:27:55 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool fetch_resource_notfound_handler(struct fetch_resource_context *ctx)
|
|
|
|
{
|
2011-11-09 01:51:42 +04:00
|
|
|
fetch_msg msg;
|
2011-02-24 01:27:55 +03:00
|
|
|
int code = 404;
|
|
|
|
char buffer[1024];
|
|
|
|
const char *title;
|
|
|
|
char key[8];
|
|
|
|
|
|
|
|
/* content is going to return error code */
|
|
|
|
fetch_set_http_code(ctx->fetchh, code);
|
|
|
|
|
|
|
|
/* content type */
|
|
|
|
if (fetch_resource_send_header(ctx, "Content-Type: text/html"))
|
|
|
|
goto fetch_resource_notfound_handler_aborted;
|
|
|
|
|
|
|
|
snprintf(key, sizeof key, "HTTP%03d", code);
|
|
|
|
title = messages_get(key);
|
|
|
|
|
|
|
|
snprintf(buffer, sizeof buffer, "<html><head><title>%s</title></head>"
|
|
|
|
"<body><h1>%s</h1>"
|
|
|
|
"<p>Error %d while fetching file %s</p></body></html>",
|
2011-10-05 12:32:52 +04:00
|
|
|
title, title, code, nsurl_access(ctx->url));
|
2011-02-24 01:27:55 +03:00
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.type = FETCH_DATA;
|
|
|
|
msg.data.header_or_data.buf = (const uint8_t *) buffer;
|
|
|
|
msg.data.header_or_data.len = strlen(buffer);
|
|
|
|
if (fetch_resource_send_callback(&msg, ctx))
|
2011-02-24 01:27:55 +03:00
|
|
|
goto fetch_resource_notfound_handler_aborted;
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
msg.type = FETCH_FINISHED;
|
|
|
|
fetch_resource_send_callback(&msg, ctx);
|
2011-02-24 01:27:55 +03:00
|
|
|
|
|
|
|
fetch_resource_notfound_handler_aborted:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** callback to initialise the resource fetcher. */
|
2011-09-27 01:07:19 +04:00
|
|
|
static bool fetch_resource_initialise(lwc_string *scheme)
|
2011-02-24 01:27:55 +03:00
|
|
|
{
|
2011-10-05 12:32:52 +04:00
|
|
|
struct fetch_resource_map_entry *e;
|
|
|
|
uint32_t i;
|
|
|
|
|
2011-10-08 02:51:31 +04:00
|
|
|
fetch_resource_path_count = 0;
|
|
|
|
|
2011-10-05 12:32:52 +04:00
|
|
|
for (i = 0; i < NOF_ELEMENTS(fetch_resource_paths); i++) {
|
2011-10-08 02:51:31 +04:00
|
|
|
e = &fetch_resource_map[fetch_resource_path_count];
|
2011-10-05 12:32:52 +04:00
|
|
|
|
|
|
|
if (lwc_intern_string(fetch_resource_paths[i],
|
|
|
|
strlen(fetch_resource_paths[i]),
|
|
|
|
&e->path) != lwc_error_ok) {
|
|
|
|
while (i > 0) {
|
|
|
|
i--;
|
|
|
|
lwc_string_unref(fetch_resource_map[i].path);
|
|
|
|
nsurl_unref(fetch_resource_map[i].url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-23 03:19:57 +04:00
|
|
|
e->url = guit->fetch->get_resource_url(fetch_resource_paths[i]);
|
2011-10-05 12:32:52 +04:00
|
|
|
if (e->url == NULL) {
|
|
|
|
lwc_string_unref(e->path);
|
2011-10-08 02:51:31 +04:00
|
|
|
} else {
|
|
|
|
fetch_resource_path_count++;
|
2011-10-05 12:32:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-24 01:27:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-05 12:32:52 +04:00
|
|
|
/** callback to finalise the resource fetcher. */
|
2011-09-27 01:07:19 +04:00
|
|
|
static void fetch_resource_finalise(lwc_string *scheme)
|
2011-02-24 01:27:55 +03:00
|
|
|
{
|
2011-10-05 12:32:52 +04:00
|
|
|
uint32_t i;
|
|
|
|
|
2011-10-08 02:51:31 +04:00
|
|
|
for (i = 0; i < fetch_resource_path_count; i++) {
|
2011-10-05 12:32:52 +04:00
|
|
|
lwc_string_unref(fetch_resource_map[i].path);
|
|
|
|
nsurl_unref(fetch_resource_map[i].url);
|
|
|
|
}
|
2011-02-24 01:27:55 +03:00
|
|
|
}
|
|
|
|
|
2011-11-27 18:14:36 +04:00
|
|
|
static bool fetch_resource_can_fetch(const nsurl *url)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-24 01:27:55 +03:00
|
|
|
/** callback to set up a resource fetch context. */
|
|
|
|
static void *
|
|
|
|
fetch_resource_setup(struct fetch *fetchh,
|
2011-09-29 19:31:54 +04:00
|
|
|
nsurl *url,
|
2011-02-24 01:27:55 +03:00
|
|
|
bool only_2xx,
|
2013-01-05 03:13:23 +04:00
|
|
|
bool downgrade_tls,
|
2011-02-24 01:27:55 +03:00
|
|
|
const char *post_urlenc,
|
|
|
|
const struct fetch_multipart_data *post_multipart,
|
|
|
|
const char **headers)
|
|
|
|
{
|
|
|
|
struct fetch_resource_context *ctx;
|
2011-10-05 12:32:52 +04:00
|
|
|
lwc_string *path;
|
2011-02-24 01:27:55 +03:00
|
|
|
|
|
|
|
ctx = calloc(1, sizeof(*ctx));
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2011-10-05 12:32:52 +04:00
|
|
|
ctx->handler = fetch_resource_notfound_handler;
|
|
|
|
|
|
|
|
if ((path = nsurl_get_component(url, NSURL_PATH)) != NULL) {
|
|
|
|
uint32_t i;
|
|
|
|
bool match;
|
|
|
|
|
|
|
|
/* Ensure requested path is valid */
|
2011-10-08 02:51:31 +04:00
|
|
|
for (i = 0; i < fetch_resource_path_count; i++) {
|
2011-10-05 12:32:52 +04:00
|
|
|
if (lwc_string_isequal(path,
|
|
|
|
fetch_resource_map[i].path,
|
|
|
|
&match) == lwc_error_ok && match) {
|
|
|
|
ctx->redirect_url =
|
|
|
|
nsurl_ref(fetch_resource_map[i].url);
|
|
|
|
ctx->handler =
|
|
|
|
fetch_resource_redirect_handler;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lwc_string_unref(path);
|
2011-02-24 01:27:55 +03:00
|
|
|
}
|
|
|
|
|
2011-10-05 12:32:52 +04:00
|
|
|
ctx->url = nsurl_ref(url);
|
2011-02-24 01:27:55 +03:00
|
|
|
|
|
|
|
ctx->fetchh = fetchh;
|
|
|
|
|
|
|
|
RING_INSERT(ring, ctx);
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** callback to free a resource fetch */
|
|
|
|
static void fetch_resource_free(void *ctx)
|
|
|
|
{
|
|
|
|
struct fetch_resource_context *c = ctx;
|
2011-10-05 12:32:52 +04:00
|
|
|
if (c->redirect_url != NULL)
|
|
|
|
nsurl_unref(c->redirect_url);
|
|
|
|
if (c->url != NULL)
|
|
|
|
nsurl_unref(c->url);
|
2011-02-24 01:27:55 +03:00
|
|
|
RING_REMOVE(ring, c);
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** callback to start a resource fetch */
|
|
|
|
static bool fetch_resource_start(void *ctx)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** callback to abort a resource fetch */
|
|
|
|
static void fetch_resource_abort(void *ctx)
|
|
|
|
{
|
|
|
|
struct fetch_resource_context *c = ctx;
|
|
|
|
|
|
|
|
/* To avoid the poll loop having to deal with the fetch context
|
|
|
|
* disappearing from under it, we simply flag the abort here.
|
|
|
|
* The poll loop itself will perform the appropriate cleanup.
|
|
|
|
*/
|
|
|
|
c->aborted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** callback to poll for additional resource fetch contents */
|
2011-09-27 01:07:19 +04:00
|
|
|
static void fetch_resource_poll(lwc_string *scheme)
|
2011-02-24 01:27:55 +03:00
|
|
|
{
|
|
|
|
struct fetch_resource_context *c, *next;
|
|
|
|
|
|
|
|
if (ring == NULL) return;
|
|
|
|
|
|
|
|
/* Iterate over ring, processing each pending fetch */
|
|
|
|
c = ring;
|
|
|
|
do {
|
|
|
|
/* Ignore fetches that have been flagged as locked.
|
|
|
|
* This allows safe re-entrant calls to this function.
|
|
|
|
* Re-entrancy can occur if, as a result of a callback,
|
|
|
|
* the interested party causes fetch_poll() to be called
|
|
|
|
* again.
|
|
|
|
*/
|
|
|
|
if (c->locked == true) {
|
2011-02-24 01:41:17 +03:00
|
|
|
next = c->r_next;
|
2011-02-24 01:27:55 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only process non-aborted fetches */
|
|
|
|
if (c->aborted == false) {
|
|
|
|
/* resource fetches can be processed in one go */
|
|
|
|
c->handler(c);
|
|
|
|
}
|
|
|
|
|
2011-02-24 01:41:17 +03:00
|
|
|
/* Compute next fetch item at the last possible moment
|
|
|
|
* as processing this item may have added to the ring
|
|
|
|
*/
|
|
|
|
next = c->r_next;
|
2011-02-24 01:27:55 +03:00
|
|
|
|
|
|
|
fetch_remove_from_queues(c->fetchh);
|
|
|
|
fetch_free(c->fetchh);
|
|
|
|
|
|
|
|
/* Advance to next ring entry, exiting if we've reached
|
|
|
|
* the start of the ring or the ring has become empty
|
|
|
|
*/
|
|
|
|
} while ( (c = next) != ring && ring != NULL);
|
|
|
|
}
|
|
|
|
|
2014-06-19 21:27:24 +04:00
|
|
|
nserror fetch_resource_register(void)
|
2011-02-24 01:27:55 +03:00
|
|
|
{
|
2014-01-25 00:08:41 +04:00
|
|
|
lwc_string *scheme = lwc_string_ref(corestring_lwc_resource);
|
2014-06-19 21:27:24 +04:00
|
|
|
const struct fetcher_operation_table fetcher_ops = {
|
|
|
|
.initialise = fetch_resource_initialise,
|
|
|
|
.acceptable = fetch_resource_can_fetch,
|
|
|
|
.setup = fetch_resource_setup,
|
|
|
|
.start = fetch_resource_start,
|
|
|
|
.abort = fetch_resource_abort,
|
|
|
|
.free = fetch_resource_free,
|
|
|
|
.poll = fetch_resource_poll,
|
|
|
|
.finalise = fetch_resource_finalise
|
|
|
|
};
|
|
|
|
|
|
|
|
return fetcher_add(scheme, &fetcher_ops);
|
2011-02-24 01:27:55 +03:00
|
|
|
}
|