builtinurl: initial working attempt
[vpp.git] / src / plugins / builtinurl / builtins.c
1 /*
2  * Copyright (c) 2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/vnet.h>
17 #include <builtinurl/builtinurl.h>
18 #include <http_static/http_static.h>
19 #include <vpp/app/version.h>
20
21 int
22 handle_get_version (u8 * request, http_session_t * hs)
23 {
24   u8 *s = 0;
25
26   /* Build some json bullshit */
27   s = format (s, "{\"vpp_details\": {");
28   s = format (s, "   \"version\": \"%s\",", VPP_BUILD_VER);
29   s = format (s, "   \"build_date\": \"%s\"}}\r\n", VPP_BUILD_DATE);
30
31   hs->data = s;
32   hs->data_offset = 0;
33   hs->cache_pool_index = ~0;
34   hs->free_data = 1;
35   return 0;
36 }
37
38 void
39 trim_path_from_request (u8 * s, char *path)
40 {
41   u8 *cp;
42   int trim_length = strlen (path) + 1 /* remove '?' */ ;
43
44   /* Get rid of the path and question-mark */
45   vec_delete (s, trim_length, 0);
46
47   /* Tail trim irrelevant browser info */
48   cp = s;
49   while ((cp - s) < vec_len (s))
50     {
51       if (*cp == ' ')
52         {
53           /*
54            * Makes request a vector which happens to look
55            * like a c-string.
56            */
57           *cp = 0;
58           _vec_len (s) = cp - s;
59           break;
60         }
61       cp++;
62     }
63 }
64
65 int
66 handle_get_interface_stats (u8 * request, http_session_t * hs)
67 {
68   u8 *s = 0, *stats = 0;
69   u32 sw_if_index;
70   uword *p;
71   vnet_sw_interface_t *si;
72   u8 *format_vnet_sw_interface_cntrs (u8 * s, vnet_interface_main_t * im,
73                                       vnet_sw_interface_t * si, int json);
74
75   vnet_main_t *vnm = vnet_get_main ();
76
77   trim_path_from_request (request, "interface_stats.json");
78
79   /* get data */
80
81   p = hash_get (vnm->interface_main.hw_interface_by_name, request);
82   if (!p)
83     {
84       clib_warning ("Couldn't find interface '%v'", request);
85
86       s = format (s, "{\"interface_stats\": {");
87       s = format (s, "   \"name\": \"%s\",", request);
88       s = format (s, "   \"stats\": \"%s\"", "ERRORUnknownInterface");
89       s = format (s, "}}\r\n");
90       goto out;
91     }
92
93   sw_if_index = p[0];
94   si = vnet_get_sw_interface (vnm, sw_if_index);
95
96   stats = format_vnet_sw_interface_cntrs (stats, &vnm->interface_main, si,
97                                           1 /* want json */ );
98   /* Build answer */
99   s = format (s, "{\"interface_stats\": {");
100   s = format (s, "\"name\": \"%s\",\n", request);
101   s = format (s, "%s", stats);
102   s = format (s, "}}\n");
103   vec_free (stats);
104
105 out:
106   hs->data = s;
107   hs->data_offset = 0;
108   hs->cache_pool_index = ~0;
109   hs->free_data = 1;
110   return 0;
111 }
112
113 void
114 builtinurl_handler_init (builtinurl_main_t * bm)
115 {
116
117   bm->register_handler (handle_get_version, "version.json",
118                         HTTP_BUILTIN_METHOD_GET);
119   bm->register_handler (handle_get_interface_stats,
120                         "interface_stats.json", HTTP_BUILTIN_METHOD_POST);
121 }
122
123 /*
124  * fd.io coding-style-patch-verification: ON
125  *
126  * Local Variables:
127  * eval: (c-set-style "gnu")
128  * End:
129  */