diff options
author | Dave Barach <dave@barachs.net> | 2019-09-17 09:47:35 -0400 |
---|---|---|
committer | Florin Coras <florin.coras@gmail.com> | 2019-09-18 14:26:59 +0000 |
commit | 43765e2b4eaa8e1f2a5f1562414e04962c777ff3 (patch) | |
tree | e5a18e7ba42c4c7fb962d034e22a79d9beb89288 /src/plugins/builtinurl/builtins.c | |
parent | 4bf849043d6ffc1d3d43e65f381e310f425d01f7 (diff) |
builtinurl: initial working attempt
Note that the builtin URLs are disabled by default. To activate,
"builtinurl enable" or use the builtinurl_enable API.
See .../extras/http/sample.md for some Hugo-friendly .md w/ embedded
Javascript that accesses the builtin URLs.
Type: feature
Signed-off-by: Dave Barach <dave@barachs.net>
Change-Id: I6d82d9292c41d6d2d90be73ba8a1a043fb20c986
Diffstat (limited to 'src/plugins/builtinurl/builtins.c')
-rw-r--r-- | src/plugins/builtinurl/builtins.c | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/src/plugins/builtinurl/builtins.c b/src/plugins/builtinurl/builtins.c new file mode 100644 index 00000000000..73582220995 --- /dev/null +++ b/src/plugins/builtinurl/builtins.c @@ -0,0 +1,129 @@ +/* + * 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 <vnet/vnet.h> +#include <builtinurl/builtinurl.h> +#include <http_static/http_static.h> +#include <vpp/app/version.h> + +int +handle_get_version (u8 * request, http_session_t * hs) +{ + u8 *s = 0; + + /* Build some json bullshit */ + s = format (s, "{\"vpp_details\": {"); + s = format (s, " \"version\": \"%s\",", VPP_BUILD_VER); + s = format (s, " \"build_date\": \"%s\"}}\r\n", VPP_BUILD_DATE); + + hs->data = s; + hs->data_offset = 0; + hs->cache_pool_index = ~0; + hs->free_data = 1; + return 0; +} + +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_len (s) = cp - s; + break; + } + cp++; + } +} + +int +handle_get_interface_stats (u8 * request, http_session_t * hs) +{ + u8 *s = 0, *stats = 0; + u32 sw_if_index; + uword *p; + vnet_sw_interface_t *si; + 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 (); + + trim_path_from_request (request, "interface_stats.json"); + + /* get data */ + + p = hash_get (vnm->interface_main.hw_interface_by_name, request); + if (!p) + { + clib_warning ("Couldn't find interface '%v'", request); + + s = format (s, "{\"interface_stats\": {"); + s = format (s, " \"name\": \"%s\",", request); + s = format (s, " \"stats\": \"%s\"", "ERRORUnknownInterface"); + s = format (s, "}}\r\n"); + goto out; + } + + sw_if_index = p[0]; + si = vnet_get_sw_interface (vnm, sw_if_index); + + stats = format_vnet_sw_interface_cntrs (stats, &vnm->interface_main, si, + 1 /* want json */ ); + /* Build answer */ + s = format (s, "{\"interface_stats\": {"); + s = format (s, "\"name\": \"%s\",\n", request); + s = format (s, "%s", stats); + s = format (s, "}}\n"); + vec_free (stats); + +out: + hs->data = s; + hs->data_offset = 0; + hs->cache_pool_index = ~0; + hs->free_data = 1; + return 0; +} + +void +builtinurl_handler_init (builtinurl_main_t * bm) +{ + + bm->register_handler (handle_get_version, "version.json", + HTTP_BUILTIN_METHOD_GET); + bm->register_handler (handle_get_interface_stats, + "interface_stats.json", HTTP_BUILTIN_METHOD_POST); +} + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ |