2003-06-30 16:44:03 +04:00
|
|
|
/*
|
2007-06-10 21:46:44 +04:00
|
|
|
* Copyright 2006,2007 Daniel Silverstone <dsilvers@digital-scurf.org>
|
2007-05-02 06:38:38 +04:00
|
|
|
* Copyright 2007 James Bursa <bursa@users.sourceforge.net>
|
2003-06-30 16:44:03 +04:00
|
|
|
* Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* 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/>.
|
2003-06-30 16:44:03 +04:00
|
|
|
*/
|
|
|
|
|
2003-09-18 03:27:33 +04:00
|
|
|
/** \file
|
|
|
|
* Fetching of data from a URL (implementation).
|
2003-04-18 01:35:02 +04:00
|
|
|
*
|
2007-01-02 16:07:28 +03:00
|
|
|
* Active fetches are held in the circular linked list ::fetch_ring. There may
|
|
|
|
* be at most ::option_max_fetchers_per_host active requests per Host: header.
|
|
|
|
* There may be at most ::option_max_fetchers active requests overall. Inactive
|
2007-06-10 21:46:44 +04:00
|
|
|
* fetchers are stored in the ::queue_ring waiting for use.
|
2003-02-09 15:58:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2007-05-02 06:38:38 +04:00
|
|
|
#include <errno.h>
|
2003-08-29 16:57:14 +04:00
|
|
|
#include <stdbool.h>
|
2003-03-15 18:53:20 +03:00
|
|
|
#include <string.h>
|
2003-04-18 01:35:02 +04:00
|
|
|
#include <strings.h>
|
2003-02-09 15:58:15 +03:00
|
|
|
#include <time.h>
|
2010-01-29 14:32:53 +03:00
|
|
|
|
2010-12-20 13:32:15 +03:00
|
|
|
#include <libwapcaplet/libwapcaplet.h>
|
|
|
|
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "utils/config.h"
|
|
|
|
#include "content/fetch.h"
|
2011-02-24 01:27:55 +03:00
|
|
|
#include "content/fetchers/resource.h"
|
2011-02-16 02:26:34 +03:00
|
|
|
#include "content/fetchers/about.h"
|
2011-02-16 02:18:10 +03:00
|
|
|
#include "content/fetchers/curl.h"
|
|
|
|
#include "content/fetchers/data.h"
|
|
|
|
#include "content/fetchers/file.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "content/urldb.h"
|
|
|
|
#include "desktop/netsurf.h"
|
|
|
|
#include "desktop/options.h"
|
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/messages.h"
|
2011-09-27 15:07:32 +04:00
|
|
|
#include "utils/nsurl.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "utils/utils.h"
|
2007-06-10 21:46:44 +04:00
|
|
|
#include "utils/ring.h"
|
2003-09-18 03:27:33 +04:00
|
|
|
|
2008-05-29 23:32:31 +04:00
|
|
|
/* Define this to turn on verbose fetch logging */
|
2011-01-06 03:31:54 +03:00
|
|
|
#undef DEBUG_FETCH_VERBOSE
|
2008-05-29 23:32:31 +04:00
|
|
|
|
2003-11-06 22:41:41 +03:00
|
|
|
bool fetch_active; /**< Fetches in progress, please call fetch_poll(). */
|
|
|
|
|
2007-06-10 21:46:44 +04:00
|
|
|
/** Information about a fetcher for a given scheme. */
|
|
|
|
typedef struct scheme_fetcher_s {
|
2011-09-27 01:07:19 +04:00
|
|
|
lwc_string *scheme_name; /**< The scheme. */
|
2011-11-27 18:14:36 +04:00
|
|
|
fetcher_can_fetch can_fetch; /**< Ensure an URL can be fetched. */
|
2007-07-04 22:05:16 +04:00
|
|
|
fetcher_setup_fetch setup_fetch; /**< Set up a fetch. */
|
|
|
|
fetcher_start_fetch start_fetch; /**< Start a fetch. */
|
|
|
|
fetcher_abort_fetch abort_fetch; /**< Abort a fetch. */
|
|
|
|
fetcher_free_fetch free_fetch; /**< Free a fetch. */
|
|
|
|
fetcher_poll_fetcher poll_fetcher; /**< Poll this fetcher. */
|
|
|
|
fetcher_finalise finaliser; /**< Clean up this fetcher. */
|
|
|
|
int refcount; /**< When zero, clean up the fetcher. */
|
|
|
|
struct scheme_fetcher_s *next_fetcher; /**< Next fetcher in the list. */
|
|
|
|
struct scheme_fetcher_s *prev_fetcher; /**< Prev fetcher in the list. */
|
2007-06-10 21:46:44 +04:00
|
|
|
} scheme_fetcher;
|
|
|
|
|
|
|
|
static scheme_fetcher *fetchers = NULL;
|
2006-02-23 18:06:54 +03:00
|
|
|
|
2003-09-18 03:27:33 +04:00
|
|
|
/** Information for a single fetch. */
|
|
|
|
struct fetch {
|
2007-06-10 21:46:44 +04:00
|
|
|
fetch_callback callback;/**< Callback function. */
|
2011-09-27 15:07:32 +04:00
|
|
|
nsurl *url; /**< URL. */
|
|
|
|
nsurl *referer; /**< Referer URL. */
|
2007-01-27 23:58:20 +03:00
|
|
|
bool send_referer; /**< Valid to send the referer */
|
2008-01-30 22:56:41 +03:00
|
|
|
bool verifiable; /**< Transaction is verifiable */
|
2003-09-18 03:27:33 +04:00
|
|
|
void *p; /**< Private data for callback. */
|
2011-09-27 15:07:32 +04:00
|
|
|
lwc_string *host; /**< Host part of URL, interned */
|
2007-01-13 03:19:02 +03:00
|
|
|
long http_code; /**< HTTP response code, or 0. */
|
2007-07-04 22:05:16 +04:00
|
|
|
scheme_fetcher *ops; /**< Fetcher operations for this fetch,
|
|
|
|
NULL if not set. */
|
|
|
|
void *fetcher_handle; /**< The handle for the fetcher. */
|
|
|
|
bool fetch_is_active; /**< This fetch is active. */
|
2006-03-14 17:21:01 +03:00
|
|
|
struct fetch *r_prev; /**< Previous active fetch in ::fetch_ring. */
|
|
|
|
struct fetch *r_next; /**< Next active fetch in ::fetch_ring. */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct fetch *fetch_ring = 0; /**< Ring of active fetches. */
|
2007-01-13 03:19:02 +03:00
|
|
|
static struct fetch *queue_ring = 0; /**< Ring of queued fetches */
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
#define fetch_ref_fetcher(F) F->refcount++
|
|
|
|
static void fetch_unref_fetcher(scheme_fetcher *fetcher);
|
2007-06-10 21:46:44 +04:00
|
|
|
static void fetch_dispatch_jobs(void);
|
2007-07-04 22:05:16 +04:00
|
|
|
static bool fetch_choose_and_dispatch(void);
|
|
|
|
static bool fetch_dispatch_job(struct fetch *fetch);
|
2006-03-14 17:21:01 +03:00
|
|
|
|
2011-09-27 15:07:32 +04:00
|
|
|
/* Static lwc_strings */
|
|
|
|
static lwc_string *fetch_http_lwc;
|
|
|
|
static lwc_string *fetch_https_lwc;
|
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
|
|
|
|
/**
|
2003-09-18 03:27:33 +04:00
|
|
|
* Initialise the fetcher.
|
2003-02-09 15:58:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
void fetch_init(void)
|
|
|
|
{
|
2007-07-04 22:05:16 +04:00
|
|
|
fetch_curl_register();
|
2008-03-05 17:21:29 +03:00
|
|
|
fetch_data_register();
|
2010-09-10 01:45:59 +04:00
|
|
|
fetch_file_register();
|
2011-02-24 01:27:55 +03:00
|
|
|
fetch_resource_register();
|
2011-02-16 02:18:10 +03:00
|
|
|
fetch_about_register();
|
2007-07-04 22:05:16 +04:00
|
|
|
fetch_active = false;
|
2011-09-27 15:07:32 +04:00
|
|
|
|
|
|
|
if (lwc_intern_string("http", SLEN("http"), &fetch_http_lwc) !=
|
|
|
|
lwc_error_ok) {
|
|
|
|
die("Failed to initialise the fetch module "
|
|
|
|
"(couldn't intern \"http\").");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lwc_intern_string("https", SLEN("https"), &fetch_https_lwc) !=
|
|
|
|
lwc_error_ok) {
|
|
|
|
die("Failed to initialise the fetch module "
|
|
|
|
"(couldn't intern \"https\").");
|
|
|
|
}
|
2007-05-02 06:38:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
/**
|
2003-09-18 03:27:33 +04:00
|
|
|
* Clean up for quit.
|
|
|
|
*
|
|
|
|
* Must be called before exiting.
|
2003-02-09 15:58:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
void fetch_quit(void)
|
|
|
|
{
|
2007-07-04 22:05:16 +04:00
|
|
|
while (fetchers != NULL) {
|
|
|
|
if (fetchers->refcount != 1) {
|
|
|
|
LOG(("Fetcher for scheme %s still active?!",
|
2011-09-27 01:07:19 +04:00
|
|
|
lwc_string_data(fetchers->scheme_name)));
|
2007-07-04 22:05:16 +04:00
|
|
|
/* We shouldn't do this, but... */
|
|
|
|
fetchers->refcount = 1;
|
|
|
|
}
|
|
|
|
fetch_unref_fetcher(fetchers);
|
|
|
|
}
|
2011-09-27 15:07:32 +04:00
|
|
|
|
|
|
|
lwc_string_unref(fetch_http_lwc);
|
|
|
|
lwc_string_unref(fetch_https_lwc);
|
2007-07-04 22:05:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-27 01:07:19 +04:00
|
|
|
bool fetch_add_fetcher(lwc_string *scheme,
|
2007-07-04 22:05:16 +04:00
|
|
|
fetcher_initialise initialiser,
|
2011-11-27 18:14:36 +04:00
|
|
|
fetcher_can_fetch can_fetch,
|
2007-07-04 22:05:16 +04:00
|
|
|
fetcher_setup_fetch setup_fetch,
|
|
|
|
fetcher_start_fetch start_fetch,
|
|
|
|
fetcher_abort_fetch abort_fetch,
|
|
|
|
fetcher_free_fetch free_fetch,
|
|
|
|
fetcher_poll_fetcher poll_fetcher,
|
|
|
|
fetcher_finalise finaliser)
|
|
|
|
{
|
|
|
|
scheme_fetcher *new_fetcher;
|
|
|
|
if (!initialiser(scheme))
|
|
|
|
return false;
|
|
|
|
new_fetcher = malloc(sizeof(scheme_fetcher));
|
|
|
|
if (new_fetcher == NULL) {
|
|
|
|
finaliser(scheme);
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-27 01:07:19 +04:00
|
|
|
new_fetcher->scheme_name = scheme;
|
2007-07-04 22:05:16 +04:00
|
|
|
new_fetcher->refcount = 0;
|
2011-11-27 18:14:36 +04:00
|
|
|
new_fetcher->can_fetch = can_fetch;
|
2007-07-04 22:05:16 +04:00
|
|
|
new_fetcher->setup_fetch = setup_fetch;
|
|
|
|
new_fetcher->start_fetch = start_fetch;
|
|
|
|
new_fetcher->abort_fetch = abort_fetch;
|
|
|
|
new_fetcher->free_fetch = free_fetch;
|
|
|
|
new_fetcher->poll_fetcher = poll_fetcher;
|
|
|
|
new_fetcher->finaliser = finaliser;
|
|
|
|
new_fetcher->next_fetcher = fetchers;
|
|
|
|
fetchers = new_fetcher;
|
|
|
|
fetch_ref_fetcher(new_fetcher);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void fetch_unref_fetcher(scheme_fetcher *fetcher)
|
|
|
|
{
|
|
|
|
if (--fetcher->refcount == 0) {
|
|
|
|
fetcher->finaliser(fetcher->scheme_name);
|
2011-09-27 01:07:19 +04:00
|
|
|
lwc_string_unref(fetcher->scheme_name);
|
2007-07-04 22:05:16 +04:00
|
|
|
if (fetcher == fetchers) {
|
|
|
|
fetchers = fetcher->next_fetcher;
|
|
|
|
if (fetchers)
|
|
|
|
fetchers->prev_fetcher = NULL;
|
|
|
|
} else {
|
|
|
|
fetcher->prev_fetcher->next_fetcher =
|
|
|
|
fetcher->next_fetcher;
|
|
|
|
if (fetcher->next_fetcher != NULL)
|
|
|
|
fetcher->next_fetcher->prev_fetcher =
|
|
|
|
fetcher->prev_fetcher;
|
|
|
|
}
|
|
|
|
free(fetcher);
|
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2003-09-18 03:27:33 +04:00
|
|
|
* Start fetching data for the given URL.
|
|
|
|
*
|
|
|
|
* The function returns immediately. The fetch may be queued for later
|
|
|
|
* processing.
|
|
|
|
*
|
|
|
|
* A pointer to an opaque struct fetch is returned, which can be passed to
|
2004-03-27 03:50:58 +03:00
|
|
|
* fetch_abort() to abort the fetch at any time. Returns 0 if memory is
|
|
|
|
* exhausted (or some other fatal error occurred).
|
2003-09-18 03:27:33 +04:00
|
|
|
*
|
|
|
|
* The caller must supply a callback function which is called when anything
|
|
|
|
* interesting happens. The callback function is first called with msg
|
2010-04-08 16:16:52 +04:00
|
|
|
* FETCH_HEADER, with the header in data, then one or more times
|
2003-09-18 03:27:33 +04:00
|
|
|
* with FETCH_DATA with some data for the url, and finally with
|
|
|
|
* FETCH_FINISHED. Alternatively, FETCH_ERROR indicates an error occurred:
|
2010-04-08 16:16:52 +04:00
|
|
|
* data contains an error message. FETCH_REDIRECT may replace the FETCH_HEADER,
|
2003-09-18 03:27:33 +04:00
|
|
|
* FETCH_DATA, FETCH_FINISHED sequence if the server sends a replacement URL.
|
2003-02-09 15:58:15 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-09-27 15:07:32 +04:00
|
|
|
struct fetch * fetch_start(nsurl *url, nsurl *referer,
|
2007-07-04 22:05:16 +04:00
|
|
|
fetch_callback callback,
|
|
|
|
void *p, bool only_2xx, const char *post_urlenc,
|
2010-04-10 21:13:53 +04:00
|
|
|
const struct fetch_multipart_data *post_multipart,
|
2013-01-05 03:13:23 +04:00
|
|
|
bool verifiable, bool downgrade_tls,
|
|
|
|
const char *headers[])
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
2004-03-27 03:50:58 +03:00
|
|
|
struct fetch *fetch;
|
2007-07-04 22:05:16 +04:00
|
|
|
scheme_fetcher *fetcher = fetchers;
|
2011-09-27 15:07:32 +04:00
|
|
|
lwc_string *scheme;
|
|
|
|
bool match;
|
2004-03-27 03:50:58 +03:00
|
|
|
|
|
|
|
fetch = malloc(sizeof (*fetch));
|
2009-05-28 15:56:56 +04:00
|
|
|
if (fetch == NULL)
|
|
|
|
return NULL;
|
2004-03-27 03:50:58 +03:00
|
|
|
|
2009-05-28 15:56:56 +04:00
|
|
|
/* The URL we're fetching must have a scheme */
|
2011-09-27 15:07:32 +04:00
|
|
|
scheme = nsurl_get_component(url, NSURL_SCHEME);
|
2012-10-11 17:38:10 +04:00
|
|
|
assert(scheme != NULL);
|
2006-06-30 02:04:56 +04:00
|
|
|
|
2008-05-29 23:32:31 +04:00
|
|
|
#ifdef DEBUG_FETCH_VERBOSE
|
2011-09-27 15:07:32 +04:00
|
|
|
LOG(("fetch %p, url '%s'", fetch, nsurl_access(url)));
|
2008-05-29 23:32:31 +04:00
|
|
|
#endif
|
2003-04-18 01:35:02 +04:00
|
|
|
|
|
|
|
/* construct a new fetch structure */
|
2003-02-09 15:58:15 +03:00
|
|
|
fetch->callback = callback;
|
2011-09-27 15:07:32 +04:00
|
|
|
fetch->url = nsurl_ref(url);
|
2008-01-30 22:56:41 +03:00
|
|
|
fetch->verifiable = verifiable;
|
2003-02-09 15:58:15 +03:00
|
|
|
fetch->p = p;
|
2006-10-01 17:17:52 +04:00
|
|
|
fetch->http_code = 0;
|
2009-05-28 15:56:56 +04:00
|
|
|
fetch->r_prev = NULL;
|
|
|
|
fetch->r_next = NULL;
|
|
|
|
fetch->referer = NULL;
|
2007-07-08 15:35:53 +04:00
|
|
|
fetch->send_referer = false;
|
|
|
|
fetch->fetcher_handle = NULL;
|
2009-05-28 15:56:56 +04:00
|
|
|
fetch->ops = NULL;
|
2007-07-04 22:05:16 +04:00
|
|
|
fetch->fetch_is_active = false;
|
2011-09-27 15:07:32 +04:00
|
|
|
fetch->host = nsurl_get_component(url, NSURL_HOST);
|
2010-12-20 13:32:15 +03:00
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
if (referer != NULL) {
|
2011-09-27 15:07:32 +04:00
|
|
|
lwc_string *ref_scheme;
|
|
|
|
fetch->referer = nsurl_ref(referer);
|
|
|
|
|
|
|
|
ref_scheme = nsurl_get_component(referer, NSURL_SCHEME);
|
|
|
|
/* Not a problem if referer has no scheme */
|
2009-05-28 15:56:56 +04:00
|
|
|
|
2009-06-19 15:15:06 +04:00
|
|
|
/* Determine whether to send the Referer header */
|
2012-03-22 13:34:34 +04:00
|
|
|
if (nsoption_bool(send_referer) && ref_scheme != NULL) {
|
2009-06-19 15:15:06 +04:00
|
|
|
/* User permits us to send the header
|
|
|
|
* Only send it if:
|
|
|
|
* 1) The fetch and referer schemes match
|
|
|
|
* or 2) The fetch is https and the referer is http
|
|
|
|
*
|
|
|
|
* This ensures that referer information is only sent
|
|
|
|
* across schemes in the special case of an https
|
|
|
|
* request from a page served over http. The inverse
|
|
|
|
* (https -> http) should not send the referer (15.1.3)
|
|
|
|
*/
|
2011-09-27 15:07:32 +04:00
|
|
|
bool match1;
|
|
|
|
bool match2;
|
2012-08-09 20:30:02 +04:00
|
|
|
if (lwc_string_isequal(scheme, ref_scheme, &match) != lwc_error_ok) {
|
|
|
|
match = false;
|
|
|
|
}
|
|
|
|
if (lwc_string_isequal(scheme, fetch_https_lwc, &match1) != lwc_error_ok) {
|
|
|
|
match1 = false;
|
|
|
|
}
|
|
|
|
if (lwc_string_isequal(ref_scheme, fetch_http_lwc, &match2) != lwc_error_ok) {
|
|
|
|
match2= false;
|
|
|
|
}
|
2011-09-27 15:07:32 +04:00
|
|
|
if (match == true || (match1 == true && match2 == true))
|
2009-06-19 15:15:06 +04:00
|
|
|
fetch->send_referer = true;
|
|
|
|
}
|
2011-09-27 15:07:32 +04:00
|
|
|
if (ref_scheme != NULL)
|
|
|
|
lwc_string_unref(ref_scheme);
|
2007-07-04 22:05:16 +04:00
|
|
|
}
|
2003-04-18 01:35:02 +04:00
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
/* Pick the scheme ops */
|
|
|
|
while (fetcher) {
|
2012-10-11 17:38:10 +04:00
|
|
|
if ((lwc_string_isequal(fetcher->scheme_name, scheme,
|
|
|
|
&match) == lwc_error_ok) && (match == true)) {
|
2007-07-04 22:05:16 +04:00
|
|
|
fetch->ops = fetcher;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fetcher = fetcher->next_fetcher;
|
|
|
|
}
|
2007-06-10 22:08:22 +04:00
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
if (fetch->ops == NULL)
|
|
|
|
goto failed;
|
2007-06-10 22:08:22 +04:00
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
/* Got a scheme fetcher, try and set up the fetch */
|
2011-09-29 19:31:54 +04:00
|
|
|
fetch->fetcher_handle = fetch->ops->setup_fetch(fetch, url,
|
2013-01-05 03:13:23 +04:00
|
|
|
only_2xx, downgrade_tls,
|
|
|
|
post_urlenc, post_multipart,
|
|
|
|
headers);
|
2007-06-10 22:08:22 +04:00
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
if (fetch->fetcher_handle == NULL)
|
|
|
|
goto failed;
|
2007-06-10 22:08:22 +04:00
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
/* Rah, got it, so ref the fetcher. */
|
|
|
|
fetch_ref_fetcher(fetch->ops);
|
2007-06-10 22:08:22 +04:00
|
|
|
|
2004-10-02 01:31:55 +04:00
|
|
|
/* these aren't needed past here */
|
2011-09-27 15:07:32 +04:00
|
|
|
lwc_string_unref(scheme);
|
2007-06-10 22:08:22 +04:00
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
/* Dump us in the queue and ask the queue to run. */
|
2006-03-14 17:21:01 +03:00
|
|
|
RING_INSERT(queue_ring, fetch);
|
2006-04-16 14:30:51 +04:00
|
|
|
fetch_dispatch_jobs();
|
2009-05-28 15:56:56 +04:00
|
|
|
|
2004-03-27 03:50:58 +03:00
|
|
|
return fetch;
|
2003-10-26 02:51:45 +04:00
|
|
|
|
2004-03-27 03:50:58 +03:00
|
|
|
failed:
|
2012-10-11 17:38:10 +04:00
|
|
|
lwc_string_unref(scheme);
|
2011-09-27 15:07:32 +04:00
|
|
|
|
|
|
|
if (fetch->host != NULL)
|
|
|
|
lwc_string_unref(fetch->host);
|
|
|
|
if (fetch->url != NULL)
|
|
|
|
nsurl_unref(fetch->url);
|
|
|
|
if (fetch->referer != NULL)
|
|
|
|
nsurl_unref(fetch->referer);
|
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
free(fetch);
|
2009-05-28 15:56:56 +04:00
|
|
|
|
|
|
|
return NULL;
|
2004-03-27 03:50:58 +03:00
|
|
|
}
|
2003-10-25 23:20:13 +04:00
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
|
2006-03-14 17:21:01 +03:00
|
|
|
/**
|
2007-07-04 22:05:16 +04:00
|
|
|
* Dispatch as many jobs as we have room to dispatch.
|
2006-03-14 17:21:01 +03:00
|
|
|
*/
|
2007-07-04 22:05:16 +04:00
|
|
|
void fetch_dispatch_jobs(void)
|
2006-03-14 17:21:01 +03:00
|
|
|
{
|
2007-07-04 22:05:16 +04:00
|
|
|
int all_active, all_queued;
|
2011-09-26 19:22:38 +04:00
|
|
|
#ifdef DEBUG_FETCH_VERBOSE
|
2008-07-29 13:05:36 +04:00
|
|
|
struct fetch *q;
|
|
|
|
struct fetch *f;
|
2011-09-26 19:22:38 +04:00
|
|
|
#endif
|
2007-07-04 22:05:16 +04:00
|
|
|
|
|
|
|
if (!queue_ring)
|
|
|
|
return; /* Nothing to do, the queue is empty */
|
|
|
|
RING_GETSIZE(struct fetch, queue_ring, all_queued);
|
|
|
|
RING_GETSIZE(struct fetch, fetch_ring, all_active);
|
|
|
|
|
2008-05-29 23:32:31 +04:00
|
|
|
#ifdef DEBUG_FETCH_VERBOSE
|
2007-07-04 22:05:16 +04:00
|
|
|
LOG(("queue_ring %i, fetch_ring %i", all_queued, all_active));
|
|
|
|
|
2008-07-29 13:05:36 +04:00
|
|
|
q = queue_ring;
|
2007-07-04 22:05:16 +04:00
|
|
|
if (q) {
|
|
|
|
do {
|
|
|
|
LOG(("queue_ring: %s", q->url));
|
|
|
|
q = q->r_next;
|
|
|
|
} while (q != queue_ring);
|
2006-03-14 17:21:01 +03:00
|
|
|
}
|
2008-07-29 13:05:36 +04:00
|
|
|
f = fetch_ring;
|
2007-07-04 22:05:16 +04:00
|
|
|
if (f) {
|
|
|
|
do {
|
|
|
|
LOG(("fetch_ring: %s", f->url));
|
|
|
|
f = f->r_next;
|
|
|
|
} while (f != fetch_ring);
|
|
|
|
}
|
2011-09-26 19:22:38 +04:00
|
|
|
#endif
|
2007-07-04 22:05:16 +04:00
|
|
|
|
2012-03-22 13:34:34 +04:00
|
|
|
while ( all_queued && all_active < nsoption_int(max_fetchers) ) {
|
2007-07-04 22:05:16 +04:00
|
|
|
/*LOG(("%d queued, %d fetching", all_queued, all_active));*/
|
|
|
|
if (fetch_choose_and_dispatch()) {
|
|
|
|
all_queued--;
|
|
|
|
all_active++;
|
|
|
|
} else {
|
|
|
|
/* Either a dispatch failed or we ran out. Just stop */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fetch_active = (all_active > 0);
|
2008-05-29 23:32:31 +04:00
|
|
|
#ifdef DEBUG_FETCH_VERBOSE
|
2007-07-04 22:05:16 +04:00
|
|
|
LOG(("Fetch ring is now %d elements.", all_active));
|
|
|
|
LOG(("Queue ring is now %d elements.", all_queued));
|
2008-05-29 23:32:31 +04:00
|
|
|
#endif
|
2006-03-14 17:21:01 +03:00
|
|
|
}
|
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
|
2006-03-14 17:21:01 +03:00
|
|
|
/**
|
2007-07-04 22:05:16 +04:00
|
|
|
* Choose and dispatch a single job. Return false if we failed to dispatch
|
|
|
|
* anything.
|
2006-03-14 17:21:01 +03:00
|
|
|
*
|
|
|
|
* We don't check the overall dispatch size here because we're not called unless
|
|
|
|
* there is room in the fetch queue for us.
|
|
|
|
*/
|
2007-07-04 22:05:16 +04:00
|
|
|
bool fetch_choose_and_dispatch(void)
|
2006-03-14 17:21:01 +03:00
|
|
|
{
|
2011-10-02 16:10:02 +04:00
|
|
|
bool same_host;
|
2006-03-14 17:21:01 +03:00
|
|
|
struct fetch *queueitem;
|
|
|
|
queueitem = queue_ring;
|
|
|
|
do {
|
|
|
|
/* We can dispatch the selected item if there is room in the
|
|
|
|
* fetch ring
|
|
|
|
*/
|
|
|
|
int countbyhost;
|
2010-12-20 13:32:15 +03:00
|
|
|
RING_COUNTBYLWCHOST(struct fetch, fetch_ring, countbyhost,
|
2011-09-27 15:07:32 +04:00
|
|
|
queueitem->host);
|
2012-03-22 13:34:34 +04:00
|
|
|
if (countbyhost < nsoption_int(max_fetchers_per_host)) {
|
2006-03-14 17:21:01 +03:00
|
|
|
/* We can dispatch this item in theory */
|
2006-04-16 14:30:51 +04:00
|
|
|
return fetch_dispatch_job(queueitem);
|
2006-03-14 17:21:01 +03:00
|
|
|
}
|
2011-10-02 16:10:02 +04:00
|
|
|
/* skip over other items with the same host */
|
|
|
|
same_host = true;
|
|
|
|
while (same_host == true && queueitem->r_next != queue_ring) {
|
|
|
|
if (lwc_string_isequal(queueitem->host,
|
|
|
|
queueitem->r_next->host, &same_host) ==
|
|
|
|
lwc_error_ok && same_host == true) {
|
|
|
|
queueitem = queueitem->r_next;
|
|
|
|
}
|
|
|
|
}
|
2006-03-14 17:21:01 +03:00
|
|
|
queueitem = queueitem->r_next;
|
|
|
|
} while (queueitem != queue_ring);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
|
2006-03-14 17:21:01 +03:00
|
|
|
/**
|
2007-07-04 22:05:16 +04:00
|
|
|
* Dispatch a single job
|
2006-03-14 17:21:01 +03:00
|
|
|
*/
|
2007-07-04 22:05:16 +04:00
|
|
|
bool fetch_dispatch_job(struct fetch *fetch)
|
2006-03-14 17:21:01 +03:00
|
|
|
{
|
2007-07-04 22:05:16 +04:00
|
|
|
RING_REMOVE(queue_ring, fetch);
|
2008-05-29 23:32:31 +04:00
|
|
|
#ifdef DEBUG_FETCH_VERBOSE
|
2007-07-04 22:05:16 +04:00
|
|
|
LOG(("Attempting to start fetch %p, fetcher %p, url %s", fetch,
|
2011-09-27 15:07:32 +04:00
|
|
|
fetch->fetcher_handle, nsurl_access(fetch->url)));
|
2008-05-29 23:32:31 +04:00
|
|
|
#endif
|
2007-07-04 22:05:16 +04:00
|
|
|
if (!fetch->ops->start_fetch(fetch->fetcher_handle)) {
|
|
|
|
RING_INSERT(queue_ring, fetch); /* Put it back on the end of the queue */
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
RING_INSERT(fetch_ring, fetch);
|
|
|
|
fetch->fetch_is_active = true;
|
|
|
|
return true;
|
2006-03-14 17:21:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
/**
|
2004-06-22 21:37:51 +04:00
|
|
|
* Abort a fetch.
|
2003-02-09 15:58:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
void fetch_abort(struct fetch *f)
|
2004-06-22 21:37:51 +04:00
|
|
|
{
|
|
|
|
assert(f);
|
2008-05-29 23:32:31 +04:00
|
|
|
#ifdef DEBUG_FETCH_VERBOSE
|
2011-09-27 15:07:32 +04:00
|
|
|
LOG(("fetch %p, fetcher %p, url '%s'", f, f->fetcher_handle,
|
|
|
|
nsurl_access(f->url)));
|
2008-05-29 23:32:31 +04:00
|
|
|
#endif
|
2007-07-04 22:05:16 +04:00
|
|
|
f->ops->abort_fetch(f->fetcher_handle);
|
2004-03-27 03:50:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free a fetch structure and associated resources.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void fetch_free(struct fetch *f)
|
|
|
|
{
|
2008-05-29 23:32:31 +04:00
|
|
|
#ifdef DEBUG_FETCH_VERBOSE
|
2007-07-04 22:05:16 +04:00
|
|
|
LOG(("Freeing fetch %p, fetcher %p", f, f->fetcher_handle));
|
2008-05-29 23:32:31 +04:00
|
|
|
#endif
|
2007-07-04 22:05:16 +04:00
|
|
|
f->ops->free_fetch(f->fetcher_handle);
|
|
|
|
fetch_unref_fetcher(f->ops);
|
2011-09-27 15:07:32 +04:00
|
|
|
nsurl_unref(f->url);
|
|
|
|
if (f->referer != NULL)
|
|
|
|
nsurl_unref(f->referer);
|
|
|
|
if (f->host != NULL)
|
|
|
|
lwc_string_unref(f->host);
|
2010-12-20 13:32:15 +03:00
|
|
|
free(f);
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2003-09-18 03:27:33 +04:00
|
|
|
* Do some work on current fetches.
|
2003-02-09 15:58:15 +03:00
|
|
|
*
|
2003-09-18 03:27:33 +04:00
|
|
|
* Must be called regularly to make progress on fetches.
|
2003-02-09 15:58:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
void fetch_poll(void)
|
|
|
|
{
|
2007-07-04 22:05:16 +04:00
|
|
|
scheme_fetcher *fetcher = fetchers;
|
2009-07-30 02:00:07 +04:00
|
|
|
scheme_fetcher *next_fetcher;
|
2007-07-04 22:05:16 +04:00
|
|
|
|
|
|
|
fetch_dispatch_jobs();
|
|
|
|
|
|
|
|
if (!fetch_active)
|
|
|
|
return; /* No point polling, there's no fetch active. */
|
|
|
|
while (fetcher != NULL) {
|
2009-07-29 22:51:35 +04:00
|
|
|
next_fetcher = fetcher->next_fetcher;
|
2011-10-09 21:22:57 +04:00
|
|
|
if (fetcher->poll_fetcher != NULL) {
|
|
|
|
/* LOG(("Polling fetcher for %s",
|
|
|
|
lwc_string_data(fetcher->scheme_name))); */
|
|
|
|
fetcher->poll_fetcher(fetcher->scheme_name);
|
|
|
|
}
|
2009-07-29 22:51:35 +04:00
|
|
|
fetcher = next_fetcher;
|
2007-07-04 22:05:16 +04:00
|
|
|
}
|
2003-10-25 20:22:11 +04:00
|
|
|
}
|
|
|
|
|
2004-04-02 17:51:13 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a URL's scheme can be fetched.
|
|
|
|
*
|
|
|
|
* \param url URL to check
|
|
|
|
* \return true if the scheme is supported
|
|
|
|
*/
|
|
|
|
|
2011-09-27 18:42:45 +04:00
|
|
|
bool fetch_can_fetch(const nsurl *url)
|
2004-04-02 17:51:13 +04:00
|
|
|
{
|
2007-07-04 22:05:16 +04:00
|
|
|
scheme_fetcher *fetcher = fetchers;
|
2011-09-27 01:07:19 +04:00
|
|
|
bool match;
|
2011-09-27 15:07:32 +04:00
|
|
|
lwc_string *scheme = nsurl_get_component(url, NSURL_SCHEME);
|
2011-09-27 01:07:19 +04:00
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
while (fetcher != NULL) {
|
2012-08-09 20:30:02 +04:00
|
|
|
if (lwc_string_isequal(fetcher->scheme_name, scheme, &match) == lwc_error_ok && match == true) {
|
2011-11-27 18:14:36 +04:00
|
|
|
break;
|
2012-08-09 20:30:02 +04:00
|
|
|
}
|
2011-11-27 18:14:36 +04:00
|
|
|
|
2007-07-04 22:05:16 +04:00
|
|
|
fetcher = fetcher->next_fetcher;
|
|
|
|
}
|
2007-06-10 22:08:22 +04:00
|
|
|
|
2011-09-27 01:07:19 +04:00
|
|
|
lwc_string_unref(scheme);
|
|
|
|
|
2011-11-27 18:14:36 +04:00
|
|
|
return fetcher == NULL ? false : fetcher->can_fetch(url);
|
2004-04-02 17:51:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
/**
|
|
|
|
* Change the callback function for a fetch.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void fetch_change_callback(struct fetch *fetch,
|
2007-07-04 22:05:16 +04:00
|
|
|
fetch_callback callback,
|
|
|
|
void *p)
|
2004-06-28 03:24:11 +04:00
|
|
|
{
|
|
|
|
assert(fetch);
|
|
|
|
fetch->callback = callback;
|
|
|
|
fetch->p = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-13 03:19:02 +03:00
|
|
|
/**
|
|
|
|
* Get the HTTP response code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
long fetch_http_code(struct fetch *fetch)
|
|
|
|
{
|
|
|
|
return fetch->http_code;
|
|
|
|
}
|
|
|
|
|
2009-02-16 23:24:54 +03:00
|
|
|
/**
|
|
|
|
* Determine if a fetch was verifiable
|
|
|
|
*
|
|
|
|
* \param fetch Fetch to consider
|
|
|
|
* \return Verifiable status of fetch
|
|
|
|
*/
|
|
|
|
bool fetch_get_verifiable(struct fetch *fetch)
|
|
|
|
{
|
|
|
|
assert(fetch);
|
|
|
|
|
|
|
|
return fetch->verifiable;
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Clone a linked list of fetch_multipart_data.
|
|
|
|
*
|
|
|
|
* \param list List to clone
|
|
|
|
* \return Pointer to head of cloned list, or NULL on failure
|
|
|
|
*/
|
|
|
|
struct fetch_multipart_data *fetch_multipart_data_clone(
|
|
|
|
const struct fetch_multipart_data *list)
|
|
|
|
{
|
|
|
|
struct fetch_multipart_data *clone, *last = NULL;
|
|
|
|
struct fetch_multipart_data *result = NULL;
|
|
|
|
|
|
|
|
for (; list != NULL; list = list->next) {
|
|
|
|
clone = malloc(sizeof(struct fetch_multipart_data));
|
|
|
|
if (clone == NULL) {
|
|
|
|
if (result != NULL)
|
|
|
|
fetch_multipart_data_destroy(result);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
clone->file = list->file;
|
|
|
|
|
|
|
|
clone->name = strdup(list->name);
|
|
|
|
if (clone->name == NULL) {
|
|
|
|
free(clone);
|
|
|
|
if (result != NULL)
|
|
|
|
fetch_multipart_data_destroy(result);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
clone->value = strdup(list->value);
|
|
|
|
if (clone->value == NULL) {
|
|
|
|
free(clone->name);
|
|
|
|
free(clone);
|
|
|
|
if (result != NULL)
|
|
|
|
fetch_multipart_data_destroy(result);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
clone->next = NULL;
|
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
result = clone;
|
|
|
|
else
|
|
|
|
last->next = clone;
|
|
|
|
|
|
|
|
last = clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free a linked list of fetch_multipart_data.
|
|
|
|
*
|
|
|
|
* \param list Pointer to head of list to free
|
|
|
|
*/
|
|
|
|
void fetch_multipart_data_destroy(struct fetch_multipart_data *list)
|
|
|
|
{
|
|
|
|
struct fetch_multipart_data *next;
|
|
|
|
|
|
|
|
for (; list != NULL; list = next) {
|
|
|
|
next = list->next;
|
|
|
|
free(list->name);
|
|
|
|
free(list->value);
|
|
|
|
free(list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-10 21:46:44 +04:00
|
|
|
void
|
2011-11-09 01:51:42 +04:00
|
|
|
fetch_send_callback(const fetch_msg *msg, struct fetch *fetch)
|
2006-02-23 18:06:54 +03:00
|
|
|
{
|
2011-11-09 01:51:42 +04:00
|
|
|
fetch->callback(msg, fetch->p);
|
2006-02-23 18:06:54 +03:00
|
|
|
}
|
|
|
|
|
2007-07-04 22:44:13 +04:00
|
|
|
|
|
|
|
void fetch_remove_from_queues(struct fetch *fetch)
|
2006-02-23 18:06:54 +03:00
|
|
|
{
|
2007-07-04 22:05:16 +04:00
|
|
|
int all_active, all_queued;
|
|
|
|
|
|
|
|
/* Go ahead and free the fetch properly now */
|
2008-05-29 23:32:31 +04:00
|
|
|
#ifdef DEBUG_FETCH_VERBOSE
|
2007-07-04 22:05:16 +04:00
|
|
|
LOG(("Fetch %p, fetcher %p can be freed", fetch, fetch->fetcher_handle));
|
2008-05-29 23:32:31 +04:00
|
|
|
#endif
|
2007-07-04 22:05:16 +04:00
|
|
|
|
|
|
|
if (fetch->fetch_is_active) {
|
|
|
|
RING_REMOVE(fetch_ring, fetch);
|
|
|
|
} else {
|
|
|
|
RING_REMOVE(queue_ring, fetch);
|
|
|
|
}
|
|
|
|
|
2007-06-10 22:12:30 +04:00
|
|
|
RING_GETSIZE(struct fetch, fetch_ring, all_active);
|
|
|
|
RING_GETSIZE(struct fetch, queue_ring, all_queued);
|
2007-07-04 22:05:16 +04:00
|
|
|
|
|
|
|
fetch_active = (all_active > 0);
|
|
|
|
|
2008-05-29 23:32:31 +04:00
|
|
|
#ifdef DEBUG_FETCH_VERBOSE
|
2007-07-04 22:05:16 +04:00
|
|
|
LOG(("Fetch ring is now %d elements.", all_active));
|
|
|
|
LOG(("Queue ring is now %d elements.", all_queued));
|
2008-05-29 23:32:31 +04:00
|
|
|
#endif
|
2006-02-23 18:06:54 +03:00
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2007-07-04 22:44:13 +04:00
|
|
|
|
2007-06-10 21:46:44 +04:00
|
|
|
void
|
|
|
|
fetch_set_http_code(struct fetch *fetch, long http_code)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
2008-05-29 23:32:31 +04:00
|
|
|
#ifdef DEBUG_FETCH_VERBOSE
|
2007-07-04 22:05:16 +04:00
|
|
|
LOG(("Setting HTTP code to %ld", http_code));
|
2008-05-29 23:32:31 +04:00
|
|
|
#endif
|
2007-07-04 22:05:16 +04:00
|
|
|
fetch->http_code = http_code;
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
|
2011-09-27 15:07:32 +04:00
|
|
|
const char *fetch_get_referer_to_send(struct fetch *fetch)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
2007-07-04 22:05:16 +04:00
|
|
|
if (fetch->send_referer)
|
2011-09-27 15:07:32 +04:00
|
|
|
return nsurl_access(fetch->referer);
|
2007-07-04 22:05:16 +04:00
|
|
|
return NULL;
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
2008-01-30 22:56:41 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
fetch_set_cookie(struct fetch *fetch, const char *data)
|
|
|
|
{
|
|
|
|
assert(fetch && data);
|
|
|
|
|
2010-04-08 15:47:05 +04:00
|
|
|
/* If the fetch is unverifiable err on the side of caution and
|
|
|
|
* do not set the cookie */
|
2008-01-30 22:56:41 +03:00
|
|
|
|
2010-04-08 15:47:05 +04:00
|
|
|
if (fetch->verifiable) {
|
2008-01-30 22:56:41 +03:00
|
|
|
/* If the transaction's verifiable, we don't require
|
|
|
|
* that the request uri and the parent domain match,
|
2010-04-08 15:47:05 +04:00
|
|
|
* so don't pass in any referer/parent in this case. */
|
2012-10-08 23:32:57 +04:00
|
|
|
urldb_set_cookie(data, fetch->url, NULL);
|
2010-04-10 16:24:25 +04:00
|
|
|
} else if (fetch->referer != NULL) {
|
|
|
|
/* Permit the cookie to be set if the fetch is unverifiable
|
|
|
|
* and the fetch URI domain matches the referer. */
|
|
|
|
/** \todo Long-term, this needs to be replaced with a
|
|
|
|
* comparison against the origin fetch URI. In the case
|
|
|
|
* where a nested object requests a fetch, the origin URI
|
|
|
|
* is the nested object's parent URI, whereas the referer
|
|
|
|
* for the fetch will be the nested object's URI. */
|
2012-10-08 23:32:57 +04:00
|
|
|
urldb_set_cookie(data, fetch->url, fetch->referer);
|
2008-01-30 22:56:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|