I wrote a simple class that works as a single-stream http server, issuing the current time and date, if you contact 127.0.0.1:

simple_server.h

#ifndef SIMPLE_SERVER_H #define SIMPLE_SERVER_H #include <QObject> #include <QTcpServer> #include <QTcpSocket> #include <QDateTime> class Simple_Server : public QTcpServer { Q_OBJECT public: explicit Simple_Server(QObject *parent = 0); void incomingConnection(qintptr handle); signals: public slots: void onReadyRead(); void onDisconnected(); }; #endif // SIMPLE_SERVER_H 

simple_server.cpp

 #include "simple_server.h" Simple_Server::Simple_Server(QObject *parent) : QTcpServer(parent) { if(listen(QHostAddress::Any, 80)) { qDebug() << "listerning..."; } else { qDebug() << "error " << errorString(); } } void Simple_Server::incomingConnection(qintptr handle) { QTcpSocket *socket = new QTcpSocket(); socket->setSocketDescriptor(handle); connect(socket, SIGNAL(readyRead()), this, SLOT(onReadyRead())); connect(socket, SIGNAL(disconnected()), this, SLOT(onDisconnected())); } void Simple_Server::onReadyRead() { QTcpSocket * socket = qobject_cast<QTcpSocket*>(sender()); qDebug() << socket->readAll(); QString response = "HTTP/1.1 200 OK\r\n\r\n%1"; socket->write(response.arg(QDateTime::currentDateTime().toString()).toUtf8()); socket->disconnectFromHost(); } void Simple_Server::onDisconnected() { QTcpSocket * socket = qobject_cast<QTcpSocket*>(sender()); socket->close(); socket->deleteLater(); } 

The question is how to add different directories (probably this is the name) For example, so that when accessing 127.0.0.1/time, the server gives the time, on request 127.0.0.1/date, the server gives the date. How can I do that?

  • You need to re-implement a subset of the HTTP protocol that will at least parse the path from a GET request. - Nofate
  • It is possible in more detail, I did not quite understand what the essence was - Nikola Krivosheya
  • 2
    Now you dump all the headers and the request body in qDebug() , and for the described task you will still have to sort it out in pieces, what's written there. - D-side

1 answer 1

To you in this line

 qDebug() << socket->readAll(); 

already come the data. It is enough to save them to a string, then apply the split method ('\ n') and split them into lines. The first line will contain three elements that can be split by the same split.

ordinary view

 GET /pub/WWW/TheProject.html HTTP/1.1 

the first is the method. There are not many options. This is usually GET / POST / PUT / DELETE, but you can also insert one. It corresponds to the request. Next comes what you call "different directories." Actually it is necessary. And the very end is the HTTP version, but you can probably just ignore it for now.

That is, somewhere like that

 QStringList h = QString(socket->readAll()).split('\n'); QStringList f = h.split(' '); QString method = f[0]; QString path = f[1]; // дальше решаем. QString response = "HTTP/1.1 200 OK\r\n\r\n%1"; if (path == '/date") { socket->write(response.arg(QDateTime::currentDate().toString()).toUtf8()); } else if (path == "/time") { socket->write(response.arg(QDateTime::currentTime().toString()).toUtf8()); } else { socket->write(response.arg("error"); } socket->disconnectFromHost(); 

Of course, this implementation is very simple and there are many, many flaws. But it should work.

  • error in QStringList f = h.split (''); corrected for QStringList f = h.at (0) .split (''); - Nikola Krivosheya
  • there is just a typo. QStringList f = h[0].split(' '); - KoVadim