http_static: api add keepalive-timeout

Type: improvement

Change-Id: Ia1e0dcf562fd1538794542207ac2cad97d168c6c
Signed-off-by: Matus Fabian <matfabia@cisco.com>
This commit is contained in:
Matus Fabian
2024-11-12 15:48:16 +01:00
committed by Florin Coras
parent 942e3d583c
commit a4597a74aa
6 changed files with 160 additions and 145 deletions

View File

@ -3,39 +3,7 @@
This file defines static http server control-plane API messages
*/
option version = "2.2.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 {
option deprecated;
/* 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 */
string www_root[256];
/* The bind URI */
string uri[256];
};
option version = "2.3.0";
/** \brief Configure and enable the static http server
@param client_index - opaque cookie to identify the sender
@ -50,6 +18,8 @@ autoreply define http_static_enable {
*/
autoreply define http_static_enable_v2 {
option deprecated;
/* Client identifier, set from api_main.my_client_index */
u32 client_index;
@ -68,3 +38,39 @@ autoreply define http_static_enable_v2 {
/* The bind URI */
string uri[256];
};
/** \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 max_age - how long a response is considered fresh (in seconds)
@param keepalive_timeout - timeout during which client connection will stay open (in seconds)
@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_v3 {
option deprecated;
/* 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;
u32 max_age [default=600];
u32 keepalive_timeout [default=60];
/* Unusual options */
u32 prealloc_fifos;
u32 private_segment_size;
/* Root of the html path */
string www_root[256];
/* The bind URI */
string uri[256];
};

View File

@ -66,7 +66,8 @@ hss_register_url_handler (hss_url_handler_fn fp, const char *url,
*/
static int
hss_enable_api (u32 fifo_size, u32 cache_limit, u32 prealloc_fifos,
u32 private_segment_size, u8 *www_root, u8 *uri, u32 max_age)
u32 private_segment_size, u8 *www_root, u8 *uri, u32 max_age,
u32 keepalive_timeout)
{
hss_main_t *hsm = &hss_main;
int rv;
@ -78,6 +79,7 @@ hss_enable_api (u32 fifo_size, u32 cache_limit, u32 prealloc_fifos,
hsm->www_root = format (0, "%s%c", www_root, 0);
hsm->uri = format (0, "%s%c", uri, 0);
hsm->max_age = max_age;
hsm->keepalive_timeout = keepalive_timeout;
if (vec_len (hsm->www_root) < 2)
return VNET_API_ERROR_INVALID_VALUE;
@ -103,25 +105,6 @@ hss_enable_api (u32 fifo_size, u32 cache_limit, u32 prealloc_fifos,
return 0;
}
/* 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;
hss_main_t *hsm = &hss_main;
int rv;
mp->uri[ARRAY_LEN (mp->uri) - 1] = 0;
mp->www_root[ARRAY_LEN (mp->www_root) - 1] = 0;
rv = hss_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,
HSS_DEFAULT_MAX_AGE);
REPLY_MACRO (VL_API_HTTP_STATIC_ENABLE_REPLY);
}
/* API message handler */
static void
vl_api_http_static_enable_v2_t_handler (vl_api_http_static_enable_v2_t *mp)
@ -136,11 +119,30 @@ vl_api_http_static_enable_v2_t_handler (vl_api_http_static_enable_v2_t *mp)
rv = hss_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,
ntohl (mp->max_age));
ntohl (mp->max_age), HSS_DEFAULT_KEEPALIVE_TIMEOUT);
REPLY_MACRO (VL_API_HTTP_STATIC_ENABLE_V2_REPLY);
}
/* API message handler */
static void
vl_api_http_static_enable_v3_t_handler (vl_api_http_static_enable_v3_t *mp)
{
vl_api_http_static_enable_v3_reply_t *rmp;
hss_main_t *hsm = &hss_main;
int rv;
mp->uri[ARRAY_LEN (mp->uri) - 1] = 0;
mp->www_root[ARRAY_LEN (mp->www_root) - 1] = 0;
rv = hss_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,
ntohl (mp->max_age), ntohl (mp->keepalive_timeout));
REPLY_MACRO (VL_API_HTTP_STATIC_ENABLE_V3_REPLY);
}
#include <http_static/http_static.api.c>
static clib_error_t *
hss_api_init (vlib_main_t *vm)

View File

@ -24,6 +24,7 @@
#include <http_static/http_cache.h>
#define HSS_DEFAULT_MAX_AGE 600
#define HSS_DEFAULT_KEEPALIVE_TIMEOUT 60
/** @file http_static.h
* Static http server definitions

View File

@ -38,95 +38,6 @@ 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>
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))
{
if (tmp >= 0x100000000ULL)
{
errmsg ("fifo-size %llu, too large", tmp);
return -99;
}
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 (uri == 0)
uri = format (0, "tcp://0.0.0.0/80%c", 0);
/* Construct the API message */
M (HTTP_STATIC_ENABLE, mp);
strncpy_s ((char *) mp->www_root, 256, (const char *) www_root, 256);
strncpy_s ((char *) mp->uri, 256, (const char *) uri, 256);
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;
}
static int
api_http_static_enable_v2 (vat_main_t *vam)
{
@ -217,6 +128,101 @@ api_http_static_enable_v2 (vat_main_t *vam)
return ret;
}
static int
api_http_static_enable_v3 (vat_main_t *vam)
{
unformat_input_t *line_input = vam->input;
vl_api_http_static_enable_v3_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;
u32 max_age = HSS_DEFAULT_MAX_AGE;
u32 keepalive_timeout = HSS_DEFAULT_KEEPALIVE_TIMEOUT;
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))
{
if (tmp >= 0x100000000ULL)
{
errmsg ("fifo-size %llu, too large", tmp);
return -99;
}
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, "max-age %d", &max_age))
;
else if (unformat (line_input, "keepalive-timeout %d",
&keepalive_timeout))
;
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 (uri == 0)
uri = format (0, "tcp://0.0.0.0/80%c", 0);
/* Construct the API message */
M (HTTP_STATIC_ENABLE_V3, mp);
strncpy_s ((char *) mp->www_root, 256, (const char *) www_root, 256);
strncpy_s ((char *) mp->uri, 256, (const char *) uri, 256);
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);
mp->max_age = ntohl (max_age);
mp->keepalive_timeout = ntohl (keepalive_timeout);
/* send it... */
S (mp);
/* Wait for a reply... */
W (ret);
return ret;
}
#include <http_static/http_static.api_test.c>
/*

View File

@ -902,7 +902,7 @@ hss_create_command_fn (vlib_main_t *vm, unformat_input_t *input,
hsm->fifo_size = 0;
hsm->cache_size = 10 << 20;
hsm->max_age = HSS_DEFAULT_MAX_AGE;
hsm->keepalive_timeout = 60;
hsm->keepalive_timeout = HSS_DEFAULT_KEEPALIVE_TIMEOUT;
/* Get a line of input. */
if (!unformat_user (input, unformat_line_input, line_input))

View File

@ -50,7 +50,7 @@ class TestHttpStaticVapi(VppAsfTestCase):
super(TestHttpStaticVapi, cls).tearDownClass()
def test_http_static_vapi(self):
self.vapi.http_static_enable(
self.vapi.http_static_enable_v3(
www_root="/tmp",
uri="tcp://0.0.0.0/80",
)