2008-03-05 17:21:29 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Rob Kendrick <rjek@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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* data: URL handling. See http://tools.ietf.org/html/rfc2397 */
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <time.h>
|
2010-01-29 14:32:53 +03:00
|
|
|
|
2008-03-05 17:21:29 +03:00
|
|
|
#include <curl/curl.h> /* for URL unescaping functions */
|
2010-01-29 14:32:53 +03:00
|
|
|
|
2008-03-05 17:21:29 +03:00
|
|
|
#include "utils/config.h"
|
|
|
|
#include "content/fetch.h"
|
2011-02-16 02:18:10 +03:00
|
|
|
#include "content/fetchers/data.h"
|
2008-03-05 17:21:29 +03:00
|
|
|
#include "content/urldb.h"
|
|
|
|
#include "desktop/netsurf.h"
|
|
|
|
#include "desktop/options.h"
|
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/messages.h"
|
|
|
|
#include "utils/url.h"
|
|
|
|
#include "utils/utils.h"
|
|
|
|
#include "utils/ring.h"
|
|
|
|
#include "utils/base64.h"
|
|
|
|
|
|
|
|
struct fetch_data_context {
|
|
|
|
struct fetch *parent_fetch;
|
|
|
|
char *url;
|
|
|
|
char *mimetype;
|
|
|
|
char *data;
|
|
|
|
size_t datalen;
|
|
|
|
bool base64;
|
2008-03-06 14:28:00 +03:00
|
|
|
|
|
|
|
bool aborted;
|
|
|
|
bool locked;
|
2008-03-05 17:21:29 +03:00
|
|
|
|
|
|
|
struct fetch_data_context *r_next, *r_prev;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct fetch_data_context *ring = NULL;
|
|
|
|
|
2008-03-06 01:57:46 +03:00
|
|
|
static CURL *curl;
|
|
|
|
|
2008-03-05 17:21:29 +03:00
|
|
|
static bool fetch_data_initialise(const char *scheme)
|
|
|
|
{
|
|
|
|
LOG(("fetch_data_initialise called for %s", scheme));
|
2008-03-06 01:57:46 +03:00
|
|
|
if ( (curl = curl_easy_init()) == NULL)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
2008-03-05 17:21:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fetch_data_finalise(const char *scheme)
|
|
|
|
{
|
|
|
|
LOG(("fetch_data_finalise called for %s", scheme));
|
2008-03-06 01:57:46 +03:00
|
|
|
curl_easy_cleanup(curl);
|
2008-03-05 17:21:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *fetch_data_setup(struct fetch *parent_fetch, const char *url,
|
|
|
|
bool only_2xx, const char *post_urlenc,
|
2010-04-10 21:13:53 +04:00
|
|
|
const struct fetch_multipart_data *post_multipart,
|
2008-03-05 17:21:29 +03:00
|
|
|
const char **headers)
|
|
|
|
{
|
|
|
|
struct fetch_data_context *ctx = calloc(1, sizeof(*ctx));
|
|
|
|
|
2008-03-05 18:01:42 +03:00
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;
|
2008-03-06 14:28:00 +03:00
|
|
|
|
2008-03-05 17:21:29 +03:00
|
|
|
ctx->parent_fetch = parent_fetch;
|
|
|
|
ctx->url = strdup(url);
|
|
|
|
|
2008-03-06 01:57:46 +03:00
|
|
|
if (ctx->url == NULL) {
|
|
|
|
free(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-06 14:28:00 +03:00
|
|
|
|
|
|
|
RING_INSERT(ring, ctx);
|
2008-03-06 01:57:46 +03:00
|
|
|
|
2008-03-05 17:21:29 +03:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool fetch_data_start(void *ctx)
|
2008-03-06 01:57:46 +03:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fetch_data_free(void *ctx)
|
|
|
|
{
|
|
|
|
struct fetch_data_context *c = ctx;
|
|
|
|
|
|
|
|
free(c->url);
|
|
|
|
free(c->data);
|
|
|
|
free(c->mimetype);
|
|
|
|
RING_REMOVE(ring, c);
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fetch_data_abort(void *ctx)
|
2008-03-05 17:21:29 +03:00
|
|
|
{
|
|
|
|
struct fetch_data_context *c = ctx;
|
2008-03-06 14:28:00 +03:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fetch_data_send_callback(fetch_msg msg,
|
|
|
|
struct fetch_data_context *c, const void *data,
|
2009-12-18 02:55:02 +03:00
|
|
|
unsigned long size, fetch_error_code errorcode)
|
2008-03-06 14:28:00 +03:00
|
|
|
{
|
|
|
|
c->locked = true;
|
2009-12-18 02:55:02 +03:00
|
|
|
fetch_send_callback(msg, c->parent_fetch, data, size, errorcode);
|
2008-03-06 14:28:00 +03:00
|
|
|
c->locked = false;
|
2008-03-06 01:57:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool fetch_data_process(struct fetch_data_context *c)
|
|
|
|
{
|
2008-03-05 17:21:29 +03:00
|
|
|
char *params;
|
|
|
|
char *comma;
|
2008-03-06 01:57:46 +03:00
|
|
|
char *unescaped;
|
2008-10-13 20:39:54 +04:00
|
|
|
int templen;
|
2008-03-05 17:21:29 +03:00
|
|
|
|
|
|
|
/* format of a data: URL is:
|
|
|
|
* data:[<mimetype>][;base64],<data>
|
|
|
|
* The mimetype is optional. If it is missing, the , before the
|
|
|
|
* data must still be there.
|
|
|
|
*/
|
|
|
|
|
2008-03-06 02:09:36 +03:00
|
|
|
LOG(("*** Processing %s", c->url));
|
|
|
|
|
|
|
|
if (strlen(c->url) < 6) {
|
|
|
|
/* 6 is the minimum possible length (data:,) */
|
2008-03-06 14:28:00 +03:00
|
|
|
fetch_data_send_callback(FETCH_ERROR, c,
|
2009-12-18 02:55:02 +03:00
|
|
|
"Malformed data: URL", 0, FETCH_ERROR_URL);
|
2008-03-06 02:09:36 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-03-05 17:21:29 +03:00
|
|
|
/* skip the data: part */
|
2008-05-25 19:51:30 +04:00
|
|
|
params = c->url + SLEN("data:");
|
2008-03-05 17:21:29 +03:00
|
|
|
|
|
|
|
/* find the comma */
|
|
|
|
if ( (comma = strchr(params, ',')) == NULL) {
|
2008-03-06 14:28:00 +03:00
|
|
|
fetch_data_send_callback(FETCH_ERROR, c,
|
2009-12-18 02:55:02 +03:00
|
|
|
"Malformed data: URL", 0, FETCH_ERROR_URL);
|
2008-03-05 17:21:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params[0] == ',') {
|
|
|
|
/* there is no mimetype here, assume text/plain */
|
|
|
|
c->mimetype = strdup("text/plain;charset=US-ASCII");
|
|
|
|
} else {
|
|
|
|
/* make a copy of everything between data: and the comma */
|
|
|
|
c->mimetype = strndup(params, comma - params);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->mimetype == NULL) {
|
2008-03-06 14:28:00 +03:00
|
|
|
fetch_data_send_callback(FETCH_ERROR, c,
|
2008-03-05 17:21:29 +03:00
|
|
|
"Unable to allocate memory for mimetype in data: URL",
|
2009-12-18 02:55:02 +03:00
|
|
|
0, FETCH_ERROR_MEMORY);
|
2008-03-05 17:21:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(c->mimetype + strlen(c->mimetype) - 7, ";base64") == 0) {
|
|
|
|
c->base64 = true;
|
|
|
|
c->mimetype[strlen(c->mimetype) - 7] = '\0';
|
|
|
|
} else {
|
|
|
|
c->base64 = false;
|
|
|
|
}
|
|
|
|
|
2008-03-06 01:57:46 +03:00
|
|
|
/* we URL unescape the data first, just incase some insane page
|
|
|
|
* decides to nest URL and base64 encoding. Like, say, Acid2.
|
|
|
|
*/
|
2008-10-13 20:39:54 +04:00
|
|
|
templen = c->datalen;
|
|
|
|
unescaped = curl_easy_unescape(curl, comma + 1, 0, &templen);
|
|
|
|
c->datalen = templen;
|
|
|
|
if (unescaped == NULL) {
|
2008-03-06 14:28:00 +03:00
|
|
|
fetch_data_send_callback(FETCH_ERROR, c,
|
2009-12-18 02:55:02 +03:00
|
|
|
"Unable to URL decode data: URL", 0,
|
|
|
|
FETCH_ERROR_ENCODING);
|
2008-03-06 01:57:46 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-03-05 17:21:29 +03:00
|
|
|
if (c->base64) {
|
2008-03-06 01:57:46 +03:00
|
|
|
c->data = malloc(c->datalen); /* safe: always gets smaller */
|
|
|
|
if (base64_decode(unescaped, c->datalen, c->data,
|
2008-03-06 14:28:00 +03:00
|
|
|
&(c->datalen)) == false) {
|
|
|
|
fetch_data_send_callback(FETCH_ERROR, c,
|
2009-12-18 02:55:02 +03:00
|
|
|
"Unable to Base64 decode data: URL", 0,
|
|
|
|
FETCH_ERROR_ENCODING);
|
2008-03-06 01:57:46 +03:00
|
|
|
curl_free(unescaped);
|
2008-03-05 17:21:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2008-03-06 01:57:46 +03:00
|
|
|
c->data = malloc(c->datalen);
|
2008-03-05 17:21:29 +03:00
|
|
|
if (c->data == NULL) {
|
2008-03-06 14:28:00 +03:00
|
|
|
fetch_data_send_callback(FETCH_ERROR, c,
|
2009-12-18 02:55:02 +03:00
|
|
|
"Unable to allocate memory for data: URL", 0,
|
|
|
|
FETCH_ERROR_MEMORY);
|
2008-03-06 01:57:46 +03:00
|
|
|
curl_free(unescaped);
|
2008-03-05 17:21:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
2008-03-06 01:57:46 +03:00
|
|
|
memcpy(c->data, unescaped, c->datalen);
|
2008-03-05 17:21:29 +03:00
|
|
|
}
|
|
|
|
|
2008-03-06 01:57:46 +03:00
|
|
|
curl_free(unescaped);
|
|
|
|
|
2008-03-05 17:21:29 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fetch_data_poll(const char *scheme)
|
|
|
|
{
|
2008-03-06 14:28:00 +03:00
|
|
|
struct fetch_data_context *c, *next;
|
2008-03-05 17:21:29 +03:00
|
|
|
|
2008-03-06 01:57:46 +03:00
|
|
|
if (ring == NULL) return;
|
2008-03-05 17:21:29 +03:00
|
|
|
|
2008-03-06 14:28:00 +03:00
|
|
|
/* Iterate over ring, processing each pending fetch */
|
|
|
|
c = ring;
|
|
|
|
do {
|
|
|
|
/* Take a copy of the next pointer as we may destroy
|
|
|
|
* the ring item we're currently processing */
|
|
|
|
next = c->r_next;
|
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only process non-aborted fetches */
|
|
|
|
if (!c->aborted && fetch_data_process(c) == true) {
|
2010-03-28 16:56:39 +04:00
|
|
|
char header[64];
|
|
|
|
|
2008-03-06 14:28:00 +03:00
|
|
|
fetch_set_http_code(c->parent_fetch, 200);
|
2008-03-19 20:36:07 +03:00
|
|
|
LOG(("setting data: MIME type to %s, length to %zd",
|
2008-03-06 14:28:00 +03:00
|
|
|
c->mimetype, c->datalen));
|
|
|
|
/* Any callback can result in the fetch being aborted.
|
|
|
|
* Therefore, we _must_ check for this after _every_
|
|
|
|
* call to fetch_data_send_callback().
|
|
|
|
*/
|
2010-03-28 16:56:39 +04:00
|
|
|
snprintf(header, sizeof header, "Content-Type: %s",
|
|
|
|
c->mimetype);
|
|
|
|
fetch_data_send_callback(FETCH_HEADER, c, header,
|
|
|
|
strlen(header), FETCH_ERROR_NO_ERROR);
|
|
|
|
|
|
|
|
snprintf(header, sizeof header, "Content-Length: %zd",
|
|
|
|
c->datalen);
|
|
|
|
fetch_data_send_callback(FETCH_HEADER, c, header,
|
|
|
|
strlen(header), FETCH_ERROR_NO_ERROR);
|
|
|
|
|
2008-03-06 14:28:00 +03:00
|
|
|
if (!c->aborted) {
|
|
|
|
fetch_data_send_callback(FETCH_DATA,
|
2009-12-18 02:55:02 +03:00
|
|
|
c, c->data, c->datalen,
|
|
|
|
FETCH_ERROR_NO_ERROR);
|
2008-03-06 14:28:00 +03:00
|
|
|
}
|
|
|
|
if (!c->aborted) {
|
|
|
|
fetch_data_send_callback(FETCH_FINISHED,
|
2010-03-28 16:56:39 +04:00
|
|
|
c, 0, 0, FETCH_ERROR_NO_ERROR);
|
2008-03-06 01:57:46 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(("Processing of %s failed!", c->url));
|
2008-03-06 14:28:00 +03:00
|
|
|
|
|
|
|
/* Ensure that we're unlocked here. If we aren't,
|
|
|
|
* then fetch_data_process() is broken.
|
|
|
|
*/
|
|
|
|
assert(c->locked == false);
|
2008-03-06 01:57:46 +03:00
|
|
|
}
|
2008-03-06 14:28:00 +03:00
|
|
|
|
|
|
|
fetch_remove_from_queues(c->parent_fetch);
|
|
|
|
fetch_free(c->parent_fetch);
|
|
|
|
|
|
|
|
/* 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);
|
2008-03-05 17:21:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void fetch_data_register(void)
|
|
|
|
{
|
|
|
|
fetch_add_fetcher("data",
|
|
|
|
fetch_data_initialise,
|
|
|
|
fetch_data_setup,
|
|
|
|
fetch_data_start,
|
|
|
|
fetch_data_abort,
|
|
|
|
fetch_data_free,
|
|
|
|
fetch_data_poll,
|
|
|
|
fetch_data_finalise);
|
|
|
|
}
|