cpp-httplib 测试https功能

cpp-httplib 下载

地址

编译 example

g++ redirect.cc -o redirect -I.../ -lpthread -DCPPHTTPLIB_OPENSSL_SUPPORT -lssl -lcrypto

编译如果报错,现在oepnssl版本为1.1,不想升级openssl。进行下面修改:

bash 复制代码
diff --git a/httplib.h b/httplib.h
index 5e2bbd0..efea3c8 100644
--- a/httplib.h
+++ b/httplib.h
@@ -327,15 +327,6 @@ using socket_t = int;
 #include <iostream>
 #include <sstream>
 
-#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
-#if OPENSSL_VERSION_NUMBER < 0x1010107f
-#error Please use OpenSSL or a current version of BoringSSL
-#endif
-#define SSL_get1_peer_certificate SSL_get_peer_certificate
-#elif OPENSSL_VERSION_NUMBER < 0x30000000L
-#error Sorry, OpenSSL versions prior to 3.0.0 are not supported
-#endif
-
 #endif // CPPHTTPLIB_OPENSSL_SUPPORT
 
 #ifdef CPPHTTPLIB_ZLIB_SUPPORT
@@ -10274,7 +10265,7 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) {
               return false;
             }
 
-            auto server_cert = SSL_get1_peer_certificate(ssl2);
+            auto server_cert = SSL_get_peer_certificate(ssl2);
             auto se = detail::scope_exit([&] { X509_free(server_cert); });
 
             if (server_cert == nullptr) {
bash 复制代码
--- a/example/redirect.cc
+++ b/example/redirect.cc
@@ -9,6 +9,7 @@
 
 #define SERVER_CERT_FILE "./cert.pem"
 #define SERVER_PRIVATE_KEY_FILE "./key.pem"
+#define CPPHTTPLIB_OPENSSL_SUPPORT 1
 
 using namespace httplib;
 
@@ -16,6 +17,7 @@ int main(void) {
   // HTTP server
   Server http;
 #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
+       std::cout<<"ssl" <<std::endl;
   SSLServer https(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
 #endif
 
@@ -47,7 +49,7 @@ int main(void) {
   auto httpThread = std::thread([&]() { http.listen("localhost", 8080); });
 
 #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
-  auto httpsThread = std::thread([&]() { https.listen("localhost", 8081); });
+  auto httpsThread = std::thread([&]() { https.listen("0.0.0.0", 8081); });
 #endif
 
   httpThread.join();

编译通过

制作证书

证书文件格式有PEM 和 P12(PKCS#12),两种核心功能基本一致,PEM 是文本 ,P12是二进制格式。

生成私钥 (需要输入密码)

openssl genpkey -algorithm RSA -out key.pem -aes256

删除私钥密码

openssl rsa -in key.pem -out key_no_pass.pem

生成证书请求文件

openssl req -new -key key.pem -out server.csr

生成自签名证书 (cert.pem)

openssl x509 -req -days 365 -in server.csr -signkey key.pem -out cert.pem

执行

sudo ./redirect

网页输入ip

相关推荐
房开民9 小时前
http服务端 mongoose常用api
网络协议·http·xcode
李少兄9 小时前
Git远程连接指南:SSH与HTTPS协议详解
git·https·ssh
好好学习,天天向上~9 小时前
一套从 HTTP 抓取到动态页面爬取的 Python 全栈爬虫框架(附安装与实战)
爬虫·python·http
tang&9 小时前
网络协议底层拆解:IP、TCP/UDP 与 HTTP 作用及结构全解析
网络协议·tcp/ip·udp
游戏开发爱好者89 小时前
iOS 开发进阶,用 SniffMaster 实现 iPhone 抓包深度分析
android·ios·小程序·https·uni-app·iphone·webview
F1FJJ1 天前
Shield CLI Postgres v0.3.10:当 142 张表挤在一张 ER 图里,我们做了什么
网络·vscode·网络协议·postgresql·开源软件
TechWayfarer1 天前
高并发场景下的IP归属地查询架构:从20ms到0.5ms的优化实践
网络协议·tcp/ip·架构
Hello_Embed1 天前
嵌入式上位机开发入门(四):TCP 编程 —— Client 端实现
网络·笔记·网络协议·tcp/ip·嵌入式
A.A呐1 天前
【Linux第二十二章】https
linux·https
大连好光景1 天前
接口测试之Postman+Jmeter
jmeter·http·postman