1

I am trying to create a simple c++ web-based GUI. I am not interested in using Qt or Visual Studio based GUI. I am rather interested in web based as my requirements are very minimal and basic.

So I came across "Mongoose" the C-based web server. After going through the examples I cooked up some code but it's not working as I have almost zero knowledge about internet programming. I was wondering if any of you have a simple example where I can retrieve the user data from the HTML form either using POST or GET request.

Here is what I have managed so far:

//////   
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mongoose.h"

static const char *s_http_port = "8000";
volatile bool kill_server = FALSE;
struct mg_mgr mgr;
struct mg_connection *nc;
bool control1_triggered = FALSE;
bool control2_triggered = FALSE;

struct file_writer_data {
    FILE *fp;
    size_t bytes_written;
};


static void handle_upload(struct mg_connection *nc, int ev, void *p) {
    printf("Signal received! %d\n", ev);
    control1_triggered = TRUE;

    struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) p;

    printf(mp->data.p);
    switch (ev) {
    case MG_EV_HTTP_PART_DATA:


        break;
    }

}

static void handle_upload2(struct mg_connection *nc, int ev, void *p) {
    printf("Signal received@2! %d\n", ev);
    control2_triggered = TRUE;

}

void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
    (void)ev_data;

    switch (ev) {
    case MG_EV_HTTP_REQUEST:
        // Invoked when the full HTTP request is in the buffer (including body).
        mg_printf(nc, "%s",
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "Connection: close\r\n"
            "\r\n"
            "<html><body>Controls"


            "<form method=\"GET\" action=\"/upload\" "
            "  enctype=\"multipart/form-data\">"
            "<input type = \"text\" name = \"fname\" value = \"John\">"

            "<input type=\"submit\" value=\"Fix Position\"  />"
            "</form>"

            "<form method=\"POST\" action=\"/Kill\" "
            "  enctype=\"multipart/form-data\">"

            "<input type=\"submit\" value=\"Kill Server\"  />"
            "</form>"
            "input.search{width: 20em;  height: 2em;}"
            "</body></html>");
        nc->flags |= MG_F_SEND_AND_CLOSE;
        break;

    }


}

int main() {


    mg_mgr_init(&mgr, NULL);
    nc = mg_bind(&mgr, s_http_port, ev_handler);

    mg_register_http_endpoint(nc, "/upload", handle_upload);
    mg_register_http_endpoint(nc, "/Kill", handle_upload2);
    // Set up HTTP server parameters
    mg_set_protocol_http_websocket(nc);
    while (1);

    return 0;

}

Please note I have been googling around 3 days now, have seen most of the links and questions. But not a lot of support with Mongoose Could you please help me with an example on how to parse GET or POST HTML request using Mongoose ?

Thank you so much.

Cheers, Avi

user1453817
  • 77
  • 2
  • 9

1 Answers1

3

You access post data from nc->content, to get a certain value you use mg_get_var(nc, size of nc, "fname", buffer, size of buffer).

Example:

int size = 1024, ret;
char *buffer = new char[size];
mg_get_var(nc, sizeof(nc), "fname", buffer, size);

Side note Gregwar made a C++ wrapper for Mongoose, its an older version (by about four years) but it might help, link.

Edit:

Method should be mg_get_http_var not mg_get_var

int size = 1024, ret;
char *buffer = new char[size];
mg_get_http_var(nc, "fname", buffer, size);

Link

Txuritan
  • 63
  • 2
  • 6