๐๐ป http_lib ์๋ฒ์ openssl๋ฅผ ์ฌ์ฉํด์ ํ
์คํธ์ฉ ์ธ์ฆ์๋ฅผ ๋ฐ์์ https๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์
๋๋ค.
This is a method to use HTTPS by obtaining a test certificate using openssl on the http_lib server.
๐๐ป ์๋๋ MacOS์ Linux์์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์
๋๋ค.
Below is how to use it on MacOS and Linux.
๐๐ป Open SSL
โ๏ธ Let’s Encrypt๊ฐ์ ๊ณต์ธ์ธ์ฆ์๊ฐ ์๋ ์ค์ค๋ก ๋ง๋๋ ์ธ์ฆ์์
๋๋ค.
It is a certificate that you create yourself, not an accredited certificate like Let’s Encrypt.
โ๏ธ ๊ณต์ธ์ธ์ฆ์ด ์๋๊ธฐ ๋๋ฌธ์ ๋ธ๋ผ์ฐ์ ์ ์์ https://์ ๋นจ๊ฐ์ค์ด ๋น๋๋ค.
Since it is not an accredited certificate, a red line appears under https:// when accessing via a browser.
โ๏ธ httpsํ
์คํธ ์ฉ์ผ๋ก ์ฌ์ฉ๊ฐ๋ฅํ๋ฉฐ ์ค์ ์๋น์ค์์๋ Let’s Encrypt๊ฐ์ ๊ณต์ธ์ธ์ฆ์๊ฐ ํ์ํฉ๋๋ค.
It can be used for HTTPS testing, but an accredited certificate like Let’s Encrypt is required for actual services.
๐๐ปopenssl ์ค์น / install openssl
โ๏ธMacOS
— Open SSL์ค์น / install openssl
brew install openssl
— ํ
์คํธ์ฉ ์ธ์ฆ์์ ํคํ์ผ ์์ฑ(cert.pem , key.pem)
Generate test certificate and key file (cert.pem, key.pem)
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
โ๏ธ Linux
— Open SSL์ค์น / Install Open SSL
sudo apt update
sudo apt install g++ libssl-dev openssl wget -y
— ํ
์คํธ์ฉ ์ธ์ฆ์์ ํคํ์ผ ์์ฑ(cert.pem , key.pem)
Generate test certificate and key file (cert.pem, key.pem)
โญ๏ธ CN=localhost๋ผ๋ ์ฌ์ฉ๊ฐ๋ฅ ๋๋ฉ์ธ์ด ์๋ค๋ฉด ๋๋ฉ์ธ ์
๋ ฅํ ๊ฒ(CN=host.domain.org)
If you have a usable domain even if CN=localhost, enter the domain (CN=host.domain.org)
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/C=KR/ST=Seoul/L=Seoul/O=MyOrg/OU=IT/CN=localhost"
๐๐ป httplib.h ๋ค์ด๋ก๋ / httplib.h Download
curl -L https://raw.githubusercontent.com/yhirose/cpp-httplib/master/httplib.h -o httplib.h
๐๐ป server.cpp
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "httplib.h"
#include <iostream>
int main( ) {
// ์ธ์ฆ์์ ํค ํ์ผ ๊ฒฝ๋ก๋ฅผ ์ง์ ํฉ๋๋ค.
// Specify the certificate and key file paths.
httplib::SSLServer svr("./cert.pem", "./key.pem" );
if (!svr.is_valid()) {
std::cerr << "SSL ์๋ฒ ์ค์ ์คํจ / SSL server setup failed!" << std::endl;
return 1;
}
svr.Get("/", [](const httplib::Request&, httplib::Response& res ) {
res.set_content("<h1> ๐๐ป HTTPS Server is Running! ๐ฅ </h1>", "text/html; charset=utf-8");
});
std::cout << "Server Listening: https://0.0.0.0:5080" << std::endl;
svr.listen("0.0.0.0", 5080 );
return 0;
}
๐๐ป ์ปดํ์ผ / Compiling
โ๏ธ MacOS
— Apple Silicon(M1,M2,M3)
g++ -o server server.cpp -std=c++11 -pthread \
-I/opt/homebrew/opt/openssl/include \
-L/opt/homebrew/opt/openssl/lib \
-lssl -lcrypto
— Intel
g++ -o server server.cpp -std=c++11 -pthread \
-I/usr/local/opt/openssl/include \
-L/usr/local/opt/openssl/lib \
-lssl -lcrypto
— ๊ฐ๋จํ pkg-config ์ฌ์ฉ๊ฐ๋ฅ / You can simply use pkg-config.
g++ -o server server.cpp -std=c++11 -pthread $(pkg-config --cflags --libs openssl)
โ๏ธ Linux
โญ๏ธ ๋ฆฌ๋
์ค์์ ์ปดํ์ผ์ warning ๋ฌธ๊ตฌ๊ฐ ๋ฐ ์ ์์ต๋๋ค. ์คํํ์ผ์ ์์ฑ๋๋ ๋ฌด์ํ๊ณ ์ฌ์ฉํ์๋ฉด ๋ฉ๋๋ค.
Warning messages may appear when compiling on Linux. An executable file will be generated, so you can ignore them and use the application.
g++ -o server server.cpp -std=c++11 -pthread -lssl -lcrypto
๐๐ป ์๋ฒ ์คํ / Run Server
โ๏ธ macOS & Linux
./server
๐๐ป ๋ธ๋ผ์ฐ์ ์ ์ / Browser access
โ๏ธ macOS

โ๏ธ Linux

โญ๏ธ ํฌ๋กฌ ๋ธ๋ผ์ฐ์ ์ ์์ localhost(์์ ํ์ง ์์)์ผ๋ก ์ด๋] ๋ฒํผ์ด ๋ํ๋์ง ์๋ ๊ฒฝ์ฐ
If the [Go to localhost (unsafe)] button does not appear when accessing via Chrome browser
๋ธ๋ผ์ฐ์ ์ ์ ์ฅ๋ HSTS ์ค์ ์ญ์
Delete HSTS settings saved in the browser
๋ง์ฝ ์ ๋ฐฉ๋ฒ์ด ์๋ํ์ง ์๋๋ค๋ฉด, ๋ธ๋ผ์ฐ์ ๊ฐ ๊ธฐ์ตํ๊ณ ์๋ ํด๋น ๋๋ฉ์ธ์ ๋ณด์ ์ ์ฑ
์ ๊ฐ์ ๋ก ์ง์์ผ ํฉ๋๋ค.ย
If the above method does not work, you must forcibly clear the security policy for the domain that the browser remembers.
1)ํฌ๋กฌ ์ฃผ์์ฐฝ์ย chrome://net-internals/#hsts๋ฅผ ์
๋ ฅํ๊ณ ์ ์ํฉ๋๋ค.
Enter chrome://net-internals/#hsts into the Chrome address bar and access it.
2)๋งจ ์๋ย Delete domain security policiesย ํญ๋ชฉ์ ์ฐพ์ต๋๋ค.
Locate the "Delete domain security policies" item at the very bottom.
3)Domainย ์นธ์ย (ex.) localhost ๋๋ host.domain.org๋ฅผ ์
๋ ฅํ๊ณ ย Deleteย ๋ฒํผ์ ๋๋ฆ
๋๋ค.
Enter (e.g.) localhost or host.domain.org in the Domain field and click the Delete button.