[Webserver]httplib webserver+client(MacOS,Linux)

๐Ÿ‘‰๐Ÿป ์›น๊ฐœ๋ฐœ์ด ์•„๋‹Œ ์ผ๋ฐ˜ c++ ์•ฑ ๊ฐœ๋ฐœ์‹œ ์›น์„œ๋ฒ„์™€ ํ†ต์‹ ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค.
This explains how to communicate with a web server when developing general C++ apps, not web development.

๐Ÿ‘‰๐Ÿป http,https,json ํŒŒ์‹ฑ์— ๋Œ€ํ•œ ๋ถ€๋ถ„์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค.
This covers http, https, and JSON parsing.

๐Ÿ‘‰๐Ÿป ๋ฐฉํ™”๋ฒฝ ์„ค์ •์ด๋‚˜ https์„ค์ •๋ถ€๋ถ„์€ ์ด์ „ ๊ฒŒ์‹œ๋ฌผ์„ ์ฐธ๊ณ  ํ•˜์„ธ์š”.
Please refer to the previous post for firewall and HTTPS settings.

๐Ÿ‘‰๐ŸปHttp ์„œ๋ฒ„(MacOS์—์„œ ํ…Œ์ŠคํŠธ)
HTTP server (tested on MacOS)

โœ”๏ธ ์ฝ”๋“œ / code

#include "httplib.h"
#include <iostream>

int main() {
    httplib::Server svr;

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

    std::cout << "Listening on http://0.0.0.0:5080\n";
    svr.listen("0.0.0.0", 5080);
}

โœ”๏ธ ์ปดํŒŒ์ผ / Compiling

g++ -o http_server http_server.cpp -std=c++11 -pthread

โœ”๏ธ ์‹คํ–‰ / run

./http_server
http server

๐Ÿ‘‰๐ŸปHttp ํด๋ผ์ด์–ธํŠธ / Http client

โœ”๏ธ ์ฝ”๋“œ / code

#include "httplib.h"
#include <iostream>

int main() {
    httplib::Client cli("http://127.0.0.1:5080");

    if (auto res = cli.Get("/")) {
        std::cout << "status: " << res->status << "\n";
        std::cout << "body: " << res->body << "\n";
    } else {
        std::cerr << "request failed\n";
    }
}

โœ”๏ธ ์ปดํŒŒ์ผ / Compiling

g++ -o http_client http_client.cpp -std=c++11 -pthread

โœ”๏ธ ์‹คํ–‰ / run

./http_client
http client

๐Ÿ‘‰๐Ÿป Https ์„œ๋ฒ„(Linux์—์„œ ํ…Œ์ŠคํŠธ)
HTTPS Server (Tested on Linux)

โญ๏ธ libssl-dev ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
libssl-dev must be installed.

sudo apt install libssl-dev

โœ”๏ธ ์ฝ”๋“œ / Code

#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "../httplib.h"
#include <iostream>

int main() {
    httplib::SSLServer svr("./fullchain.pem", "./privkey.pem");

    if (!svr.is_valid()) {
        std::cerr << "SSL server setup failed\n";
        return 1;
    }

    svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
        res.set_content("<h1>Hello HTTPS</h1>", "text/html");
    });

    std::cout << "Listening on https://0.0.0.0:5080\n";
    svr.listen("0.0.0.0", 5080);
}

โœ”๏ธ ์ปดํŒŒ์ผ / Compiling

g++ https_server.cpp -o https_server -std=c++11 -pthread -lssl -lcrypto

โœ”๏ธ ์‹คํ–‰ / Run

./https_server
https server

๐Ÿ‘‰๐ŸปHttps ํด๋ผ์ด์–ธํŠธ / Https Client

โœ”๏ธ ์ฝ”๋“œ / Code

#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "../httplib.h"
#include <iostream>

int main() {
    httplib::Client cli("https://yourhost.domain.org:5080");

    if (auto res = cli.Get("/")) {
        std::cout << res->status << "\n";
        std::cout << res->body << "\n";
    } else {
        std::cerr << "request failed\n";
    }
}

โœ”๏ธ ์ปดํŒŒ์ผ / Compiling

g++ https_client.cpp -o https_client -std=c++11 -pthread -lssl -lcrypto

โœ”๏ธ ์‹คํ–‰ / Run

./https_client
https client

๐Ÿ‘‰๐Ÿป Https Json(Linux์—์„œ ํ…Œ์ŠคํŠธ)
HTTPS JSON (Tested on Linux)

โœ”๏ธ Linux

sudo apt update
sudo apt install nlohmann-json3-dev

โœ”๏ธ ์„ค์น˜ ๊ฒฝ๋กœ / Installation path

/usr/include/nlohmann/json.hpp

โœ”๏ธ MacOS

brew install nlohmann-json

โœ”๏ธ ์„ค์น˜๊ฒฝ๋กœ / Installation path

/opt/homebrew/include/nlohmann/json.hpp

โœ”๏ธ ์ƒ์„ฑ๋œ json.httpํŒŒ์ผ์„ ์ž‘์—…์ค‘์ธ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.
Copy the generated json.http file to the working directory.

sudo cp /usr/include/nlohmann/json.hpp ./

โœ”๏ธ ์„œ๋ฒ„์ฝ”๋“œ / Server Code

— server.cpp

#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "../httplib.h"
#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

int main() {
    httplib::SSLServer svr("./fullchain.pem", "./privkey.pem");

    if (!svr.is_valid()) {
        std::cerr << "SSL server setup failed\n";
        return 1;
    }

    svr.Get("/api", [](const httplib::Request&, httplib::Response& res) {
        json j = {
            {"ok", true},
            {"msg", "hello"},
            {"value", 123}
        };
        res.set_content(j.dump(), "application/json");
    });

    std::cout << "Listening on https://0.0.0.0:5080\n";
    svr.listen("0.0.0.0", 5080);
}

โœ”๏ธ ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ / Client Code

— client.cpp

#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "../httplib.h"
#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

int main() {
    httplib::Client cli("https://yourhost.domain.org:5080");

    if (auto res = cli.Get("/api")) {
        json j = json::parse(res->body);
        std::cout << "ok: " << j["ok"] << "\n";
        std::cout << "msg: " << j["msg"] << "\n";
        std::cout << "value: " << j["value"] << "\n";
    } else {
        std::cerr << "request failed\n";
    }
}

โœ”๏ธ ์ปดํŒŒ์ผ / Compiling

— Linux

g++ -o server.cpp -std=c++11 -pthread -lssl -lcrypto
g++ -o client.cpp -std=c++11 -pthread -lssl -lcrypto

— MacOS

g++ server.cpp -o server -std=c++11 -pthread \
  -I/opt/homebrew/opt/openssl@3/include \
  -L/opt/homebrew/opt/openssl@3/lib \
  -lssl -lcrypto -framework Security -framework CoreFoundation

g++ client.cpp -o client -std=c++11 -pthread \
  -I/opt/homebrew/opt/openssl@3/include \
  -L/opt/homebrew/opt/openssl@3/lib \
  -lssl -lcrypto -framework Security -framework CoreFoundation

โœ”๏ธ ์‹คํ–‰ / Run

— ์„œ๋ฒ„ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์„œ๋กœ ๋‹ค๋ฅธ ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
Run the server and client in different terminals.

./server # server
./client # client
https json server
https json client

Leave a Reply