tufao安装和实例详解

安装tufao

  1. 获取代码 github

  2. 编译和安装

    • sudo apt-get install cmake qtsdk

    • 在tufao目录下创建build目录

    • cd build

    • cmake .. -DCMAKE_INSTALL_PREFIX=/usr

    • make

    • sudo make install

  3. 创建工程

    • 创建空的工程

    • 工程文件中增加CONFIG += TUFAO1 C++11

    • 增加一个类MyServer,一定是QObject派生类

    • 增加一个main.cpp实现main函数

    • 在MyServer的构造函数,创建Tufao::HttpServer对象server

    • 将server的信号requestReady和自己写的槽函数slotRequestReady连接

    • slotRequestReady函数中,实现http协议的响应报文。

 

 

> 步骤:

 

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");

    }

}

 

 

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值