Static http server
[vpp.git] / src / plugins / http_static / http_static.c
1 /*
2  * http_static.c - skeleton vpp engine plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <http_static/http_static.h>
21
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vpp/app/version.h>
25
26 /* define message IDs */
27 #include <http_static/http_static_msg_enum.h>
28
29 /* define message structures */
30 #define vl_typedefs
31 #include <http_static/http_static_all_api_h.h>
32 #undef vl_typedefs
33
34 /* define generated endian-swappers */
35 #define vl_endianfun
36 #include <http_static/http_static_all_api_h.h>
37 #undef vl_endianfun
38
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #define vl_printfun
42 #include <http_static/http_static_all_api_h.h>
43 #undef vl_printfun
44
45 /* Get the API version number */
46 #define vl_api_version(n,v) static u32 api_version=(v);
47 #include <http_static/http_static_all_api_h.h>
48 #undef vl_api_version
49
50 #define REPLY_MSG_ID_BASE hmp->msg_id_base
51 #include <vlibapi/api_helper_macros.h>
52
53 http_static_main_t http_static_main;
54
55 /* List of message types that this plugin understands */
56
57 #define foreach_http_static_plugin_api_msg                           \
58 _(HTTP_STATIC_ENABLE, http_static_enable)
59
60 /* API message handler */
61 static void vl_api_http_static_enable_t_handler
62   (vl_api_http_static_enable_t * mp)
63 {
64   vl_api_http_static_enable_reply_t *rmp;
65   http_static_main_t *hmp = &http_static_main;
66   int rv;
67
68   mp->uri[ARRAY_LEN (mp->uri) - 1] = 0;
69   mp->www_root[ARRAY_LEN (mp->www_root) - 1] = 0;
70
71   rv = http_static_server_enable_api
72     (ntohl (mp->fifo_size), ntohl (mp->cache_size_limit),
73      ntohl (mp->prealloc_fifos),
74      ntohl (mp->private_segment_size), mp->www_root, mp->uri);
75
76   REPLY_MACRO (VL_API_HTTP_STATIC_ENABLE_REPLY);
77 }
78
79 /* Set up the API message handling tables */
80 static clib_error_t *
81 http_static_plugin_api_hookup (vlib_main_t * vm)
82 {
83   http_static_main_t *hmp = &http_static_main;
84 #define _(N,n)                                                  \
85     vl_msg_api_set_handlers((VL_API_##N + hmp->msg_id_base),     \
86                            #n,                                  \
87                            vl_api_##n##_t_handler,              \
88                            vl_noop_handler,                     \
89                            vl_api_##n##_t_endian,               \
90                            vl_api_##n##_t_print,                \
91                            sizeof(vl_api_##n##_t), 1);
92   foreach_http_static_plugin_api_msg;
93 #undef _
94
95   return 0;
96 }
97
98 #define vl_msg_name_crc_list
99 #include <http_static/http_static_all_api_h.h>
100 #undef vl_msg_name_crc_list
101
102 static void
103 setup_message_id_table (http_static_main_t * hmp, api_main_t * am)
104 {
105 #define _(id,n,crc)   vl_msg_api_add_msg_name_crc (am, #n  #crc, id + hmp->msg_id_base);
106   foreach_vl_msg_name_crc_http_static;
107 #undef _
108 }
109
110 static clib_error_t *
111 http_static_init (vlib_main_t * vm)
112 {
113   http_static_main_t *hmp = &http_static_main;
114   clib_error_t *error = 0;
115   u8 *name;
116
117   hmp->vlib_main = vm;
118   hmp->vnet_main = vnet_get_main ();
119
120   name = format (0, "http_static_%08x%c", api_version, 0);
121
122   /* Ask for a correctly-sized block of API message decode slots */
123   hmp->msg_id_base = vl_msg_api_get_msg_ids
124     ((char *) name, VL_MSG_FIRST_AVAILABLE);
125
126   error = http_static_plugin_api_hookup (vm);
127
128   /* Add our API messages to the global name_crc hash table */
129   setup_message_id_table (hmp, &api_main);
130
131   vec_free (name);
132
133   return error;
134 }
135
136 VLIB_INIT_FUNCTION (http_static_init);
137
138 /* *INDENT-OFF* */
139 VLIB_PLUGIN_REGISTER () =
140 {
141   .version = VPP_BUILD_VER,
142   .description = "http static server plugin"
143 };
144 /* *INDENT-ON* */
145
146 /*
147  * fd.io coding-style-patch-verification: ON
148  *
149  * Local Variables:
150  * eval: (c-set-style "gnu")
151  * End:
152  */