http_static: code cleanup
[vpp.git] / src / plugins / http_static / http_static.c
1 /*
2  * Copyright (c) 2017-2022 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 <vnet/plugin/plugin.h>
18 #include <http_static/http_static.h>
19
20 #include <vlibapi/api.h>
21 #include <vlibmemory/api.h>
22 #include <vpp/app/version.h>
23
24 /* define message IDs */
25 #include <http_static/http_static.api_enum.h>
26 #include <http_static/http_static.api_types.h>
27
28 #include <vpp/api/types.h>
29
30 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
31
32 #define REPLY_MSG_ID_BASE hsm->msg_id_base
33 #include <vlibapi/api_helper_macros.h>
34
35 /** \brief Register a builtin GET or POST handler
36  */
37 __clib_export void
38 http_static_server_register_builtin_handler (void *fp, char *url,
39                                              http_req_method_t request_type)
40 {
41   hss_main_t *hsm = &hss_main;
42   uword *p, *builtin_table;
43
44   builtin_table = (request_type == HTTP_REQ_GET) ? hsm->get_url_handlers :
45                                                    hsm->post_url_handlers;
46
47   p = hash_get_mem (builtin_table, url);
48
49   if (p)
50     {
51       clib_warning ("WARNING: attempt to replace handler for %s '%s' ignored",
52                     (request_type == HTTP_REQ_GET) ? "GET" : "POST", url);
53       return;
54     }
55
56   hash_set_mem (builtin_table, url, (uword) fp);
57
58   /*
59    * Need to update the hash table pointer in http_static_server_main
60    * in case we just expanded it...
61    */
62   if (request_type == HTTP_REQ_GET)
63     hsm->get_url_handlers = builtin_table;
64   else
65     hsm->post_url_handlers = builtin_table;
66 }
67
68 /** \brief API helper function for vl_api_http_static_enable_t messages
69  */
70 static int
71 hss_enable_api (u32 fifo_size, u32 cache_limit, u32 prealloc_fifos,
72                 u32 private_segment_size, u8 *www_root, u8 *uri)
73 {
74   hss_main_t *hsm = &hss_main;
75   int rv;
76
77   hsm->fifo_size = fifo_size;
78   hsm->cache_limit = cache_limit;
79   hsm->prealloc_fifos = prealloc_fifos;
80   hsm->private_segment_size = private_segment_size;
81   hsm->www_root = format (0, "%s%c", www_root, 0);
82   hsm->uri = format (0, "%s%c", uri, 0);
83
84   if (vec_len (hsm->www_root) < 2)
85     return VNET_API_ERROR_INVALID_VALUE;
86
87   if (hsm->app_index != ~0)
88     return VNET_API_ERROR_APP_ALREADY_ATTACHED;
89
90   vnet_session_enable_disable (hsm->vlib_main, 1 /* turn on TCP, etc. */);
91
92   rv = hss_create (hsm->vlib_main);
93   switch (rv)
94     {
95     case 0:
96       break;
97     default:
98       vec_free (hsm->www_root);
99       vec_free (hsm->uri);
100       return VNET_API_ERROR_INIT_FAILED;
101     }
102   return 0;
103 }
104
105 /* API message handler */
106 static void vl_api_http_static_enable_t_handler
107   (vl_api_http_static_enable_t * mp)
108 {
109   vl_api_http_static_enable_reply_t *rmp;
110   hss_main_t *hsm = &hss_main;
111   int rv;
112
113   mp->uri[ARRAY_LEN (mp->uri) - 1] = 0;
114   mp->www_root[ARRAY_LEN (mp->www_root) - 1] = 0;
115
116   rv =
117     hss_enable_api (ntohl (mp->fifo_size), ntohl (mp->cache_size_limit),
118                     ntohl (mp->prealloc_fifos),
119                     ntohl (mp->private_segment_size), mp->www_root, mp->uri);
120
121   REPLY_MACRO (VL_API_HTTP_STATIC_ENABLE_REPLY);
122 }
123
124 #include <http_static/http_static.api.c>
125 static clib_error_t *
126 hss_api_init (vlib_main_t *vm)
127 {
128   hss_main_t *hsm = &hss_main;
129
130   /* Ask for a correctly-sized block of API message decode slots */
131   hsm->msg_id_base = setup_message_id_table ();
132
133   return 0;
134 }
135
136 VLIB_INIT_FUNCTION (hss_api_init);
137
138 VLIB_PLUGIN_REGISTER () =
139 {
140   .version = VPP_BUILD_VER,
141   .description = "HTTP Static Server"
142 };
143
144 /*
145  * fd.io coding-style-patch-verification: ON
146  *
147  * Local Variables:
148  * eval: (c-set-style "gnu")
149  * End:
150  */