修改mongoose mg_http_serve_dir和mg_http_serve_file服务的文件的输出的最佳方法是什么?
我使用这个例子https://github.com/cesanta/mongoose/blob/master/examples/http-restful-server/main.c
我正在寻找一种方法来修改这样的输出
} else if (mg_http_match_uri(hm, "/api/f2/*")) {
mg_http_reply(c, 200, "", "{\"result\": \"%.*s\"}\n", (int) hm->uri.len,
hm->uri.ptr);
} else {
struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
mg_http_serve_dir(c, ev_data, &opts);
//After this call, I want to modify the output send by mongoose if possible for example replacing a string, and then setting the content length to the new size.
}
}
(void) fn_data;
}
字符串
目前,我将mg_http_serve_dir重新实现为local_mg_http_serve_dir,将mg_http_serve_file重新实现为local_mg_http_serve_file
我问自己是否有一个更简单的方法,我无法找到。
作为另一个问题,这是我定制的serve dir函数,以澄清我想做的事情:
void local_mg_http_serve_dir(struct mg_connection* c, struct mg_http_message* hm, const struct mg_http_serve_opts* opts) {
char path[MG_PATH_MAX];
const char* sp = opts->ssi_pattern;
int flags = uri_to_path(c, hm, opts, path, sizeof(path));
if (flags < 0) {
// Do nothing: the response has already been sent by uri_to_path()
}
else if (flags & MG_FS_DIR) {
#if MG_ENABLE_DIRLIST
listdir(c, hm, opts, path);
#else
mg_http_reply(c, 403, "", "Forbidden\n");
#endif
}
else if (flags && sp != NULL &&
mg_globmatch(sp, strlen(sp), path, strlen(path))) {
mg_http_serve_ssi(c, opts->root_dir, path);
}
else {
if (mg_http_match_uri(hm, "#.csp") || mg_http_match_uri(hm, "/")) {
if(!local_mg_http_serve_404(c, hm, path, opts)) {
char* cont;
cont = readFile(path);
Poco::JSON::Object::Ptr jdb = jdbRead("./var/db");
std::string menuHtml = jdbMenuToHtml("mainmenu", jdb);
replaceInPlace(cont, "<* menu *>", menuHtml.c_str());
mg_http_reply(c, 200, "content-type: text/html\r\n", "%s", cont);
}
}
else {
local_mg_http_serve_file(c, hm, path, opts);
}
}
}
型
1条答案
按热度按时间sf6xfgos1#
经过一些调试,我发现了我错过的东西。除了手动打开文件并在修改后作为响应发送之外,我还可以定义一个自定义的回调函数,并将其设置为默认值
字符串
它是从mg_http_serve_file函数调用的
型
不幸的是,这种方法仍然需要重新实现mg_http_serve_file一次,以修改文件描述符的内容长度,然后替换输出。
因此,我仍然在事件处理程序回调函数中使用我的旧local_mg_http_serve_file,例如:
https://github.com/cesanta/mongoose/blob/master/examples/huge-response/main.c