Set Mongoose document root to an html file inside Qt Resource File



  • Hi,

    I am using Mongoose library to create an instance of web server inside my application (the instance of Mongoose is created inside a worker thread, see my example code below). Currently, the index.html file of the web server sits inside a "web_root" folder in the same folder as the application and when I run my application, I am able to access the file using http://ip-address:8000

    I would like to be able to embed the index.html file inside Qt resource file (*.qrc) and have mongoose point to this location. Is that possible?

    extern "C" {
    #include "mongoose.h"
    }
    
    #include "worker.h"
    #include <QString>
    
    char command[50];
    
    static const char *s_http_port = "8000";
    static struct mg_serve_http_opts s_http_server_opts;
    
    Worker *worker;
    
    void Worker::ev_handler(struct mg_connection *nc, int ev, void *p) {
        struct http_message *hm = (struct http_message *) p;
    
        if (ev == MG_EV_HTTP_REQUEST) {
            if (mg_vcmp(&hm->uri, "/control") == 0) {
                mg_get_http_var(&hm->body, "command", command,
                                sizeof(command));
    
                QString receivedCommand(command);
                emit worker->complete(receivedCommand);
    
                mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%.*s",
                          (unsigned long) hm->body.len, (int) hm->body.len, hm->body.p);
            } else {
                mg_serve_http(nc, (struct http_message *) p, s_http_server_opts);
            }
        }
    }
    
    
    Worker::Worker(QObject *parent) :
        QThread(parent)
    {
    }
    
    void Worker::run()
    {
        worker = this; //This is a workaround to emit signal from static function
    
        printf("Starting web server on port %s\n", s_http_port);
        mg_mgr_init(&this->mgr, NULL);
        this->nc = mg_bind(&this->mgr, s_http_port, this->ev_handler);
    
        mg_set_protocol_http_websocket(this->nc);
        s_http_server_opts.document_root = "web_root";
    
        for (;;) {
            mg_mgr_poll(&this->mgr, 1000);
        }
        mg_mgr_free(&this->mgr);
    }
    
    

  • Lifetime Qt Champion

    Hi,

    Since Mongoose doesn't know anything about QIODevice the best option would be to copy the file from qrc to a temp location and serve them from there.



  • @SGaist said:

    Since Mongoose doesn't know anything about QIODevice the best option would be to copy the file from qrc to a temp location and serve them from there.

    Hi SGaist, thanks for trick! If there is no other way to do it, maybe I'll do just that.


  • Lifetime Qt Champion

    Are you providing the file content yourself or does Mongoose do everything himself ?



  • @SGaist

    Hi SGaist, the content (basically just an index.html file) comes from me. That's why if possible, I would like to embed it in the exe itself.


  • Lifetime Qt Champion

    Sorry, I meant do you return the content of the file to Mongoose so it can serve it or does it only search for files and you give it path for that ?



  • Hi SGaist,

    Sorry for not being clear! I pass the web root path (currently the same as the app executable) to Mongoose for it to serve.


  • Lifetime Qt Champion

    Then unless you modify Mongoose to use QFile/QDir, the temp dir copy is pretty much your only solution.



  • Got it! Many thanks SGaist!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.