我正在开发一个简单的基于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来构建项目
有没有人知道可能导致此错误的原因?
答案 0 :(得分: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);
答案 1 :(得分:0)
确保“path”不为NULL。分配默认值。