(原創)linux下Microsoft/cpprestsdk支援https(server)

來源:互聯網
上載者:User

標籤:method   comment   map   https   window   images   tfs   self   etl   

原創,轉載請標明源地址

之前看網上一堆的資料說Microsoft/cpprestsdk不支援https或者說只支援window下的https,差點就被誤導了,沒辦法,只好自己去翻了下原始碼

先說明下linux版本把,centos7

建立ssl認證啥的,網上一堆的資料,這邊就不再說了,直接貼原始碼了


#include <stdio.h>
#include <cpprest/uri.h>
#include <cpprest/http_listener.h>
#include <cpprest/http_client.h>
#include <cpprest/asyncrt_utils.h>
#include <cpprest/filestream.h>


#pragma comment(lib, "cpprest_2_7.lib")
#pragma comment(lib, "bcrypt.lib")
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "winhttp.lib")
#pragma comment(lib, "httpapi.lib")


using namespace web;
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::task<void> open() { return m_listener.open(); } pplx::task<void> 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: " << message.method() << std::endl; ucout << "URI: " << http::uri::decode(message.relative_uri().path()) << std::endl; ucout << "Query: " << http::uri::decode(message.relative_uri().query()) << std::endl << std::endl; message.reply(status_codes::OK, "ACCEPTED"); }}void CommandHandler::handle_post(http_request message){ //std::unordered_map<std::string, std::unordered_map<std::string, std::vector<unsigned char>>> multipart_form_data = granada::httpex::parserex::ParseMultipartFormData(message); ucout << "Method: " << message.method() << std::endl; ucout << "URI: " << http::uri::decode(message.relative_uri().path()) << std::endl; ucout << "Query: " << http::uri::decode(message.relative_uri().query()) << std::endl << std::endl; message.reply(status_codes::OK, "POST");} void CommandHandler::handle_put(http_request message){ ucout << "Method: " << message.method() << std::endl; ucout << "URI: " << http::uri::decode(message.relative_uri().path()) << std::endl; ucout << "Query: " << http::uri::decode(message.relative_uri().query()) << std::endl << std::endl; message.reply(status_codes::OK, "PUT");}void CommandHandler::handle_del(http_request message){ ucout << "Method: " << message.method() << std::endl; ucout << "URI: " << http::uri::decode(message.relative_uri().path()) << std::endl; ucout << "Query: " << http::uri::decode(message.relative_uri().query()) << std::endl << 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+1X94UjMA0GCSqGSIb3DQEBBQUAMGIxCzAJBgNVBAYTAkNOMQswCQYDVQQIDAJTSDELMAkGA1UEBwwCU0gxEjAQBgNVBAoMCU1JQ1JPU09GVDERMA8GA1UECwwISFBDLVBBQ0sxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0xNTA4MTkwOTA0MjhaFw00MzAxMDMwOTA0MjhaMGIxCzAJBgNVBAYTAkNOMQswCQYDVQQIDAJTSDELMAkGA1UEBwwCU0gxEjAQBgNVBAoMCU1JQ1JPU09GVDERMA8GA1UECwwISFBDLVBBQ0sxEjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALLv7AAPa+4wYpa+3tqc9HIHhh8kv/MpV2Dm+oKG27iHzOugMNAPqLzMAaWCzDRyw27I+jPS3pzAAu6rQ0v2H6XNrie1Y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xt92BDI5w8WVTU2lI1fqEHTpNZ9Te5WbWgfCpf9axH74VzCCtT74Bq7l1kFdp0IqOKpcpJu8VtETHcG5LtQKBgQC4Tx7El1Xb4hsI4dvEh43j3KBb3evlz6vaqgz0BArahYAz2UkkOYDSOPs4K6aOxxXjO0BjqQqCx/tCPcU5AvLsTlswO+wDLXM1DoKxzFBZL5o8927niqW+vZpzyGc1uPmC1MG7+MDKdZsR+e+9XzJTD4slrGSJrcpLt/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 << utility::string_t(U("Listening for requests at: ")) << addr << std::endl; ucout << U("Press ENTER key to quit...") << std::endl; std::string line; std::getline(std::cin, line); handler.close().wait(); } catch (std::exception& ex) { ucout << U("Exception: ") << ex.what() << std::endl; ucout << U("Press ENTER key to quit...") << std::endl; std::string line; std::getline(std::cin, line); } return 0;}

下面是訪問結果

 

(原創)linux下Microsoft/cpprestsdk支援https(server)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.