基于开源库tufao,一个C + +的异步 Web 框架,使用Qt的对象的通信系统(信号与槽)

基于开源库tufao,一个C + +的异步 Web 框架,使用Qt的对象的通信系统(信号与槽)

tufao是一个由QT编写的HTTP服务器。 

tufao代替apache来实现http的通信。

本实例在window平台,以源码形式加载到Qt pro项目,Qt mingw版本编译测试运行。

ps:code依赖boost库,请自行下载,修改.pro所依赖boost路径。



完整版本源码下载




基于开源库tufao,一个C + +的异步 Web 框架,使用Qt的对象的通信系统(信号与槽)

> 步骤:

1) 在main.c

#include <QCoreApplication>
#include "MyServer.h"

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    new MyServer;

    return 0;
}

2) 在MyServer.h中添加: 

#include <Tufao/httpserver.h>     // 引入头文件

// create http server
Tufao::HttpServer *server;

MyServer.cpp
MyServer::MyServer(QObject *parent) : QObject(parent)
{
     // 创建一个服务器
    server = new Tufao::HttpServer;
     // 开启监听
    if (server->listen(QHostAddress::Any, 8080))
    {
        qDebug() << "listen ok at 8080";
    }
    else
    {
        qDebug() << "listen error at 8080";
    }
}


当有client端发送请求来的时候, tufao用的是信号和槽的机制。

3)当有请求来的时候要添加槽函数 

MyServer.h 
public slots:
    void slotQequestReady(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&);

MyServer.cpp
// 连接
connect(server, SIGNAL(requestReady(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&)),
            this, SLOT(slotQequestReady(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&));

// 或者用函数是指针的方式
connect(server, &Tufao::HttpServer::requestReady, this, &MyServer::slotQequestReady);

槽函数编写:

void MyServer::slotQequestReady(Tufao::HttpServerRequest& request,Tufao::HttpServerResponse& response)
{
    //1. status line
    response.writeHead(Tufao::HttpResponseStatus::OK);

    //2. write content
    response.write("this my first tufao<br/>");
    response.write("hahahah &nbsp");

    //3. tips: write end and send data 重要
    response.end("byebye tufao end<br/>");
}

然后访问主机加上端口名就可以访问了。


> url链接带请求, 例如:

`http://192.168.49.136?user=aa&password=bbb`

1) 当有链接请求的时候,可以在槽函数里来获取url传过来的东西:
```c++
//先导入头文件: include <QUrl>
QString url = request.url().toString();
// 响应报文
response.end(url.toUtf8());
```
然后会返回: `/?user=name&password=pass`


curl命令请求post数据, 用 -d:
curl "http://192.168.49.136:8081" -d "this is post data"

传输文件用:@+路径
curl "http://192.168.49.136:8081" -d "@test.txt"

上传文件时,如果文件过大,那么会有一个:
connect (&request, &Tufao::HttpServerRequest::ready, );

最后一次上传文件时:
connect (&request, &Tufao::HttpServerRequest::end, );

> post请求 案例

MyServerHandle.h

#ifndef MYSERVERHANDLEPOST_H
#define MYSERVERHANDLEPOST_H

#include <QObject>
#include <Tufao/HttpServer>
#include <Tufao/HttpServerRequest>
#include <Tufao/HttpServerResponse>

class MyServerHandlePost : public QObject
{
    Q_OBJECT
public:
    explicit MyServerHandlePost(QObject *parent = 0);

    Tufao::HttpServer *server;

    void handlePostData(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&);

signals:

public slots:
    void slotRequestReady(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&);


};

#endif // MYSERVERHANDLEPOST_H


MyServerHandle.cpp

#include "MyServerHandlePost.h"
#include <QFile>

MyServerHandlePost::MyServerHandlePost(QObject *parent) : QObject(parent)
{
    server = new Tufao::HttpServer;

    if(server->listen(QHostAddress::Any, 8081))
    {
        qDebug() << "listen ok at 8081";
    }
    else
    {
        qDebug() << "listen error at 8081";
    }

    connect(server, SIGNAL(requestReady(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&)),
            this, SLOT(slotRequestReady(Tufao::HttpServerRequest&,Tufao::HttpServerResponse&)));
}

void MyServerHandlePost::handlePostData(Tufao::HttpServerRequest &request, Tufao::HttpServerResponse &response)
{
    QFile file("/home/xueguoliang/b.txt");
    file.open(QFile::WriteOnly);
    QByteArray data = request.readBody();
    file.write(data);
    file.close();
#if 0
    // read body
    qDebug() << request.readBody();
#endif

    response.writeHead(Tufao::HttpResponseStatus::OK);
    response.write("post data is write ok\n");
}

void MyServerHandlePost::slotRequestReady(Tufao::HttpServerRequest &request, Tufao::HttpServerResponse &response)
{
    if (request.method() == "POST")
    {
        // end signal: all data is ready   &: get all param
        connect(&request, &Tufao::HttpServerRequest::end, [&](){
            handlePostData(request, response);
        });
    }
    else
    {
        response.writeHead(Tufao::HttpResponseStatus::OK);
        response.end("i need post method\n");
    }
}