(原创)linux下Microsoft/cpprestsdk支持https(server)
2021-06-17 08:05
标签:method comment map https window images tfs self etl 原创,转载请标明源地址 之前看网上一堆的资料说Microsoft/cpprestsdk不支持https或者说只支持window下的https,差点就被误导了,没办法,只好自己去翻了下源代码 先说明下linux版本把,centos7 创建ssl证书啥的,网上一堆的资料,这边就不再说了,直接贴源代码了 #include #pragma comment(lib, "cpprest_2_7.lib") using namespace web; 下面是访问结果 (原创)linux下Microsoft/cpprestsdk支持https(server) 标签:method comment map https window images tfs self etl 原文地址:http://www.cnblogs.com/lydit/p/7266637.html
#include
#include
#include
#include
#include
#pragma comment(lib, "bcrypt.lib")
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "winhttp.lib")
#pragma comment(lib, "httpapi.lib")
using namespace http;
using namespace utility;
using namespace http::experimental::listener;
using namespace web::http;
using namespace web::http::client;
//using namespace concurrency;
class CommandHandler
{
public:
CommandHandler() {}
CommandHandler(utility::string_t url,http_listener_config server_config);
pplx::taskvoid> open() { return m_listener.open(); }
pplx::taskvoid> close() { return m_listener.close(); }
private:
void handle_get(http_request message);
void handle_post(http_request message);
void handle_put(http_request message);
void handle_del(http_request message);
http_listener m_listener;
};
CommandHandler::CommandHandler(utility::string_t url,http_listener_config server_config) : m_listener(url,server_config)
{
m_listener.support(methods::GET, std::bind(&CommandHandler::handle_get, this, std::placeholders::_1));
m_listener.support(methods::POST, std::bind(&CommandHandler::handle_post, this, std::placeholders::_1));
m_listener.support(methods::PUT, std::bind(&CommandHandler::handle_put, this, std::placeholders::_1));
m_listener.support(methods::DEL, std::bind(&CommandHandler::handle_del, this, std::placeholders::_1));
}
void CommandHandler::handle_get(http_request message)
{
ucout "Method: " std::endl;
ucout "URI: " std::endl;
ucout "Query: " std::endl;
message.reply(status_codes::OK, "ACCEPTED");
}
}
void CommandHandler::handle_post(http_request message)
{
//std::unordered_map<:string std::unordered_map std::vector char>>> multipart_form_data = granada::httpex::parserex::ParseMultipartFormData(message);
ucout "Method: " std::endl;
ucout "URI: " std::endl;
ucout "Query: " std::endl;
message.reply(status_codes::OK, "POST");
}
void CommandHandler::handle_put(http_request message)
{
ucout "Method: " std::endl;
ucout "URI: " std::endl;
ucout "Query: " std::endl;
message.reply(status_codes::OK, "PUT");
}
void CommandHandler::handle_del(http_request message)
{
ucout "Method: " std::endl;
ucout "URI: " std::endl;
ucout "Query: " std::endl;
message.reply(status_codes::OK, "DEL");
}
int main(int argc, char *argv[])
{
try
{
//用C++ REST SDK编写HTTP服务端,需要在所有的网络接口上监听时,
//Windows下应使用地址"http://*:8080",
//Linux下应使用地址"http://0.0.0.0:8080"。
const char * self_signed_cert = R"(
-----BEGIN CERTIFICATE-----
MIIDlzCCAn+gAwIBAgIJAP9ZV+1X94UjMA0GCSqGSIb3DQEBBQUAMGIxCzAJBgNV
BAYTAkNOMQswCQYDVQQIDAJTSDELMAkGA1UEBwwCU0gxEjAQBgNVBAoMCU1JQ1JP
U09GVDERMA8GA1UECwwISFBDLVBBQ0sxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0x
NTA4MTkwOTA0MjhaFw00MzAxMDMwOTA0MjhaMGIxCzAJBgNVBAYTAkNOMQswCQYD
VQQIDAJTSDELMAkGA1UEBwwCU0gxEjAQBgNVBAoMCU1JQ1JPU09GVDERMA8GA1UE
CwwISFBDLVBBQ0sxEjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEB
BQADggEPADCCAQoCggEBALLv7AAPa+4wYpa+3tqc9HIHhh8kv/MpV2Dm+oKG27iH
zOugMNAPqLzMAaWCzDRyw27I+jPS3pzAAu6rQ0v2H6XNrie1YEEV27j1WOUS9iFy
vcf6Y+ywUKXvFlN/VM/ZFz9Z8U3jc7Y6unIyoUs8UdX/RRITspb2m7SUxlmLJ+4c
qiLrHwstNB2NHIZN72oc8DaS5eBqBdT9h6NO62RSBTrAlR7Vk9eU/5trYkd5+PoC
pispvU+7Fe24uVerGgU6Yoyd7DMj+3BpbG3g/VkOlGhgH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6aOxxXjO0BjqQqCx/tCPcU5
AvLsTlswO+wDLXM1DoKxzFBZL5o8927niqW+vZpzyGc1uPmC1MG7+MDKdZsR+e+9
XzJTD4slrGSJrcpLt/g/Jqqdjg==
-----END PRIVATE KEY-----
)";
boost::asio::const_buffer cert(self_signed_cert, std::strlen(self_signed_cert));
boost::asio::const_buffer key(private_key, std::strlen(private_key));
http_listener_config server_config;
server_config.set_ssl_context_callback(
[&](boost::asio::ssl::context& ctx)
{
ctx.set_options(boost::asio::ssl::context::default_workarounds);
ctx.use_certificate_chain(cert);
ctx.use_private_key(key, boost::asio::ssl::context::pem);
});
int nPort = 8080;
char pAddr[32];
memset(pAddr,0,sizeof(pAddr));
sprintf(pAddr,"https://0.0.0.0:%d",nPort);
utility::string_t address = pAddr;//U("http://0.0.0.0:8080");
uri_builder uri(address);
auto addr = uri.to_uri().to_string();
CommandHandler handler(addr,server_config);
handler.open().wait();
ucout "Listening for requests at: ")) std::endl;
ucout "Press ENTER key to quit...") std::endl;
std::string line;
std::getline(std::cin, line);
handler.close().wait();
}
catch (std::exception& ex)
{
ucout "Exception: ") std::endl;
ucout "Press ENTER key to quit...") std::endl;
std::string line;
std::getline(std::cin, line);
}
return 0;
}
上一篇:Ajax
下一篇:jQuery对象转化为DOM对象
文章标题:(原创)linux下Microsoft/cpprestsdk支持https(server)
文章链接:http://soscw.com/index.php/essay/94957.html