Mongoose C++: How to parse HTTP GET or POST request using mongoose? - html

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

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

Related

Access violation writing location 0x00000003

So, I'm writing a DQL Neural network. When I run the code through the debugger, it throws this exception
Exception thrown at 0x00D05DFB in Deep Q-learning Neural
Network.exe: 0xC0000005: Access violation writing location 0x00000003.
Here is the relevant code:
The neuron structure:
typedef struct neuron_t {
// not entirely relevant to this problem, but it's nicer to have the full picture
float activation;
float* outputWeights;
float bias;
float z;
float dactv;
float* dw;
float dbias;
float dz;
}Neuron;
The layer structure:
typedef struct layer_t {
int numberOfNeurons;
Neuron* neu;
}Layer;
the main function:
Layer* testTarget = NULL;
Layer* test = NULL;
int numberOfLayers = 3;
int* neuronInlayer[] = { 2,3,4 };
test = createPredictionArchitecture(test, testTarget, numberOfLayers, neuronInlayer); /* I plan to turn this into merely
createPredictionArchitecture(test, testTarget, numberOfLayers, neuronInlayer) once this problem
is fixed.*/
The relevant bit of the createArchitecture function:
int createArchitecture(Layer* predictionNetwork, Layer* targetNetwork, int numberOfLayers, int* neuronInEachLayer) {
predictionNetwork = (Layer*)malloc(numberOfLayers * sizeof(Layer)); if (predictionNetwork == NULL) { fprintf(stderr, "Failed to allocate memory to 'predictionNetwork' in line %d\n ", __LINE__); exit(1); }
else {
printf("Memory successfully allocated to 'predictionNetwork'\n");
}
targetNetwork = (Layer*)malloc(numberOfLayers * sizeof(Layer)); if (targetNetwork == NULL) { fprintf(stderr, "Failed to allocate memory to 'predictionNetwork' in line %d\n ", __LINE__); exit(1); }
else {
printf("Memory successfully allocated to 'targetNetwork'\n");
}
Layer** targetNW = &targetNetwork;
...
}
The exception occurs when I do this:
*targetNW[i] = createLayer(neuronInEachLayer[i]);
Why does this happen, and how should I fix it?
And also, do say if you need to see more of the code.
Well, I figured out the problem; I wasn't assigning any memory to targetNW.

Why does the serial monitor just show an endless loop of the "dot" and not the other serial prints?

The code should transfer an JSON object and the print "hello" in the void loop, but it just repeat the "dot" from the void setup. What did i miss?
I tried different serial prints in the main loop but none of them is actually transferred. Maybe there is an infinite loop in the void setup?
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <vector>
const char* apSsid = "ap-ssid";
const char* apPassword = "ap-password";
const char* clientSsid = "client-ssid";
const char* clientPassword = "client-password";
WiFiEventHandler probeRequestPrintHandler;
String macToString(const unsigned char* mac) {
char buf[20];
snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(buf);
}
std::vector<WiFiEventSoftAPModeProbeRequestReceived> myList;
void onProbeRequestPrint(const WiFiEventSoftAPModeProbeRequestReceived& evt) {
myList.push_back(evt);
}
void setup() {
Serial.begin(115200);
Serial.print("Hello!");
// Don't save WiFi configuration in flash - optional
WiFi.persistent(false);
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(apSsid, apPassword);
WiFi.begin(clientSsid, clientPassword);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.print("");
probeRequestPrintHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestPrint);
}
void loop() {
delay(3000);
String json = "";
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonArray& probes = root.createNestedArray("probes");
for(WiFiEventSoftAPModeProbeRequestReceived w : myList){
JsonObject& probe = probes.createNestedObject();
probe["address"] = macToString(w.mac);
probe["rssi"] = w.rssi;
}
myList.clear();
root.printTo(json);
Serial.print(json);
Serial.print("hallo");
}
It should transfer the dot, a json object and the word "hallo"
This is because the ESP8266 can't find a WiFi access point with SSID client-ssid and password client-password.
Unless it gets connected to the said access point it will print . on the screen.
You can change the access point parameters to one that visible in the range of ESP8266 alternatively you can change the access point SSID and password to one in the code.
You can use the WiFi manager to avoid hard-code SSID names and password.

How is it possible to compile code from code

I want to experiment with the programs that write programs in C code, and i want to use construction like following:
int main() {
char* srcCode="int f(int x) { return x+42; }";
int (*compiledFun)(int) = compile(srcCode);
printf("result=%d", (*compiledFun)(123));
return 0;
}
Desired output should be printed "result=165".
My question is about compile() function. I may try to put srcCode in a file, then invoke external compiler, like gcc, then try to read produced binary, probably fix some addresses, and so to fill the compiledFun memory. But I feel like that would be a very inefficient stub. Is there any way to compile a program from within a program, directly from memory to memory? Maybe some library or a subset that can be ripped off gcc sources, responsible for producting binary code from source text?
That may be important addition, all source code that should be compiled is a function that takes arguments and returns. It will not call any external libraries and function like printf, but only do some calculations and return.
Use libtcc an in-memory C compiler from TinyC.
A complete example from here https://github.com/TinyCC/tinycc/blob/mob/tests/libtcc_test.c
/*
* Simple Test program for libtcc
*
* libtcc can be useful to use tcc as a "backend" for a code generator.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "libtcc.h"
/* this function is called by the generated code */
int add(int a, int b)
{
return a + b;
}
/* this strinc is referenced by the generated code */
const char hello[] = "Hello World!";
char my_program[] =
"#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
"extern int add(int a, int b);\n"
"#ifdef _WIN32\n" /* dynamically linked data needs 'dllimport' */
" __attribute__((dllimport))\n"
"#endif\n"
"extern const char hello[];\n"
"int fib(int n)\n"
"{\n"
" if (n <= 2)\n"
" return 1;\n"
" else\n"
" return fib(n-1) + fib(n-2);\n"
"}\n"
"\n"
"int foo(int n)\n"
"{\n"
" printf(\"%s\\n\", hello);\n"
" printf(\"fib(%d) = %d\\n\", n, fib(n));\n"
" printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"
" return 0;\n"
"}\n";
int main(int argc, char **argv)
{
TCCState *s;
int i;
int (*func)(int);
s = tcc_new();
if (!s) {
fprintf(stderr, "Could not create tcc state\n");
exit(1);
}
/* if tcclib.h and libtcc1.a are not installed, where can we find them */
for (i = 1; i < argc; ++i) {
char *a = argv[i];
if (a[0] == '-') {
if (a[1] == 'B')
tcc_set_lib_path(s, a+2);
else if (a[1] == 'I')
tcc_add_include_path(s, a+2);
else if (a[1] == 'L')
tcc_add_library_path(s, a+2);
}
}
/* MUST BE CALLED before any compilation */
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
if (tcc_compile_string(s, my_program) == -1)
return 1;
/* as a test, we add symbols that the compiled program can use.
You may also open a dll with tcc_add_dll() and use symbols from that */
tcc_add_symbol(s, "add", add);
tcc_add_symbol(s, "hello", hello);
/* relocate the code */
if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
return 1;
/* get entry symbol */
func = tcc_get_symbol(s, "foo");
if (!func)
return 1;
/* run the code */
func(32);
/* delete the state */
tcc_delete(s);
return 0;
}

passing 2 parameters of undefined type to a constructor (of 2 expected)

I have a constructor where as first 2 parameteres I would like to pass:
ID3D11ShaderResourceView* as a steady downloaded texture OR
const CHAR* as a filename to downlowd this texture and assign it to a class ID3D11ShaderResourceView* member (with following release on demand), but I can not understand the way I should do it correctly. it looks this way:
class {button
public
button() {};
button(data1 (or texture or filname), data2 (or texture or filname), rest data....);
...
~button();
}
so I tried:
templates but failed, may be cause of knowledge lack, templates
define one type while I need a choice of 2. Varradic templates, or I didnt get them right but they mean undetermined amount of variables when I need to differ only 2 first.
Unions but it had conflict with class variable set - said could not match const char [amount] with const char* and unions do not work with std::string.
tried void* with typeid.name() but it always showed me "void *"
I don't want to overload constructors, becuase this will create 4+ of them barely differing one from another. Do you think boost::variant helps me in this case? Is there any smooth and effective way to build that kind of constructor? My c++ knowledge is on beginning level, sorry if its a duplicate topic, read all it suggested to me while creating it but didn't seem to find out anything closely similar, thanks:)
Update:
Applied boost::any, got next results:
class button : public frame {
public:
button() {};
button(boost::any _texture,
boost::any _hover_texture,
...
};
if (_texture.type() == typeid(ID3D11ShaderResourceView*)) texture = boost::any_cast<ID3D11ShaderResourceView*>(_texture);
if (_texture.type() == typeid(const char*))
{
if ( FAILED(D3DX11CreateShaderResourceViewFromFile(gvDevice,boost::any_cast<const char*>(_texture), NULL, NULL, &texture, NULL )) )
mboxout( "loading texture failed.", "UI texture load error", true );
};
if (_hover_texture.type() == typeid(ID3D11ShaderResourceView*)) hover_texture = boost::any_cast<ID3D11ShaderResourceView*>(_hover_texture);
if (_hover_texture.type() == typeid(const char*))
{
if ( FAILED(D3DX11CreateShaderResourceViewFromFile(gvDevice, boost::any_cast<const char*>(_hover_texture), NULL, NULL, &hover_texture, NULL )) )
mboxout( "loading texture failed.", "UI texture load error", true );
};
Is it the only possible decision because this one seems akward for me? Thanks :)
As always, when you have combinatoric explosion/tedious repetition, refactor your code into reusable units.
In this case, your constructor could be just
template <typename T1, typename T2>
button(T1 const& texture, T2 const& hover_texture)
: _texture(load(texture)),
_hover_texture(load(hover_texture))
{
};
And all the loading logic would be inside... you guess it the load function. A sample implementation of that:
static ID3D11ShaderResourceView* load(ID3D11ShaderResourceView* v){
return v; // just return the resources passed in
}
static ID3D11ShaderResourceView* load(char const* fname) {
ID3D11ShaderResourceView* p = NULL;
if (FAILED(D3DX11CreateShaderResourceViewFromFile(gvDevice, fname, NULL, NULL, &p, NULL)))
throw std::runtime_error(std::string("loading texture failed (") + fname + ")");
return p;
}
Note: while we were at it we separated UI from business logic. You do not want to display messageboxes from inside constructors. Ever. You just want to notify the caller of the problem and the caller decides what to do (use another resource, try a different path, retry a download, write a warning message to the log, shut down etc.)
Full Demo
Live On Coliru
#include <iostream>
#include <stdexcept>
///////////////////////////////////////////////////////
// mocking ID3D*
struct ID3D11ShaderResourceView;
enum ERROR_CODE { ERR_OK };
#define FAILED(e) (ERR_OK != (e))
static int gvDevice = 42;
ERROR_CODE D3DX11CreateShaderResourceViewFromFile(int, char const* fname, void*, void*, ID3D11ShaderResourceView**, void*) {
std::cout << "Loaded from " << fname << "\n";
return ERR_OK;
}
//
///////////////////////////////////////////////////////
struct frame{ virtual ~frame() = default; };
class button : public frame {
public:
button() {};
template <typename T1, typename T2>
button(T1 const& texture, T2 const& hover_texture)
: _texture(load(texture)),
_hover_texture(load(hover_texture))
{
};
private:
// TODO Rule-Of-Three constructor/destructorl
// SUGGEST: Rule-Of-Zero using shared pointers instead
ID3D11ShaderResourceView* _texture;
ID3D11ShaderResourceView* _hover_texture;
static ID3D11ShaderResourceView* load(ID3D11ShaderResourceView* v) { return v; }
static ID3D11ShaderResourceView* load(char const* fname) {
ID3D11ShaderResourceView* p = NULL;
if (FAILED(D3DX11CreateShaderResourceViewFromFile(gvDevice, fname, NULL, NULL, &p, NULL)))
throw std::runtime_error(std::string("loading texture failed (") + fname + ")");
return p;
}
};
#include <cassert>
int main() {
ID3D11ShaderResourceView* default_texture = NULL;
assert(!FAILED( D3DX11CreateShaderResourceViewFromFile(gvDevice, "default_texture.bin", NULL, NULL, &default_texture, NULL)));
try {
button button1("t1.bin", "hover1.bin");
button button2(default_texture, "hover2.bin");
button button3("t3.bin", default_texture);
button button4(default_texture, default_texture);
} catch(std::exception const& e) {
std::cout << "Oops: " << e.what() << "\n";
}
}
Prints:
Loaded from default_texture.bin
Loaded from t1.bin
Loaded from hover1.bin
Loaded from hover2.bin
Loaded from t3.bin
There's still a lot to be improved (see the comments, e.g.) but this is a start.

How to call exported function from dll in C/C++?

My aim is to call some function via its address. How can I do it?
I have done the next work for such aim, but at first(1) - I've got access violation ( don't know why ) and with the second I have some problems with calling function is ASM with ESP value...
The first (the problem with access violation):
#include <iostream>
#include <Windows.h>
const DWORD_PTR offset = 0x00001a90;
typedef void (__stdcall *uef)(int);
int main(void)
{
HMODULE hModule = LoadLibrary(L"C:\\Windows\\system32\\OpenAL32.dll");
DWORD_PTR addr = (DWORD_PTR)hModule + offset;
uef func = (uef)offset;
func(0);
return 0;
}
The second (problems at runtime with ESP value):
#include <iostream>
#include <Windows.h>
typedef void (__stdcall *uef)(int);
int main(void)
{
HMODULE hModule = LoadLibrary(L"C:\\Windows\\system32\\OpenAL32.dll");
uef obj = NULL;
if(hModule != NULL)
{
obj = reinterpret_cast<uef>(GetProcAddress(hModule, "alEnable"));
}
if(obj != NULL)
{
(*obj)(0);
}
if(hModule != NULL)
{
FreeLibrary(hModule);
}
return 0;
}
How could I solve this problem?
PS
And the another main question is:
How can I dynamically calculate the function address in runtime for next calling?
Thanks,
Best Regards!
First, there is a major issue (hence the access violation) with the hardcoded address offset (const DWORD_PTR offset = 0x00001a90). Don't do that! How can you know that the offsett will not be changed because of ASLR?