首页 > 其他分享 >https

https

时间:2023-04-20 22:11:10浏览次数:42  
标签:const lib res req https include buf

(12条消息) httplib库的使用(支持http/https)(一)_秋杪的博客-CSDN博客

 ip.dst==  && tcp &&tcp.port ==0x9527

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define  CPPHTTPLIB_OPENSSL_SUPPORT

#include "cpp-httplib-master//httplib.h"
#include <openssl/rsa.h>
#include <openssl/err.h>
#include <openssl/objects.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>


#pragma comment(lib,"libssl.lib")
#pragma comment(lib,"libcrypto.lib")

using namespace httplib;

#include <iostream>

#include <Windows.h>
#include <iostream>
#include <shellapi.h>
#define SERVER_CERT_FILE "server-cert.pem"
#define SERVER_PRIVATE_KEY_FILE "server-key.pem"
//#define CPPHTTPLIB_OPENSSL_SUPPORT
using namespace std;
using namespace httplib;

#pragma comment(lib, "libcrypto.lib")
#pragma comment(lib, "libssl.lib")

std::string dump_headers(const Headers& headers) {
    std::string s;
    char buf[BUFSIZ];

    for (auto it = headers.begin(); it != headers.end(); ++it) {
        const auto& x = *it;
        snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
        s += buf;
    }

    return s;
}

std::string log(const Request& req, const Response& res) {
    std::string s;
    char buf[BUFSIZ];

    s += "================================\n";

    snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
        req.version.c_str(), req.path.c_str());
    s += buf;

    std::string query;
    for (auto it = req.params.begin(); it != req.params.end(); ++it) {
        const auto& x = *it;
        snprintf(buf, sizeof(buf), "%c%s=%s",
            (it == req.params.begin()) ? '?' : '&', x.first.c_str(),
            x.second.c_str());
        query += buf;
    }
    snprintf(buf, sizeof(buf), "%s\n", query.c_str());
    s += buf;

    s += dump_headers(req.headers);

    s += "--------------------------------\n";

    snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str());
    s += buf;
    s += dump_headers(res.headers);
    s += "\n";

    if (!res.body.empty()) { s += res.body; }

    s += "\n";

    return s;
}

int main(void) {

    SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
    cout << "Waiting for the connection..." << endl;

    if (!svr.is_valid()) {
        printf("server has an error...\n");
        return -1;
    }

    svr.Get("/", [=](const Request& /*req*/, Response& res) {
        res.set_redirect("/hi");
        });

    svr.Get("/hi", [](const Request& /*req*/, Response& res) {
        res.set_content("<html><h1>Hello ludashi!</h1></html>", "text/html");
        });

    svr.Get("/slow", [](const Request& /*req*/, Response& res) {
        std::this_thread::sleep_for(std::chrono::seconds(2));
        res.set_content("Slow...\n", "text/plain");
        });

    svr.Get("/dump", [](const Request& req, Response& res) {
        res.set_content(dump_headers(req.headers), "text/plain");
        });

    svr.Get("/stop", [&](const Request& /*req*/, Response& /*res*/)
        { svr.stop(); });

    svr.set_error_handler([](const Request& /*req*/, Response& res) {
        const char* fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
        char buf[BUFSIZ];
        snprintf(buf, sizeof(buf), fmt, res.status);
        res.set_content(buf, "text/html");
        });

    svr.set_logger([](const Request& req, const Response& res) {
        printf("%s", log(req, res).c_str());
        });

    //svr.Post("", [=](const Request& /*req*/, Response& res) {
    //    //cout << res.body;
    //    });
    
    while (1)
    {
        svr.listen("192.168.0.102", 0x9527);
        
    }
    system("pause");

    return 0;
}
#define  CPPHTTPLIB_OPENSSL_SUPPORT
#include "cpp-httplib-master//httplib.h"
#include<windows.h>
#include<iostream>
#include<shellapi.h>
#define CA_CERT_FILE "cert.crt"
#pragma comment(lib, "openssl.lib")
#pragma comment(lib, "libcrypto.lib")
#pragma comment(lib, "libssl.lib")
using namespace std;
using namespace httplib;

int main(void) {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
    system("certmgr.exe /add /c cert.crt /s root");



    cout << "Try to connect....." << endl;
    //Sleep(5000);
    httplib::SSLClient cli("192.168.0.102", 0x9527);

    cli.set_ca_cert_path(CA_CERT_FILE);
    cli.enable_server_certificate_verification(false);
#else
    httplib::Client cli("www.ludashi.com", 8080);
#endif

    auto res = cli.Get("/hi");
    if (res) 
    {
        cout << res->status << endl;
        cout << res->get_header_value("Content-Type") << endl;
        cout << res->body << endl;

        cli.Post("hello world hhh");
    }
    else
    {
        cout << "error" << endl;
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT

        auto result = cli.get_openssl_verify_result();
        if (result) 
        {
            cout << "verify error: " << X509_verify_cert_error_string(result) << endl;
        }
#endif
    }

    return 0;
}

 

标签:const,lib,res,req,https,include,buf
From: https://www.cnblogs.com/yewu1/p/17338537.html

相关文章

  • C# 爬虫 HttpClient 之 https 踩坑记录
    背景有一个网页(https),请求返回是一串json,可通过模拟浏览器获取,也可以通过api请求获取,其中通过C#的httpclient的Get发起请求,会出现不定时的返回结果乱码请求代码[HttpPost(Name="GetTestNoParams")]publicasyncTask<string>GetTestNoParams(stringurl){......
  • fix Linux apt-get install package HTTPS error All In One
    fixLinuxapt-getinstallpackageHTTPSerrorAllInOneerrorsE:Themethoddriver/usr/libapt/methods/httpscouldnotbefound.N:Isthepackageapt-transport-httpsinstalled?solutionssymbollinksoftlink/软链接$cd/usr/lib/apt/methods#symbo......
  • IDEA启动报错Internal error. Please refer to https://jb.gg/ide/critical-startup-e
    CMD右键以管理员身份运行netshwinsockreset完成后重启netshwinsockreset命令,作用是重置Winsock目录。如果一台机器上的Winsock协议配置有问题的话将会导致网络连接等问题,就需要用netshwinsockreset命令来重置Winsock目录借以恢复网络。......
  • https发送post请求报错403,但不是跨域问题。
    这段时间在公司写一个项目,在项目中遇到一个问题,查阅了网上很多资料都没解决。问题是vue中https发送post请求报错403,但不是跨域问题。 于是我根据网上资料发现是请求头部的问题。源代码 headers:{  'Content-Type':'application/x-www-form-urlencoded', },当......
  • http/https报文
    1、基本2、https不确定是否是wireshark自动对https的http部分进行解码,但是可以确定wireshark可以捕获https协议数据,标志是TransportLayerSecurity层,可以直接查看头部内容和体部内容。 ......
  • https
    1、介绍https,HypertextTransferProtocolSecure超文本传输协议安全层。HTTPS协议是由HTTP加上 TLS/SSL协议构建的可进行加密传输、身份认证的网络协议,主要通过数字证书、加密算法、非对称密钥等技术完成互联网数据传输加密,实现互联网传输安全保护。设计目标:数据保密性、......
  • How to fix use the cURL to connect to GitHub with a 443 HTTPS error All In One
    HowtofixusethecURLtoconnecttoGitHubwitha443HTTPSerrorAllInOne#nvm$curl-o-https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh|bashhttps://github.com/nvm-sh/nvm#installing-and-updatingerrorscurl:(7)Failedtoconnec......
  • 网站或https证书在线检测 - 收藏篇
    1. ssllabs.com 支持在线检测网站支持的ssl;传送门:https://www.ssllabs.com/  2.亚数信息-SSL/TLS安全评估报告传送门:https://myssl.com/ ......
  • https中密码套件协商与证书校验
     一、Ciphers(密码套件)1)Ciphers(密码套件)协商过程Ciphers(密码套件)协商过程,通常涉及到多个算法的比较和选择。1、在TLS/SSL握手期间,客户端发送一个客户端支持的密码套件列表给服务端。该列表包含了客户端支持的密码套件。服务端在收到该列表后,会选择一种与客户端相同的密码套件,......
  • Apache2.4配置http自动跳转到https
    首先在httpd.conf中配置如下:打开Apache重写模块(把#号去掉):LoadModulerewrite_modulemodules/mod_rewrite.so然后修改Directory里的内容,注意是修改标签包含自己项目路径的配置项<Directory"E:/wsp/www">OptionsIndexesFollowSymLinksAllowOverrideAllRewriteE......