3

我正在开发一个简单的基于 Moongose 的 Web 服务器,以通过 HTTP 发送作为参数传递的文件,无论请求是什么,但在每个请求上,我都会收到堆栈溢出错误。

这是我的代码:

#include <stdio.h>
#include <string.h>
#include "mongoose.h"

// file path
char *path;

static void *callback(enum mg_event event, struct mg_connection *conn) 
{
    const struct mg_request_info *request_info = mg_get_request_info(conn);
    mg_send_file(conn, path);

    return "";
}

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

    // registers file
    path = argv[1];
    ctx = mg_start(&callback, NULL, options);
    printf("%s", path);
    getchar();  // Wait until user hits "enter"
    mg_stop(ctx);

    return 0;
}

我正在使用 Visual Studio 2010 来构建项目

有谁知道可能导致此错误的原因?

4

2 回答 2

1

您没有为回调函数分配返回值,根据定义,这是未定义的行为。检查回调的正确返回结果要求,因为void *不是void. 我很确定返回值取决于回调事件,但不要引用我的话。(该死的……太晚了)。

取自 mongoose 标头(至少是我可以访问的版本),描述了提供给的回调函数的目的和职责mg_start()

// Prototype for the user-defined function. Mongoose calls this function
// on every event mentioned above.
//
// Parameters:
//   event: which event has been triggered.
//   conn: opaque connection handler. Could be used to read, write data to the
//         client, etc. See functions below that accept "mg_connection *".
//   request_info: Information about HTTP request.
//
// Return:
//   If handler returns non-NULL, that means that handler has processed the
//   request by sending appropriate HTTP reply to the client. Mongoose treats
//   the request as served.
//   If callback returns NULL, that means that callback has not processed
//   the request. Handler must not send any data to the client in this case.
//   Mongoose proceeds with request handling as if nothing happened.

typedef void * (*mg_callback_t)(enum mg_event event,
                   struct mg_connection *conn,
                   const struct mg_request_info *request_info);
于 2013-01-07T21:56:28.430 回答
0

确保“路径”不为 NULL。分配一个默认值。

于 2013-12-04T09:57:23.830 回答