From 80dd1a8ee3414f728baa9e45dd2b14d7045effea Mon Sep 17 00:00:00 2001 From: Jim Morris Date: Fri, 7 Sep 2018 11:30:56 +0100 Subject: [PATCH] Add http get request queryt to immediaterly return the query string (like ?) add q telnet command to do likewise --- src/libs/Network/uip/Network.cpp | 6 ++++++ src/libs/Network/uip/telnetd/shell.cpp | 11 +++++++++-- src/libs/Network/uip/webserver/httpd.c | 18 +++++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/libs/Network/uip/Network.cpp b/src/libs/Network/uip/Network.cpp index d119b124..983bbaa4 100644 --- a/src/libs/Network/uip/Network.cpp +++ b/src/libs/Network/uip/Network.cpp @@ -397,6 +397,12 @@ void Network::on_main_loop(void *argument) } +extern "C" const char *get_query_string() +{ + const char* dup= strdup(THEKERNEL->get_query_string().c_str()); + return dup; +} + // select between webserver and telnetd server extern "C" void app_select_appcall(void) { diff --git a/src/libs/Network/uip/telnetd/shell.cpp b/src/libs/Network/uip/telnetd/shell.cpp index 9d84fcdc..d4e4fb88 100644 --- a/src/libs/Network/uip/telnetd/shell.cpp +++ b/src/libs/Network/uip/telnetd/shell.cpp @@ -61,7 +61,7 @@ bool Shell::parse(register char *str, const struct ptentry *t) { const struct ptentry *p; for (p = t; p->command != 0; ++p) { - if (strncasecmp(str, p->command, strlen(p->command)) == 0) { + if (strcasecmp(str, p->command) == 0) { break; } } @@ -73,13 +73,19 @@ bool Shell::parse(register char *str, const struct ptentry *t) /*---------------------------------------------------------------------------*/ static void help(char *str, Shell *sh) { - sh->output("Available commands: All others are passed on\n"); + sh->output("Available telent commands: All others are passed to the command handler\n"); sh->output("netstat - show network info\n"); sh->output("h - show network help\n"); + sh->output("? - show current query status\n"); sh->output("help - show command help\n"); sh->output("exit, quit - exit shell\n"); } +static void query(char *str, Shell *sh) +{ + sh->output(THEKERNEL->get_query_string().c_str()); +} + /*---------------------------------------------------------------------------*/ static const char *states[] = { "CLOSED", @@ -191,6 +197,7 @@ static const struct ptentry parsetab[] = { {"exit", quit}, {"quit", quit}, {"ntest", ntest}, + {"?", query}, {"h", help}, /* Default action */ diff --git a/src/libs/Network/uip/webserver/httpd.c b/src/libs/Network/uip/webserver/httpd.c index 1bedfc91..f9b91fd3 100644 --- a/src/libs/Network/uip/webserver/httpd.c +++ b/src/libs/Network/uip/webserver/httpd.c @@ -98,6 +98,7 @@ //#define DEBUG_PRINTF printf #define DEBUG_PRINTF(...) +extern const char *get_query_string(); // this callback gets the results of a command, line by line. need to check if // we need to stall the upstream sender return 0 if stalled 1 if ok to keep @@ -314,6 +315,16 @@ static PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr)) { return send_headers_3(s, statushdr, 1); } +static PT_THREAD(send_string(struct httpd_state *s, const char *str, int duped)) +{ + PSOCK_BEGIN(&s->sout); + + PSOCK_SEND_STR(&s->sout, str); + if(duped) { + free((void *)str); + } + PSOCK_END(&s->sout); +} /*---------------------------------------------------------------------------*/ static PT_THREAD(handle_output(struct httpd_state *s)) @@ -355,7 +366,12 @@ PT_THREAD(handle_output(struct httpd_state *s)) } else { // Presume method GET - if (!fs_open(s)) { // Note this has the side effect of opening the file + + if (strcmp(s->filename, "/query") == 0) { // query short cut + PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200)); + PT_WAIT_THREAD(&s->outputpt, send_string(s, get_query_string(), 1)); + + } else if (!fs_open(s)) { // Note this has the side effect of opening the file DEBUG_PRINTF("404 file not found\n"); httpd_fs_open(http_404_html, &s->file); strcpy(s->filename, http_404_html); -- 2.20.1