Qt_23 - 8BitsCoding/RobotMentor GitHub Wiki

connect μ—°κ²°λΆ€ κ΅¬ν˜„

#include "asioclient.h"
#include <qdebug.h>

AsioClient::AsioClient() :
    work(new boost::asio::io_service::work(ioservice)),
    resolver(ioservice),
    socket(ioservice)
{
    worker = std::thread([&](){
        ioservice.run();
    });

}

AsioClient::~AsioClient(){
    ioservice.stop();
    worker.join();
    work.reset();
}

void AsioClient::Get(const QString& url, const QString& p)
{
    // 1. url -> ep resolve
    server = url.toStdString();
    path = p.toStdString();

    boost::asio::ip::tcp::resolver::query query(server, "http");
    resolver.async_resolve(query, boost::bind(&AsioClient::handle_resolve,
                                              this, boost::asio::placeholders::error,
                                              boost::asio::placeholders::iterator));

    // 2. connect

    // 3. send

    // 4. recv
}


void AsioClient::handle_resolve(const boost::system::error_code& err, boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
{
    if(!err) {
        boost::asio::ip::tcp::endpoint ep = *endpoint_iterator;
        qDebug() << ep.address().to_string().c_str();

        socket.async_connect(*endpoint_iterator, boost::bind(&AsioClient::handle_connect, this, boost::asio::placeholders::error, endpoint_iterator));
    }
}

void AsioClient::handle_connect(const boost::system::error_code& err,boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
{
    if(!err) {
        // success
        // write
        std::ostream os(&requestbuf);

        os << "GET" << path << " HTTP/1.0\r\n";
        os << "Host: " << server << "\r\n";
        os << "Accept: */*\r\n";
        os << "Connection: close\r\n\r\n";

        boost::asio::async_write(socket, requestbuf,
                                 boost::bind(&AsioClient::handle_write, this, boost::asio::placeholders::error));
    } else {
        qDebug() << err.message().c_str() << endl;
    }
}

void AsioClient::handle_write(const boost::system::error_code& err)
{

}
#ifndef ASIOCLIENT_H
#define ASIOCLIENT_H

#include <boost\asio.hpp>
#include <boost/bind.hpp>

#include <QObject>
#include <QString>
#include <memory>
#include <thread>

class AsioClient
{
public:
    AsioClient();
    virtual ~AsioClient();

    void Get(const QString& url, const QString& path);
    void handle_resolve(const boost::system::error_code& err,
                        boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
    void handle_connect(const boost::system::error_code& err,
                        boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
    void handle_write(const boost::system::error_code& err);

private:
    boost::asio::io_service ioservice;
    std::shared_ptr<boost::asio::io_service::work> work;
    std::thread worker;

    std::string server, path;
    boost::asio::ip::tcp::resolver resolver;

    boost::asio::ip::tcp::socket socket;

    boost::asio::streambuf requestbuf;
};

#endif // ASIOCLIENT_H
#include "widget.h"
#include "ui_widget.h"
#include "QString"
#include <QRegularExpression>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , client(new AsioClient)
{
    ui->setupUi(this);

    ui->paramTable->setColumnCount(2);
    ui->paramTable->setHorizontalHeaderLabels(QString("key;value").split(";"));
    ui->paramTable->setSelectionBehavior(QAbstractItemView::SelectRows);

    ui->urlEdit->setText("http://127.0.0.1:5000/");
}

Widget::~Widget()
{
    delete ui;
    delete client;
}


void Widget::on_getBtn_clicked()
{
    QString url = ui->urlEdit->text();
//    client->Get("www.boost.org","/");

    // https://gist.github.com/voodooGQ/4057330
    // μœ„ μ‚¬μ΄νŠΈμ—μ„œ μ•„λž˜μ˜ μ •κ·œν‘œν˜„μ‹μ„ 받아와 μ‚¬μš©ν•œλ‹€.
    // 참고둜 μ •κ·œν‘œν˜„μ‹ ν™•μΈμ‚¬μ΄νŠΈλŠ” https://regex101.com
    QRegularExpression re("^(?:([A-Za-z]+):)?(\\/{0,3})([0-9.\\-A-Za-z]+)(?::(\\d+))?(?:\\/([^?#]*))?(?:\\?([^#]*))?(?:#(.*))?$");
    QRegularExpressionMatch match = re.match(url);
    if(match.hasMatch()) {
        auto hostName = match.captured(3);      // match 3이 hostName
        auto urlPath = "/" + match.captured(5);       // match 5κ°€ url

        client->Get(hostName, urlPath);
    }
}
⚠️ **GitHub.com Fallback** ⚠️