Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Set Mongoose document root to an html file inside Qt Resource File
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
mongooseworkerthread
9 Posts 2 Posters 4.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pbdot
    wrote on last edited by
    #1

    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);
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pbdot
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          P 1 Reply Last reply
          0
          • SGaistS SGaist

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

            P Offline
            P Offline
            pbdot
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by SGaist
              #6

              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 ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pbdot
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    pbdot
                    wrote on last edited by
                    #9

                    Got it! Many thanks SGaist!

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved