yhirose/cpp-httplib

Segmentation Failed on client side only?

mcagriaksoy opened this issue · 2 comments

Hello,

I have investigated the previous issues which linked with the segmentation fails.
So I know that this issue can be caused by certificate location, openssl and so on. But, the program is working when I not reading the body of message.

My server code:

 httplib::SSLServer svr(
            "/home/mcagriaksoy/https_server_client/cert/cert.pem",
            "/home/mcagriaksoy/https_server_client/cert/key.pem");

svr.Get("/", [](const httplib::Request &req, httplib::Response &res)
                        { res.set_content("Alive!", "text/plain"); });

svr.listen("127.0.0.1", 8080);

My Client code:

httplib::Client cli("127.0.0.1", 8080);
cli.set_ca_cert_path(/home/mcagriaksoy/https_server_client/cert/cert.pem);
cli.enable_server_certificate_verification(false);

auto res = cli.Get("/");
std::cout << "" << res->version << std::endl; // IT FAILS
// OR
std::cout << "" << res->body << std::endl; // IT FAILS

My gcc rules:
 -O0 -std=c++11 -Wall -lssl -lcrypto

EDIT:
I checked the return value of res which returns NULL. But I set it server side w.o any error.
I implemented this:

auto res = cli.Get("/");
 if (res)
 {
     std::cout << res->status << std::endl;
     std::cout << res->get_header_value("Content-Type") << std::endl;
     std::cout << res->body << std::endl;
 }
 else
 {
     auto result = cli.get_openssl_verify_result();
     if (result)
     {
         std::cout << "verify error: " << X509_verify_cert_error_string(result) << std::endl;
     }
 }

The stdout will be:

verify error: unknown certificate verification error

But as I mentioned earlier I can display the message on browser like:
image

To run the client and server same time I used std:thread.
std::thread serverThread(server_thread);
std::thread clientThread(client_thread);
serverThread.join();
clientThread.join();

Thank you for your time :)

@mcagriaksoy thanks for the feedback. I still don't understand what you are asking though, I found at least one problem in your code.

httplib::Client cli("127.0.0.1", 8080);

If you want to issue HTTPS requests, the code should be

httplib::SSLClient cli("127.0.0.1", 8080);

or

httplib::Client cli("https://127.0.0.1:8080");

Hope it helps!

What a silly mistake I made, thanks :)