c - Mongoose 嵌入式网络服务器

标签 c mongoose-web-server

此代码取自 Mongoose 嵌入式 Web 服务器中的示例程序。

事件 MG_NEW_REQUEST 被调用两次。这是预期的行为吗?为什么它会处理同一个请求两次?您将如何防止这种情况发生?

int i =0;
static void *callback(enum mg_event event,
                      struct mg_connection *conn,
                      const struct mg_request_info *request_info)
{
    if (event == MG_NEW_REQUEST)
    {    
        i++;
        printf("%d \n", i);

        // Echo requested URI back to the client
        mg_printf(conn, "HTTP/1.1 200 OK\r\n"
              "Content-Type: text/html\r\n\r\n"
              "%s", request_info->uri);    
        mg_printf(conn, "Hello World!!"
                  "%s", "");

        //request_info->query_string
        return const_cast<char *>("done");  // Mark as processed
    }
    else
    {
        return NULL;
    }
}

int main(void) {
    struct mg_context *ctx;
    const char *options[] = {"listening_ports", "8080", NULL};

    printf("Test web server, open browser to http://localhost:8080 ");
    ctx = mg_start(&callback, NULL, options);
    getchar();  // Wait until user hits "enter"
    mg_stop(ctx);

    return 0;
}

最佳答案

我假设浏览器实际上执行了两个请求。一个用于 URL /,另一个用于 /favicon.ico

关于c - Mongoose 嵌入式网络服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10988412/

相关文章:

c++ - Mongoose 应该渲染一个 html 文件

c++ - 确定是否取消选择 TreeView 中所有节点的最安全方法?

不能将一个数组分配给另一个

c - 这个奇怪的函数定义是什么意思?

c++ - System() 调用返回 255,但随后执行

c++ - mongoose web 服务器 helloworld 程序

c - 与 C 中的函数传递的数组一起堆叠

c - 如何使用 libgit2 通过 ssh 推送 git 存储库

c - Web 服务器配置 - shttpd/Mongoose

c++ - Mongoose C++ Http 服务器只获取 MG_OPEN_FILE 事件