【发布时间】:2016-10-04 01:08:29
【问题描述】:
我正在使用 Simple-Web-Server 库创建简单的 Web 服务,用于将 XML 转换为 JSON,反之亦然。反过来,它使用了几个 boost 库以及其中的 boost::coroutine。对于 XMLJSON 转换,我使用 boost::property_tree 库进行中间表示。代码如下:
#include <iostream>
#include <sstream>
#include <server_http.hpp>
#define BOOST_SPIRIT_THREADSAFE
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
using namespace std;
using namespace boost::property_tree;
using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>;
int main()
{
HttpServer server(8080, 1);
server.resource["^/json_to_xml$"]["POST"] = [](auto& response, auto request) {
try
{
ptree pt;
read_json(request->content, pt);
ostringstream json, xml;
write_json(json, pt);
clog << "JSON request content:" << endl << json.str() << endl;
write_xml(xml, pt, xml_writer_make_settings<ptree::key_type>(' ', 1u));
clog << "XML response content:" << endl << xml.str() << endl;
response << "HTTP/1.1 200 OK\r\nContent-Length: " << xml.str().length() << "\r\n\r\n" << xml.str();
}
catch(const exception& e)
{
cerr << "Error:" << endl << e.what() << endl;
response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what();
}
};
server.resource["^/xml_to_json$"]["POST"] = [](auto& response, auto request) {
try
{
ptree pt;
read_xml(request->content, pt, xml_parser::trim_whitespace | xml_parser::no_comments);
ostringstream xml, json;
write_xml(xml, pt, xml_writer_make_settings<ptree::key_type>(' ', 1u));
clog << "XML request content:" << endl << xml.str() << endl;
write_json(json, pt);
clog << "JSON response content: " << endl << json.str() << endl;
response << "HTTP/1.1 200 OK\r\nContent-Length: " << json.str().length() << "\r\n\r\n" << json.str();
}
catch(const exception& e)
{
cerr << "Error:" << endl << e.what() << endl;
response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what();
}
};
server.start();
return 0;
}
JSON 到 XML 的转换工作正常,但相反会导致程序崩溃。当我不在服务器回调中使用 boost::property_tree 的 XML 解析器时,程序运行良好。 Here 是执行的结果。 Here 是 GDB 回溯。最后here 是Valgrind 输出。
boost 库的使用版本是 1.58.0,但在最新版本 1.61.0 上观察到的结果相同。使用Simple-Web-Server的1.4.2版本。
【问题讨论】:
-
那是很多代码。看起来好像 Simple-Web-Server 不是生产就绪代码。我已经看了一段时间了,但是有很多活动部件,很难掌握。
-
Web 服务不再与不再使用 boost::coroutine 的 Simple-Web-Server 的 version 2.0 崩溃,但我想知道这是否是 boost::coroutine 和 boost::property_tree 之间的某种不兼容,因为我过去两者都使用过,并且我在@中描述了类似的奇怪崩溃987654326@ 问题。那时候我没有把它和boost::property_tree XML解析联系起来,但现在我认为可能有一些联系。
标签: c++ boost boost-asio boost-propertytree boost-coroutine