Files
cpp-httplib/docs-src/pages/ja/tour/05-tls-setup.md
yhirose 797758a742 Documentation Site on GitHub Pages (#2376)
* Add initial documentations

* Update documentation for Basic Client and add WebSocket section

* feat: add a static site generator with multi-language support

- Introduced a new Rust-based static site generator in the `docs-gen` directory.
- Implemented core functionality for building sites from markdown files, including:
  - Configuration loading from `config.toml`.
  - Markdown rendering with frontmatter support.
  - Navigation generation based on page structure.
  - Static file copying and output directory management.
- Added templates for base layout, pages, and portal.
- Created a CSS file for styling and a JavaScript file for interactive features like language selection and theme toggling.
- Updated documentation source with new configuration and example pages in English and Japanese.
- Added a `justfile` target for building the documentation site.

* Add language/theme toggle functionality

- Created a new Japanese tour index page at docs/ja/tour/index.html
- Implemented navigation links for various sections of the cpp-httplib tutorial
- Added a language selector to switch between English and Japanese
- Introduced theme toggle functionality to switch between light and dark modes
- Added mobile sidebar toggle for better navigation on smaller screens
2026-02-28 14:45:40 -05:00

3.3 KiB
Raw Blame History

title, order
title order
TLS Setup 5

ここまではHTTP平文でやってきましたが、実際のWebではHTTPS暗号化通信が当たり前ですよね。cpp-httplibでHTTPSを使うには、TLSライブラリが必要です。

このTourではOpenSSLを使います。最も広く使われていて、情報も豊富です。

OpenSSLのインストール

お使いのOSに合わせてインストールしましょう。

OS インストール方法
macOS Homebrew (brew install openssl)
Ubuntu / Debian sudo apt install libssl-dev
Windows vcpkg (vcpkg install openssl)

コンパイルオプション

TLS機能を有効にするには、CPPHTTPLIB_OPENSSL_SUPPORT マクロを定義してコンパイルします。前章までのコンパイルコマンドに、いくつかオプションが増えます。

# macOS (Homebrew)
clang++ -std=c++17 -DCPPHTTPLIB_OPENSSL_SUPPORT \
    -I$(brew --prefix openssl)/include \
    -L$(brew --prefix openssl)/lib \
    -lssl -lcrypto \
    -framework CoreFoundation -framework Security \
    -o server server.cpp

# Linux
clang++ -std=c++17 -pthread -DCPPHTTPLIB_OPENSSL_SUPPORT \
    -lssl -lcrypto \
    -o server server.cpp

# Windows (Developer Command Prompt)
cl /EHsc /std:c++17 /DCPPHTTPLIB_OPENSSL_SUPPORT server.cpp libssl.lib libcrypto.lib

それぞれのオプションの役割を見てみましょう。

  • -DCPPHTTPLIB_OPENSSL_SUPPORT — TLS機能を有効にするマクロ定義
  • -lssl -lcrypto — OpenSSLのライブラリをリンク
  • -I / -LmacOSのみ— Homebrew版OpenSSLのパスを指定
  • -framework CoreFoundation -framework SecuritymacOSのみ— Keychainからシステム証明書を自動で読み込むために必要です

動作確認

ちゃんと動くか確認してみましょう。httplib::Client にHTTPSのURLを渡してアクセスするだけのプログラムです。

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

int main() {
    httplib::Client cli("https://www.google.com");

    auto res = cli.Get("/");
    if (res) {
        std::cout << "Status: " << res->status << std::endl;
    } else {
        std::cout << "Error: " << httplib::to_string(res.error()) << std::endl;
    }
}

コンパイルして実行してみてください。Status: 200 と表示されれば、セットアップ完了です。

他のTLSバックエンド

cpp-httplibはOpenSSL以外にも、Mbed TLSとwolfSSLに対応しています。マクロ定義とリンクするライブラリを変えるだけで切り替えられます。

バックエンド マクロ定義 リンクするライブラリ
OpenSSL CPPHTTPLIB_OPENSSL_SUPPORT libssl, libcrypto
Mbed TLS CPPHTTPLIB_MBEDTLS_SUPPORT libmbedtls, libmbedx509, libmbedcrypto
wolfSSL CPPHTTPLIB_WOLFSSL_SUPPORT libwolfssl

このTourではOpenSSLを前提に進めますが、APIはどのバックエンドでも共通です。

次のステップ

TLSの準備ができましたね。次は、HTTPSサイトにリクエストを送ってみましょう。

次: HTTPS Client