0

此代码取自 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;
}
4

1 回答 1

1

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

于 2012-12-01T10:17:46.217 回答