(original) Linux under MICROSOFT/CPPRESTSDK support HTTPS (server)

Source: Internet
Author: User

Original, reproduced please indicate the source address

Before looking at a bunch of information on the internet said MICROSOFT/CPPRESTSDK does not support HTTPS or only support window under the HTTPS, almost misled, no way, had to go through the source code

Let's start with the Linux version, Centos7.

Create an SSL certificate what, a bunch of information on the Internet, this way no longer say, directly paste the source code

#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;


Classcommandhandler{ Public: CommandHandler () {} commandhandler (utility::string_t url,http_listener_config server_config); Pplx::task<void> Open () {returnM_listener.open ();} Pplx::task<void> Close () {returnm_listener.close ();}Private: voidhandle_get (http_request message); voidhandle_post (http_request message); voidhandle_put (http_request message); voidHandle_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::p laceholders::_1)); M_listener.support (methods::P OST, Std::bind (&commandhandler::handle_post, This, std::p laceholders::_1)); M_listener.support (methods::P ut, Std::bind (&commandhandler::handle_put, This, std::p laceholders::_1)); M_listener.support (Methods::D el, Std::bind (&commandhandler::handle_del, This, std::p laceholders::_1));} voidcommandhandler::handle_get (Http_request message) {ucout<<"Method:"<< Message.method () <<Std::endl; Ucout<<"URI:"<< Http::uri::d ecode (Message.relative_uri (). Path ()) <<Std::endl; Ucout<<"Query:"<< Http::uri::d ecode (Message.relative_uri (). query ()) << Std::endl <<Std::endl; Message.reply (Status_codes::ok,"ACCEPTED"); }}voidcommandhandler::handle_post (Http_request message) {//std::unordered_map<std::string, std::unordered_map<std::string, std::vector<unsigned char>> > multipart_form_data = Granada::httpex::p arserex::P arsemultipartformdata (message);Ucout <<"Method:"<< Message.method () <<Std::endl; Ucout<<"URI:"<< Http::uri::d ecode (Message.relative_uri (). Path ()) <<Std::endl; Ucout<<"Query:"<< Http::uri::d ecode (Message.relative_uri (). query ()) << Std::endl <<Std::endl; Message.reply (Status_codes::ok,the POST");} voidcommandhandler::handle_put (Http_request message) {ucout<<"Method:"<< Message.method () <<Std::endl; Ucout<<"URI:"<< Http::uri::d ecode (Message.relative_uri (). Path ()) <<Std::endl; Ucout<<"Query:"<< Http::uri::d ecode (Message.relative_uri (). query ()) << Std::endl <<Std::endl; Message.reply (Status_codes::ok,"PUT");}voidCommandhandler::handle_del (Http_request message) {ucout<<"Method:"<< Message.method () <<Std::endl; Ucout<<"URI:"<< Http::uri::d ecode (Message.relative_uri (). Path ()) <<Std::endl; Ucout<<"Query:"<< Http::uri::d ecode (Message.relative_uri (). query ()) << Std::endl <<Std::endl; Message.reply (Status_codes::ok,"DEL");}intMainintargcChar*argv[]) { Try { //The HTTP server is written in the C + + REST SDK and needs to be monitored on all network interfaces .//Windows should use the address "http://*:8080 ",//Linux should use the address "http://0.0.0.0: 8080 ". Const Char* Self_signed_cert = R"(-----BEGIN CERTIFICATE-----Miidlzccan+gawibagijap9zv+1x94ujma0gcsqgsib3dqebbquamgixczajbgnvbaytaknomqswcqydvqqidajtsdelmakga1uebwwcu0gxejaqbgnvbaomcu1jq1jpu09gvderma8ga1u Ecwwisfbdlvbbq0sxejaqbgnvbammcwxvy2fsag9zddaefw0xnta4mtkwota0mjhafw00mzaxmdmwota0mjhamgixczajbgnvbaytaknomqswcqydvqqidajt Sdelmakga1uebwwcu0gxejaqbgnvbaomcu1jq1jpu09gvderma8ga1uecwwisfbdlvbbq0sxejaqbgnvbammcwxvy2fsag9zddccasiwdqyjkozihvcnaqebb Qadggepadccaqocggeballv7aapa+4wypa+3tqc9hihhh8kv/mpv2dm+okg27ihzougmnapqlzmaawczdryw27i+jps3pzaau6rq0v2h6xnrie1yeev27j1wous9ifyvcf6y+ywukxvfln/vm/zfz9z8u3jc7y6uniyous8udx/rritspb2m7suxlmlj+4cqilrhwstnb2nhizn72oc8das5ebqbdt9h6no62rsbtralr7vk9eu/5trykd5+Pocpispvu+7fe24uverggu6yoyd7dmj+3bpbg3g/vkolghgh0dntbku3v/xomnzdzn6dzooogfnpg1neh2xv0vnvezp6wg4h/tfsafbjmonnnmcaweaaanqme4whqydvr0obbyefo1majamlk1j0it93xfczae5mxgzmb8ga1udiwqymbaafo1majamlk1j0it93xfczae5mxgzmawga1u dewqfmambaf8wdqyjkozihvcnaqefbqadggebafb8aacf5o+spe3pz8ipgwzb+bcxdoxc2rngr/GOAYO019TZYNLUHRZW9FTPLZW25IBQ9JNCB+Jmy2ill7zf3tx4ohhewscj1g2lbaqzfqlwsbyjmczvrnszsbf3rignqd5qhdphvvbdvvgtznveatjtofkuyfhcxf4dmb6emfau6il/vjcsmw0j3hynqjpm3v/plxngfd9t4hpcud8mk2xg4rqjazh6x/6v0fc6mrhbs5+QTWYSDGFWITRU1PP2L9QFEGPMANAOM7BDENU8UIVD+vrlng2fkvfsssjvfaxpflkaicftjy9a3/cwnz1acbe5el7aadctopihourlab0=-----END CERTIFICATE----- )"; Const Char* Private_key = R"(-----BEGIN PRIVATE KEY-----Miievwibadanbgkqhkig9w0baqefaascbkkwggslageaaoibaqcy7+WAD2VUMGKWVT7ANPRYB4YFJL/zkvdg5vqchtu4h8zroddqd6i8zaglgsw0csnuypoz0t6cwaluq0nl9h+lza4ntwbbfdu49vjlevyhcr3h+mpssfcl7xztf1tp2rc/WFFN43O2ORPYMQFLPFHV/0use7kw9pu0lmzziyfuhkoi6x8lltqdjrygte9qhpa2kuxgagxu/Yejtutkugu6wjue1zpxlp+ba2jhefj6aqyrkb1puxxtullxqxofomkmnewzi/Twawxt4p1zdproyb9azbwyrt7/1zpp83wz+nc6dqbhtarttxh9l79l57xgt+LHUIF0XUMNWSTDJTZZAGMBAAECGGEAENZD8LSCL1QTWLK6ODAE7SHVX/bklwv5um4kwdslavceqc7p+ymdanatufzg6ig+29FB5KNOLUKJPZMHQZHJPZ4CPZZBG3IXDHV2UQI2L8NZWLDWOIK3Q770A4FYSMD0SHSJQYWXO4CKLJQX8WADJPGTCEHL8G0YHPVSQE0MEKOLDXDQAZNXP Rchscxefwagaysixeo+v+ZOBAPNF4I8PMBKOMNCZWZBLCDKHRL7ALEWNGPQP1XSWYON8FPOONPL2QTSOP3NSC2INPWGCYPAJ3VXDASC3+DZ8JEJI2AMXPVB13BLKD3NDZOWIMZILU9FV+nmj1vb9xdc249zoqo68qkbgqdigkws1w429nqdtetqdr5ebhtdp4gzlnt6vwx5obglcmbazoyubfnccbtcyscpj8hxxnfiparpffkigex9+w0n7blayl6sd2xd4q+yza1/j4loakxc7n9z8cyu+/YHIFVLHZWQGFNKLFFNVQ9NTF8TATHUYCRBCPAWJLZ0WR/CNQKBGQDKPAYNTZQPLOOBAE4DFNJNN2ZCTGU8G5XP0L/ed8o1t9ajjv2xvo8pdpndzaxmzgnibweu9iwrslbr7nloxelkh/Qlitjabsxehsuruq1smdgiauhetdaaj1sqszzwy2bzqnxmdilocgvzgnoyybr2u49zunarhyhmkmzmdiiktwkbgqdezak/heqfvfuunpzpzcbeu+ulgkvpffmsfoyjednab0clvl80z6qbze8xeomvjhkkk9nbjyuyosyehyy2om2s+hfkbsuokct27q+ihryd5bx+/Afvm41rzc8141isalbw1rmamlvsszojsmmuh7pznpxkulinecpuaisqqetwjqkbgqddlavsvdeuowusjeo+ys2velhiav1dunh79u1fmrd2sv085p1wayrqe+y4qmvug/EMJVJMEEBNT+HI7FMDGPOVRYJXT92BDI5W8WVTU2LI1FQEHTPNZ9TE5WBWGFCPF9AXH74VZCCTT74BQ7L1KFDP0IQOKPCPJU8VTETHCG5LTQKBGQC4TX7EL1XB4HSI4DV EH43J3KBB3EVLZ6VAQGZ0BARAHYAZ2UKKOYDSOPS4K6AOXXXJO0BJQQQCX/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::d efault_workarounds); Ctx.use_certificate_chain (CERT); Ctx.use_private_key (Key, Boost::asio::ssl::context::p em); }); intNport =8080; Charpaddr[ +]; 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::stringLine ; 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::stringLine ; Std::getline (std::cin, line); } return 0;}

Here is the access result

(original) Linux under MICROSOFT/CPPRESTSDK support HTTPS (server)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.