summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2014-08-25 15:58:56 -0700
committerVincent Sanders <vince@kyllikki.org>2014-08-25 15:59:52 -0700
commit37755fb135f9e582bd35ef6d282515ee71d619f2 (patch)
tree05f74bda80ba63bde9c97948879ae1ab83585316
parent5b815861f36700548c124102eb9f0e986dac8a20 (diff)
downloadnetsurf-37755fb135f9e582bd35ef6d282515ee71d619f2.tar.gz
netsurf-37755fb135f9e582bd35ef6d282515ee71d619f2.tar.bz2
add incomplete scheme fetcher for javascript urls
-rw-r--r--content/fetch.c2
-rw-r--r--content/fetchers.h21
-rw-r--r--javascript/Makefile4
-rw-r--r--javascript/fetcher.c238
-rw-r--r--javascript/fetcher.h28
-rw-r--r--utils/corestrings.c3
-rw-r--r--utils/corestrings.h1
7 files changed, 290 insertions, 7 deletions
diff --git a/content/fetch.c b/content/fetch.c
index 6948fcbea..e10f0abcb 100644
--- a/content/fetch.c
+++ b/content/fetch.c
@@ -57,6 +57,7 @@
#include "content/fetchers/curl.h"
#include "content/fetchers/data.h"
#include "content/fetchers/file.h"
+#include "javascript/fetcher.h"
#include "content/urldb.h"
/* Define this to turn on verbose fetch logging */
@@ -291,6 +292,7 @@ nserror fetcher_init(void)
fetch_file_register();
fetch_resource_register();
fetch_about_register();
+ fetch_javascript_register();
return NSERROR_OK;
}
diff --git a/content/fetchers.h b/content/fetchers.h
index d123de986..95034bbff 100644
--- a/content/fetchers.h
+++ b/content/fetchers.h
@@ -16,7 +16,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-/** \file content/fetchers.h
+/**
+ * \file content/fetchers.h
+ *
* Interface for fetchers factory.
*/
@@ -34,6 +36,15 @@ struct fetch;
* Fetcher operations API
*
* These are the operations a fetcher must implement.
+ *
+ * Each fetcher is called once for initialisaion and finalisation.
+ * The poll entry point will be called to allow all active fetches to progress.
+ * The flow of a fetch operation is:
+ * URL is checked for aceptability.
+ * setup with all applicable data.
+ * start is called before teh first poll
+ * after completion or abort it is freed
+ *
*/
struct fetcher_operation_table {
/**
@@ -44,7 +55,7 @@ struct fetcher_operation_table {
bool (*initialise)(lwc_string *scheme);
/**
- * can this fetcher accept a url.
+ * Can this fetcher accept a url.
*
* \param url the URL to check
* \return true if the fetcher can handle the url else false.
@@ -80,7 +91,7 @@ struct fetcher_operation_table {
void (*poll)(lwc_string *scheme);
/**
- * finalise the fetcher.
+ * Finalise the fetcher.
*/
void (*finalise)(lwc_string *scheme);
};
@@ -97,9 +108,9 @@ nserror fetcher_add(lwc_string *scheme, const struct fetcher_operation_table *op
/**
- * Initialise the fetchers.
+ * Initialise all registered fetchers.
*
- * @return NSERROR_OK or error code
+ * \return NSERROR_OK or error code
*/
nserror fetcher_init(void);
diff --git a/javascript/Makefile b/javascript/Makefile
index 693ed6541..04eed66b6 100644
--- a/javascript/Makefile
+++ b/javascript/Makefile
@@ -54,12 +54,12 @@ ifeq ($(WANT_JS_SOURCE),YES)
S_JSAPI :=
-S_JAVASCRIPT += content.c jsapi.c $(addprefix jsapi/,$(S_JSAPI))
+S_JAVASCRIPT += content.c jsapi.c fetcher.c $(addprefix jsapi/,$(S_JSAPI))
$(eval $(foreach V,$(filter JSAPI_BINDING_%,$(.VARIABLES)),$(call convert_jsapi_binding,$($(V)),$(OBJROOT)/$(patsubst JSAPI_BINDING_%,%,$(V)).c,$(OBJROOT)/$(patsubst JSAPI_BINDING_%,%,$(V)).h,$(patsubst JSAPI_BINDING_%,%,$(V))_jsapi)))
else
-S_JAVASCRIPT += none.c
+S_JAVASCRIPT += none.c fetcher.c
endif
S_JAVASCRIPT := $(addprefix javascript/,$(S_JAVASCRIPT)) $(S_JSAPI_BINDING) \ No newline at end of file
diff --git a/javascript/fetcher.c b/javascript/fetcher.c
new file mode 100644
index 000000000..690751cd3
--- /dev/null
+++ b/javascript/fetcher.c
@@ -0,0 +1,238 @@
+/*
+ * Copyright 2012 Vincent Sanders <vince@kyllikki.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/>.
+ */
+
+/** \file
+ * implementation for javascript scheme fetcher
+ *
+ * This fetcher implements http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#javascript-protocol
+ */
+
+#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>
+
+#include <libwapcaplet/libwapcaplet.h>
+
+#include "utils/config.h"
+#include "utils/errors.h"
+#include "utils/corestrings.h"
+#include "utils/nsoption.h"
+#include "utils/log.h"
+#include "utils/messages.h"
+#include "utils/utils.h"
+#include "utils/ring.h"
+#include "desktop/gui_factory.h"
+
+#include "content/fetch.h"
+#include "content/fetchers.h"
+#include "javascript/fetcher.h"
+#include "content/urldb.h"
+
+/** Context for an resource fetch */
+struct fetch_javascript_context {
+ struct fetch_javascript_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 */
+
+ nsurl *url;
+};
+
+static struct fetch_javascript_context *ring = NULL;
+
+
+/** issue fetch callbacks with locking */
+static inline bool fetch_javascript_send_callback(const fetch_msg *msg,
+ struct fetch_javascript_context *ctx)
+{
+ ctx->locked = true;
+ fetch_send_callback(msg, ctx->fetchh);
+ ctx->locked = false;
+
+ return ctx->aborted;
+}
+
+
+/**
+ * called from poll to progress fetch.
+ *
+ * \todo This is currently completely unimplemented and just returns 204
+ */
+static bool fetch_javascript_handler(struct fetch_javascript_context *ctx)
+{
+ fetch_msg msg;
+ int code = 204;
+
+ /* content is going to return error code */
+ fetch_set_http_code(ctx->fetchh, code);
+
+ msg.type = FETCH_FINISHED;
+ fetch_javascript_send_callback(&msg, ctx);
+
+ return true;
+}
+
+
+
+/** callback to initialise the resource fetcher. */
+static bool fetch_javascript_initialise(lwc_string *scheme)
+{
+ return true;
+}
+
+/** callback to finalise the resource fetcher. */
+static void fetch_javascript_finalise(lwc_string *scheme)
+{
+}
+
+static bool fetch_javascript_can_fetch(const nsurl *url)
+{
+ return true;
+}
+
+/** callback to set up a resource fetch context. */
+static void *
+fetch_javascript_setup(struct fetch *fetchh,
+ nsurl *url,
+ bool only_2xx,
+ bool downgrade_tls,
+ const char *post_urlenc,
+ const struct fetch_multipart_data *post_multipart,
+ const char **headers)
+{
+ struct fetch_javascript_context *ctx;
+
+ ctx = calloc(1, sizeof(*ctx));
+ if (ctx == NULL)
+ return NULL;
+
+ ctx->url = nsurl_ref(url);
+
+ ctx->fetchh = fetchh;
+
+ RING_INSERT(ring, ctx);
+
+ return ctx;
+}
+
+/** callback to free a resource fetch */
+static void fetch_javascript_free(void *ctx)
+{
+ struct fetch_javascript_context *c = ctx;
+ if (c->url != NULL) {
+ nsurl_unref(c->url);
+ }
+ RING_REMOVE(ring, c);
+ free(ctx);
+}
+
+/** callback to start a resource fetch */
+static bool fetch_javascript_start(void *ctx)
+{
+ return true;
+}
+
+/** callback to abort a resource fetch */
+static void fetch_javascript_abort(void *ctx)
+{
+ struct fetch_javascript_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 */
+static void fetch_javascript_poll(lwc_string *scheme)
+{
+ struct fetch_javascript_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) {
+ next = c->r_next;
+ continue;
+ }
+
+ /* Only process non-aborted fetches */
+ if (c->aborted == false) {
+ /* resource fetches can be processed in one go */
+ fetch_javascript_handler(c);
+ }
+
+ /* Compute next fetch item at the last possible moment
+ * as processing this item may have added to the ring
+ */
+ next = c->r_next;
+
+ 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);
+}
+
+/**
+ * Register javascript scheme fetcher with fetcher factory.
+ *
+ * \return NSERROR_OK on success or appropriate error code on faliure.
+*/
+nserror fetch_javascript_register(void)
+{
+ lwc_string *scheme = lwc_string_ref(corestring_lwc_javascript);
+ const struct fetcher_operation_table fetcher_ops = {
+ .initialise = fetch_javascript_initialise,
+ .acceptable = fetch_javascript_can_fetch,
+ .setup = fetch_javascript_setup,
+ .start = fetch_javascript_start,
+ .abort = fetch_javascript_abort,
+ .free = fetch_javascript_free,
+ .poll = fetch_javascript_poll,
+ .finalise = fetch_javascript_finalise
+ };
+
+ return fetcher_add(scheme, &fetcher_ops);
+}
diff --git a/javascript/fetcher.h b/javascript/fetcher.h
new file mode 100644
index 000000000..f39714089
--- /dev/null
+++ b/javascript/fetcher.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2014 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/>.
+ */
+
+/** \file
+ * javascript scheme handler
+ */
+
+#ifndef NETSURF_JAVASCRIPT_FETCHER_H
+#define NETSURF_JAVASCRIPT_FETCHER_H
+
+nserror fetch_javascript_register(void);
+
+#endif
diff --git a/utils/corestrings.c b/utils/corestrings.c
index db6d72e13..cb340e9a6 100644
--- a/utils/corestrings.c
+++ b/utils/corestrings.c
@@ -69,6 +69,7 @@ lwc_string *corestring_lwc_iframe;
lwc_string *corestring_lwc_image;
lwc_string *corestring_lwc_img;
lwc_string *corestring_lwc_input;
+lwc_string *corestring_lwc_javascript;
lwc_string *corestring_lwc_justify;
lwc_string *corestring_lwc_left;
lwc_string *corestring_lwc_li;
@@ -327,6 +328,7 @@ void corestrings_fini(void)
CSS_LWC_STRING_UNREF(image);
CSS_LWC_STRING_UNREF(img);
CSS_LWC_STRING_UNREF(input);
+ CSS_LWC_STRING_UNREF(javascript);
CSS_LWC_STRING_UNREF(justify);
CSS_LWC_STRING_UNREF(left);
CSS_LWC_STRING_UNREF(li);
@@ -610,6 +612,7 @@ nserror corestrings_init(void)
CSS_LWC_STRING_INTERN(image);
CSS_LWC_STRING_INTERN(img);
CSS_LWC_STRING_INTERN(input);
+ CSS_LWC_STRING_INTERN(javascript);
CSS_LWC_STRING_INTERN(justify);
CSS_LWC_STRING_INTERN(left);
CSS_LWC_STRING_INTERN(li);
diff --git a/utils/corestrings.h b/utils/corestrings.h
index 2a1f88b09..b41d1f859 100644
--- a/utils/corestrings.h
+++ b/utils/corestrings.h
@@ -74,6 +74,7 @@ extern lwc_string *corestring_lwc_iframe;
extern lwc_string *corestring_lwc_image;
extern lwc_string *corestring_lwc_img;
extern lwc_string *corestring_lwc_input;
+extern lwc_string *corestring_lwc_javascript;
extern lwc_string *corestring_lwc_justify;
extern lwc_string *corestring_lwc_left;
extern lwc_string *corestring_lwc_li;