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. How to use QHttpServer missing handler?
Forum Updated to NodeBB v4.3 + New Features

How to use QHttpServer missing handler?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 576 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.
  • T Offline
    T Offline
    Tarae
    wrote on last edited by
    #1

    Hi, I cannot figure out, how to use the missing handler in QHttpServer. My server:

    //QT = core httpserver
    #include <QCoreApplication>
    #include <QDebug>
    #include <QHttpServer>
    
    void missingHandler(const QHttpServerRequest & request, QHttpServerResponder && responder)
    {
      qWarning() << "invalid http request" << request.url().path();
      responder.writeBody("Oh no! Invalid request!");
      responder.write(QHttpServerResponder::StatusCode::NotFound);
    }
    
    int main(int argc, char *argv[])
    {
      QCoreApplication a(argc, argv);
      QHttpServer server;
      server.setMissingHandler(missingHandler);
      server.route("/foo", [] () { return "foo"; });
      server.listen(QHostAddress::Any, 1234);
      qInfo() << "server running";
      return a.exec();
    }
    

    What it does:

    $ curl localhost:1234/foo
    foo
    $ curl localhost:1234/bar
    curl: (1) Received HTTP/0.9 when not allowed
    

    What am I doing wrong? I can't find any example where a missing handler is used.
    Thank you for your help.

    JonBJ 1 Reply Last reply
    0
    • T Tarae

      Hi, I cannot figure out, how to use the missing handler in QHttpServer. My server:

      //QT = core httpserver
      #include <QCoreApplication>
      #include <QDebug>
      #include <QHttpServer>
      
      void missingHandler(const QHttpServerRequest & request, QHttpServerResponder && responder)
      {
        qWarning() << "invalid http request" << request.url().path();
        responder.writeBody("Oh no! Invalid request!");
        responder.write(QHttpServerResponder::StatusCode::NotFound);
      }
      
      int main(int argc, char *argv[])
      {
        QCoreApplication a(argc, argv);
        QHttpServer server;
        server.setMissingHandler(missingHandler);
        server.route("/foo", [] () { return "foo"; });
        server.listen(QHostAddress::Any, 1234);
        qInfo() << "server running";
        return a.exec();
      }
      

      What it does:

      $ curl localhost:1234/foo
      foo
      $ curl localhost:1234/bar
      curl: (1) Received HTTP/0.9 when not allowed
      

      What am I doing wrong? I can't find any example where a missing handler is used.
      Thank you for your help.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Tarae
      It would help if you said whether your missingHandler() gets called or not!

      While you await a better C++ expert than I: where you call server.setMissingHandler(missingHandler); do you not need to wrap missingHandler in a std::function? The signature requires

      std::function<void(const QHttpServerRequest &request, QHttpServerResponder &&responder)>.
      

      I'm not sure that a plain function name is the same thing as that. Then again, you didn't geta compile error, so who knows.....

      Does something like:

      server.setMissingHandler([]() { qDebug() << "Help me"; })
      

      get through?

      T 1 Reply Last reply
      0
      • JonBJ JonB

        @Tarae
        It would help if you said whether your missingHandler() gets called or not!

        While you await a better C++ expert than I: where you call server.setMissingHandler(missingHandler); do you not need to wrap missingHandler in a std::function? The signature requires

        std::function<void(const QHttpServerRequest &request, QHttpServerResponder &&responder)>.
        

        I'm not sure that a plain function name is the same thing as that. Then again, you didn't geta compile error, so who knows.....

        Does something like:

        server.setMissingHandler([]() { qDebug() << "Help me"; })
        

        get through?

        T Offline
        T Offline
        Tarae
        wrote on last edited by
        #3

        @JonB It calls the function, the message 'invalid http request...' is printed out to the console. Just curl doesn't like the response.

        JonBJ 1 Reply Last reply
        0
        • T Tarae

          @JonB It calls the function, the message 'invalid http request...' is printed out to the console. Just curl doesn't like the response.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Tarae
          Oh, you mean just you want to know what to put in the body of missingHandler() to not cause that. I did not understand that was your issue from the question.

          Since Content-type will be "application/x-empty" I don't suppose just removing responder.writeBody() makes it any better?

          I take it you know what curl will show.

          Anyway I leave it to you now, sorry.

          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