提交 74cc7163a95a90c60a72043ea603d5a632ede897
Merge branch 'master' of http://gitlab.ctune.cn/DMapServer/DMapServer4.1
正在显示
8 个修改的文件
包含
368 行增加
和
278 行删除
| @@ -13,15 +13,6 @@ | @@ -13,15 +13,6 @@ | ||
| 13 | #include <boost/property_tree/ini_parser.hpp> | 13 | #include <boost/property_tree/ini_parser.hpp> |
| 14 | #include <boost/property_tree/ptree.hpp> | 14 | #include <boost/property_tree/ptree.hpp> |
| 15 | 15 | ||
| 16 | -// bool DmpApplication::running_from_build_dir_ = false; | ||
| 17 | -// bool DmpApplication::initialized_ = false; | ||
| 18 | -// std::string DmpApplication::plugin_path_ = ""; | ||
| 19 | -// std::string DmpApplication::prefix_path_ = ""; | ||
| 20 | -// std::string DmpApplication::libexec_path_ = ""; | ||
| 21 | -// std::string DmpApplication::library_path_ = ""; | ||
| 22 | -// std::string DmpApplication::build_source_path_ = ""; | ||
| 23 | -// std::string DmpApplication::build_output_path_ = ""; | ||
| 24 | -// std::string DmpApplication::pkgDataPath_ = ""; | ||
| 25 | 16 | ||
| 26 | DmpApplication::DmpApplication() | 17 | DmpApplication::DmpApplication() |
| 27 | { | 18 | { |
| @@ -60,7 +51,6 @@ void DmpApplication::initialize() | @@ -60,7 +51,6 @@ void DmpApplication::initialize() | ||
| 60 | setPrefixPath(prefixPath); | 51 | setPrefixPath(prefixPath); |
| 61 | } | 52 | } |
| 62 | } | 53 | } |
| 63 | - // createIniFile(libexecPath_); | ||
| 64 | initialized_ = true; | 54 | initialized_ = true; |
| 65 | } | 55 | } |
| 66 | bool DmpApplication::isRunningFromBuildDir() | 56 | bool DmpApplication::isRunningFromBuildDir() |
| @@ -195,21 +185,4 @@ std::string DmpApplication::buildOutputPath() | @@ -195,21 +185,4 @@ std::string DmpApplication::buildOutputPath() | ||
| 195 | std::string DmpApplication::dmapSettingsDirPath() | 185 | std::string DmpApplication::dmapSettingsDirPath() |
| 196 | { | 186 | { |
| 197 | return ""; | 187 | return ""; |
| 198 | -} | ||
| 199 | - | ||
| 200 | -void DmpApplication::createIniFile(const std::string &filePath) | ||
| 201 | -{ | ||
| 202 | - std::string fileName = filePath + iniFileName_; | ||
| 203 | - if(!boost::filesystem::exists(fileName)) { | ||
| 204 | - boost::property_tree::ptree pt; | ||
| 205 | - pt.put<std::string>("Core.Filter","%Severity% >= debug"); | ||
| 206 | - pt.put<std::string>("Sinks.TextFileSettings.Destination","TextFile"); | ||
| 207 | - // pt.put<std::string>("Sinks.TextFileSettings.FileName","/var/log/dmap/%Y%m%d.log"); | ||
| 208 | - // pt.put<bool>("Sinks.TextFileSettings.AutoFlush",true); | ||
| 209 | - boost::property_tree::ini_parser::write_ini(fileName,pt); | ||
| 210 | - } | ||
| 211 | - | ||
| 212 | - // boost::property_tree::ptree root_node, tag_system; | ||
| 213 | - // boost::property_tree::ini_parser::read_ini("config.ini", root_node); | ||
| 214 | - | ||
| 215 | -} | 188 | +} |
| @@ -26,10 +26,9 @@ public: | @@ -26,10 +26,9 @@ public: | ||
| 26 | //static bool CreateDatabase(); | 26 | //static bool CreateDatabase(); |
| 27 | std::string resolvePkgPath(); | 27 | std::string resolvePkgPath(); |
| 28 | std::string applicationDirPath(); | 28 | std::string applicationDirPath(); |
| 29 | - bool searchFile(const boost::filesystem::path &dir, const std::string fileName, boost::filesystem::path &path); | 29 | + std::string dmapSettingsDirPath(); |
| 30 | + | ||
| 30 | bool isRunningFromBuildDir(); | 31 | bool isRunningFromBuildDir(); |
| 31 | - static std::string dmapSettingsDirPath(); | ||
| 32 | - | ||
| 33 | std::string prefixPath(); | 32 | std::string prefixPath(); |
| 34 | void setPrefixPath(const std::string &prefixPath); | 33 | void setPrefixPath(const std::string &prefixPath); |
| 35 | void setPluginPath(const std::string &pluginPath); | 34 | void setPluginPath(const std::string &pluginPath); |
| @@ -40,12 +39,11 @@ public: | @@ -40,12 +39,11 @@ public: | ||
| 40 | std::string pkgDataPath(); | 39 | std::string pkgDataPath(); |
| 41 | std::string libraryPath(); | 40 | std::string libraryPath(); |
| 42 | std::string buildOutputPath(); | 41 | std::string buildOutputPath(); |
| 43 | - | ||
| 44 | - void createIniFile(const std::string &filePath); | ||
| 45 | - | 42 | + |
| 46 | private: | 43 | private: |
| 47 | DmpApplication(); | 44 | DmpApplication(); |
| 48 | - | 45 | + bool searchFile(const boost::filesystem::path &dir, const std::string fileName, boost::filesystem::path &path); |
| 46 | + | ||
| 49 | std::string prefixPath_; | 47 | std::string prefixPath_; |
| 50 | std::string libexecPath_; | 48 | std::string libexecPath_; |
| 51 | std::string libraryPath_; | 49 | std::string libraryPath_; |
| @@ -53,9 +51,9 @@ private: | @@ -53,9 +51,9 @@ private: | ||
| 53 | std::string buildSourcePath_; | 51 | std::string buildSourcePath_; |
| 54 | std::string buildOutputPath_; | 52 | std::string buildOutputPath_; |
| 55 | std::string pkgDataPath_; | 53 | std::string pkgDataPath_; |
| 54 | + std::string iniFilePath_; | ||
| 56 | bool runningFromBuildDir_; | 55 | bool runningFromBuildDir_; |
| 57 | bool initialized_; | 56 | bool initialized_; |
| 58 | - const std::string iniFileName_ = "dmap.ini"; | ||
| 59 | }; | 57 | }; |
| 60 | 58 | ||
| 61 | #endif //__dmpapplication_h__ | 59 | #endif //__dmpapplication_h__ |
| @@ -18,6 +18,7 @@ SET (DMAP_SERVER_SRCS | @@ -18,6 +18,7 @@ SET (DMAP_SERVER_SRCS | ||
| 18 | dmprequesthandler.cpp | 18 | dmprequesthandler.cpp |
| 19 | dmpserver.cpp | 19 | dmpserver.cpp |
| 20 | dmpserverapi.cpp | 20 | dmpserverapi.cpp |
| 21 | + dmpserverconfig.cpp | ||
| 21 | dmpservercontext.cpp | 22 | dmpservercontext.cpp |
| 22 | dmpserverapicontext.cpp | 23 | dmpserverapicontext.cpp |
| 23 | dmpserverapihandler.cpp | 24 | dmpserverapihandler.cpp |
| @@ -48,8 +49,9 @@ SET (DMAP_SERVER_HDRS | @@ -48,8 +49,9 @@ SET (DMAP_SERVER_HDRS | ||
| 48 | dmpserver.h | 49 | dmpserver.h |
| 49 | dmpserverapi.h | 50 | dmpserverapi.h |
| 50 | dmpserverapihandler.h | 51 | dmpserverapihandler.h |
| 51 | - dmpservercontext.h | ||
| 52 | dmpserverapicontext.h | 52 | dmpserverapicontext.h |
| 53 | + dmpservercontext.h | ||
| 54 | + dmpserverconfig.h | ||
| 53 | dmpserverentity.h | 55 | dmpserverentity.h |
| 54 | dmpserverfilter.h | 56 | dmpserverfilter.h |
| 55 | dmpserverinterface.h | 57 | dmpserverinterface.h |
| @@ -20,6 +20,7 @@ | @@ -20,6 +20,7 @@ | ||
| 20 | #include "dmpspserverrequest.h" | 20 | #include "dmpspserverrequest.h" |
| 21 | #include "dmpspserverresponse.h" | 21 | #include "dmpspserverresponse.h" |
| 22 | #include "dmpapplication.h" | 22 | #include "dmpapplication.h" |
| 23 | +#include "dmpserverconfig.h" | ||
| 23 | 24 | ||
| 24 | DmpMainServer *pDmpServer = nullptr; | 25 | DmpMainServer *pDmpServer = nullptr; |
| 25 | class SP_HttpEchoHandler : public SP_HttpHandler | 26 | class SP_HttpEchoHandler : public SP_HttpHandler |
| @@ -113,6 +114,8 @@ int main(int argc, char *argv[]) | @@ -113,6 +114,8 @@ int main(int argc, char *argv[]) | ||
| 113 | int c; | 114 | int c; |
| 114 | DmpApplication::Instance()->initialize(); | 115 | DmpApplication::Instance()->initialize(); |
| 115 | pDmpServer = new DmpMainServer(); | 116 | pDmpServer = new DmpMainServer(); |
| 117 | + // std::string conn = DmpServerConfig::Instance()->getPqsqlConnect(); | ||
| 118 | + // std::string email = DmpServerConfig::Instance()->getValue("MetaData","email"); | ||
| 116 | while ((c = getopt(argc, argv, "p:t:s:v")) != EOF) | 119 | while ((c = getopt(argc, argv, "p:t:s:v")) != EOF) |
| 117 | { | 120 | { |
| 118 | switch (c) | 121 | switch (c) |
| 1 | -/************************************************************************** | ||
| 2 | -* file: dmphttputils.cpp | ||
| 3 | - | ||
| 4 | -* Author: lijiahuan | ||
| 5 | -* Date: 2021-12-09 17:41:00 | ||
| 6 | -* Email: jiahuanl@chinadci.com | ||
| 7 | -* copyright: 广州城市信息研究所有限公司 | ||
| 8 | -***************************************************************************/ | ||
| 9 | -#include "dmphttputils.h" | ||
| 10 | -#include <boost/bind.hpp> | ||
| 11 | - | ||
| 12 | -DmpHttp::DmpHttpUtils::DmpHttpUtils(boost::asio::io_service& io_service) | ||
| 13 | -: resolver_(io_service), socket_(io_service) {} | ||
| 14 | -DmpHttp::DmpHttpUtils::~DmpHttpUtils() {} | ||
| 15 | - | ||
| 16 | -int DmpHttp::DmpHttpUtils::post(const std::string& url) | ||
| 17 | -{ | ||
| 18 | - handle_request_resolve(url, DmpHttpBase::buildPostRequest); | ||
| 19 | - return HTTP_SUCCESS; | ||
| 20 | -} | ||
| 21 | -int DmpHttp::DmpHttpUtils::get(const std::string& url) | ||
| 22 | -{ | ||
| 23 | - handle_request_resolve(url, DmpHttpBase::buildGetRequest); | ||
| 24 | - return HTTP_SUCCESS; | ||
| 25 | -} | ||
| 26 | -void DmpHttp::DmpHttpUtils::handle_request_resolve(const std::string& url, pBuildRequest func) { | ||
| 27 | - try { | ||
| 28 | - responseData_.clear(); | ||
| 29 | - // 解析URL | ||
| 30 | - std::string server, port, path; | ||
| 31 | - parseUrl(url, server, port, path); | ||
| 32 | - | ||
| 33 | - std::ostream request_stream(&request_); | ||
| 34 | - // 合成请求 | ||
| 35 | - func(server, path, request_stream); | ||
| 36 | - | ||
| 37 | - // 解析服务地址\端口 | ||
| 38 | - boost::asio::ip::tcp::resolver::query query(server, port); | ||
| 39 | - resolver_.async_resolve(query, | ||
| 40 | - boost::bind(&DmpHttpUtils::handle_resolve, this, | ||
| 41 | - boost::asio::placeholders::error, | ||
| 42 | - boost::asio::placeholders::iterator)); | ||
| 43 | - } | ||
| 44 | - catch (std::exception& e) { | ||
| 45 | - socket_.close(); | ||
| 46 | - std::cout << "Exception: " << e.what() << "\n"; | ||
| 47 | - } | ||
| 48 | - return; | ||
| 49 | -} | ||
| 50 | -void DmpHttp::DmpHttpUtils::handle_resolve(const boost::system::error_code& err, | ||
| 51 | - boost::asio::ip::tcp::resolver::iterator endpoint_iterator) { | ||
| 52 | - if (err) { | ||
| 53 | - std::cout << "Error: " << err << "\n"; | ||
| 54 | - return; | ||
| 55 | - } | ||
| 56 | - // 尝试连接 | ||
| 57 | - boost::asio::async_connect(socket_, endpoint_iterator, | ||
| 58 | - boost::bind(&DmpHttpUtils::handle_connect, this, | ||
| 59 | - boost::asio::placeholders::error)); | ||
| 60 | -} | ||
| 61 | -void DmpHttp::DmpHttpUtils::handle_connect(const boost::system::error_code& err) { | ||
| 62 | - if (err) { | ||
| 63 | - std::cout << "Error: " << err << "\n"; | ||
| 64 | - return; | ||
| 65 | - } | ||
| 66 | - // 发送request请求 | ||
| 67 | - boost::asio::async_write(socket_, request_, | ||
| 68 | - boost::bind(&DmpHttpUtils::handle_write_request, this, | ||
| 69 | - boost::asio::placeholders::error)); | ||
| 70 | -} | ||
| 71 | -void DmpHttp::DmpHttpUtils::handle_write_request(const boost::system::error_code& err) { | ||
| 72 | - if (err) { | ||
| 73 | - std::cout << "Error: " << err << "\n"; | ||
| 74 | - return; | ||
| 75 | - } | ||
| 76 | - // 异步持续读数据到response_,直到接收协议符号 \r\n 为止 | ||
| 77 | - boost::asio::async_read_until(socket_, response_, "\r\n", | ||
| 78 | - boost::bind(&DmpHttpUtils::handle_read_status_line, this, | ||
| 79 | - boost::asio::placeholders::error)); | ||
| 80 | -} | ||
| 81 | -void DmpHttp::DmpHttpUtils::handle_read_status_line(const boost::system::error_code& err) { | ||
| 82 | - if (err) { | ||
| 83 | - std::cout << "Error: " << err << "\n"; | ||
| 84 | - return; | ||
| 85 | - } | ||
| 86 | - // 解析buff | ||
| 87 | - std::istream response_stream(&response_); | ||
| 88 | - unsigned int status_code; | ||
| 89 | - std::string http_version, status_message; | ||
| 90 | - response_stream >> http_version; | ||
| 91 | - response_stream >> status_code; | ||
| 92 | - std::getline(response_stream, status_message); | ||
| 93 | - | ||
| 94 | - // 核对是否是正确返回 | ||
| 95 | - if (!response_stream || http_version.substr(0, 5) != "HTTP/") { | ||
| 96 | - std::cout << "错误的响应数据\n"; | ||
| 97 | - return; | ||
| 98 | - } | ||
| 99 | - if (status_code != 200) { | ||
| 100 | - std::cout << "服务器响应的状态码: " << status_code << "\n"; | ||
| 101 | - return; | ||
| 102 | - } | ||
| 103 | - // 读取响应头,直到接收协议符号 \r\n\r\n 为止 | ||
| 104 | - boost::asio::async_read_until(socket_, response_, "\r\n\r\n", | ||
| 105 | - boost::bind(&DmpHttpUtils::handle_read_headers, this, | ||
| 106 | - boost::asio::placeholders::error)); | ||
| 107 | -} | ||
| 108 | - | ||
| 109 | -void DmpHttp::DmpHttpUtils::handle_read_headers(const boost::system::error_code& err) { | ||
| 110 | - if (err) { | ||
| 111 | - std::cout << "Error: " << err << "\n"; | ||
| 112 | - return; | ||
| 113 | - } | ||
| 114 | - // 输出响应头 | ||
| 115 | - std::istream response_stream(&response_); | ||
| 116 | - std::string header; | ||
| 117 | - while (std::getline(response_stream, header) && header != "\r") { | ||
| 118 | -#ifdef _DEBUG | ||
| 119 | - std::cout << header << "\n"; | ||
| 120 | -#endif | ||
| 121 | - } | ||
| 122 | -#ifdef _DEBUG | ||
| 123 | - std::cout << "\n"; | ||
| 124 | -#endif | ||
| 125 | - | ||
| 126 | - // 写完所有剩余的内容 | ||
| 127 | - if (response_.size() > 0) { | ||
| 128 | - boost::asio::streambuf::const_buffers_type cbt = response_.data(); | ||
| 129 | - responseData_ += std::string(boost::asio::buffers_begin(cbt), boost::asio::buffers_end(cbt)); | ||
| 130 | -#ifdef _DEBUG | ||
| 131 | - std::cout << &response_; | ||
| 132 | -#endif | ||
| 133 | - } | ||
| 134 | - | ||
| 135 | - // 开始读取剩余所有内容 | ||
| 136 | - boost::asio::async_read(socket_, response_, | ||
| 137 | - boost::asio::transfer_at_least(1), | ||
| 138 | - boost::bind(&DmpHttpUtils::handle_read_content, this, | ||
| 139 | - boost::asio::placeholders::error)); | ||
| 140 | -} | ||
| 141 | -void DmpHttp::DmpHttpUtils::handle_read_content(const boost::system::error_code& err) { | ||
| 142 | - if (!err) { | ||
| 143 | - | ||
| 144 | - // 输出读到的数据 | ||
| 145 | - boost::asio::streambuf::const_buffers_type cbt = response_.data(); | ||
| 146 | - | ||
| 147 | - responseData_ = std::string(boost::asio::buffers_begin(cbt), boost::asio::buffers_end(cbt)); | ||
| 148 | -#ifdef _DEBUG | ||
| 149 | - std::cout << &response_; | ||
| 150 | -#endif | ||
| 151 | - | ||
| 152 | - // 继续读取剩余内容,直到读到EOF | ||
| 153 | - boost::asio::async_read(socket_, response_, | ||
| 154 | - boost::asio::transfer_at_least(1), | ||
| 155 | - boost::bind(&DmpHttpUtils::handle_read_content, this, | ||
| 156 | - boost::asio::placeholders::error)); | ||
| 157 | - | ||
| 158 | - } | ||
| 159 | - else if (err != boost::asio::error::eof) { | ||
| 160 | - std::cout << "Error: " << err << "\n"; | ||
| 161 | - } | ||
| 162 | - else { | ||
| 163 | - socket_.close(); | ||
| 164 | - resolver_.cancel(); | ||
| 165 | - std::cout << "读取响应数据完毕." << std::endl; | ||
| 166 | - //std::cout << responseData_; | ||
| 167 | - } | ||
| 168 | -} | ||
| 169 | - | ||
| 170 | - | ||
| 171 | -std::string DmpHttp::post(std::string url) { | ||
| 172 | - boost::asio::io_service io; | ||
| 173 | - DmpHttp::DmpHttpUtils c(io); | ||
| 174 | - c.post(url); | ||
| 175 | - io.run(); | ||
| 176 | - return c.getResponse(); | ||
| 177 | -} | ||
| 178 | - | ||
| 179 | -std::string DmpHttp::get(std::string url) { | ||
| 180 | - boost::asio::io_service io; | ||
| 181 | - DmpHttp::DmpHttpUtils c(io); | ||
| 182 | - c.get(url); | ||
| 183 | - io.run(); | ||
| 184 | - return c.getResponse(); | 1 | +/************************************************************************** |
| 2 | +* file: dmphttputils.cpp | ||
| 3 | + | ||
| 4 | +* Author: lijiahuan | ||
| 5 | +* Date: 2021-12-09 17:41:00 | ||
| 6 | +* Email: jiahuanl@chinadci.com | ||
| 7 | +* copyright: 广州城市信息研究所有限公司 | ||
| 8 | +***************************************************************************/ | ||
| 9 | +#include "dmphttputils.h" | ||
| 10 | +#include <boost/bind.hpp> | ||
| 11 | + | ||
| 12 | +DmpHttp::DmpHttpUtils::DmpHttpUtils(boost::asio::io_service& io_service) | ||
| 13 | +: resolver_(io_service), socket_(io_service) {} | ||
| 14 | +DmpHttp::DmpHttpUtils::~DmpHttpUtils() {} | ||
| 15 | + | ||
| 16 | +int DmpHttp::DmpHttpUtils::post(const std::string& url) | ||
| 17 | +{ | ||
| 18 | + handle_request_resolve(url, DmpHttpBase::buildPostRequest); | ||
| 19 | + return HTTP_SUCCESS; | ||
| 20 | +} | ||
| 21 | +int DmpHttp::DmpHttpUtils::get(const std::string& url) | ||
| 22 | +{ | ||
| 23 | + handle_request_resolve(url, DmpHttpBase::buildGetRequest); | ||
| 24 | + return HTTP_SUCCESS; | ||
| 25 | +} | ||
| 26 | +void DmpHttp::DmpHttpUtils::handle_request_resolve(const std::string& url, pBuildRequest func) { | ||
| 27 | + try { | ||
| 28 | + responseData_.clear(); | ||
| 29 | + // 解析URL | ||
| 30 | + std::string server, port, path; | ||
| 31 | + parseUrl(url, server, port, path); | ||
| 32 | + | ||
| 33 | + std::ostream request_stream(&request_); | ||
| 34 | + // 合成请求 | ||
| 35 | + func(server, path, request_stream); | ||
| 36 | + | ||
| 37 | + // 解析服务地址\端口 | ||
| 38 | + boost::asio::ip::tcp::resolver::query query(server, port); | ||
| 39 | + resolver_.async_resolve(query, | ||
| 40 | + boost::bind(&DmpHttpUtils::handle_resolve, this, | ||
| 41 | + boost::asio::placeholders::error, | ||
| 42 | + boost::asio::placeholders::iterator)); | ||
| 43 | + } | ||
| 44 | + catch (std::exception& e) { | ||
| 45 | + socket_.close(); | ||
| 46 | + std::cout << "Exception: " << e.what() << "\n"; | ||
| 47 | + } | ||
| 48 | + return; | ||
| 49 | +} | ||
| 50 | +void DmpHttp::DmpHttpUtils::handle_resolve(const boost::system::error_code& err, | ||
| 51 | + boost::asio::ip::tcp::resolver::iterator endpoint_iterator) { | ||
| 52 | + if (err) { | ||
| 53 | + std::cout << "Error: " << err << "\n"; | ||
| 54 | + return; | ||
| 55 | + } | ||
| 56 | + // 尝试连接 | ||
| 57 | + boost::asio::async_connect(socket_, endpoint_iterator, | ||
| 58 | + boost::bind(&DmpHttpUtils::handle_connect, this, | ||
| 59 | + boost::asio::placeholders::error)); | ||
| 60 | +} | ||
| 61 | +void DmpHttp::DmpHttpUtils::handle_connect(const boost::system::error_code& err) { | ||
| 62 | + if (err) { | ||
| 63 | + std::cout << "Error: " << err << "\n"; | ||
| 64 | + return; | ||
| 65 | + } | ||
| 66 | + // 发送request请求 | ||
| 67 | + boost::asio::async_write(socket_, request_, | ||
| 68 | + boost::bind(&DmpHttpUtils::handle_write_request, this, | ||
| 69 | + boost::asio::placeholders::error)); | ||
| 70 | +} | ||
| 71 | +void DmpHttp::DmpHttpUtils::handle_write_request(const boost::system::error_code& err) { | ||
| 72 | + if (err) { | ||
| 73 | + std::cout << "Error: " << err << "\n"; | ||
| 74 | + return; | ||
| 75 | + } | ||
| 76 | + // 异步持续读数据到response_,直到接收协议符号 \r\n 为止 | ||
| 77 | + boost::asio::async_read_until(socket_, response_, "\r\n", | ||
| 78 | + boost::bind(&DmpHttpUtils::handle_read_status_line, this, | ||
| 79 | + boost::asio::placeholders::error)); | ||
| 80 | +} | ||
| 81 | +void DmpHttp::DmpHttpUtils::handle_read_status_line(const boost::system::error_code& err) { | ||
| 82 | + if (err) { | ||
| 83 | + std::cout << "Error: " << err << "\n"; | ||
| 84 | + return; | ||
| 85 | + } | ||
| 86 | + // 解析buff | ||
| 87 | + std::istream response_stream(&response_); | ||
| 88 | + unsigned int status_code; | ||
| 89 | + std::string http_version, status_message; | ||
| 90 | + response_stream >> http_version; | ||
| 91 | + response_stream >> status_code; | ||
| 92 | + std::getline(response_stream, status_message); | ||
| 93 | + | ||
| 94 | + // 核对是否是正确返回 | ||
| 95 | + if (!response_stream || http_version.substr(0, 5) != "HTTP/") { | ||
| 96 | + std::cout << "错误的响应数据\n"; | ||
| 97 | + return; | ||
| 98 | + } | ||
| 99 | + if (status_code != 200) { | ||
| 100 | + std::cout << "服务器响应的状态码: " << status_code << "\n"; | ||
| 101 | + return; | ||
| 102 | + } | ||
| 103 | + // 读取响应头,直到接收协议符号 \r\n\r\n 为止 | ||
| 104 | + boost::asio::async_read_until(socket_, response_, "\r\n\r\n", | ||
| 105 | + boost::bind(&DmpHttpUtils::handle_read_headers, this, | ||
| 106 | + boost::asio::placeholders::error)); | ||
| 107 | +} | ||
| 108 | + | ||
| 109 | +void DmpHttp::DmpHttpUtils::handle_read_headers(const boost::system::error_code& err) { | ||
| 110 | + if (err) { | ||
| 111 | + std::cout << "Error: " << err << "\n"; | ||
| 112 | + return; | ||
| 113 | + } | ||
| 114 | + // 输出响应头 | ||
| 115 | + std::istream response_stream(&response_); | ||
| 116 | + std::string header; | ||
| 117 | + while (std::getline(response_stream, header) && header != "\r") { | ||
| 118 | +#ifdef _DEBUG | ||
| 119 | + std::cout << header << "\n"; | ||
| 120 | +#endif | ||
| 121 | + } | ||
| 122 | +#ifdef _DEBUG | ||
| 123 | + std::cout << "\n"; | ||
| 124 | +#endif | ||
| 125 | + | ||
| 126 | + // 写完所有剩余的内容 | ||
| 127 | + if (response_.size() > 0) { | ||
| 128 | + boost::asio::streambuf::const_buffers_type cbt = response_.data(); | ||
| 129 | + responseData_ += std::string(boost::asio::buffers_begin(cbt), boost::asio::buffers_end(cbt)); | ||
| 130 | +#ifdef _DEBUG | ||
| 131 | + std::cout << &response_; | ||
| 132 | +#endif | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + // 开始读取剩余所有内容 | ||
| 136 | + boost::asio::async_read(socket_, response_, | ||
| 137 | + boost::asio::transfer_at_least(1), | ||
| 138 | + boost::bind(&DmpHttpUtils::handle_read_content, this, | ||
| 139 | + boost::asio::placeholders::error)); | ||
| 140 | +} | ||
| 141 | +void DmpHttp::DmpHttpUtils::handle_read_content(const boost::system::error_code& err) { | ||
| 142 | + if (!err) { | ||
| 143 | + | ||
| 144 | + // 输出读到的数据 | ||
| 145 | + boost::asio::streambuf::const_buffers_type cbt = response_.data(); | ||
| 146 | + | ||
| 147 | + responseData_ = std::string(boost::asio::buffers_begin(cbt), boost::asio::buffers_end(cbt)); | ||
| 148 | +#ifdef _DEBUG | ||
| 149 | + std::cout << &response_; | ||
| 150 | +#endif | ||
| 151 | + | ||
| 152 | + // 继续读取剩余内容,直到读到EOF | ||
| 153 | + boost::asio::async_read(socket_, response_, | ||
| 154 | + boost::asio::transfer_at_least(1), | ||
| 155 | + boost::bind(&DmpHttpUtils::handle_read_content, this, | ||
| 156 | + boost::asio::placeholders::error)); | ||
| 157 | + | ||
| 158 | + } | ||
| 159 | + else if (err != boost::asio::error::eof) { | ||
| 160 | + std::cout << "Error: " << err << "\n"; | ||
| 161 | + } | ||
| 162 | + else { | ||
| 163 | + socket_.close(); | ||
| 164 | + resolver_.cancel(); | ||
| 165 | + } | ||
| 166 | +} | ||
| 167 | + | ||
| 168 | + | ||
| 169 | +std::string DmpHttp::post(const std::string &url) { | ||
| 170 | + boost::asio::io_service io; | ||
| 171 | + DmpHttp::DmpHttpUtils c(io); | ||
| 172 | + c.post(url); | ||
| 173 | + io.run(); | ||
| 174 | + return c.getResponse(); | ||
| 175 | +} | ||
| 176 | + | ||
| 177 | +std::string DmpHttp::get(const std::string &url) { | ||
| 178 | + boost::asio::io_service io; | ||
| 179 | + DmpHttp::DmpHttpUtils c(io); | ||
| 180 | + c.get(url); | ||
| 181 | + io.run(); | ||
| 182 | + return c.getResponse(); | ||
| 185 | } | 183 | } |
| 1 | -/************************************************************************** | ||
| 2 | -* file: dmphttputils.h | ||
| 3 | - | ||
| 4 | -* Author: lijiahuan | ||
| 5 | -* Date: 2021-12-09 17:41:00 | ||
| 6 | -* Email: jiahuanl@chinadci.com | ||
| 7 | -* copyright: 广州城市信息研究所有限公司 | ||
| 8 | -***************************************************************************/ | ||
| 9 | -#ifndef __dmppghttputils_h__ | ||
| 10 | -#define __dmppghttputils_h__ | ||
| 11 | -#include <iostream> | ||
| 12 | -#include <boost/asio.hpp> | ||
| 13 | -#include "dmphttpbase.h" | ||
| 14 | - | ||
| 15 | -namespace DmpHttp | ||
| 16 | -{ | ||
| 17 | - class DmpHttpUtils : public DmpHttpBase | ||
| 18 | - { | ||
| 19 | - public: | ||
| 20 | - DmpHttpUtils(boost::asio::io_service& io_service); | ||
| 21 | - virtual ~DmpHttpUtils(); | ||
| 22 | - virtual int post(const std::string& url); | ||
| 23 | - virtual int get(const std::string& url); | ||
| 24 | - | ||
| 25 | - virtual std::string getResponse(void) {return responseData_;} | ||
| 26 | - private: | ||
| 27 | - // 建立请求 | ||
| 28 | - void handle_request_resolve(const std::string& url, pBuildRequest func); | ||
| 29 | - // 解析后 | ||
| 30 | - void handle_resolve(const boost::system::error_code& err, | ||
| 31 | - boost::asio::ip::tcp::resolver::iterator endpoint_iterator); | ||
| 32 | - // 连接后 | ||
| 33 | - void handle_connect(const boost::system::error_code& err); | ||
| 34 | - // 发送请求后 | ||
| 35 | - void handle_write_request(const boost::system::error_code& err); | ||
| 36 | - // 读取响应后 | ||
| 37 | - void handle_read_status_line(const boost::system::error_code& err); | ||
| 38 | - // 读取响应头后 | ||
| 39 | - void handle_read_headers(const boost::system::error_code& err); | ||
| 40 | - // 读取正文数据后 | ||
| 41 | - void handle_read_content(const boost::system::error_code& err); | ||
| 42 | - private: | ||
| 43 | - // 解析器 | ||
| 44 | - boost::asio::ip::tcp::resolver resolver_; | ||
| 45 | - // 套接字 | ||
| 46 | - boost::asio::ip::tcp::socket socket_; | ||
| 47 | - // 请求缓冲区 | ||
| 48 | - boost::asio::streambuf request_; | ||
| 49 | - // 响应缓冲区 | ||
| 50 | - boost::asio::streambuf response_; | ||
| 51 | - // 响应数据 | ||
| 52 | - std::string responseData_; | ||
| 53 | - }; | ||
| 54 | - std::string post(std::string url); | ||
| 55 | - std::string get(std::string url); | ||
| 56 | -} | ||
| 57 | - | 1 | +/************************************************************************** |
| 2 | +* file: dmphttputils.h | ||
| 3 | + | ||
| 4 | +* Author: lijiahuan | ||
| 5 | +* Date: 2021-12-09 17:41:00 | ||
| 6 | +* Email: jiahuanl@chinadci.com | ||
| 7 | +* copyright: 广州城市信息研究所有限公司 | ||
| 8 | +***************************************************************************/ | ||
| 9 | +#ifndef __dmppghttputils_h__ | ||
| 10 | +#define __dmppghttputils_h__ | ||
| 11 | +#include <iostream> | ||
| 12 | +#include <boost/asio.hpp> | ||
| 13 | +#include "dmphttpbase.h" | ||
| 14 | + | ||
| 15 | +namespace DmpHttp | ||
| 16 | +{ | ||
| 17 | + class DmpHttpUtils : public DmpHttpBase | ||
| 18 | + { | ||
| 19 | + public: | ||
| 20 | + DmpHttpUtils(boost::asio::io_service& io_service); | ||
| 21 | + virtual ~DmpHttpUtils(); | ||
| 22 | + virtual int post(const std::string& url); | ||
| 23 | + virtual int get(const std::string& url); | ||
| 24 | + | ||
| 25 | + virtual std::string getResponse(void) {return responseData_;} | ||
| 26 | + private: | ||
| 27 | + // 建立请求 | ||
| 28 | + void handle_request_resolve(const std::string& url, pBuildRequest func); | ||
| 29 | + // 解析后 | ||
| 30 | + void handle_resolve(const boost::system::error_code& err, | ||
| 31 | + boost::asio::ip::tcp::resolver::iterator endpoint_iterator); | ||
| 32 | + // 连接后 | ||
| 33 | + void handle_connect(const boost::system::error_code& err); | ||
| 34 | + // 发送请求后 | ||
| 35 | + void handle_write_request(const boost::system::error_code& err); | ||
| 36 | + // 读取响应后 | ||
| 37 | + void handle_read_status_line(const boost::system::error_code& err); | ||
| 38 | + // 读取响应头后 | ||
| 39 | + void handle_read_headers(const boost::system::error_code& err); | ||
| 40 | + // 读取正文数据后 | ||
| 41 | + void handle_read_content(const boost::system::error_code& err); | ||
| 42 | + private: | ||
| 43 | + // 解析器 | ||
| 44 | + boost::asio::ip::tcp::resolver resolver_; | ||
| 45 | + // 套接字 | ||
| 46 | + boost::asio::ip::tcp::socket socket_; | ||
| 47 | + // 请求缓冲区 | ||
| 48 | + boost::asio::streambuf request_; | ||
| 49 | + // 响应缓冲区 | ||
| 50 | + boost::asio::streambuf response_; | ||
| 51 | + // 响应数据 | ||
| 52 | + std::string responseData_; | ||
| 53 | + }; | ||
| 54 | + std::string post(const std::string &url); | ||
| 55 | + std::string get(const std::string &url); | ||
| 56 | +} | ||
| 57 | + | ||
| 58 | #endif //__dmphttpsutils_h__ | 58 | #endif //__dmphttpsutils_h__ |
src/server/dmpserverconfig.cpp
0 → 100644
| 1 | +/************************************************************************** | ||
| 2 | +* file: dmpserverconfig.cpp | ||
| 3 | + | ||
| 4 | +* Author: wanzhongping | ||
| 5 | +* Date: 2021-12-16 16:55:51 | ||
| 6 | +* Email: zhongpingw@chinadci.com | ||
| 7 | +* copyright: 广州城市信息研究所有限公司 | ||
| 8 | +***************************************************************************/ | ||
| 9 | + | ||
| 10 | +#include <iostream> | ||
| 11 | +#include "dmpserverconfig.h" | ||
| 12 | +#include "dmpapplication.h" | ||
| 13 | + | ||
| 14 | +DmpServerConfig::DmpServerConfig() | ||
| 15 | +{ | ||
| 16 | + std::string iniFile = DmpApplication::Instance()->libexecPath() + iniFileName_; | ||
| 17 | + if (boost::filesystem::exists(iniFile)) | ||
| 18 | + { | ||
| 19 | + boost::property_tree::ini_parser::read_ini(iniFile, ptIni_); | ||
| 20 | + } | ||
| 21 | + else | ||
| 22 | + { | ||
| 23 | + ptIni_.put<std::string>("MetaData.pgsqlConnect", "\"hostaddr=localhost port=5432 dbname='dmap_dms' user='postgres' password='chinadci'\""); | ||
| 24 | + ptIni_.put<std::string>("MetaData.metaUrl", "http://ip:port"); | ||
| 25 | + boost::property_tree::ini_parser::write_ini(iniFile, ptIni_); | ||
| 26 | + } | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +DmpServerConfig::~DmpServerConfig() | ||
| 30 | +{ | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +DmpServerConfig *DmpServerConfig::Instance() | ||
| 34 | +{ | ||
| 35 | + static DmpServerConfig instance; | ||
| 36 | + return &instance; | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +std::string DmpServerConfig::getPqsqlConnect() | ||
| 40 | +{ | ||
| 41 | + std::string conn = ""; | ||
| 42 | + try | ||
| 43 | + { | ||
| 44 | + boost::property_tree::ptree ptMeta; | ||
| 45 | + ptMeta = ptIni_.get_child("MetaData"); | ||
| 46 | + conn = ptMeta.get<std::string>("pgsqlConnect"); | ||
| 47 | + } | ||
| 48 | + catch (std::exception &e) | ||
| 49 | + { | ||
| 50 | + std::cerr << "Exception: " << e.what() << "\n"; | ||
| 51 | + } | ||
| 52 | + return conn; | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +std::string DmpServerConfig::getMetaUrl() | ||
| 56 | +{ | ||
| 57 | + std::string url = ""; | ||
| 58 | + try | ||
| 59 | + { | ||
| 60 | + boost::property_tree::ptree ptMeta; | ||
| 61 | + ptMeta = ptIni_.get_child("MetaData"); | ||
| 62 | + url = ptMeta.get<std::string>("metaUrl"); | ||
| 63 | + } | ||
| 64 | + catch (std::exception &e) | ||
| 65 | + { | ||
| 66 | + std::cerr << "Exception: " << e.what() << "\n"; | ||
| 67 | + } | ||
| 68 | + return url; | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +std::string DmpServerConfig::getValue(const std::string §ion,const std::string &key) | ||
| 72 | +{ | ||
| 73 | + std::string value = ""; | ||
| 74 | + try | ||
| 75 | + { | ||
| 76 | + boost::property_tree::ptree ptMeta; | ||
| 77 | + ptMeta = ptIni_.get_child(section); | ||
| 78 | + value = ptMeta.get<std::string>(key); | ||
| 79 | + } | ||
| 80 | + catch (std::exception &e) | ||
| 81 | + { | ||
| 82 | + std::cerr << "Exception: " << e.what() << "\n"; | ||
| 83 | + } | ||
| 84 | + return value; | ||
| 85 | +} |
src/server/dmpserverconfig.h
0 → 100644
| 1 | +/************************************************************************** | ||
| 2 | +* file: dmpserverconfig.h | ||
| 3 | + | ||
| 4 | +* Author: wanzhongping | ||
| 5 | +* Date: 2021-12-16 16:55:44 | ||
| 6 | +* Email: zhongpingw@chinadci.com | ||
| 7 | +* copyright: 广州城市信息研究所有限公司 | ||
| 8 | +***************************************************************************/ | ||
| 9 | + | ||
| 10 | +#ifndef __dmpserverconfig_h__ | ||
| 11 | +#define __dmpserverconfig_h__ | ||
| 12 | + | ||
| 13 | +#include "dmap_server.h" | ||
| 14 | +#include <boost/property_tree/ini_parser.hpp> | ||
| 15 | +#include <boost/property_tree/ptree.hpp> | ||
| 16 | + | ||
| 17 | +class SERVER_EXPORT DmpServerConfig | ||
| 18 | +{ | ||
| 19 | +public: | ||
| 20 | + ~DmpServerConfig(); | ||
| 21 | + static DmpServerConfig* Instance(); | ||
| 22 | + std::string getPqsqlConnect(); | ||
| 23 | + std::string getMetaUrl(); | ||
| 24 | + std::string getValue(const std::string §ion,const std::string &key); | ||
| 25 | +private: | ||
| 26 | + DmpServerConfig(); | ||
| 27 | + boost::property_tree::ptree ptIni_; | ||
| 28 | + const std::string iniFileName_ = "dmpserver.ini"; | ||
| 29 | +}; | ||
| 30 | + | ||
| 31 | +#endif // __dmpserverconfig_h__ |
请
注册
或
登录
后发表评论