Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP Connection Is Being Established But Handler Never Triggers Process() Please Help! #39

Open
omidnabi opened this issue Oct 14, 2023 · 0 comments

Comments

@omidnabi
Copy link

omidnabi commented Oct 14, 2023

Hi guys, I'm having a very weird problem, with the server I'm trying to setup. Everything seems to go well as far connecting to the server, but for some reason process() never gets triggered. Note I use server.setHandler() in my api to assign an instantiaton of the class below. Then I call server.listen(). Please see the code below:

`#ifndef HTTP_HANDLER_H
#define HTTP_HANDLER_H

#include
#include <qhttpengine/handler.h>

class HttpHandler : public QHttpEngine::Handler
{
Q_OBJECT

public:
HttpHandler(QObject* parent = nullptr);
virtual ~HttpHandler();

signals:
void messageReceived(QHttpEngine::Socket* socket, const QString& path);

protected:
void process(QHttpEngine::Socket *socket, const QString &path) override;
};

#endif
`

`#include "HttpHandler.h"
#include <qhttpengine/socket.h>

HttpHandler::HttpHandler(QObject *parent) : QHttpEngine::Handler(parent)
{
}

HttpHandler::~HttpHandler()
{

}

void HttpHandler::process(QHttpEngine::Socket *socket, const QString &path)
{
qDebug() << "HTTP request received! Path:" << path;

// Read the raw request data as a QByteArray
QByteArray requestData = socket->readAll();

// Process the raw data as needed
qDebug() << "Raw Request Data:" << requestData;

// You can now parse and handle the requestData
// For example, you can parse it as JSON if it's in JSON format:
// QJsonDocument jsonDocument = QJsonDocument::fromJson(requestData);
// QJsonObject jsonObject = jsonDocument.object();
// ... (perform further processing)

// Respond to the request (for example, with a simple OK response)
socket->setStatusCode(QHttpEngine::Socket::OK);
socket->writeHeaders();
socket->close();
emit messageReceived(socket, path);

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant