#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
//#include <QTcpSocket>
#include <./cpp-httplib-master/httplib.h>
#define CPPHTTPLIB_OPENSSL_SUPPORT
using namespace httplib;
using namespace std;
#include <chrono>
#include <cstdio>
//#define SERVER_CERT_FILE "./cert.pem"
//#define SERVER_PRIVATE_KEY_FILE "./key.pem"
int main(int argc, char *argv[])
{
qDebug() << "start ServerTest";
httplib::Server svr;
svr.Get("/", [](const httplib::Request& req, httplib::Response& res) {
std::string hd = res.get_header_value("Content-Security-Policy");
qDebug() << "before, header CSP = " << QString::fromStdString(hd);
res.set_header("Access-Control-Allow-Origin", "*");
res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
res.set_header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept, Origin, Authorization");
res.set_header("Access-Control-Allow-Methods", "OPTIONS, GET, POST, HEAD");
res.set_header("Content-Security-Policy", "default-src 'self' localhost:*");
//res.set_header("Content-Security-Policy", "default-src 'self'");
hd = res.get_header_value("Content-Security-Policy");
qDebug() << "after, header CSP = " << QString::fromStdString(hd);
res.set_content(R"-({"message":"Hello World2!"})-", "application/json");
qDebug() << "Get---/";
});
svr.Get("/api/hello", [](const httplib::Request& req, httplib::Response& res) {
std::string hd = res.get_header_value("Content-Security-Policy");
qDebug() << "before, header CSP = " << QString::fromStdString(hd);
res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin").c_str());
res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
res.set_header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept, Origin, Authorization");
res.set_header("Access-Control-Allow-Methods", "OPTIONS, GET, POST, HEAD");
res.set_header("Content-Security-Policy", "default-src 'self' localhost:*");
hd = res.get_header_value("Content-Security-Policy");
qDebug() << "after, header = " << QString::fromStdString(hd);
res.set_content(R"-({"message":"Hello World!"})-", "application/json");
qDebug() << "Get---/api/hello";
});
svr.Get("/Api/Hello", [](const httplib::Request& req, httplib::Response& res) {
std::string hd = res.get_header_value("Content-Security-Policy");
qDebug() << "before, header CSP = " << QString::fromStdString(hd);
res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin").c_str());
res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
res.set_header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept, Origin, Authorization");
res.set_header("Access-Control-Allow-Methods", "OPTIONS, GET, POST, HEAD");
res.set_header("Content-Security-Policy", "default-src 'self' localhost:*");
hd = res.get_header_value("Content-Security-Policy");
qDebug() << "after, header = " << QString::fromStdString(hd);
res.set_content(R"-({"message":"Hello World!"})-", "application/json");
qDebug() << "Get---/api/hello";
});
bool success = svr.listen("localhost", 8086);
qDebug() << "success=" << success;
return 0;
}
cpp-httplib 用法
最新推荐文章于 2024-04-18 18:20:35 发布