I've written several http[s] servers in Qt.

You can have a look at the very simple qmlwebmodelserver at 
https://github.com/jhihn/qmlwebmodelserver It's quick, dirty, but done right. 
It's not as nice as inheriting from a class, but it is pretty easy. It does not 
use SSL yet, but the changes from QTcpSocket to QSslSocket are trivial. It 
supports HTTP1.1 and will do chuncked length encoded returns as needed, and 
uses a thread pool

Basically, you take that project, delete the CsvFile, ModelMapper classes, 
provide your own version of ModelMapper (request handler) and you are done.

Your server application code goes in to your replacement for ModelMapper, and 
things are set up as in ServerThread::run() (included below)
Basically, to return stuff back through the server to the client, your class 
**emits** one or more of the following:
A httpResponse(QHttpResponseHeader,QByteArray) - the response header to return 
and a QByteArray

OR
A httpResponseHead(QHttpResponseHeader), followed by 0 or more:

httpResponseContent(QByteArray) x N, (chunk-length encoded) followed by a:

httpResponseClose() x 1


Note that httpResponseClose() doesn't actually close if it is HTTP1.1.


void ServerThread::run()
{
SlotSocket *socket = new SlotSocket();
RequestParser *httpParser = new RequestParser();
ModelMapper *modelMapper = new ModelMapper();
if (!socket->setSocketDescriptor(m_socketDescriptor))
{
qDebug() << Q_FUNC_INFO <<"cannot set setSocketDescriptor";
return;
}
QObject::connect(socket, SIGNAL(read(QByteArray)), httpParser, 
SLOT(read(QByteArray)));
QObject::connect(httpParser, SIGNAL(write(QByteArray)), socket, 
SLOT(write(QByteArray)));
QObject::connect(httpParser, SIGNAL(httpGet(QHttpRequestHeader)), modelMapper, 
SLOT(getModel(QHttpRequestHeader)));
QObject::connect(httpParser, SIGNAL(httpPost(QHttpRequestHeader,QByteArray)), 
modelMapper, SLOT(postModel(QHttpRequestHeader,QByteArray)));
QObject::connect(modelMapper, 
SIGNAL(httpResponse(QHttpResponseHeader,QByteArray)), httpParser, 
SLOT(httpResponse(QHttpResponseHeader,QByteArray)));
QObject::connect(modelMapper, SIGNAL(httpResponseHead(QHttpResponseHeader)), 
httpParser, SLOT(httpResponseHead(QHttpResponseHeader)));
QObject::connect(modelMapper, SIGNAL(httpResponseContent(QByteArray)), 
httpParser, SLOT(httpResponseContent(QByteArray)));
QObject::connect(modelMapper, SIGNAL(httpResponseClose()), httpParser, 
SLOT(httpResponseClose()));

#ifdef USETHREADPOOL
QEventLoop loop;
loop.exec();
#else
exec();
#endif

if (socket) socket->deleteLater();
if (httpParser) httpParser->deleteLater();
if (modelMapper) modelMapper->deleteLater();
}




________________________________
 From: Daniel Bowen <qtmailingli...@bowensite.com>
To: interest@qt-project.org 
Sent: Thursday, March 6, 2014 4:22 PM
Subject: [Interest] Cross platform Web services server
 

Cross platform Web services server
I’m wondering what other people are usingthese daysfor providing web services 
from a server point of viewfor embedded devices,where the server code 
isintegrated as a shared libraryand geared more toward providing RESTful 
servicesfor a limited number of clients on the local network. Something that’s 
also cross-platform. Ideally,you’dbe able tohave different handlers 
fordifferent base paths.  For example, registeringaCcallback or 
handlerC++class/interfaceor Qt signal/slotfor“/path1”, and if a request came in 
for“/path1/thing1?a=b”, then the registered hander could deal with that.
SSL integration (withQSsl and friends, or openssl)andauthentication supportare 
important too.
Thanks!
-Daniel
_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest
_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to