net: shell: Add HTTP server connection monitoring

The "net http monitor" command turns on HTTP monitoring,
which means that for each incoming HTTP or HTTPS request,
a information about source and destination address, and
the HTTP request URL is printed.
User can disable the monitoring by "net http" command.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2017-06-05 17:32:07 +03:00
commit fc125e0e2c
3 changed files with 46 additions and 7 deletions

View file

@ -776,8 +776,10 @@ typedef void (*http_server_cb_t)(struct http_server_ctx *entry,
void *user_data);
void http_server_conn_foreach(http_server_cb_t cb, void *user_data);
void http_server_conn_monitor(http_server_cb_t cb, void *user_data);
#else
#define http_server_conn_foreach(...)
#define http_server_conn_monitor(...)
#endif /* CONFIG_NET_DEBUG_HTTP_CONN */
/**

View file

@ -949,8 +949,12 @@ static char *http_str_output(char *output, int outlen, const char *str, int len)
len = outlen;
}
memcpy(output, str, len);
output[len] = '\0';
if (len == 0) {
memset(output, 0, outlen);
} else {
memcpy(output, str, len);
output[len] = '\0';
}
return output;
}
@ -990,9 +994,27 @@ int net_shell_cmd_http(int argc, char *argv[])
ARG_UNUSED(argv);
#if defined(CONFIG_NET_DEBUG_HTTP_CONN) && defined(CONFIG_HTTP_SERVER)
int count = 0;
static int count;
int arg = 1;
http_server_conn_foreach(http_server_cb, &count);
count = 0;
/* Turn off monitoring if it was enabled */
http_server_conn_monitor(NULL, NULL);
if (strcmp(argv[0], "http")) {
arg++;
}
if (argv[arg]) {
if (strcmp(argv[arg], "monitor") == 0) {
printk("Activating HTTP monitor. Type \"net http\" "
"to disable HTTP connection monitoring.\n");
http_server_conn_monitor(http_server_cb, &count);
}
} else {
http_server_conn_foreach(http_server_cb, &count);
}
#else
printk("Enable CONFIG_NET_DEBUG_HTTP_CONN and CONFIG_HTTP_SERVER "
"to get HTTP server connection information\n");
@ -1773,7 +1795,9 @@ static struct shell_cmd net_commands[] = {
"dns <hostname> [A or AAAA]\n\tQuery IPv4 address (default) or "
"IPv6 address for a host name" },
{ "http", net_shell_cmd_http,
"\n\tPrint information about active HTTP connections" },
"\n\tPrint information about active HTTP connections\n"
"http monitor\n\tStart monitoring HTTP connections\n"
"http\n\tTurn off HTTP connection monitoring" },
{ "iface", net_shell_cmd_iface,
"\n\tPrint information about network interfaces" },
{ "mem", net_shell_cmd_mem,

View file

@ -64,9 +64,16 @@ static void https_disable(struct http_server_ctx *ctx);
/** List of HTTP connections */
static sys_slist_t http_conn;
static http_server_cb_t ctx_mon;
static void *mon_user_data;
static void http_server_conn_add(struct http_server_ctx *ctx)
{
sys_slist_prepend(&http_conn, &ctx->node);
if (ctx_mon) {
ctx_mon(ctx, mon_user_data);
}
}
static void http_server_conn_del(struct http_server_ctx *ctx)
@ -82,6 +89,12 @@ void http_server_conn_foreach(http_server_cb_t cb, void *user_data)
cb(ctx, user_data);
}
}
void http_server_conn_monitor(http_server_cb_t cb, void *user_data)
{
ctx_mon = cb;
mon_user_data = user_data;
}
#else
#define http_server_conn_add(...)
#define http_server_conn_del(...)
@ -435,8 +448,6 @@ static inline void new_client(struct http_server_ctx *http_ctx,
sprint_ipaddr(buf, sizeof(buf), addr),
net_ctx);
#endif /* CONFIG_NET_DEBUG_HTTP */
http_server_conn_add(http_ctx);
}
static inline void new_server(struct http_server_ctx *ctx,
@ -500,6 +511,8 @@ static int on_url(struct http_parser *parser, const char *at, size_t length)
ctx->req.url = at;
ctx->req.url_len = length;
http_server_conn_add(ctx);
return 0;
}