aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/http_static
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/http_static')
-rw-r--r--src/plugins/http_static/CMakeLists.txt4
-rw-r--r--src/plugins/http_static/FEATURE.yaml20
-rw-r--r--src/plugins/http_static/builtinurl/json_urls.c192
-rw-r--r--src/plugins/http_static/http_cache.c450
-rw-r--r--src/plugins/http_static/http_cache.h78
-rw-r--r--src/plugins/http_static/http_static.c99
-rw-r--r--src/plugins/http_static/http_static.h226
-rw-r--r--src/plugins/http_static/static_server.c1737
8 files changed, 1400 insertions, 1406 deletions
diff --git a/src/plugins/http_static/CMakeLists.txt b/src/plugins/http_static/CMakeLists.txt
index f9ccb15beae..5e51704dc96 100644
--- a/src/plugins/http_static/CMakeLists.txt
+++ b/src/plugins/http_static/CMakeLists.txt
@@ -14,9 +14,11 @@
add_vpp_plugin(http_static
SOURCES
+ http_cache.c
+ http_cache.h
http_static.c
static_server.c
- http_static.h
+ builtinurl/json_urls.c
API_FILES
http_static.api
diff --git a/src/plugins/http_static/FEATURE.yaml b/src/plugins/http_static/FEATURE.yaml
index d40855f2de2..ff4e147c495 100644
--- a/src/plugins/http_static/FEATURE.yaml
+++ b/src/plugins/http_static/FEATURE.yaml
@@ -1,10 +1,18 @@
---
-name: Static http https server
-maintainer: Dave Barach <dave@barachs.net>
+name: Static HTTP(S) Server
+maintainer:
+ - Dave Barach <dave@barachs.net>
+ - Florin Coras <fcoras@cisco.com>
features:
- - An extensible static http/https server with caching
-description: "A simple caching static http / https server
- A built-in vpp host stack application.
- Supports HTTP GET and HTTP POST methods."
+ - HTTP GET/POST handling
+ - LRU file caching
+ - pluggable URL handlers
+ - builtin json URL handles:
+ - version.json - vpp version info
+ - interface_list.json - list of interfaces
+ - interface_stats - single interface via HTTP POST
+ - interface_stats - all intfcs via HTTP GET."
+description: "Static HTTP(S) server implemented as a
+ built-in vpp host stack application. "
state: production
properties: [API, CLI, MULTITHREAD]
diff --git a/src/plugins/http_static/builtinurl/json_urls.c b/src/plugins/http_static/builtinurl/json_urls.c
new file mode 100644
index 00000000000..808893aac79
--- /dev/null
+++ b/src/plugins/http_static/builtinurl/json_urls.c
@@ -0,0 +1,192 @@
+/*
+ * Copyright (c) 2019 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <http_static/http_static.h>
+#include <vpp/app/version.h>
+
+hss_url_handler_rc_t
+handle_get_version (hss_url_handler_args_t *args)
+{
+ u8 *s = 0;
+
+ s = format (s, "{\"vpp_details\": {");
+ s = format (s, " \"version\": \"%s\",", VPP_BUILD_VER);
+ s = format (s, " \"build_date\": \"%s\"}}\r\n", VPP_BUILD_DATE);
+
+ args->data = s;
+ args->data_len = vec_len (s);
+ args->free_vec_data = 1;
+ return HSS_URL_HANDLER_OK;
+}
+
+void
+trim_path_from_request (u8 *s, char *path)
+{
+ u8 *cp;
+ int trim_length = strlen (path) + 1 /* remove '?' */;
+
+ /* Get rid of the path and question-mark */
+ vec_delete (s, trim_length, 0);
+
+ /* Tail trim irrelevant browser info */
+ cp = s;
+ while ((cp - s) < vec_len (s))
+ {
+ if (*cp == ' ')
+ {
+ /*
+ * Makes request a vector which happens to look
+ * like a c-string.
+ */
+ *cp = 0;
+ vec_set_len (s, cp - s);
+ break;
+ }
+ cp++;
+ }
+}
+
+hss_url_handler_rc_t
+handle_get_interface_stats (hss_url_handler_args_t *args)
+{
+ u8 *s = 0, *stats = 0;
+ uword *p;
+ u32 *sw_if_indices = 0;
+ vnet_hw_interface_t *hi;
+ vnet_sw_interface_t *si;
+ char *q = "\"";
+ int i;
+ int need_comma = 0;
+ u8 *format_vnet_sw_interface_cntrs (u8 * s, vnet_interface_main_t * im,
+ vnet_sw_interface_t * si, int json);
+ vnet_main_t *vnm = vnet_get_main ();
+ vnet_interface_main_t *im = &vnm->interface_main;
+
+ /* Get stats for a single interface via http POST */
+ if (args->reqtype == HTTP_REQ_POST)
+ {
+ trim_path_from_request (args->request, "interface_stats.json");
+
+ /* Find the sw_if_index */
+ p = hash_get (im->hw_interface_by_name, args->request);
+ if (!p)
+ {
+ s = format (s, "{\"interface_stats\": {[\n");
+ s = format (s, " \"name\": \"%s\",", args->request);
+ s = format (s, " \"error\": \"%s\"", "UnknownInterface");
+ s = format (s, "]}\n");
+ goto out;
+ }
+
+ vec_add1 (sw_if_indices, p[0]);
+ }
+ else /* default, HTTP_BUILTIN_METHOD_GET */
+ {
+ pool_foreach (hi, im->hw_interfaces)
+ {
+ vec_add1 (sw_if_indices, hi->sw_if_index);
+ }
+ }
+
+ s = format (s, "{%sinterface_stats%s: [\n", q, q);
+
+ for (i = 0; i < vec_len (sw_if_indices); i++)
+ {
+ si = vnet_get_sw_interface (vnm, sw_if_indices[i]);
+ if (need_comma)
+ s = format (s, ",\n");
+
+ need_comma = 1;
+
+ s = format (s, "{%sname%s: %s%U%s, ", q, q, q,
+ format_vnet_sw_if_index_name, vnm, sw_if_indices[i], q);
+
+ stats = format_vnet_sw_interface_cntrs (stats, &vnm->interface_main, si,
+ 1 /* want json */);
+ if (vec_len (stats))
+ s = format (s, "%v}", stats);
+ else
+ s = format (s, "%snone%s: %strue%s}", q, q, q, q);
+ vec_reset_length (stats);
+ }
+
+ s = format (s, "]}\n");
+
+out:
+ args->data = s;
+ args->data_len = vec_len (s);
+ args->free_vec_data = 1;
+ vec_free (sw_if_indices);
+ vec_free (stats);
+ return HSS_URL_HANDLER_OK;
+}
+
+hss_url_handler_rc_t
+handle_get_interface_list (hss_url_handler_args_t *args)
+{
+ u8 *s = 0;
+ int i;
+ vnet_main_t *vnm = vnet_get_main ();
+ vnet_interface_main_t *im = &vnm->interface_main;
+ vnet_hw_interface_t *hi;
+ u32 *hw_if_indices = 0;
+ int need_comma = 0;
+
+ /* Construct vector of active hw_if_indexes ... */
+ pool_foreach (hi, im->hw_interfaces)
+ {
+ /* No point in mentioning "local0"... */
+ if (hi - im->hw_interfaces)
+ vec_add1 (hw_if_indices, hi - im->hw_interfaces);
+ }
+
+ /* Build answer */
+ s = format (s, "{\"interface_list\": [\n");
+ for (i = 0; i < vec_len (hw_if_indices); i++)
+ {
+ if (need_comma)
+ s = format (s, ",\n");
+ hi = pool_elt_at_index (im->hw_interfaces, hw_if_indices[i]);
+ s = format (s, "\"%v\"", hi->name);
+ need_comma = 1;
+ }
+ s = format (s, "]}\n");
+ vec_free (hw_if_indices);
+
+ args->data = s;
+ args->data_len = vec_len (s);
+ args->free_vec_data = 1;
+ return HSS_URL_HANDLER_OK;
+}
+
+void
+hss_builtinurl_json_handlers_init (void)
+{
+ hss_register_url_handler (handle_get_version, "version.json", HTTP_REQ_GET);
+ hss_register_url_handler (handle_get_interface_list, "interface_list.json",
+ HTTP_REQ_GET);
+ hss_register_url_handler (handle_get_interface_stats, "interface_stats.json",
+ HTTP_REQ_GET);
+ hss_register_url_handler (handle_get_interface_stats, "interface_stats.json",
+ HTTP_REQ_POST);
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/http_static/http_cache.c b/src/plugins/http_static/http_cache.c
new file mode 100644
index 00000000000..8b9751b7f78
--- /dev/null
+++ b/src/plugins/http_static/http_cache.c
@@ -0,0 +1,450 @@
+/*
+ * Copyright (c) 2022 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <http_static/http_cache.h>
+#include <vppinfra/bihash_template.c>
+#include <vppinfra/unix.h>
+#include <vlib/vlib.h>
+
+static void
+hss_cache_lock (hss_cache_t *hc)
+{
+ clib_spinlock_lock (&hc->cache_lock);
+}
+
+static void
+hss_cache_unlock (hss_cache_t *hc)
+{
+ clib_spinlock_unlock (&hc->cache_lock);
+}
+
+/** \brief Sanity-check the forward and reverse LRU lists
+ */
+static inline void
+lru_validate (hss_cache_t *hc)
+{
+#if CLIB_DEBUG > 0
+ f64 last_timestamp;
+ u32 index;
+ int i;
+ hss_cache_entry_t *ce;
+
+ last_timestamp = 1e70;
+ for (i = 1, index = hc->first_index; index != ~0;)
+ {
+ ce = pool_elt_at_index (hc->cache_pool, index);
+ /* Timestamps should be smaller (older) as we walk the fwd list */
+ if (ce->last_used > last_timestamp)
+ {
+ clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f", index,
+ i, ce->last_used, last_timestamp);
+ }
+ index = ce->next_index;
+ last_timestamp = ce->last_used;
+ i++;
+ }
+
+ last_timestamp = 0.0;
+ for (i = 1, index = hc->last_index; index != ~0;)
+ {
+ ce = pool_elt_at_index (hc->cache_pool, index);
+ /* Timestamps should be larger (newer) as we walk the rev list */
+ if (ce->last_used < last_timestamp)
+ {
+ clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f", index,
+ i, ce->last_used, last_timestamp);
+ }
+ index = ce->prev_index;
+ last_timestamp = ce->last_used;
+ i++;
+ }
+#endif
+}
+
+/** \brief Remove a data cache entry from the LRU lists
+ */
+static inline void
+lru_remove (hss_cache_t *hc, hss_cache_entry_t *ce)
+{
+ hss_cache_entry_t *next_ep, *prev_ep;
+ u32 ce_index;
+
+ lru_validate (hc);
+
+ ce_index = ce - hc->cache_pool;
+
+ /* Deal with list heads */
+ if (ce_index == hc->first_index)
+ hc->first_index = ce->next_index;
+ if (ce_index == hc->last_index)
+ hc->last_index = ce->prev_index;
+
+ /* Fix next->prev */
+ if (ce->next_index != ~0)
+ {
+ next_ep = pool_elt_at_index (hc->cache_pool, ce->next_index);
+ next_ep->prev_index = ce->prev_index;
+ }
+ /* Fix prev->next */
+ if (ce->prev_index != ~0)
+ {
+ prev_ep = pool_elt_at_index (hc->cache_pool, ce->prev_index);
+ prev_ep->next_index = ce->next_index;
+ }
+ lru_validate (hc);
+}
+
+/** \brief Add an entry to the LRU lists, tag w/ supplied timestamp
+ */
+static inline void
+lru_add (hss_cache_t *hc, hss_cache_entry_t *ce, f64 now)
+{
+ hss_cache_entry_t *next_ce;
+ u32 ce_index;
+
+ lru_validate (hc);
+
+ ce_index = ce - hc->cache_pool;
+
+ /*
+ * Re-add at the head of the forward LRU list,
+ * tail of the reverse LRU list
+ */
+ if (hc->first_index != ~0)
+ {
+ next_ce = pool_elt_at_index (hc->cache_pool, hc->first_index);
+ next_ce->prev_index = ce_index;
+ }
+
+ ce->prev_index = ~0;
+
+ /* ep now the new head of the LRU forward list */
+ ce->next_index = hc->first_index;
+ hc->first_index = ce_index;
+
+ /* single session case: also the tail of the reverse LRU list */
+ if (hc->last_index == ~0)
+ hc->last_index = ce_index;
+ ce->last_used = now;
+
+ lru_validate (hc);
+}
+
+/** \brief Remove and re-add a cache entry from/to the LRU lists
+ */
+static inline void
+lru_update (hss_cache_t *hc, hss_cache_entry_t *ep, f64 now)
+{
+ lru_remove (hc, ep);
+ lru_add (hc, ep, now);
+}
+
+static void
+hss_cache_attach_entry (hss_cache_t *hc, u32 ce_index, u8 **data,
+ u64 *data_len)
+{
+ hss_cache_entry_t *ce;
+
+ /* Expect ce_index to be validated outside */
+ ce = pool_elt_at_index (hc->cache_pool, ce_index);
+ ce->inuse++;
+ *data = ce->data;
+ *data_len = vec_len (ce->data);
+
+ /* Update the cache entry, mark it in-use */
+ lru_update (hc, ce, vlib_time_now (vlib_get_main ()));
+
+ if (hc->debug_level > 1)
+ clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);
+}
+
+/** \brief Detach cache entry from session
+ */
+void
+hss_cache_detach_entry (hss_cache_t *hc, u32 ce_index)
+{
+ hss_cache_entry_t *ce;
+
+ hss_cache_lock (hc);
+
+ ce = pool_elt_at_index (hc->cache_pool, ce_index);
+ ce->inuse--;
+
+ if (hc->debug_level > 1)
+ clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);
+
+ hss_cache_unlock (hc);
+}
+
+static u32
+hss_cache_lookup (hss_cache_t *hc, u8 *path)
+{
+ BVT (clib_bihash_kv) kv;
+ int rv;
+
+ kv.key = (u64) path;
+ kv.value = ~0;
+
+ /* Value updated only if lookup succeeds */
+ rv = BV (clib_bihash_search) (&hc->name_to_data, &kv, &kv);
+ ASSERT (!rv || kv.value == ~0);
+
+ if (hc->debug_level > 1)
+ clib_warning ("lookup '%s' %s", kv.key, kv.value == ~0 ? "fail" : "found");
+
+ return kv.value;
+}
+
+u32
+hss_cache_lookup_and_attach (hss_cache_t *hc, u8 *path, u8 **data,
+ u64 *data_len)
+{
+ u32 ce_index;
+
+ /* Make sure nobody removes the entry while we look it up */
+ hss_cache_lock (hc);
+
+ ce_index = hss_cache_lookup (hc, path);
+ if (ce_index != ~0)
+ hss_cache_attach_entry (hc, ce_index, data, data_len);
+
+ hss_cache_unlock (hc);
+
+ return ce_index;
+}
+
+static void
+hss_cache_do_evictions (hss_cache_t *hc)
+{
+ BVT (clib_bihash_kv) kv;
+ hss_cache_entry_t *ce;
+ u32 free_index;
+
+ free_index = hc->last_index;
+
+ while (free_index != ~0)
+ {
+ /* pick the LRU */
+ ce = pool_elt_at_index (hc->cache_pool, free_index);
+ /* Which could be in use... */
+ if (ce->inuse)
+ {
+ if (hc->debug_level > 1)
+ clib_warning ("index %d in use refcnt %d", free_index, ce->inuse);
+ }
+ free_index = ce->prev_index;
+ kv.key = (u64) (ce->filename);
+ kv.value = ~0ULL;
+ if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 0 /* is_add */) <
+ 0)
+ {
+ clib_warning ("LRU delete '%s' FAILED!", ce->filename);
+ }
+ else if (hc->debug_level > 1)
+ clib_warning ("LRU delete '%s' ok", ce->filename);
+
+ lru_remove (hc, ce);
+ hc->cache_size -= vec_len (ce->data);
+ hc->cache_evictions++;
+ vec_free (ce->filename);
+ vec_free (ce->data);
+
+ if (hc->debug_level > 1)
+ clib_warning ("pool put index %d", ce - hc->cache_pool);
+
+ pool_put (hc->cache_pool, ce);
+ if (hc->cache_size < hc->cache_limit)
+ break;
+ }
+}
+
+u32
+hss_cache_add_and_attach (hss_cache_t *hc, u8 *path, u8 **data, u64 *data_len)
+{
+ BVT (clib_bihash_kv) kv;
+ hss_cache_entry_t *ce;
+ clib_error_t *error;
+ u8 *file_data;
+ u32 ce_index;
+
+ hss_cache_lock (hc);
+
+ /* Need to recycle one (or more cache) entries? */
+ if (hc->cache_size > hc->cache_limit)
+ hss_cache_do_evictions (hc);
+
+ /* Read the file */
+ error = clib_file_contents ((char *) path, &file_data);
+ if (error)
+ {
+ clib_warning ("Error reading '%s'", path);
+ clib_error_report (error);
+ return ~0;
+ }
+
+ /* Create a cache entry for it */
+ pool_get_zero (hc->cache_pool, ce);
+ ce->filename = vec_dup (path);
+ ce->data = file_data;
+
+ /* Attach cache entry without additional lock */
+ ce->inuse++;
+ *data = file_data;
+ *data_len = vec_len (file_data);
+ lru_add (hc, ce, vlib_time_now (vlib_get_main ()));
+
+ hc->cache_size += vec_len (ce->data);
+ ce_index = ce - hc->cache_pool;
+
+ if (hc->debug_level > 1)
+ clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);
+
+ /* Add to the lookup table */
+
+ kv.key = (u64) vec_dup (path);
+ kv.value = ce_index;
+
+ if (hc->debug_level > 1)
+ clib_warning ("add '%s' value %lld", kv.key, kv.value);
+
+ if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 1 /* is_add */) < 0)
+ {
+ clib_warning ("BUG: add failed!");
+ }
+
+ hss_cache_unlock (hc);
+
+ return ce_index;
+}
+
+u32
+hss_cache_clear (hss_cache_t *hc)
+{
+ u32 free_index, busy_items = 0;
+ hss_cache_entry_t *ce;
+ BVT (clib_bihash_kv) kv;
+
+ hss_cache_lock (hc);
+
+ /* Walk the LRU list to find active entries */
+ free_index = hc->last_index;
+ while (free_index != ~0)
+ {
+ ce = pool_elt_at_index (hc->cache_pool, free_index);
+ free_index = ce->prev_index;
+ /* Which could be in use... */
+ if (ce->inuse)
+ {
+ busy_items++;
+ free_index = ce->next_index;
+ continue;
+ }
+ kv.key = (u64) (ce->filename);
+ kv.value = ~0ULL;
+ if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 0 /* is_add */) <
+ 0)
+ {
+ clib_warning ("BUG: cache clear delete '%s' FAILED!", ce->filename);
+ }
+
+ lru_remove (hc, ce);
+ hc->cache_size -= vec_len (ce->data);
+ hc->cache_evictions++;
+ vec_free (ce->filename);
+ vec_free (ce->data);
+ if (hc->debug_level > 1)
+ clib_warning ("pool put index %d", ce - hc->cache_pool);
+ pool_put (hc->cache_pool, ce);
+ free_index = hc->last_index;
+ }
+
+ hss_cache_unlock (hc);
+
+ return busy_items;
+}
+
+void
+hss_cache_init (hss_cache_t *hc, uword cache_size, u8 debug_level)
+{
+ clib_spinlock_init (&hc->cache_lock);
+
+ /* Init path-to-cache hash table */
+ BV (clib_bihash_init) (&hc->name_to_data, "http cache", 128, 32 << 20);
+
+ hc->cache_limit = cache_size;
+ hc->debug_level = debug_level;
+ hc->first_index = hc->last_index = ~0;
+}
+
+/** \brief format a file cache entry
+ */
+static u8 *
+format_hss_cache_entry (u8 *s, va_list *args)
+{
+ hss_cache_entry_t *ep = va_arg (*args, hss_cache_entry_t *);
+ f64 now = va_arg (*args, f64);
+
+ /* Header */
+ if (ep == 0)
+ {
+ s = format (s, "%40s%12s%20s", "File", "Size", "Age");
+ return s;
+ }
+ s = format (s, "%40s%12lld%20.2f", ep->filename, vec_len (ep->data),
+ now - ep->last_used);
+ return s;
+}
+
+u8 *
+format_hss_cache (u8 *s, va_list *args)
+{
+ hss_cache_t *hc = va_arg (*args, hss_cache_t *);
+ u32 verbose = va_arg (*args, u32);
+ hss_cache_entry_t *ce;
+ vlib_main_t *vm;
+ u32 index;
+ f64 now;
+
+ if (verbose == 0)
+ {
+ s = format (s, "cache size %lld bytes, limit %lld bytes, evictions %lld",
+ hc->cache_size, hc->cache_limit, hc->cache_evictions);
+ return 0;
+ }
+
+ vm = vlib_get_main ();
+ now = vlib_time_now (vm);
+
+ s = format (s, "%U", format_hss_cache_entry, 0 /* header */, now);
+
+ for (index = hc->first_index; index != ~0;)
+ {
+ ce = pool_elt_at_index (hc->cache_pool, index);
+ index = ce->next_index;
+ s = format (s, "%U", format_hss_cache_entry, ce, now);
+ }
+
+ s = format (s, "%40s%12lld", "Total Size", hc->cache_size);
+
+ return s;
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/http_static/http_cache.h b/src/plugins/http_static/http_cache.h
new file mode 100644
index 00000000000..a89ed5e7e94
--- /dev/null
+++ b/src/plugins/http_static/http_cache.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2022 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SRC_PLUGINS_HTTP_STATIC_HTTP_CACHE_H_
+#define SRC_PLUGINS_HTTP_STATIC_HTTP_CACHE_H_
+
+#include <vppinfra/bihash_vec8_8.h>
+
+typedef struct hss_cache_entry_
+{
+ /** Name of the file */
+ u8 *filename;
+ /** Contents of the file, as a u8 * vector */
+ u8 *data;
+ /** Last time the cache entry was used */
+ f64 last_used;
+ /** Cache LRU links */
+ u32 next_index;
+ u32 prev_index;
+ /** Reference count, so we don't recycle while referenced */
+ int inuse;
+} hss_cache_entry_t;
+
+typedef struct hss_cache_
+{
+ /** Unified file data cache pool */
+ hss_cache_entry_t *cache_pool;
+ /** Hash table which maps file name to file data */
+ BVT (clib_bihash) name_to_data;
+
+ /** Session pool lock */
+ clib_spinlock_t cache_lock;
+
+ /** Current cache size */
+ u64 cache_size;
+ /** Max cache size in bytes */
+ u64 cache_limit;
+ /** Number of cache evictions */
+ u64 cache_evictions;
+
+ /** Cache LRU listheads */
+ u32 first_index;
+ u32 last_index;
+
+ u8 debug_level;
+} hss_cache_t;
+
+u32 hss_cache_lookup_and_attach (hss_cache_t *hc, u8 *path, u8 **data,
+ u64 *data_len);
+u32 hss_cache_add_and_attach (hss_cache_t *hc, u8 *path, u8 **data,
+ u64 *data_len);
+void hss_cache_detach_entry (hss_cache_t *hc, u32 ce_index);
+u32 hss_cache_clear (hss_cache_t *hc);
+void hss_cache_init (hss_cache_t *hc, uword cache_size, u8 debug_level);
+
+u8 *format_hss_cache (u8 *s, va_list *args);
+
+#endif /* SRC_PLUGINS_HTTP_STATIC_HTTP_CACHE_H_ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/http_static/http_static.c b/src/plugins/http_static/http_static.c
index 48ae593718a..8f8fe37b7c1 100644
--- a/src/plugins/http_static/http_static.c
+++ b/src/plugins/http_static/http_static.c
@@ -1,7 +1,5 @@
/*
- * http_static.c - skeleton vpp engine plug-in
- *
- * Copyright (c) <current-year> <your-organization>
+ * Copyright (c) 2017-2022 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
@@ -29,57 +27,116 @@
#include <vpp/api/types.h>
-#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
-#define REPLY_MSG_ID_BASE hmp->msg_id_base
+#define REPLY_MSG_ID_BASE hsm->msg_id_base
#include <vlibapi/api_helper_macros.h>
-http_static_main_t http_static_main;
+__clib_export void
+hss_register_url_handler (hss_url_handler_fn fp, const char *url,
+ http_req_method_t request_type)
+{
+ hss_main_t *hsm = &hss_main;
+ uword *p, *url_table;
+
+ url_table = (request_type == HTTP_REQ_GET) ? hsm->get_url_handlers :
+ hsm->post_url_handlers;
+
+ p = hash_get_mem (url_table, url);
+
+ if (p)
+ {
+ clib_warning ("WARNING: attempt to replace handler for %s '%s' ignored",
+ (request_type == HTTP_REQ_GET) ? "GET" : "POST", url);
+ return;
+ }
+
+ hash_set_mem (url_table, url, (uword) fp);
+
+ /*
+ * Need to update the hash table pointer in http_static_server_main
+ * in case we just expanded it...
+ */
+ if (request_type == HTTP_REQ_GET)
+ hsm->get_url_handlers = url_table;
+ else
+ hsm->post_url_handlers = url_table;
+}
+
+/** \brief API helper function for vl_api_http_static_enable_t messages
+ */
+static int
+hss_enable_api (u32 fifo_size, u32 cache_limit, u32 prealloc_fifos,
+ u32 private_segment_size, u8 *www_root, u8 *uri)
+{
+ hss_main_t *hsm = &hss_main;
+ int rv;
+
+ hsm->fifo_size = fifo_size;
+ hsm->cache_size = cache_limit;
+ hsm->prealloc_fifos = prealloc_fifos;
+ hsm->private_segment_size = private_segment_size;
+ hsm->www_root = format (0, "%s%c", www_root, 0);
+ hsm->uri = format (0, "%s%c", uri, 0);
+
+ if (vec_len (hsm->www_root) < 2)
+ return VNET_API_ERROR_INVALID_VALUE;
+
+ if (hsm->app_index != ~0)
+ return VNET_API_ERROR_APP_ALREADY_ATTACHED;
+
+ vnet_session_enable_disable (hsm->vlib_main, 1 /* turn on TCP, etc. */);
+
+ rv = hss_create (hsm->vlib_main);
+ switch (rv)
+ {
+ case 0:
+ break;
+ default:
+ vec_free (hsm->www_root);
+ vec_free (hsm->uri);
+ return VNET_API_ERROR_INIT_FAILED;
+ }
+ return 0;
+}
/* API message handler */
static void vl_api_http_static_enable_t_handler
(vl_api_http_static_enable_t * mp)
{
vl_api_http_static_enable_reply_t *rmp;
- http_static_main_t *hmp = &http_static_main;
+ hss_main_t *hsm = &hss_main;
int rv;
mp->uri[ARRAY_LEN (mp->uri) - 1] = 0;
mp->www_root[ARRAY_LEN (mp->www_root) - 1] = 0;
- rv = http_static_server_enable_api
- (ntohl (mp->fifo_size),
- ntohl (mp->cache_size_limit),
- ntohl (mp->prealloc_fifos),
- ntohl (mp->private_segment_size), mp->www_root, mp->uri);
+ rv =
+ hss_enable_api (ntohl (mp->fifo_size), ntohl (mp->cache_size_limit),
+ ntohl (mp->prealloc_fifos),
+ ntohl (mp->private_segment_size), mp->www_root, mp->uri);
REPLY_MACRO (VL_API_HTTP_STATIC_ENABLE_REPLY);
}
#include <http_static/http_static.api.c>
static clib_error_t *
-http_static_init (vlib_main_t * vm)
+hss_api_init (vlib_main_t *vm)
{
- http_static_main_t *hmp = &http_static_main;
-
- hmp->vlib_main = vm;
- hmp->vnet_main = vnet_get_main ();
+ hss_main_t *hsm = &hss_main;
/* Ask for a correctly-sized block of API message decode slots */
- hmp->msg_id_base = setup_message_id_table ();
+ hsm->msg_id_base = setup_message_id_table ();
return 0;
}
-VLIB_INIT_FUNCTION (http_static_init);
+VLIB_INIT_FUNCTION (hss_api_init);
-/* *INDENT-OFF* */
VLIB_PLUGIN_REGISTER () =
{
.version = VPP_BUILD_VER,
.description = "HTTP Static Server"
};
-/* *INDENT-ON* */
/*
* fd.io coding-style-patch-verification: ON
diff --git a/src/plugins/http_static/http_static.h b/src/plugins/http_static/http_static.h
index 8ee0f92cd44..2850d356b74 100644
--- a/src/plugins/http_static/http_static.h
+++ b/src/plugins/http_static/http_static.h
@@ -1,8 +1,5 @@
-
/*
- * http_static.h - skeleton vpp engine plug-in header file
- *
- * Copyright (c) <current-year> <your-organization>
+ * Copyright (c) 2017-2022 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
@@ -18,199 +15,160 @@
#ifndef __included_http_static_h__
#define __included_http_static_h__
-#include <vnet/vnet.h>
-#include <vnet/session/application.h>
#include <vnet/session/application_interface.h>
#include <vnet/session/session.h>
-#include <vnet/ip/ip.h>
-#include <vnet/ethernet/ethernet.h>
+#include <http/http.h>
#include <vppinfra/hash.h>
#include <vppinfra/error.h>
-#include <vppinfra/time_range.h>
-#include <vppinfra/tw_timer_2t_1w_2048sl.h>
-#include <vppinfra/bihash_vec8_8.h>
+#include <http_static/http_cache.h>
/** @file http_static.h
* Static http server definitions
*/
-typedef struct
-{
- /* API message ID base */
- u16 msg_id_base;
-
- /* convenience */
- vlib_main_t *vlib_main;
- vnet_main_t *vnet_main;
-} http_static_main_t;
-
-extern http_static_main_t http_static_main;
-
-/** \brief Session States
- */
-
-typedef enum
-{
- /** Session is closed */
- HTTP_STATE_CLOSED,
- /** Session is established */
- HTTP_STATE_ESTABLISHED,
- /** Session has sent an OK response */
- HTTP_STATE_OK_SENT,
- /** Session has sent an HTML response */
- HTTP_STATE_SEND_MORE_DATA,
- /** Number of states */
- HTTP_STATE_N_STATES,
-} http_session_state_t;
-
-typedef enum
-{
- CALLED_FROM_RX,
- CALLED_FROM_TX,
- CALLED_FROM_TIMER,
-} http_state_machine_called_from_t;
-
-typedef enum
-{
- HTTP_BUILTIN_METHOD_GET = 0,
- HTTP_BUILTIN_METHOD_POST,
-} http_builtin_method_type_t;
-
-
/** \brief Application session
*/
typedef struct
{
CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
- /** Base class instance variables */
-#define _(type, name) type name;
- foreach_app_session_field
-#undef _
+ u32 session_index;
/** rx thread index */
u32 thread_index;
- /** rx buffer */
- u8 *rx_buf;
/** vpp session index, handle */
u32 vpp_session_index;
- u64 vpp_session_handle;
- /** Timeout timer handle */
- u32 timer_handle;
+ session_handle_t vpp_session_handle;
/** Fully-resolved file path */
u8 *path;
- /** File data, a vector */
+ /** Data to send */
u8 *data;
+ /** Data length */
+ u64 data_len;
/** Current data send offset */
u32 data_offset;
/** Need to free data in detach_cache_entry */
int free_data;
-
/** File cache pool index */
u32 cache_pool_index;
- /** state machine called from... */
- http_state_machine_called_from_t called_from;
-} http_session_t;
+ /** Content type, e.g. text, text/javascript, etc. */
+ http_content_type_t content_type;
+} hss_session_t;
-/** \brief In-memory file data cache entry
- */
-typedef struct
+typedef struct hss_session_handle_
{
- /** Name of the file */
- u8 *filename;
- /** Contents of the file, as a u8 * vector */
- u8 *data;
- /** Last time the cache entry was used */
- f64 last_used;
- /** Cache LRU links */
- u32 next_index;
- u32 prev_index;
- /** Reference count, so we don't recycle while referenced */
- int inuse;
-} file_data_cache_t;
+ union
+ {
+ struct
+ {
+ u32 session_index;
+ u32 thread_index;
+ };
+ u64 as_u64;
+ };
+} hss_session_handle_t;
+
+STATIC_ASSERT_SIZEOF (hss_session_handle_t, sizeof (u64));
+
+
+typedef struct hss_url_handler_args_
+{
+ hss_session_handle_t sh;
+
+ union
+ {
+ /* Request args */
+ struct
+ {
+ u8 *request;
+ http_req_method_t reqtype;
+ };
+
+ /* Reply args */
+ struct
+ {
+ u8 *data;
+ uword data_len;
+ u8 free_vec_data;
+ http_status_code_t sc;
+ };
+ };
+} hss_url_handler_args_t;
+
+typedef enum hss_url_handler_rc_
+{
+ HSS_URL_HANDLER_OK,
+ HSS_URL_HANDLER_ERROR,
+ HSS_URL_HANDLER_ASYNC,
+} hss_url_handler_rc_t;
+
+typedef hss_url_handler_rc_t (*hss_url_handler_fn) (hss_url_handler_args_t *);
+typedef void (*hss_register_url_fn) (hss_url_handler_fn, char *, int);
+typedef void (*hss_session_send_fn) (hss_url_handler_args_t *args);
/** \brief Main data structure
*/
-
typedef struct
{
/** Per thread vector of session pools */
- http_session_t **sessions;
- /** Session pool reader writer lock */
- clib_rwlock_t sessions_lock;
- /** vpp session to http session index map */
- u32 **session_to_http_session;
-
- /** Enable debug messages */
- int debug_level;
-
- /** vpp message/event queue */
- svm_msg_q_t **vpp_queue;
-
- /** Unified file data cache pool */
- file_data_cache_t *cache_pool;
- /** Hash table which maps file name to file data */
- BVT (clib_bihash) name_to_data;
+ hss_session_t **sessions;
/** Hash tables for built-in GET and POST handlers */
uword *get_url_handlers;
uword *post_url_handlers;
- /** Current cache size */
- u64 cache_size;
- /** Max cache size in bytes */
- u64 cache_limit;
- /** Number of cache evictions */
- u64 cache_evictions;
-
- /** Cache LRU listheads */
- u32 first_index;
- u32 last_index;
+ hss_cache_t cache;
/** root path to be served */
u8 *www_root;
- /** Server's event queue */
- svm_queue_t *vl_input_queue;
-
- /** API client handle */
- u32 my_client_index;
-
/** Application index */
u32 app_index;
- /** Process node index for event scheduling */
- u32 node_index;
-
/** Cert and key pair for tls */
u32 ckpair_index;
- /** Session cleanup timer wheel */
- tw_timer_wheel_2t_1w_2048sl_t tw;
- clib_spinlock_t tw_lock;
+ /* API message ID base */
+ u16 msg_id_base;
+
+ vlib_main_t *vlib_main;
- /** Time base, so we can generate browser cache control http spew */
- clib_timebase_t timebase;
+ /*
+ * Config
+ */
+ /** Enable debug messages */
+ int debug_level;
/** Number of preallocated fifos, usually 0 */
u32 prealloc_fifos;
/** Private segment size, usually 0 */
- u32 private_segment_size;
+ u64 private_segment_size;
/** Size of the allocated rx, tx fifos, roughly 8K or so */
u32 fifo_size;
/** The bind URI, defaults to tcp://0.0.0.0/80 */
u8 *uri;
- vlib_main_t *vlib_main;
-} http_static_server_main_t;
+ /** Threshold for switching to ptr data in http msgs */
+ u64 use_ptr_thresh;
+ /** Enable the use of builtinurls */
+ u8 enable_url_handlers;
+ /** Max cache size before LRU occurs */
+ u64 cache_size;
+
+ /** hash table of file extensions to mime types string indices */
+ uword *mime_type_indices_by_file_extensions;
+} hss_main_t;
-extern http_static_server_main_t http_static_server_main;
+extern hss_main_t hss_main;
-int http_static_server_enable_api (u32 fifo_size, u32 cache_limit,
- u32 prealloc_fifos,
- u32 private_segment_size,
- u8 * www_root, u8 * uri);
+int hss_create (vlib_main_t *vm);
-void http_static_server_register_builtin_handler
- (void *fp, char *url, int type);
+/**
+ * Register a GET or POST URL handler
+ */
+void hss_register_url_handler (hss_url_handler_fn fp, const char *url,
+ http_req_method_t type);
+void hss_session_send_data (hss_url_handler_args_t *args);
+void hss_builtinurl_json_handlers_init (void);
+hss_session_t *hss_session_get (u32 thread_index, u32 hs_index);
#endif /* __included_http_static_h__ */
diff --git a/src/plugins/http_static/static_server.c b/src/plugins/http_static/static_server.c
index c715dfa6fb8..040cdca9d7a 100644
--- a/src/plugins/http_static/static_server.c
+++ b/src/plugins/http_static/static_server.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017-2019 Cisco and/or its affiliates.
+ * Copyright (c) 2017-2022 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
@@ -13,158 +13,48 @@
* limitations under the License.
*/
-#include <vnet/vnet.h>
-#include <vnet/session/application.h>
-#include <vnet/session/application_interface.h>
-#include <vnet/session/session.h>
-#include <vppinfra/unix.h>
+#include <http_static/http_static.h>
+
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
-#include <http_static/http_static.h>
-
-#include <vppinfra/bihash_template.c>
/** @file static_server.c
- * Static http server, sufficient to
- * serve .html / .css / .js content.
+ * Static http server, sufficient to serve .html / .css / .js content.
*/
/*? %%clicmd:group_label Static HTTP Server %% ?*/
-http_static_server_main_t http_static_server_main;
-
-/** \brief Format the called-from enum
- */
-
-static u8 *
-format_state_machine_called_from (u8 * s, va_list * args)
-{
- http_state_machine_called_from_t cf =
- va_arg (*args, http_state_machine_called_from_t);
- char *which = "bogus!";
-
- switch (cf)
- {
- case CALLED_FROM_RX:
- which = "from rx";
- break;
- case CALLED_FROM_TX:
- which = "from tx";
- break;
- case CALLED_FROM_TIMER:
- which = "from timer";
- break;
-
- default:
- break;
- }
-
- s = format (s, "%s", which);
- return s;
-}
-
-
-/** \brief Acquire reader lock on the sessions pools
- */
-static void
-http_static_server_sessions_reader_lock (void)
-{
- clib_rwlock_reader_lock (&http_static_server_main.sessions_lock);
-}
-
-/** \brief Drop reader lock on the sessions pools
- */
-static void
-http_static_server_sessions_reader_unlock (void)
-{
- clib_rwlock_reader_unlock (&http_static_server_main.sessions_lock);
-}
-
-/** \brief Acquire writer lock on the sessions pools
- */
-static void
-http_static_server_sessions_writer_lock (void)
-{
- clib_rwlock_writer_lock (&http_static_server_main.sessions_lock);
-}
-
-/** \brief Drop writer lock on the sessions pools
- */
-static void
-http_static_server_sessions_writer_unlock (void)
-{
- clib_rwlock_writer_unlock (&http_static_server_main.sessions_lock);
-}
-
-/** \brief Start a session cleanup timer
- */
-static void
-http_static_server_session_timer_start (http_session_t * hs)
-{
- http_static_server_main_t *hsm = &http_static_server_main;
- u32 hs_handle;
+#define HSS_FIFO_THRESH (16 << 10)
- /* The session layer may fire a callback at a later date... */
- if (!pool_is_free (hsm->sessions[hs->thread_index], hs))
- {
- hs_handle = hs->thread_index << 24 | hs->session_index;
- clib_spinlock_lock (&http_static_server_main.tw_lock);
- hs->timer_handle = tw_timer_start_2t_1w_2048sl
- (&http_static_server_main.tw, hs_handle, 0, 60);
- clib_spinlock_unlock (&http_static_server_main.tw_lock);
- }
-}
+hss_main_t hss_main;
-/** \brief stop a session cleanup timer
- */
-static void
-http_static_server_session_timer_stop (http_session_t * hs)
+static hss_session_t *
+hss_session_alloc (u32 thread_index)
{
- if (hs->timer_handle == ~0)
- return;
- clib_spinlock_lock (&http_static_server_main.tw_lock);
- tw_timer_stop_2t_1w_2048sl (&http_static_server_main.tw, hs->timer_handle);
- clib_spinlock_unlock (&http_static_server_main.tw_lock);
-}
+ hss_main_t *hsm = &hss_main;
+ hss_session_t *hs;
-/** \brief Allocate an http session
- */
-static http_session_t *
-http_static_server_session_alloc (u32 thread_index)
-{
- http_static_server_main_t *hsm = &http_static_server_main;
- http_session_t *hs;
- pool_get_aligned_zero_numa (hsm->sessions[thread_index], hs,
- 0 /* not aligned */ ,
- 1 /* zero */ ,
- os_get_numa_index ());
+ pool_get_zero (hsm->sessions[thread_index], hs);
hs->session_index = hs - hsm->sessions[thread_index];
hs->thread_index = thread_index;
- hs->timer_handle = ~0;
hs->cache_pool_index = ~0;
return hs;
}
-/** \brief Get an http session by index
- */
-static http_session_t *
-http_static_server_session_get (u32 thread_index, u32 hs_index)
+__clib_export hss_session_t *
+hss_session_get (u32 thread_index, u32 hs_index)
{
- http_static_server_main_t *hsm = &http_static_server_main;
+ hss_main_t *hsm = &hss_main;
if (pool_is_free_index (hsm->sessions[thread_index], hs_index))
return 0;
return pool_elt_at_index (hsm->sessions[thread_index], hs_index);
}
-/** \brief Free an http session
- */
static void
-http_static_server_session_free (http_session_t * hs)
+hss_session_free (hss_session_t *hs)
{
- http_static_server_main_t *hsm = &http_static_server_main;
+ hss_main_t *hsm = &hss_main;
- /* Make sure the timer is stopped... */
- http_static_server_session_timer_stop (hs);
pool_put (hsm->sessions[hs->thread_index], hs);
if (CLIB_DEBUG)
@@ -173,974 +63,571 @@ http_static_server_session_free (http_session_t * hs)
save_thread_index = hs->thread_index;
/* Poison the entry, preserve timer state and thread index */
memset (hs, 0xfa, sizeof (*hs));
- hs->timer_handle = ~0;
hs->thread_index = save_thread_index;
}
}
-/** \brief add a session to the vpp < -- > http session index map
+/** \brief Disconnect a session
*/
static void
-http_static_server_session_lookup_add (u32 thread_index, u32 s_index,
- u32 hs_index)
+hss_session_disconnect_transport (hss_session_t *hs)
{
- http_static_server_main_t *hsm = &http_static_server_main;
- vec_validate (hsm->session_to_http_session[thread_index], s_index);
- hsm->session_to_http_session[thread_index][s_index] = hs_index;
+ vnet_disconnect_args_t _a = { 0 }, *a = &_a;
+ a->handle = hs->vpp_session_handle;
+ a->app_index = hss_main.app_index;
+ vnet_disconnect_session (a);
}
-/** \brief Remove a session from the vpp < -- > http session index map
- */
static void
-http_static_server_session_lookup_del (u32 thread_index, u32 s_index)
+start_send_data (hss_session_t *hs, http_status_code_t status)
{
- http_static_server_main_t *hsm = &http_static_server_main;
- hsm->session_to_http_session[thread_index][s_index] = ~0;
-}
+ http_msg_t msg;
+ session_t *ts;
+ int rv;
-/** \brief lookup a session in the vpp < -- > http session index map
- */
+ ts = session_get (hs->vpp_session_index, hs->thread_index);
-static http_session_t *
-http_static_server_session_lookup (u32 thread_index, u32 s_index)
-{
- http_static_server_main_t *hsm = &http_static_server_main;
- u32 hs_index;
+ msg.type = HTTP_MSG_REPLY;
+ msg.code = status;
+ msg.content_type = hs->content_type;
+ msg.data.len = hs->data_len;
- if (s_index < vec_len (hsm->session_to_http_session[thread_index]))
+ if (hs->data_len > hss_main.use_ptr_thresh)
{
- hs_index = hsm->session_to_http_session[thread_index][s_index];
- return http_static_server_session_get (thread_index, hs_index);
- }
- return 0;
-}
-
-/** \brief Detach cache entry from session
- */
+ msg.data.type = HTTP_MSG_DATA_PTR;
+ rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
+ ASSERT (rv == sizeof (msg));
-static void
-http_static_server_detach_cache_entry (http_session_t * hs)
-{
- http_static_server_main_t *hsm = &http_static_server_main;
- file_data_cache_t *ep;
+ uword data = pointer_to_uword (hs->data);
+ rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (data), (u8 *) &data);
+ ASSERT (rv == sizeof (sizeof (data)));
- /*
- * Decrement cache pool entry reference count
- * Note that if e.g. a file lookup fails, the cache pool index
- * won't be set
- */
- if (hs->cache_pool_index != ~0)
- {
- ep = pool_elt_at_index (hsm->cache_pool, hs->cache_pool_index);
- ep->inuse--;
- if (hsm->debug_level > 1)
- clib_warning ("index %d refcnt now %d", hs->cache_pool_index,
- ep->inuse);
+ goto done;
}
- hs->cache_pool_index = ~0;
- if (hs->free_data)
- vec_free (hs->data);
- hs->data = 0;
- hs->data_offset = 0;
- hs->free_data = 0;
- vec_free (hs->path);
-}
-/** \brief Disconnect a session
- */
-static void
-http_static_server_session_disconnect (http_session_t * hs)
-{
- vnet_disconnect_args_t _a = { 0 }, *a = &_a;
- a->handle = hs->vpp_session_handle;
- a->app_index = http_static_server_main.app_index;
- vnet_disconnect_session (a);
-}
+ msg.data.type = HTTP_MSG_DATA_INLINE;
-/* *INDENT-OFF* */
-/** \brief http error boilerplate
- */
-static const char *http_error_template =
- "HTTP/1.1 %s\r\n"
- "Date: %U GMT\r\n"
- "Content-Type: text/html\r\n"
- "Connection: close\r\n"
- "Pragma: no-cache\r\n"
- "Content-Length: 0\r\n\r\n";
-
-/** \brief http response boilerplate
- */
-static const char *http_response_template =
- "Date: %U GMT\r\n"
- "Expires: %U GMT\r\n"
- "Server: VPP Static\r\n"
- "Content-Type: %s\r\n"
- "Content-Length: %d\r\n\r\n";
-
-/* *INDENT-ON* */
-
-/** \brief send http data
- @param hs - http session
- @param data - the data vector to transmit
- @param length - length of data
- @param offset - transmit offset for this operation
- @return offset for next transmit operation, may be unchanged w/ full fifo
-*/
+ rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
+ ASSERT (rv == sizeof (msg));
-static u32
-static_send_data (http_session_t * hs, u8 * data, u32 length, u32 offset)
-{
- u32 bytes_to_send;
- http_static_server_main_t *hsm = &http_static_server_main;
+ if (!msg.data.len)
+ goto done;
- bytes_to_send = length - offset;
+ rv = svm_fifo_enqueue (ts->tx_fifo, hs->data_len, hs->data);
- while (bytes_to_send > 0)
+ if (rv != hs->data_len)
{
- int actual_transfer;
-
- actual_transfer = svm_fifo_enqueue
- (hs->tx_fifo, bytes_to_send, data + offset);
-
- /* Made any progress? */
- if (actual_transfer <= 0)
- {
- if (hsm->debug_level > 0 && bytes_to_send > 0)
- clib_warning ("WARNING: still %d bytes to send", bytes_to_send);
- return offset;
- }
- else
- {
- offset += actual_transfer;
- bytes_to_send -= actual_transfer;
-
- if (hsm->debug_level && bytes_to_send > 0)
- clib_warning ("WARNING: still %d bytes to send", bytes_to_send);
-
- if (svm_fifo_set_event (hs->tx_fifo))
- session_send_io_evt_to_thread (hs->tx_fifo,
- SESSION_IO_EVT_TX_FLUSH);
- return offset;
- }
+ hs->data_offset = rv;
+ svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
}
- /* NOTREACHED */
- return ~0;
-}
-/** \brief Send an http error string
- @param hs - the http session
- @param str - the error string, e.g. "404 Not Found"
-*/
-static void
-send_error (http_session_t * hs, char *str)
-{
- http_static_server_main_t *hsm = &http_static_server_main;
- u8 *data;
- f64 now;
-
- now = clib_timebase_now (&hsm->timebase);
- data = format (0, http_error_template, str, format_clib_timebase_time, now);
- static_send_data (hs, data, vec_len (data), 0);
- vec_free (data);
+done:
+
+ if (svm_fifo_set_event (ts->tx_fifo))
+ session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
}
-/** \brief Retrieve data from the application layer
- */
-static int
-session_rx_request (http_session_t * hs)
+__clib_export void
+hss_session_send_data (hss_url_handler_args_t *args)
{
- u32 max_dequeue, cursize;
- int n_read;
+ hss_session_t *hs;
- cursize = vec_len (hs->rx_buf);
- max_dequeue = svm_fifo_max_dequeue (hs->rx_fifo);
- if (PREDICT_FALSE (max_dequeue == 0))
- return -1;
+ hs = hss_session_get (args->sh.thread_index, args->sh.session_index);
+ if (!hs)
+ return;
- vec_validate (hs->rx_buf, cursize + max_dequeue - 1);
- n_read = app_recv_stream_raw (hs->rx_fifo, hs->rx_buf + cursize,
- max_dequeue, 0, 0 /* peek */ );
- ASSERT (n_read == max_dequeue);
- if (svm_fifo_is_empty (hs->rx_fifo))
- svm_fifo_unset_event (hs->rx_fifo);
+ if (hs->data && hs->free_data)
+ vec_free (hs->data);
- _vec_len (hs->rx_buf) = cursize + n_read;
- return 0;
+ hs->data = args->data;
+ hs->data_len = args->data_len;
+ hs->free_data = args->free_vec_data;
+ start_send_data (hs, args->sc);
}
-/** \brief Sanity-check the forward and reverse LRU lists
+/*
+ * path_has_known_suffix()
+ * Returns 1 if the request ends with a known suffix, like .htm or .ico
+ * Used to avoid looking for "/favicon.ico/index.html" or similar.
*/
-static inline void
-lru_validate (http_static_server_main_t * hsm)
+
+static int
+path_has_known_suffix (u8 *request)
{
-#if CLIB_DEBUG > 0
- f64 last_timestamp;
- u32 index;
- int i;
- file_data_cache_t *ep;
-
- last_timestamp = 1e70;
- for (i = 1, index = hsm->first_index; index != ~0;)
- {
- ep = pool_elt_at_index (hsm->cache_pool, index);
- index = ep->next_index;
- /* Timestamps should be smaller (older) as we walk the fwd list */
- if (ep->last_used > last_timestamp)
- {
- clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f",
- ep - hsm->cache_pool, i,
- ep->last_used, last_timestamp);
- }
- last_timestamp = ep->last_used;
- i++;
- }
+ u8 *ext;
+ uword *p;
- last_timestamp = 0.0;
- for (i = 1, index = hsm->last_index; index != ~0;)
+ if (vec_len (request) == 0)
{
- ep = pool_elt_at_index (hsm->cache_pool, index);
- index = ep->prev_index;
- /* Timestamps should be larger (newer) as we walk the rev list */
- if (ep->last_used < last_timestamp)
- {
- clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f",
- ep - hsm->cache_pool, i,
- ep->last_used, last_timestamp);
- }
- last_timestamp = ep->last_used;
- i++;
+ return 0;
}
-#endif
-}
-/** \brief Remove a data cache entry from the LRU lists
- */
-static inline void
-lru_remove (http_static_server_main_t * hsm, file_data_cache_t * ep)
-{
- file_data_cache_t *next_ep, *prev_ep;
- u32 ep_index;
+ ext = request + vec_len (request) - 1;
- lru_validate (hsm);
+ while (ext > request && ext[0] != '.')
+ ext--;
- ep_index = ep - hsm->cache_pool;
+ if (ext == request)
+ return 0;
- /* Deal with list heads */
- if (ep_index == hsm->first_index)
- hsm->first_index = ep->next_index;
- if (ep_index == hsm->last_index)
- hsm->last_index = ep->prev_index;
+ p = hash_get_mem (hss_main.mime_type_indices_by_file_extensions, ext);
+ if (p)
+ return 1;
- /* Fix next->prev */
- if (ep->next_index != ~0)
- {
- next_ep = pool_elt_at_index (hsm->cache_pool, ep->next_index);
- next_ep->prev_index = ep->prev_index;
- }
- /* Fix prev->next */
- if (ep->prev_index != ~0)
- {
- prev_ep = pool_elt_at_index (hsm->cache_pool, ep->prev_index);
- prev_ep->next_index = ep->next_index;
- }
- lru_validate (hsm);
+ return 0;
}
-/** \brief Add an entry to the LRU lists, tag w/ supplied timestamp
+/*
+ * content_type_from_request
+ * Returns the index of the request's suffix in the
+ * http-layer http_content_type_str[] array.
*/
-static inline void
-lru_add (http_static_server_main_t * hsm, file_data_cache_t * ep, f64 now)
+static http_content_type_t
+content_type_from_request (u8 *request)
{
- file_data_cache_t *next_ep;
- u32 ep_index;
+ u8 *ext;
+ uword *p;
+ /* default to text/html */
+ http_content_type_t rv = HTTP_CONTENT_TEXT_HTML;
- lru_validate (hsm);
+ ASSERT (vec_len (request) > 0);
- ep_index = ep - hsm->cache_pool;
+ ext = request + vec_len (request) - 1;
- /*
- * Re-add at the head of the forward LRU list,
- * tail of the reverse LRU list
- */
- if (hsm->first_index != ~0)
- {
- next_ep = pool_elt_at_index (hsm->cache_pool, hsm->first_index);
- next_ep->prev_index = ep_index;
- }
+ while (ext > request && ext[0] != '.')
+ ext--;
- ep->prev_index = ~0;
+ if (ext == request)
+ return rv;
- /* ep now the new head of the LRU forward list */
- ep->next_index = hsm->first_index;
- hsm->first_index = ep_index;
+ p = hash_get_mem (hss_main.mime_type_indices_by_file_extensions, ext);
- /* single session case: also the tail of the reverse LRU list */
- if (hsm->last_index == ~0)
- hsm->last_index = ep_index;
- ep->last_used = now;
+ if (p == 0)
+ return rv;
- lru_validate (hsm);
+ rv = p[0];
+ return rv;
}
-/** \brief Remove and re-add a cache entry from/to the LRU lists
- */
-
-static inline void
-lru_update (http_static_server_main_t * hsm, file_data_cache_t * ep, f64 now)
+static int
+try_url_handler (hss_main_t *hsm, hss_session_t *hs, http_req_method_t rt,
+ u8 *request)
{
- lru_remove (hsm, ep);
- lru_add (hsm, ep, now);
-}
+ http_status_code_t sc = HTTP_STATUS_OK;
+ hss_url_handler_args_t args = {};
+ uword *p, *url_table;
+ http_content_type_t type;
+ int rv;
-/** \brief Session-layer (main) data rx callback.
- Parse the http request, and reply to it.
- Future extensions might include POST processing, active content, etc.
-*/
+ if (!hsm->enable_url_handlers || !request)
+ return -1;
-/* svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL)
-get shoulder-tap when transport dequeues something, set in
-xmit routine. */
+ /* zero-length? try "index.html" */
+ if (vec_len (request) == 0)
+ {
+ request = format (request, "index.html");
+ }
-/** \brief closed state - should never really get here
- */
-static int
-state_closed (session_t * s, http_session_t * hs,
- http_state_machine_called_from_t cf)
-{
- clib_warning ("WARNING: http session %d, called from %U",
- hs->session_index, format_state_machine_called_from, cf);
- return -1;
-}
+ type = content_type_from_request (request);
-static void
-close_session (http_session_t * hs)
-{
- http_static_server_session_disconnect (hs);
-}
+ /* Look for built-in GET / POST handlers */
+ url_table =
+ (rt == HTTP_REQ_GET) ? hsm->get_url_handlers : hsm->post_url_handlers;
-/** \brief Register a builtin GET or POST handler
- */
-__clib_export void http_static_server_register_builtin_handler
- (void *fp, char *url, int request_type)
-{
- http_static_server_main_t *hsm = &http_static_server_main;
- uword *p, *builtin_table;
+ p = hash_get_mem (url_table, request);
+ if (!p)
+ return -1;
- builtin_table = (request_type == HTTP_BUILTIN_METHOD_GET)
- ? hsm->get_url_handlers : hsm->post_url_handlers;
+ hs->path = 0;
+ hs->data_offset = 0;
+ hs->cache_pool_index = ~0;
- p = hash_get_mem (builtin_table, url);
+ if (hsm->debug_level > 0)
+ clib_warning ("%s '%s'", (rt == HTTP_REQ_GET) ? "GET" : "POST", request);
- if (p)
+ args.reqtype = rt;
+ args.request = request;
+ args.sh.thread_index = hs->thread_index;
+ args.sh.session_index = hs->session_index;
+
+ rv = ((hss_url_handler_fn) p[0]) (&args);
+
+ /* Wait for data from handler */
+ if (rv == HSS_URL_HANDLER_ASYNC)
+ return 0;
+
+ if (rv == HSS_URL_HANDLER_ERROR)
{
- clib_warning ("WARNING: attempt to replace handler for %s '%s' ignored",
- (request_type == HTTP_BUILTIN_METHOD_GET) ?
- "GET" : "POST", url);
- return;
+ clib_warning ("builtin handler %llx hit on %s '%s' but failed!", p[0],
+ (rt == HTTP_REQ_GET) ? "GET" : "POST", request);
+ sc = HTTP_STATUS_NOT_FOUND;
}
- hash_set_mem (builtin_table, url, (uword) fp);
+ hs->data = args.data;
+ hs->data_len = args.data_len;
+ hs->free_data = args.free_vec_data;
+ hs->content_type = type;
- /*
- * Need to update the hash table pointer in http_static_server_main
- * in case we just expanded it...
- */
- if (request_type == HTTP_BUILTIN_METHOD_GET)
- hsm->get_url_handlers = builtin_table;
- else
- hsm->post_url_handlers = builtin_table;
+ start_send_data (hs, sc);
+
+ if (!hs->data)
+ hss_session_disconnect_transport (hs);
+
+ return 0;
}
-static int
-v_find_index (u8 * vec, char *str)
+static u8
+file_path_is_valid (u8 *path)
{
- int start_index;
- u32 slen = (u32) strnlen_s_inline (str, 8);
- u32 vlen = vec_len (vec);
+ struct stat _sb, *sb = &_sb;
- ASSERT (slen > 0);
+ if (stat ((char *) path, sb) < 0 /* can't stat the file */
+ || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */)
+ return 0;
- if (vlen <= slen)
- return -1;
+ return 1;
+}
- for (start_index = 0; start_index < (vlen - slen); start_index++)
- {
- if (!memcmp (vec, str, slen))
- return start_index;
- }
+static u32
+try_index_file (hss_main_t *hsm, hss_session_t *hs, u8 *path)
+{
+ u8 *port_str = 0, *redirect;
+ transport_endpoint_t endpt;
+ transport_proto_t proto;
+ int print_port = 0;
+ u16 local_port;
+ session_t *ts;
+ u32 plen;
+
+ /* Remove the trailing space */
+ vec_dec_len (path, 1);
+ plen = vec_len (path);
+
+ /* Append "index.html" */
+ if (path[plen - 1] != '/')
+ path = format (path, "/index.html%c", 0);
+ else
+ path = format (path, "index.html%c", 0);
- return -1;
-}
+ if (hsm->debug_level > 0)
+ clib_warning ("trying to find index: %s", path);
-/** \brief established state - waiting for GET, POST, etc.
- */
-static int
-state_established (session_t * s, http_session_t * hs,
- http_state_machine_called_from_t cf)
-{
- http_static_server_main_t *hsm = &http_static_server_main;
- u8 *request = 0;
- u8 *path;
- int i, rv;
- struct stat _sb, *sb = &_sb;
- clib_error_t *error;
- u8 request_type = HTTP_BUILTIN_METHOD_GET;
- u8 save_byte = 0;
- uword *p, *builtin_table;
+ if (!file_path_is_valid (path))
+ return HTTP_STATUS_NOT_FOUND;
- /* Read data from the sessison layer */
- rv = session_rx_request (hs);
+ /*
+ * We found an index.html file, build a redirect
+ */
+ vec_delete (path, vec_len (hsm->www_root) - 1, 0);
- /* No data? Odd, but stay in this state and await further instructions */
- if (rv)
- return 0;
+ ts = session_get (hs->vpp_session_index, hs->thread_index);
+ session_get_endpoint (ts, &endpt, 1 /* is_local */);
- /* Process the client request */
- request = hs->rx_buf;
- if (vec_len (request) < 8)
- {
- send_error (hs, "400 Bad Request");
- close_session (hs);
- return -1;
- }
+ local_port = clib_net_to_host_u16 (endpt.port);
+ proto = session_type_transport_proto (ts->session_type);
- if ((i = v_find_index (request, "GET ")) >= 0)
- goto find_end;
- else if ((i = v_find_index (request, "POST ")) >= 0)
+ if ((proto == TRANSPORT_PROTO_TCP && local_port != 80) ||
+ (proto == TRANSPORT_PROTO_TLS && local_port != 443))
{
- request_type = HTTP_BUILTIN_METHOD_POST;
- goto find_end;
+ print_port = 1;
+ port_str = format (0, ":%u", (u32) local_port);
}
- if (hsm->debug_level > 1)
- clib_warning ("Unknown http method");
+ redirect =
+ format (0,
+ "Location: http%s://%U%s%s\r\n\r\n",
+ proto == TRANSPORT_PROTO_TLS ? "s" : "", format_ip46_address,
+ &endpt.ip, endpt.is_ip4, print_port ? port_str : (u8 *) "", path);
- send_error (hs, "405 Method Not Allowed");
- close_session (hs);
- return -1;
+ if (hsm->debug_level > 0)
+ clib_warning ("redirect: %s", redirect);
-find_end:
+ vec_free (port_str);
- /* Lose "GET " or "POST " */
- vec_delete (request, i + 5 + request_type, 0);
+ hs->data = redirect;
+ hs->data_len = vec_len (redirect);
+ hs->free_data = 1;
- /* Temporarily drop in a NULL byte for lookup purposes */
- for (i = 0; i < vec_len (request); i++)
- {
- if (request[i] == ' ' || request[i] == '?')
- {
- save_byte = request[i];
- request[i] = 0;
- break;
- }
- }
+ return HTTP_STATUS_MOVED;
+}
+
+static int
+try_file_handler (hss_main_t *hsm, hss_session_t *hs, http_req_method_t rt,
+ u8 *request)
+{
+ http_status_code_t sc = HTTP_STATUS_OK;
+ u8 *path;
+ u32 ce_index;
+ http_content_type_t type;
+
+ /* Feature not enabled */
+ if (!hsm->www_root)
+ return -1;
+
+ type = content_type_from_request (request);
/*
- * Now we can construct the file to open
+ * Construct the file to open
* Browsers are capable of sporadically including a leading '/'
*/
- if (request[0] == '/')
+ if (!request)
+ path = format (0, "%s%c", hsm->www_root, 0);
+ else if (request[0] == '/')
path = format (0, "%s%s%c", hsm->www_root, request, 0);
else
path = format (0, "%s/%s%c", hsm->www_root, request, 0);
if (hsm->debug_level > 0)
- clib_warning ("%s '%s'", (request_type) == HTTP_BUILTIN_METHOD_GET ?
- "GET" : "POST", path);
+ clib_warning ("%s '%s'", (rt == HTTP_REQ_GET) ? "GET" : "POST", path);
- /* Look for built-in GET / POST handlers */
- builtin_table = (request_type == HTTP_BUILTIN_METHOD_GET) ?
- hsm->get_url_handlers : hsm->post_url_handlers;
-
- p = hash_get_mem (builtin_table, request);
-
- if (save_byte != 0)
- request[i] = save_byte;
+ if (hs->data && hs->free_data)
+ vec_free (hs->data);
- if (p)
- {
- int rv;
- int (*fp) (http_builtin_method_type_t, u8 *, http_session_t *);
- fp = (void *) p[0];
- hs->path = path;
- rv = (*fp) (request_type, request, hs);
- if (rv)
- {
- clib_warning ("builtin handler %llx hit on %s '%s' but failed!",
- p[0], (request_type == HTTP_BUILTIN_METHOD_GET) ?
- "GET" : "POST", request);
- send_error (hs, "404 Not Found");
- close_session (hs);
- return -1;
- }
- vec_reset_length (hs->rx_buf);
- goto send_ok;
- }
- vec_reset_length (hs->rx_buf);
- /* poison request, it's not valid anymore */
- request = 0;
- /* The static server itself doesn't do POSTs */
- if (request_type == HTTP_BUILTIN_METHOD_POST)
- {
- send_error (hs, "404 Not Found");
- close_session (hs);
- return -1;
- }
+ hs->data_offset = 0;
- /* Try to find the file. 2x special cases to find index.html */
- if (stat ((char *) path, sb) < 0 /* cant even stat the file */
- || sb->st_size < 20 /* file too small */
- || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */ )
+ ce_index =
+ hss_cache_lookup_and_attach (&hsm->cache, path, &hs->data, &hs->data_len);
+ if (ce_index == ~0)
{
- u32 save_length = vec_len (path) - 1;
- /* Try appending "index.html"... */
- _vec_len (path) -= 1;
- path = format (path, "index.html%c", 0);
- if (stat ((char *) path, sb) < 0 /* cant even stat the file */
- || sb->st_size < 20 /* file too small */
- || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */ )
+ if (!file_path_is_valid (path))
{
- _vec_len (path) = save_length;
- path = format (path, "/index.html%c", 0);
-
- /* Send a redirect, otherwise the browser will confuse itself */
- if (stat ((char *) path, sb) < 0 /* cant even stat the file */
- || sb->st_size < 20 /* file too small */
- || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */ )
- {
- vec_free (path);
- send_error (hs, "404 Not Found");
- close_session (hs);
- return -1;
- }
- else
+ /*
+ * Generate error 404 right now if we can't find a path with
+ * a known file extension. It's silly to look for
+ * "favicon.ico/index.html" if you can't find
+ * "favicon.ico"; realistic example which used to happen.
+ */
+ if (path_has_known_suffix (path))
{
- transport_endpoint_t endpoint;
- transport_proto_t proto;
- u16 local_port;
- int print_port = 0;
- u8 *port_str = 0;
-
- /*
- * To make this bit work correctly, we need to know our local
- * IP address, etc. and send it in the redirect...
- */
- u8 *redirect;
-
- vec_delete (path, vec_len (hsm->www_root) - 1, 0);
-
- session_get_endpoint (s, &endpoint, 1 /* is_local */ );
-
- local_port = clib_net_to_host_u16 (endpoint.port);
-
- proto = session_type_transport_proto (s->session_type);
-
- if ((proto == TRANSPORT_PROTO_TCP && local_port != 80)
- || (proto == TRANSPORT_PROTO_TLS && local_port != 443))
- {
- print_port = 1;
- port_str = format (0, ":%u", (u32) local_port);
- }
-
- redirect = format (0, "HTTP/1.1 301 Moved Permanently\r\n"
- "Location: http%s://%U%s%s\r\n\r\n",
- proto == TRANSPORT_PROTO_TLS ? "s" : "",
- format_ip46_address, &endpoint.ip,
- endpoint.is_ip4,
- print_port ? port_str : (u8 *) "", path);
- if (hsm->debug_level > 0)
- clib_warning ("redirect: %s", redirect);
-
- vec_free (port_str);
-
- static_send_data (hs, redirect, vec_len (redirect), 0);
- hs->session_state = HTTP_STATE_CLOSED;
- hs->path = 0;
- vec_free (redirect);
- vec_free (path);
- close_session (hs);
- return -1;
+ sc = HTTP_STATUS_NOT_FOUND;
+ goto done;
}
+ sc = try_index_file (hsm, hs, path);
+ goto done;
+ }
+ ce_index =
+ hss_cache_add_and_attach (&hsm->cache, path, &hs->data, &hs->data_len);
+ if (ce_index == ~0)
+ {
+ sc = HTTP_STATUS_INTERNAL_ERROR;
+ goto done;
}
}
- /* find or read the file if we haven't done so yet. */
- if (hs->data == 0)
- {
- BVT (clib_bihash_kv) kv;
- file_data_cache_t *dp;
+ hs->path = path;
+ hs->cache_pool_index = ce_index;
- hs->path = path;
+done:
- /* First, try the cache */
- kv.key = (u64) hs->path;
- if (BV (clib_bihash_search) (&hsm->name_to_data, &kv, &kv) == 0)
- {
- if (hsm->debug_level > 1)
- clib_warning ("lookup '%s' returned %lld", kv.key, kv.value);
-
- /* found the data.. */
- dp = pool_elt_at_index (hsm->cache_pool, kv.value);
- hs->data = dp->data;
- /* Update the cache entry, mark it in-use */
- lru_update (hsm, dp, vlib_time_now (vlib_get_main ()));
- hs->cache_pool_index = dp - hsm->cache_pool;
- dp->inuse++;
- if (hsm->debug_level > 1)
- clib_warning ("index %d refcnt now %d", hs->cache_pool_index,
- dp->inuse);
- }
- else
- {
- if (hsm->debug_level > 1)
- clib_warning ("lookup '%s' failed", kv.key, kv.value);
- /* Need to recycle one (or more cache) entries? */
- if (hsm->cache_size > hsm->cache_limit)
- {
- int free_index = hsm->last_index;
-
- while (free_index != ~0)
- {
- /* pick the LRU */
- dp = pool_elt_at_index (hsm->cache_pool, free_index);
- free_index = dp->prev_index;
- /* Which could be in use... */
- if (dp->inuse)
- {
- if (hsm->debug_level > 1)
- clib_warning ("index %d in use refcnt %d",
- dp - hsm->cache_pool, dp->inuse);
-
- }
- kv.key = (u64) (dp->filename);
- kv.value = ~0ULL;
- if (BV (clib_bihash_add_del) (&hsm->name_to_data, &kv,
- 0 /* is_add */ ) < 0)
- {
- clib_warning ("LRU delete '%s' FAILED!", dp->filename);
- }
- else if (hsm->debug_level > 1)
- clib_warning ("LRU delete '%s' ok", dp->filename);
-
- lru_remove (hsm, dp);
- hsm->cache_size -= vec_len (dp->data);
- hsm->cache_evictions++;
- vec_free (dp->filename);
- vec_free (dp->data);
- if (hsm->debug_level > 1)
- clib_warning ("pool put index %d", dp - hsm->cache_pool);
- pool_put (hsm->cache_pool, dp);
- if (hsm->cache_size < hsm->cache_limit)
- break;
- }
- }
+ hs->content_type = type;
+ start_send_data (hs, sc);
+ if (!hs->data)
+ hss_session_disconnect_transport (hs);
- /* Read the file */
- error = clib_file_contents ((char *) (hs->path), &hs->data);
- if (error)
- {
- clib_warning ("Error reading '%s'", hs->path);
- clib_error_report (error);
- vec_free (hs->path);
- close_session (hs);
- return -1;
- }
- /* Create a cache entry for it */
- pool_get (hsm->cache_pool, dp);
- memset (dp, 0, sizeof (*dp));
- dp->filename = vec_dup (hs->path);
- dp->data = hs->data;
- hs->cache_pool_index = dp - hsm->cache_pool;
- dp->inuse++;
- if (hsm->debug_level > 1)
- clib_warning ("index %d refcnt now %d", hs->cache_pool_index,
- dp->inuse);
- lru_add (hsm, dp, vlib_time_now (vlib_get_main ()));
- kv.key = (u64) vec_dup (hs->path);
- kv.value = dp - hsm->cache_pool;
- /* Add to the lookup table */
- if (hsm->debug_level > 1)
- clib_warning ("add '%s' value %lld", kv.key, kv.value);
-
- if (BV (clib_bihash_add_del) (&hsm->name_to_data, &kv,
- 1 /* is_add */ ) < 0)
- {
- clib_warning ("BUG: add failed!");
- }
- hsm->cache_size += vec_len (dp->data);
- }
- hs->data_offset = 0;
- }
- /* send 200 OK first */
-send_ok:
- static_send_data (hs, (u8 *) "HTTP/1.1 200 OK\r\n", 17, 0);
- hs->session_state = HTTP_STATE_OK_SENT;
- return 1;
+ return 0;
}
static int
-state_send_more_data (session_t * s, http_session_t * hs,
- http_state_machine_called_from_t cf)
+handle_request (hss_session_t *hs, http_req_method_t rt, u8 *request)
{
+ hss_main_t *hsm = &hss_main;
- /* Start sending data */
- hs->data_offset = static_send_data (hs, hs->data, vec_len (hs->data),
- hs->data_offset);
+ if (!try_url_handler (hsm, hs, rt, request))
+ return 0;
- /* Did we finish? */
- if (hs->data_offset < vec_len (hs->data))
- {
- /* No: ask for a shoulder-tap when the tx fifo has space */
- svm_fifo_add_want_deq_ntf (hs->tx_fifo,
- SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
- hs->session_state = HTTP_STATE_SEND_MORE_DATA;
- return 0;
- }
- /* Finished with this transaction, back to HTTP_STATE_ESTABLISHED */
+ if (!try_file_handler (hsm, hs, rt, request))
+ return 0;
+
+ /* Handler did not find anything return 404 */
+ start_send_data (hs, HTTP_STATUS_NOT_FOUND);
+ hss_session_disconnect_transport (hs);
- /* Let go of the file cache entry */
- http_static_server_detach_cache_entry (hs);
- hs->session_state = HTTP_STATE_ESTABLISHED;
return 0;
}
static int
-state_sent_ok (session_t * s, http_session_t * hs,
- http_state_machine_called_from_t cf)
+hss_ts_rx_callback (session_t *ts)
{
- http_static_server_main_t *hsm = &http_static_server_main;
- char *suffix;
- char *http_type;
- u8 *http_response;
- f64 now;
- u32 offset;
-
- /* What kind of dog food are we serving? */
- suffix = (char *) (hs->path + vec_len (hs->path) - 1);
- while ((u8 *) suffix >= hs->path && *suffix != '.')
- suffix--;
- suffix++;
- http_type = "text/html";
- if (!clib_strcmp (suffix, "css"))
- http_type = "text/css";
- else if (!clib_strcmp (suffix, "js"))
- http_type = "text/javascript";
- else if (!clib_strcmp (suffix, "json"))
- http_type = "application/json";
-
- if (hs->data == 0)
+ hss_session_t *hs;
+ u8 *request = 0;
+ http_msg_t msg;
+ int rv;
+
+ hs = hss_session_get (ts->thread_index, ts->opaque);
+
+ /* Read the http message header */
+ rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
+ ASSERT (rv == sizeof (msg));
+
+ if (msg.type != HTTP_MSG_REQUEST ||
+ (msg.method_type != HTTP_REQ_GET && msg.method_type != HTTP_REQ_POST))
{
- clib_warning ("BUG: hs->data not set for session %d",
- hs->session_index);
- close_session (hs);
+ hs->data = 0;
+ start_send_data (hs, HTTP_STATUS_METHOD_NOT_ALLOWED);
return 0;
}
- /*
- * Send an http response, which needs the current time,
- * the expiration time, and the data length
- */
- now = clib_timebase_now (&hsm->timebase);
- http_response = format (0, http_response_template,
- /* Date */
- format_clib_timebase_time, now,
- /* Expires */
- format_clib_timebase_time, now + 600.0,
- http_type, vec_len (hs->data));
- offset = static_send_data (hs, http_response, vec_len (http_response), 0);
- if (offset != vec_len (http_response))
+ /* Read request */
+ if (msg.data.len)
{
- clib_warning ("BUG: couldn't send response header!");
- close_session (hs);
- return 0;
+ vec_validate (request, msg.data.len - 1);
+ rv = svm_fifo_dequeue (ts->rx_fifo, msg.data.len, request);
+ ASSERT (rv == msg.data.len);
+ /* request must be a proper C-string in addition to a vector */
+ vec_add1 (request, 0);
}
- vec_free (http_response);
- /* Send data from the beginning... */
- hs->data_offset = 0;
- hs->session_state = HTTP_STATE_SEND_MORE_DATA;
- return 1;
-}
+ /* Find and send data */
+ handle_request (hs, msg.method_type, request);
-static void *state_funcs[HTTP_STATE_N_STATES] = {
- state_closed,
- /* Waiting for GET, POST, etc. */
- state_established,
- /* Sent OK */
- state_sent_ok,
- /* Send more data */
- state_send_more_data,
-};
+ vec_free (request);
-static inline int
-http_static_server_rx_tx_callback (session_t * s,
- http_state_machine_called_from_t cf)
+ return 0;
+}
+
+static int
+hss_ts_tx_callback (session_t *ts)
{
- http_session_t *hs;
- int (*fp) (session_t *, http_session_t *, http_state_machine_called_from_t);
+ hss_session_t *hs;
+ u32 to_send;
int rv;
- /* Acquire a reader lock on the session table */
- http_static_server_sessions_reader_lock ();
- hs = http_static_server_session_lookup (s->thread_index, s->session_index);
+ hs = hss_session_get (ts->thread_index, ts->opaque);
+ if (!hs || !hs->data)
+ return 0;
- if (!hs)
+ to_send = hs->data_len - hs->data_offset;
+ rv = svm_fifo_enqueue (ts->tx_fifo, to_send, hs->data + hs->data_offset);
+
+ if (rv <= 0)
{
- clib_warning ("No http session for thread %d session_index %d",
- s->thread_index, s->session_index);
- http_static_server_sessions_reader_unlock ();
+ svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
return 0;
}
- /* Execute state machine for this session */
- do
+ if (rv < to_send)
{
- fp = state_funcs[hs->session_state];
- rv = (*fp) (s, hs, cf);
- if (rv < 0)
- goto session_closed;
+ hs->data_offset += rv;
+ svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
}
- while (rv);
- /* Reset the session expiration timer */
- http_static_server_session_timer_stop (hs);
- http_static_server_session_timer_start (hs);
+ if (svm_fifo_set_event (ts->tx_fifo))
+ session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
-session_closed:
- http_static_server_sessions_reader_unlock ();
return 0;
}
-static int
-http_static_server_rx_callback (session_t * s)
-{
- return http_static_server_rx_tx_callback (s, CALLED_FROM_RX);
-}
-
-static int
-http_static_server_tx_callback (session_t * s)
-{
- return http_static_server_rx_tx_callback (s, CALLED_FROM_TX);
-}
-
-
/** \brief Session accept callback
*/
-
static int
-http_static_server_session_accept_callback (session_t * s)
+hss_ts_accept_callback (session_t *ts)
{
- http_static_server_main_t *hsm = &http_static_server_main;
- http_session_t *hs;
-
- hsm->vpp_queue[s->thread_index] =
- session_main_get_vpp_event_queue (s->thread_index);
+ hss_session_t *hs;
+ u32 thresh;
- http_static_server_sessions_writer_lock ();
+ hs = hss_session_alloc (ts->thread_index);
- hs = http_static_server_session_alloc (s->thread_index);
- http_static_server_session_lookup_add (s->thread_index, s->session_index,
- hs->session_index);
- hs->rx_fifo = s->rx_fifo;
- hs->tx_fifo = s->tx_fifo;
- hs->vpp_session_index = s->session_index;
- hs->vpp_session_handle = session_handle (s);
- hs->session_state = HTTP_STATE_ESTABLISHED;
- http_static_server_session_timer_start (hs);
+ hs->vpp_session_index = ts->session_index;
+ hs->vpp_session_handle = session_handle (ts);
- http_static_server_sessions_writer_unlock ();
+ /* The application sets a threshold for it's fifo to get notified when
+ * additional data can be enqueued. We want to keep the TX fifo reasonably
+ * full, however avoid entering a state where the
+ * fifo is full all the time and small chunks of data are being enqueued
+ * each time. If the fifo is small (under 16K) we set
+ * the threshold to it's size, meaning a notification will be given when the
+ * fifo empties.
+ */
+ thresh = clib_min (svm_fifo_size (ts->tx_fifo), HSS_FIFO_THRESH);
+ svm_fifo_set_deq_thresh (ts->tx_fifo, thresh);
- s->session_state = SESSION_STATE_READY;
+ ts->opaque = hs->session_index;
+ ts->session_state = SESSION_STATE_READY;
return 0;
}
-/** \brief Session disconnect callback
- */
-
static void
-http_static_server_session_disconnect_callback (session_t * s)
+hss_ts_disconnect_callback (session_t *ts)
{
- http_static_server_main_t *hsm = &http_static_server_main;
+ hss_main_t *hsm = &hss_main;
vnet_disconnect_args_t _a = { 0 }, *a = &_a;
- a->handle = session_handle (s);
+ a->handle = session_handle (ts);
a->app_index = hsm->app_index;
vnet_disconnect_session (a);
}
-/** \brief Session reset callback
- */
-
static void
-http_static_server_session_reset_callback (session_t * s)
+hss_ts_reset_callback (session_t *ts)
{
- http_static_server_main_t *hsm = &http_static_server_main;
+ hss_main_t *hsm = &hss_main;
vnet_disconnect_args_t _a = { 0 }, *a = &_a;
- a->handle = session_handle (s);
+ a->handle = session_handle (ts);
a->app_index = hsm->app_index;
vnet_disconnect_session (a);
}
static int
-http_static_server_session_connected_callback (u32 app_index, u32 api_context,
- session_t * s,
- session_error_t err)
+hss_ts_connected_callback (u32 app_index, u32 api_context, session_t *ts,
+ session_error_t err)
{
clib_warning ("called...");
return -1;
}
static int
-http_static_server_add_segment_callback (u32 client_index, u64 segment_handle)
+hss_add_segment_callback (u32 client_index, u64 segment_handle)
{
return 0;
}
static void
-http_static_session_cleanup (session_t * s, session_cleanup_ntf_t ntf)
+hss_ts_cleanup (session_t *s, session_cleanup_ntf_t ntf)
{
- http_session_t *hs;
+ hss_main_t *hsm = &hss_main;
+ hss_session_t *hs;
if (ntf == SESSION_CLEANUP_TRANSPORT)
return;
- http_static_server_sessions_writer_lock ();
-
- hs = http_static_server_session_lookup (s->thread_index, s->session_index);
+ hs = hss_session_get (s->thread_index, s->opaque);
if (!hs)
- goto done;
+ return;
- http_static_server_detach_cache_entry (hs);
- http_static_server_session_lookup_del (hs->thread_index,
- hs->vpp_session_index);
- vec_free (hs->rx_buf);
- http_static_server_session_free (hs);
+ if (hs->cache_pool_index != ~0)
+ {
+ hss_cache_detach_entry (&hsm->cache, hs->cache_pool_index);
+ hs->cache_pool_index = ~0;
+ }
-done:
- http_static_server_sessions_writer_unlock ();
+ if (hs->free_data)
+ vec_free (hs->data);
+ hs->data = 0;
+ hs->data_offset = 0;
+ hs->free_data = 0;
+ vec_free (hs->path);
+
+ hss_session_free (hs);
}
-/** \brief Session-layer virtual function table
- */
-static session_cb_vft_t http_static_server_session_cb_vft = {
- .session_accept_callback = http_static_server_session_accept_callback,
- .session_disconnect_callback =
- http_static_server_session_disconnect_callback,
- .session_connected_callback = http_static_server_session_connected_callback,
- .add_segment_callback = http_static_server_add_segment_callback,
- .builtin_app_rx_callback = http_static_server_rx_callback,
- .builtin_app_tx_callback = http_static_server_tx_callback,
- .session_reset_callback = http_static_server_session_reset_callback,
- .session_cleanup_callback = http_static_session_cleanup,
+static session_cb_vft_t hss_cb_vft = {
+ .session_accept_callback = hss_ts_accept_callback,
+ .session_disconnect_callback = hss_ts_disconnect_callback,
+ .session_connected_callback = hss_ts_connected_callback,
+ .add_segment_callback = hss_add_segment_callback,
+ .builtin_app_rx_callback = hss_ts_rx_callback,
+ .builtin_app_tx_callback = hss_ts_tx_callback,
+ .session_reset_callback = hss_ts_reset_callback,
+ .session_cleanup_callback = hss_ts_cleanup,
};
static int
-http_static_server_attach ()
+hss_attach ()
{
vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
- http_static_server_main_t *hsm = &http_static_server_main;
+ hss_main_t *hsm = &hss_main;
u64 options[APP_OPTIONS_N_OPTIONS];
vnet_app_attach_args_t _a, *a = &_a;
u32 segment_size = 128 << 20;
@@ -1152,8 +639,8 @@ http_static_server_attach ()
segment_size = hsm->private_segment_size;
a->api_client_index = ~0;
- a->name = format (0, "test_http_static_server");
- a->session_cb_vft = &http_static_server_session_cb_vft;
+ a->name = format (0, "http_static_server");
+ a->session_cb_vft = &hss_cb_vft;
a->options = options;
a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
@@ -1186,19 +673,20 @@ http_static_server_attach ()
}
static int
-http_static_transport_needs_crypto (transport_proto_t proto)
+hss_transport_needs_crypto (transport_proto_t proto)
{
return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
proto == TRANSPORT_PROTO_QUIC;
}
static int
-http_static_server_listen ()
+hss_listen (void)
{
- http_static_server_main_t *hsm = &http_static_server_main;
+ hss_main_t *hsm = &hss_main;
session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
vnet_listen_args_t _a, *a = &_a;
char *uri = "tcp://0.0.0.0/80";
+ u8 need_crypto;
int rv;
clib_memset (a, 0, sizeof (*a));
@@ -1210,8 +698,12 @@ http_static_server_listen ()
if (parse_uri (uri, &sep))
return -1;
+ need_crypto = hss_transport_needs_crypto (sep.transport_proto);
+
+ sep.transport_proto = TRANSPORT_PROTO_HTTP;
clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
- if (http_static_transport_needs_crypto (a->sep_ext.transport_proto))
+
+ if (need_crypto)
{
session_endpoint_alloc_ext_cfg (&a->sep_ext,
TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
@@ -1219,257 +711,142 @@ http_static_server_listen ()
}
rv = vnet_listen (a);
- if (a->sep_ext.ext_cfg)
+
+ if (need_crypto)
clib_mem_free (a->sep_ext.ext_cfg);
- return rv;
-}
-static void
-http_static_server_session_close_cb (void *hs_handlep)
-{
- http_static_server_main_t *hsm = &http_static_server_main;
- http_session_t *hs;
- uword hs_handle;
- hs_handle = pointer_to_uword (hs_handlep);
- hs =
- http_static_server_session_get (hs_handle >> 24, hs_handle & 0x00FFFFFF);
-
- if (hsm->debug_level > 1)
- clib_warning ("terminate thread %d index %d hs %llx",
- hs_handle >> 24, hs_handle & 0x00FFFFFF, hs);
- if (!hs)
- return;
- hs->timer_handle = ~0;
- http_static_server_session_disconnect (hs);
+ return rv;
}
-/** \brief Expired session timer-wheel callback
- */
static void
-http_expired_timers_dispatch (u32 * expired_timers)
+hss_url_handlers_init (hss_main_t *hsm)
{
- u32 hs_handle;
- int i;
-
- for (i = 0; i < vec_len (expired_timers); i++)
+ if (!hsm->get_url_handlers)
{
- /* Get session handle. The first bit is the timer id */
- hs_handle = expired_timers[i] & 0x7FFFFFFF;
- session_send_rpc_evt_to_thread (hs_handle >> 24,
- http_static_server_session_close_cb,
- uword_to_pointer (hs_handle, void *));
+ hsm->get_url_handlers = hash_create_string (0, sizeof (uword));
+ hsm->post_url_handlers = hash_create_string (0, sizeof (uword));
}
-}
-
-/** \brief Timer-wheel expiration process
- */
-static uword
-http_static_server_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
- vlib_frame_t * f)
-{
- http_static_server_main_t *hsm = &http_static_server_main;
- f64 now, timeout = 1.0;
- uword *event_data = 0;
- uword __clib_unused event_type;
-
- while (1)
- {
- vlib_process_wait_for_event_or_clock (vm, timeout);
- now = vlib_time_now (vm);
- event_type = vlib_process_get_events (vm, (uword **) & event_data);
-
- /* expire timers */
- clib_spinlock_lock (&http_static_server_main.tw_lock);
- tw_timer_expire_timers_2t_1w_2048sl (&hsm->tw, now);
- clib_spinlock_unlock (&http_static_server_main.tw_lock);
- vec_reset_length (event_data);
- }
- return 0;
+ hss_builtinurl_json_handlers_init ();
}
-/* *INDENT-OFF* */
-VLIB_REGISTER_NODE (http_static_server_process_node) =
-{
- .function = http_static_server_process,
- .type = VLIB_NODE_TYPE_PROCESS,
- .name = "static-http-server-process",
- .state = VLIB_NODE_STATE_DISABLED,
-};
-/* *INDENT-ON* */
-
-static int
-http_static_server_create (vlib_main_t * vm)
+int
+hss_create (vlib_main_t *vm)
{
vlib_thread_main_t *vtm = vlib_get_thread_main ();
- http_static_server_main_t *hsm = &http_static_server_main;
+ hss_main_t *hsm = &hss_main;
u32 num_threads;
- vlib_node_t *n;
num_threads = 1 /* main thread */ + vtm->n_threads;
- vec_validate (hsm->vpp_queue, num_threads - 1);
vec_validate (hsm->sessions, num_threads - 1);
- vec_validate (hsm->session_to_http_session, num_threads - 1);
- clib_rwlock_init (&hsm->sessions_lock);
- clib_spinlock_init (&hsm->tw_lock);
-
- if (http_static_server_attach ())
+ if (hss_attach ())
{
clib_warning ("failed to attach server");
return -1;
}
- if (http_static_server_listen ())
+ if (hss_listen ())
{
clib_warning ("failed to start listening");
return -1;
}
- /* Init path-to-cache hash table */
- BV (clib_bihash_init) (&hsm->name_to_data, "http cache", 128, 32 << 20);
-
- hsm->get_url_handlers = hash_create_string (0, sizeof (uword));
- hsm->post_url_handlers = hash_create_string (0, sizeof (uword));
+ if (hsm->www_root)
+ hss_cache_init (&hsm->cache, hsm->cache_size, hsm->debug_level);
- /* Init timer wheel and process */
- tw_timer_wheel_init_2t_1w_2048sl (&hsm->tw, http_expired_timers_dispatch,
- 1.0 /* timer interval */ , ~0);
- vlib_node_set_state (vm, http_static_server_process_node.index,
- VLIB_NODE_STATE_POLLING);
- n = vlib_get_node (vm, http_static_server_process_node.index);
- vlib_start_process (vm, n->runtime_index);
+ if (hsm->enable_url_handlers)
+ hss_url_handlers_init (hsm);
return 0;
}
-/** \brief API helper function for vl_api_http_static_enable_t messages
- */
-int
-http_static_server_enable_api (u32 fifo_size, u32 cache_limit,
- u32 prealloc_fifos,
- u32 private_segment_size,
- u8 * www_root, u8 * uri)
-{
- http_static_server_main_t *hsm = &http_static_server_main;
- int rv;
-
- hsm->fifo_size = fifo_size;
- hsm->cache_limit = cache_limit;
- hsm->prealloc_fifos = prealloc_fifos;
- hsm->private_segment_size = private_segment_size;
- hsm->www_root = format (0, "%s%c", www_root, 0);
- hsm->uri = format (0, "%s%c", uri, 0);
-
- if (vec_len (hsm->www_root) < 2)
- return VNET_API_ERROR_INVALID_VALUE;
-
- if (hsm->my_client_index != ~0)
- return VNET_API_ERROR_APP_ALREADY_ATTACHED;
-
- vnet_session_enable_disable (hsm->vlib_main, 1 /* turn on TCP, etc. */ );
-
- rv = http_static_server_create (hsm->vlib_main);
- switch (rv)
- {
- case 0:
- break;
- default:
- vec_free (hsm->www_root);
- vec_free (hsm->uri);
- return VNET_API_ERROR_INIT_FAILED;
- }
- return 0;
-}
-
static clib_error_t *
-http_static_server_create_command_fn (vlib_main_t * vm,
- unformat_input_t * input,
- vlib_cli_command_t * cmd)
+hss_create_command_fn (vlib_main_t *vm, unformat_input_t *input,
+ vlib_cli_command_t *cmd)
{
- http_static_server_main_t *hsm = &http_static_server_main;
unformat_input_t _line_input, *line_input = &_line_input;
+ hss_main_t *hsm = &hss_main;
+ clib_error_t *error = 0;
u64 seg_size;
- u8 *www_root = 0;
int rv;
+ if (hsm->app_index != (u32) ~0)
+ return clib_error_return (0, "http server already running...");
+
hsm->prealloc_fifos = 0;
hsm->private_segment_size = 0;
hsm->fifo_size = 0;
- /* 10mb cache limit, before LRU occurs */
- hsm->cache_limit = 10 << 20;
+ hsm->cache_size = 10 << 20;
/* Get a line of input. */
if (!unformat_user (input, unformat_line_input, line_input))
- goto no_wwwroot;
+ goto no_input;
while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
{
- if (unformat (line_input, "www-root %s", &www_root))
+ if (unformat (line_input, "www-root %s", &hsm->www_root))
;
else
if (unformat (line_input, "prealloc-fifos %d", &hsm->prealloc_fifos))
;
else if (unformat (line_input, "private-segment-size %U",
unformat_memory_size, &seg_size))
- {
- if (seg_size >= 0x100000000ULL)
- {
- vlib_cli_output (vm, "private segment size %llu, too large",
- seg_size);
- return 0;
- }
- hsm->private_segment_size = seg_size;
- }
+ hsm->private_segment_size = seg_size;
else if (unformat (line_input, "fifo-size %d", &hsm->fifo_size))
hsm->fifo_size <<= 10;
else if (unformat (line_input, "cache-size %U", unformat_memory_size,
- &hsm->cache_limit))
- {
- if (hsm->cache_limit < (128 << 10))
- {
- return clib_error_return (0,
- "cache-size must be at least 128kb");
- }
- }
-
+ &hsm->cache_size))
+ ;
else if (unformat (line_input, "uri %s", &hsm->uri))
;
else if (unformat (line_input, "debug %d", &hsm->debug_level))
;
else if (unformat (line_input, "debug"))
hsm->debug_level = 1;
+ else if (unformat (line_input, "ptr-thresh %U", unformat_memory_size,
+ &hsm->use_ptr_thresh))
+ ;
+ else if (unformat (line_input, "url-handlers"))
+ hsm->enable_url_handlers = 1;
else
- return clib_error_return (0, "unknown input `%U'",
- format_unformat_error, line_input);
+ {
+ error = clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, line_input);
+ break;
+ }
}
+
unformat_free (line_input);
- if (www_root == 0)
+no_input:
+
+ if (error)
+ goto done;
+
+ if (hsm->www_root == 0 && !hsm->enable_url_handlers)
{
- no_wwwroot:
- return clib_error_return (0, "Must specify www-root <path>");
+ error = clib_error_return (0, "Must set www-root or url-handlers");
+ goto done;
}
- if (hsm->my_client_index != (u32) ~ 0)
+ if (hsm->cache_size < (128 << 10))
{
- vec_free (www_root);
- return clib_error_return (0, "http server already running...");
+ error = clib_error_return (0, "cache-size must be at least 128kb");
+ vec_free (hsm->www_root);
+ goto done;
}
- hsm->www_root = www_root;
-
vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
- rv = http_static_server_create (vm);
- switch (rv)
+ if ((rv = hss_create (vm)))
{
- case 0:
- break;
- default:
+ error = clib_error_return (0, "server_create returned %d", rv);
vec_free (hsm->www_root);
- return clib_error_return (0, "server_create returned %d", rv);
}
- return 0;
+
+done:
+
+ return error;
}
/*?
@@ -1484,92 +861,33 @@ http_static_server_create_command_fn (vlib_main_t * vm,
* @cliexcmd{http static server www-root <path> [prealloc-fios <nn>]
* [private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]}
?*/
-/* *INDENT-OFF* */
-VLIB_CLI_COMMAND (http_static_server_create_command, static) =
-{
+VLIB_CLI_COMMAND (hss_create_command, static) = {
.path = "http static server",
- .short_help = "http static server www-root <path> [prealloc-fifos <nn>]\n"
- "[private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]\n"
- "[debug [nn]]\n",
- .function = http_static_server_create_command_fn,
+ .short_help =
+ "http static server www-root <path> [prealloc-fifos <nn>]\n"
+ "[private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]\n"
+ "[ptr-thresh <nn>] [url-handlers] [debug [nn]]\n",
+ .function = hss_create_command_fn,
};
-/* *INDENT-ON* */
-
-/** \brief format a file cache entry
- */
-u8 *
-format_hsm_cache_entry (u8 * s, va_list * args)
-{
- file_data_cache_t *ep = va_arg (*args, file_data_cache_t *);
- f64 now = va_arg (*args, f64);
-
- /* Header */
- if (ep == 0)
- {
- s = format (s, "%40s%12s%20s", "File", "Size", "Age");
- return s;
- }
- s = format (s, "%40s%12lld%20.2f", ep->filename, vec_len (ep->data),
- now - ep->last_used);
- return s;
-}
-u8 *
-format_http_session_state (u8 * s, va_list * args)
-{
- http_session_state_t state = va_arg (*args, http_session_state_t);
- char *state_string = "bogus!";
-
- switch (state)
- {
- case HTTP_STATE_CLOSED:
- state_string = "closed";
- break;
- case HTTP_STATE_ESTABLISHED:
- state_string = "established";
- break;
- case HTTP_STATE_OK_SENT:
- state_string = "ok sent";
- break;
- case HTTP_STATE_SEND_MORE_DATA:
- state_string = "send more data";
- break;
- default:
- break;
- }
-
- return format (s, "%s", state_string);
-}
-
-u8 *
-format_http_session (u8 * s, va_list * args)
+static u8 *
+format_hss_session (u8 *s, va_list *args)
{
- http_session_t *hs = va_arg (*args, http_session_t *);
- int verbose = va_arg (*args, int);
+ hss_session_t *hs = va_arg (*args, hss_session_t *);
+ int __clib_unused verbose = va_arg (*args, int);
- s = format (s, "[%d]: state %U", hs->session_index,
- format_http_session_state, hs->session_state);
- if (verbose > 0)
- {
- s = format (s, "\n path %s, data length %u, data_offset %u",
- hs->path ? hs->path : (u8 *) "[none]",
- vec_len (hs->data), hs->data_offset);
- }
+ s = format (s, "\n path %s, data length %u, data_offset %u",
+ hs->path ? hs->path : (u8 *) "[none]", hs->data_len,
+ hs->data_offset);
return s;
}
static clib_error_t *
-http_show_static_server_command_fn (vlib_main_t * vm,
- unformat_input_t * input,
- vlib_cli_command_t * cmd)
+hss_show_command_fn (vlib_main_t *vm, unformat_input_t *input,
+ vlib_cli_command_t *cmd)
{
- http_static_server_main_t *hsm = &http_static_server_main;
- file_data_cache_t *ep, **entries = 0;
- int verbose = 0;
- int show_cache = 0;
- int show_sessions = 0;
- u32 index;
- f64 now;
+ int verbose = 0, show_cache = 0, show_sessions = 0;
+ hss_main_t *hsm = &hss_main;
if (hsm->www_root == 0)
return clib_error_return (0, "Static server disabled");
@@ -1592,61 +910,29 @@ http_show_static_server_command_fn (vlib_main_t * vm,
return clib_error_return (0, "specify one or more of cache, sessions");
if (show_cache)
- {
- if (verbose == 0)
- {
- vlib_cli_output
- (vm, "www_root %s, cache size %lld bytes, limit %lld bytes, "
- "evictions %lld",
- hsm->www_root, hsm->cache_size, hsm->cache_limit,
- hsm->cache_evictions);
- return 0;
- }
-
- now = vlib_time_now (vm);
-
- vlib_cli_output (vm, "%U", format_hsm_cache_entry, 0 /* header */ ,
- now);
-
- for (index = hsm->first_index; index != ~0;)
- {
- ep = pool_elt_at_index (hsm->cache_pool, index);
- index = ep->next_index;
- vlib_cli_output (vm, "%U", format_hsm_cache_entry, ep, now);
- }
-
- vlib_cli_output (vm, "%40s%12lld", "Total Size", hsm->cache_size);
-
- vec_free (entries);
- }
+ vlib_cli_output (vm, "%U", format_hss_cache, &hsm->cache, verbose);
if (show_sessions)
{
u32 *session_indices = 0;
- http_session_t *hs;
+ hss_session_t *hs;
int i, j;
- http_static_server_sessions_reader_lock ();
for (i = 0; i < vec_len (hsm->sessions); i++)
{
- /* *INDENT-OFF* */
pool_foreach (hs, hsm->sessions[i])
- {
vec_add1 (session_indices, hs - hsm->sessions[i]);
- }
- /* *INDENT-ON* */
for (j = 0; j < vec_len (session_indices); j++)
{
- vlib_cli_output (vm, "%U", format_http_session,
- pool_elt_at_index
- (hsm->sessions[i], session_indices[j]),
- verbose);
+ vlib_cli_output (
+ vm, "%U", format_hss_session,
+ pool_elt_at_index (hsm->sessions[i], session_indices[j]),
+ verbose);
}
vec_reset_length (session_indices);
}
- http_static_server_sessions_reader_unlock ();
vec_free (session_indices);
}
return 0;
@@ -1662,63 +948,24 @@ http_show_static_server_command_fn (vlib_main_t * vm,
* @cliend
* @cliexcmd{show http static server sessions cache [verbose [nn]]}
?*/
-/* *INDENT-OFF* */
-VLIB_CLI_COMMAND (http_show_static_server_command, static) =
-{
+VLIB_CLI_COMMAND (hss_show_command, static) = {
.path = "show http static server",
.short_help = "show http static server sessions cache [verbose [<nn>]]",
- .function = http_show_static_server_command_fn,
+ .function = hss_show_command_fn,
};
-/* *INDENT-ON* */
static clib_error_t *
-http_clear_static_cache_command_fn (vlib_main_t * vm,
- unformat_input_t * input,
- vlib_cli_command_t * cmd)
+hss_clear_cache_command_fn (vlib_main_t *vm, unformat_input_t *input,
+ vlib_cli_command_t *cmd)
{
- http_static_server_main_t *hsm = &http_static_server_main;
- file_data_cache_t *dp;
- u32 free_index;
+ hss_main_t *hsm = &hss_main;
u32 busy_items = 0;
- BVT (clib_bihash_kv) kv;
if (hsm->www_root == 0)
return clib_error_return (0, "Static server disabled");
- http_static_server_sessions_reader_lock ();
-
- /* Walk the LRU list to find active entries */
- free_index = hsm->last_index;
- while (free_index != ~0)
- {
- dp = pool_elt_at_index (hsm->cache_pool, free_index);
- free_index = dp->prev_index;
- /* Which could be in use... */
- if (dp->inuse)
- {
- busy_items++;
- free_index = dp->next_index;
- continue;
- }
- kv.key = (u64) (dp->filename);
- kv.value = ~0ULL;
- if (BV (clib_bihash_add_del) (&hsm->name_to_data, &kv,
- 0 /* is_add */ ) < 0)
- {
- clib_warning ("BUG: cache clear delete '%s' FAILED!", dp->filename);
- }
+ busy_items = hss_cache_clear (&hsm->cache);
- lru_remove (hsm, dp);
- hsm->cache_size -= vec_len (dp->data);
- hsm->cache_evictions++;
- vec_free (dp->filename);
- vec_free (dp->data);
- if (hsm->debug_level > 1)
- clib_warning ("pool put index %d", dp - hsm->cache_pool);
- pool_put (hsm->cache_pool, dp);
- free_index = hsm->last_index;
- }
- http_static_server_sessions_reader_unlock ();
if (busy_items > 0)
vlib_cli_output (vm, "Note: %d busy items still in cache...", busy_items);
else
@@ -1737,32 +984,34 @@ http_clear_static_cache_command_fn (vlib_main_t * vm,
* @cliend
* @cliexcmd{clear http static cache}
?*/
-/* *INDENT-OFF* */
-VLIB_CLI_COMMAND (clear_http_static_cache_command, static) =
-{
+VLIB_CLI_COMMAND (clear_hss_cache_command, static) = {
.path = "clear http static cache",
.short_help = "clear http static cache",
- .function = http_clear_static_cache_command_fn,
+ .function = hss_clear_cache_command_fn,
};
-/* *INDENT-ON* */
static clib_error_t *
-http_static_server_main_init (vlib_main_t * vm)
+hss_main_init (vlib_main_t *vm)
{
- http_static_server_main_t *hsm = &http_static_server_main;
+ hss_main_t *hsm = &hss_main;
- hsm->my_client_index = ~0;
+ hsm->app_index = ~0;
hsm->vlib_main = vm;
- hsm->first_index = hsm->last_index = ~0;
- clib_timebase_init (&hsm->timebase, 0 /* GMT */ ,
- CLIB_TIMEBASE_DAYLIGHT_NONE,
- &vm->clib_time /* share the system clock */ );
+ /* Set up file extension to mime type index map */
+ hsm->mime_type_indices_by_file_extensions =
+ hash_create_string (0, sizeof (uword));
+
+#define _(def, ext, str) \
+ hash_set_mem (hsm->mime_type_indices_by_file_extensions, ext, \
+ HTTP_CONTENT_##def);
+ foreach_http_content_type;
+#undef _
return 0;
}
-VLIB_INIT_FUNCTION (http_static_server_main_init);
+VLIB_INIT_FUNCTION (hss_main_init);
/*
* fd.io coding-style-patch-verification: ON