Mongoose跨域解决方法

Mongoose 是一个面向 C/C++ 的网络库。它实现了事件驱动 TCP、UDP、HTTP、WebSocket、MQTT 的非阻塞 API。Mongoose让嵌入式网络编程变得快速, 坚固且轻松。

官网地址 GitHub - cesanta/mongoose: Embedded web server, with TCP/IP network stack, MQTT and Websocket · GitHub

复制代码
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
	
	if (ev == MG_EV_HTTP_CHUNK)
	{
		mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods:*\r\nAccess-Control-Allow-Headers:*\r\n");
	}

	if (ev == MG_EV_HTTP_REQUEST) 
	{
		char addr[32];
		struct http_message *hm = (struct http_message *)p;
		mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);

		MLOG << "from " << addr << ":" << QByteArray(hm->method.p, hm->method.len).data() << QByteArray(hm->uri.p, hm->uri.len).data();
		

		
		QByteArray alldata = "11";

		//Content-Type: application/json\r\n
		//mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nAccess-Control-Allow-Origin: *\r\n\Access-Control-Allow-Headers: Content-Type\r\n");
		//mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods:GET, POST, OPTIONS\r\nAccess-Control-Allow-Headers:*\r\n");
		//mg_send_http_chunk(nc, alldata.constData(), alldata.length());
		//mg_send_http_chunk(nc, "", 0);
		
		mg_send(nc, alldata.constData(), alldata.length());
		nc->flags |= MG_F_SEND_AND_CLOSE;
	}
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QTextCodec *codec = QTextCodec::codecForName("GBK");
#if QT_VERSION < 0x050000
    QTextCodec::setCodecForTr(codec);
    QTextCodec::setCodecForCStrings(codec);
#endif
    QTextCodec::setCodecForLocale(codec);

    qDebug() << "+++++++++++++" << "verion " << APPVERSION << "+++++++++++++";

    struct mg_mgr mgr;
    struct mg_connection *nc;

    mg_mgr_init(&mgr, NULL);
    //printf("Starting web server on port %s\n", s_http_port);
    nc = mg_bind(&mgr, s_http_port, ev_handler);
    if (nc == NULL) {
        printf("Failed to create listener\n");
        exit(0);
    }

    // Set up HTTP server parameters
    mg_set_protocol_http_websocket(nc);
    s_http_server_opts.document_root = ".";  // Serve current directory
    s_http_server_opts.enable_directory_listing = "yes";

    for (;;) {
        mg_mgr_poll(&mgr, 1000);
    }
    mg_mgr_free(&mgr);



    return a.exec();
}

重点是

参考 C++ 通过mongoose 实现http server 解决跨域问题_mongoose跨域-CSDN博客

相关推荐
MC皮蛋侠客38 分钟前
TDengine C++ 系列(11):性能模型与调优——从测量到优化
c++·php·tdengine
兔兔兔兔11 小时前
记录C++ 3
开发语言·c++
代码地平线1 小时前
C++类与对象(中):默认成员函数与日期类实战
c++·笔记·算法
hansang_IR1 小时前
【题解】可持久化区间仿射区间和(Persistent Range Affine Range Sum)
c++·算法·线段树
瑞码空间2 小时前
01背包:动态规划的Hello World
c++·算法·0/1背包问题
鱼子星_2 小时前
【C++】反向迭代器:反向迭代器的底层认识与模拟实现
开发语言·c++·笔记·stl
郝学胜-神的一滴2 小时前
[简化版 GAMES 104] 现代游戏引擎 05:游戏引擎世界构建核心机制深度解析
c++·程序人生·unity·游戏引擎·计算机图形学·opengl
Darkwanderor3 小时前
C++的流简介和简单使用
开发语言·c++
乐观勇敢坚强的老彭3 小时前
C++ STL 常用容器的速查表
java·c++·算法
lingran__3 小时前
C++ STL map 与 set 底层剖析与模拟实现万字详解|基于红黑树,复刻 SGI-STL 泛型复用架构
开发语言·c++·stl·set·map·泛型编程·sgi-stl