Static http server

Good enough to serve the vpp sphinx and doxygen docs. Knows about
html, css, and javascript files.

Change-Id: Ib18c19f07f35f91ba935ea26ed7be406dacf2205
Signed-off-by: Dave Barach <dave@barachs.net>
This commit is contained in:
Dave Barach
2019-04-11 17:06:21 -04:00
committed by Florin Coras
parent 28aa539f7d
commit 22bc2c46e3
9 changed files with 1960 additions and 0 deletions

View File

@ -0,0 +1,6 @@
set term pag off
create tap host-if-name lstack host-ip4-addr 192.168.10.2/24
set int ip address tap0 192.168.10.1/24
set int state tap0 up
http static server www-root /tmp/www uri tcp://0.0.0.0/80 cache-size 1m

View File

@ -0,0 +1,30 @@
# Copyright (c) <current-year> <your-organization>
# 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.
add_vpp_plugin(http_static
SOURCES
http_static.c
static_server.c
http_static.h
API_FILES
http_static.api
INSTALL_HEADERS
http_static_all_api_h.h
http_static_msg_enum.h
API_TEST_SOURCES
http_static_test.c
)

View File

@ -0,0 +1,35 @@
/** \file
This file defines static http servercontrol-plane API messages
*/
option version = "1.0.0";
/** \brief Configure and enable the static http server
@param client_index - opaque cookie to identify the sender
@param context - sender context, to match reply w/ request
@param fifo_size - size (in bytes) of the session FIFOs
@param cache_size_limit - size (in bytes) of the in-memory file data cache
@param prealloc_fifos - number of preallocated fifos (usually 0)
@param private_segment_size - fifo segment size (usually 0)
@param www_root - html root path
@param uri - bind URI, defaults to "tcp://0.0.0.0/80"
*/
autoreply define http_static_enable {
/* Client identifier, set from api_main.my_client_index */
u32 client_index;
/* Arbitrary context, so client can match reply to request */
u32 context;
/* Typical options */
u32 fifo_size;
u32 cache_size_limit;
/* Unusual options */
u32 prealloc_fifos;
u32 private_segment_size;
/* Root of the html path */
u8 www_root[256];
/* The bind URI */
u8 uri[256];
};

View File

@ -0,0 +1,152 @@
/*
* http_static.c - skeleton vpp engine plug-in
*
* Copyright (c) <current-year> <your-organization>
* 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 <vnet/plugin/plugin.h>
#include <http_static/http_static.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vpp/app/version.h>
/* define message IDs */
#include <http_static/http_static_msg_enum.h>
/* define message structures */
#define vl_typedefs
#include <http_static/http_static_all_api_h.h>
#undef vl_typedefs
/* define generated endian-swappers */
#define vl_endianfun
#include <http_static/http_static_all_api_h.h>
#undef vl_endianfun
/* instantiate all the print functions we know about */
#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
#define vl_printfun
#include <http_static/http_static_all_api_h.h>
#undef vl_printfun
/* Get the API version number */
#define vl_api_version(n,v) static u32 api_version=(v);
#include <http_static/http_static_all_api_h.h>
#undef vl_api_version
#define REPLY_MSG_ID_BASE hmp->msg_id_base
#include <vlibapi/api_helper_macros.h>
http_static_main_t http_static_main;
/* List of message types that this plugin understands */
#define foreach_http_static_plugin_api_msg \
_(HTTP_STATIC_ENABLE, http_static_enable)
/* 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;
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);
REPLY_MACRO (VL_API_HTTP_STATIC_ENABLE_REPLY);
}
/* Set up the API message handling tables */
static clib_error_t *
http_static_plugin_api_hookup (vlib_main_t * vm)
{
http_static_main_t *hmp = &http_static_main;
#define _(N,n) \
vl_msg_api_set_handlers((VL_API_##N + hmp->msg_id_base), \
#n, \
vl_api_##n##_t_handler, \
vl_noop_handler, \
vl_api_##n##_t_endian, \
vl_api_##n##_t_print, \
sizeof(vl_api_##n##_t), 1);
foreach_http_static_plugin_api_msg;
#undef _
return 0;
}
#define vl_msg_name_crc_list
#include <http_static/http_static_all_api_h.h>
#undef vl_msg_name_crc_list
static void
setup_message_id_table (http_static_main_t * hmp, api_main_t * am)
{
#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n #crc, id + hmp->msg_id_base);
foreach_vl_msg_name_crc_http_static;
#undef _
}
static clib_error_t *
http_static_init (vlib_main_t * vm)
{
http_static_main_t *hmp = &http_static_main;
clib_error_t *error = 0;
u8 *name;
hmp->vlib_main = vm;
hmp->vnet_main = vnet_get_main ();
name = format (0, "http_static_%08x%c", api_version, 0);
/* Ask for a correctly-sized block of API message decode slots */
hmp->msg_id_base = vl_msg_api_get_msg_ids
((char *) name, VL_MSG_FIRST_AVAILABLE);
error = http_static_plugin_api_hookup (vm);
/* Add our API messages to the global name_crc hash table */
setup_message_id_table (hmp, &api_main);
vec_free (name);
return error;
}
VLIB_INIT_FUNCTION (http_static_init);
/* *INDENT-OFF* */
VLIB_PLUGIN_REGISTER () =
{
.version = VPP_BUILD_VER,
.description = "http static server plugin"
};
/* *INDENT-ON* */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -0,0 +1,54 @@
/*
* http_static.h - skeleton vpp engine plug-in header file
*
* Copyright (c) <current-year> <your-organization>
* 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 __included_http_static_h__
#define __included_http_static_h__
#include <vnet/vnet.h>
#include <vnet/ip/ip.h>
#include <vnet/ethernet/ethernet.h>
#include <vppinfra/hash.h>
#include <vppinfra/error.h>
#include <vppinfra/time_range.h>
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;
int http_static_server_enable_api (u32 fifo_size, u32 cache_limit,
u32 prealloc_fifos,
u32 private_segment_size,
u8 * www_root, u8 * uri);
#endif /* __included_http_static_h__ */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -0,0 +1,19 @@
/*
* http_static_all_api_h.h - skeleton vpp engine plug-in api #include file
*
* Copyright (c) <current-year> <your-organization>
* 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 the generated file, see BUILT_SOURCES in Makefile.am */
#include <http_static/http_static.api.h>

View File

@ -0,0 +1,31 @@
/*
* http_static_msg_enum.h - skeleton vpp engine plug-in message enumeration
*
* Copyright (c) <current-year> <your-organization>
* 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 included_http_static_msg_enum_h
#define included_http_static_msg_enum_h
#include <vppinfra/byte_order.h>
#define vl_msg_id(n,h) n,
typedef enum {
#include <http_static/http_static_all_api_h.h>
/* We'll want to know how many messages IDs we need... */
VL_MSG_FIRST_AVAILABLE,
} vl_msg_id_t;
#undef vl_msg_id
#endif /* included_http_static_msg_enum_h */

View File

@ -0,0 +1,241 @@
/*
* http_static.c - skeleton vpp-api-test plug-in
*
* Copyright (c) <current-year> <your-organization>
* 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 <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vppinfra/error.h>
uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
/* Declare message IDs */
#include <http_static/http_static_msg_enum.h>
/* define message structures */
#define vl_typedefs
#include <http_static/http_static_all_api_h.h>
#undef vl_typedefs
/* declare message handlers for each api */
#define vl_endianfun /* define message structures */
#include <http_static/http_static_all_api_h.h>
#undef vl_endianfun
/* instantiate all the print functions we know about */
#define vl_print(handle, ...)
#define vl_printfun
#include <http_static/http_static_all_api_h.h>
#undef vl_printfun
/* Get the API version number. */
#define vl_api_version(n,v) static u32 api_version=(v);
#include <http_static/http_static_all_api_h.h>
#undef vl_api_version
typedef struct
{
/* API message ID base */
u16 msg_id_base;
vat_main_t *vat_main;
} http_static_test_main_t;
http_static_test_main_t http_static_test_main;
#define __plugin_msg_base http_static_test_main.msg_id_base
#include <vlibapi/vat_helper_macros.h>
#define foreach_standard_reply_retval_handler \
_(http_static_enable_reply)
#define _(n) \
static void vl_api_##n##_t_handler \
(vl_api_##n##_t * mp) \
{ \
vat_main_t * vam = http_static_test_main.vat_main; \
i32 retval = ntohl(mp->retval); \
if (vam->async_mode) { \
vam->async_errors += (retval < 0); \
} else { \
vam->retval = retval; \
vam->result_ready = 1; \
} \
}
foreach_standard_reply_retval_handler;
#undef _
/*
* Table of message reply handlers, must include boilerplate handlers
* we just generated
*/
#define foreach_vpe_api_reply_msg \
_(HTTP_STATIC_ENABLE_REPLY, http_static_enable_reply)
static int
api_http_static_enable (vat_main_t * vam)
{
unformat_input_t *line_input = vam->input;
vl_api_http_static_enable_t *mp;
u64 tmp;
u8 *www_root = 0;
u8 *uri = 0;
u32 prealloc_fifos = 0;
u32 private_segment_size = 0;
u32 fifo_size = 8 << 10;
u32 cache_size_limit = 1 << 20;
int ret;
/* Parse args required to build the message */
while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
{
if (unformat (line_input, "www-root %s", &www_root))
;
else if (unformat (line_input, "prealloc-fifos %d", &prealloc_fifos))
;
else if (unformat (line_input, "private-segment-size %U",
unformat_memory_size, &tmp))
{
if (tmp >= 0x100000000ULL)
{
errmsg ("private segment size %llu, too large", tmp);
return -99;
}
private_segment_size = (u32) tmp;
}
else if (unformat (line_input, "fifo-size %U", unformat_memory_size,
&tmp))
{
fifo_size = (u32) tmp;
}
else if (unformat (line_input, "cache-size %U", unformat_memory_size,
&tmp))
{
if (tmp < (128ULL << 10))
{
errmsg ("cache-size must be at least 128kb");
return -99;
}
cache_size_limit = (u32) tmp;
}
else if (unformat (line_input, "uri %s", &uri))
;
else
{
errmsg ("unknown input `%U'", format_unformat_error, line_input);
return -99;
}
}
if (www_root == 0)
{
errmsg ("Must specify www-root");
return -99;
}
if (www_root == 0)
{
errmsg ("Must specify www-root");
return -99;
}
if (uri == 0)
uri = format (0, "tcp://0.0.0.0/80%c", 0);
/* Construct the API message */
M (HTTP_STATIC_ENABLE, mp);
clib_strncpy ((char *) mp->www_root, (char *) www_root,
ARRAY_LEN (mp->www_root) - 1);
clib_strncpy ((char *) mp->uri, (char *) uri, ARRAY_LEN (mp->uri) - 1);
mp->fifo_size = ntohl (fifo_size);
mp->cache_size_limit = ntohl (cache_size_limit);
mp->prealloc_fifos = ntohl (prealloc_fifos);
mp->private_segment_size = ntohl (private_segment_size);
/* send it... */
S (mp);
/* Wait for a reply... */
W (ret);
return ret;
}
/*
* List of messages that the api test plugin sends,
* and that the data plane plugin processes
*/
#define foreach_vpe_api_msg \
_(http_static_enable, "www-root <path> [prealloc-fios <nn>]\n" \
"[private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]\n")
static void
http_static_api_hookup (vat_main_t * vam)
{
http_static_test_main_t *htmp = &http_static_test_main;
/* Hook up handlers for replies from the data plane plug-in */
#define _(N,n) \
vl_msg_api_set_handlers((VL_API_##N + htmp->msg_id_base), \
#n, \
vl_api_##n##_t_handler, \
vl_noop_handler, \
vl_api_##n##_t_endian, \
vl_api_##n##_t_print, \
sizeof(vl_api_##n##_t), 1);
foreach_vpe_api_reply_msg;
#undef _
/* API messages we can send */
#define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
foreach_vpe_api_msg;
#undef _
/* Help strings */
#define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
foreach_vpe_api_msg;
#undef _
}
clib_error_t *
vat_plugin_register (vat_main_t * vam)
{
http_static_test_main_t *htmp = &http_static_test_main;
u8 *name;
htmp->vat_main = vam;
/* Ask the vpp engine for the first assigned message-id */
name = format (0, "http_static_%08x%c", api_version, 0);
htmp->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
if (htmp->msg_id_base != (u16) ~ 0)
http_static_api_hookup (vam);
vec_free (name);
return 0;
}
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

File diff suppressed because it is too large Load Diff