mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-04-12 11:48:30 +00:00
* 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
194 lines
14 KiB
HTML
194 lines
14 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>TLS Setup - cpp-httplib</title>
|
|
<link rel="stylesheet" href="/css/main.css">
|
|
<script>
|
|
(function() {
|
|
var t = localStorage.getItem('preferred-theme');
|
|
if (!t) t = window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
|
|
if (t === 'light') document.documentElement.setAttribute('data-theme', 'light');
|
|
})();
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<header class="header">
|
|
<div class="header-inner">
|
|
<a href="/en/" class="header-title">cpp-httplib <span style="font-size:0.75em;font-weight:normal;margin-left:4px">v0.35.0</span></a>
|
|
<div class="header-spacer"></div>
|
|
<nav class="header-nav">
|
|
<a href="/en/">Home</a>
|
|
<a href="/en/tour/">Tour</a>
|
|
</nav>
|
|
<div class="header-tools">
|
|
<button class="theme-toggle" aria-label="Toggle theme"></button>
|
|
<div class="lang-selector">
|
|
<button class="lang-btn" aria-label="Language">EN</button>
|
|
<ul class="lang-popup">
|
|
|
|
<li><a href="#" data-lang="en">EN</a></li>
|
|
|
|
<li><a href="#" data-lang="ja">JA</a></li>
|
|
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<button class="sidebar-toggle" aria-label="Menu">☰</button>
|
|
</div>
|
|
</header>
|
|
|
|
|
|
|
|
<div class="layout has-sidebar">
|
|
|
|
<aside class="sidebar">
|
|
<nav class="sidebar-nav">
|
|
|
|
<div class="nav-section">
|
|
<a href="/en/tour/" class="nav-section-title active">A Tour of cpp-httplib</a>
|
|
|
|
<ul class="nav-list">
|
|
|
|
<li><a href="/en/tour/01-getting-started/" class="">Getting Started</a></li>
|
|
|
|
<li><a href="/en/tour/02-basic-client/" class="">Basic Client</a></li>
|
|
|
|
<li><a href="/en/tour/03-basic-server/" class="">Basic Server</a></li>
|
|
|
|
<li><a href="/en/tour/04-static-file-server/" class="">Static File Server</a></li>
|
|
|
|
<li><a href="/en/tour/05-tls-setup/" class="active">TLS Setup</a></li>
|
|
|
|
<li><a href="/en/tour/06-https-client/" class="">HTTPS Client</a></li>
|
|
|
|
<li><a href="/en/tour/07-https-server/" class="">HTTPS Server</a></li>
|
|
|
|
<li><a href="/en/tour/08-websocket/" class="">WebSocket</a></li>
|
|
|
|
<li><a href="/en/tour/09-whats-next/" class="">What's Next</a></li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</nav>
|
|
</aside>
|
|
<main class="content">
|
|
<article>
|
|
<h1>TLS Setup</h1>
|
|
<p>So far we've been using plain HTTP, but in the real world, HTTPS is the norm. To use HTTPS with cpp-httplib, you need a TLS library.</p>
|
|
<p>In this tour, we'll use OpenSSL. It's the most widely used option, and you'll find plenty of resources online.</p>
|
|
<h2>Installing OpenSSL</h2>
|
|
<p>Install it for your OS.</p>
|
|
<table><thead><tr><th>OS</th><th>How to install</th></tr></thead><tbody>
|
|
<tr><td>macOS</td><td><a href="https://brew.sh/">Homebrew</a> (<code>brew install openssl</code>)</td></tr>
|
|
<tr><td>Ubuntu / Debian</td><td><code>sudo apt install libssl-dev</code></td></tr>
|
|
<tr><td>Windows</td><td><a href="https://vcpkg.io/">vcpkg</a> (<code>vcpkg install openssl</code>)</td></tr>
|
|
</tbody></table>
|
|
<h2>Compile Options</h2>
|
|
<p>To enable TLS, define the <code>CPPHTTPLIB_OPENSSL_SUPPORT</code> macro when compiling. You'll need a few extra options compared to the previous chapters.</p>
|
|
<div class="code-dark"><pre style="background-color:#2d2d2d;">
|
|
<span style="color:#747369;"># macOS (Homebrew)
|
|
</span><span style="color:#6699cc;">clang++</span><span style="color:#f2777a;"> -std</span><span style="color:#d3d0c8;">=c++17</span><span style="color:#f2777a;"> -DCPPHTTPLIB_OPENSSL_SUPPORT </span><span style="color:#d3d0c8;">\
|
|
</span><span style="color:#f2777a;"> -I</span><span style="color:#d3d0c8;">$</span><span style="color:#f2777a;">(</span><span style="color:#6699cc;">brew</span><span style="color:#f2777a;"> --prefix openssl)</span><span style="color:#d3d0c8;">/include \
|
|
</span><span style="color:#f2777a;"> -L</span><span style="color:#d3d0c8;">$</span><span style="color:#f2777a;">(</span><span style="color:#6699cc;">brew</span><span style="color:#f2777a;"> --prefix openssl)</span><span style="color:#d3d0c8;">/lib \
|
|
</span><span style="color:#f2777a;"> -lssl -lcrypto </span><span style="color:#d3d0c8;">\
|
|
</span><span style="color:#f2777a;"> -framework</span><span style="color:#d3d0c8;"> CoreFoundation</span><span style="color:#f2777a;"> -framework</span><span style="color:#d3d0c8;"> Security \
|
|
</span><span style="color:#f2777a;"> -o</span><span style="color:#d3d0c8;"> server server.cpp
|
|
</span><span style="color:#d3d0c8;">
|
|
</span><span style="color:#747369;"># Linux
|
|
</span><span style="color:#6699cc;">clang++</span><span style="color:#f2777a;"> -std</span><span style="color:#d3d0c8;">=c++17</span><span style="color:#f2777a;"> -pthread -DCPPHTTPLIB_OPENSSL_SUPPORT </span><span style="color:#d3d0c8;">\
|
|
</span><span style="color:#f2777a;"> -lssl -lcrypto </span><span style="color:#d3d0c8;">\
|
|
</span><span style="color:#f2777a;"> -o</span><span style="color:#d3d0c8;"> server server.cpp
|
|
</span><span style="color:#d3d0c8;">
|
|
</span><span style="color:#747369;"># Windows (Developer Command Prompt)
|
|
</span><span style="color:#6699cc;">cl</span><span style="color:#d3d0c8;"> /EHsc /std:c++17 /DCPPHTTPLIB_OPENSSL_SUPPORT server.cpp libssl.lib libcrypto.lib
|
|
</span></pre>
|
|
</div><div class="code-light"><pre style="background-color:#eff1f5;">
|
|
<span style="color:#a7adba;"># macOS (Homebrew)
|
|
</span><span style="color:#8fa1b3;">clang++</span><span style="color:#bf616a;"> -std</span><span style="color:#4f5b66;">=c++17</span><span style="color:#bf616a;"> -DCPPHTTPLIB_OPENSSL_SUPPORT </span><span style="color:#4f5b66;">\
|
|
</span><span style="color:#bf616a;"> -I</span><span style="color:#4f5b66;">$</span><span style="color:#bf616a;">(</span><span style="color:#8fa1b3;">brew</span><span style="color:#bf616a;"> --prefix openssl)</span><span style="color:#4f5b66;">/include \
|
|
</span><span style="color:#bf616a;"> -L</span><span style="color:#4f5b66;">$</span><span style="color:#bf616a;">(</span><span style="color:#8fa1b3;">brew</span><span style="color:#bf616a;"> --prefix openssl)</span><span style="color:#4f5b66;">/lib \
|
|
</span><span style="color:#bf616a;"> -lssl -lcrypto </span><span style="color:#4f5b66;">\
|
|
</span><span style="color:#bf616a;"> -framework</span><span style="color:#4f5b66;"> CoreFoundation</span><span style="color:#bf616a;"> -framework</span><span style="color:#4f5b66;"> Security \
|
|
</span><span style="color:#bf616a;"> -o</span><span style="color:#4f5b66;"> server server.cpp
|
|
</span><span style="color:#4f5b66;">
|
|
</span><span style="color:#a7adba;"># Linux
|
|
</span><span style="color:#8fa1b3;">clang++</span><span style="color:#bf616a;"> -std</span><span style="color:#4f5b66;">=c++17</span><span style="color:#bf616a;"> -pthread -DCPPHTTPLIB_OPENSSL_SUPPORT </span><span style="color:#4f5b66;">\
|
|
</span><span style="color:#bf616a;"> -lssl -lcrypto </span><span style="color:#4f5b66;">\
|
|
</span><span style="color:#bf616a;"> -o</span><span style="color:#4f5b66;"> server server.cpp
|
|
</span><span style="color:#4f5b66;">
|
|
</span><span style="color:#a7adba;"># Windows (Developer Command Prompt)
|
|
</span><span style="color:#8fa1b3;">cl</span><span style="color:#4f5b66;"> /EHsc /std:c++17 /DCPPHTTPLIB_OPENSSL_SUPPORT server.cpp libssl.lib libcrypto.lib
|
|
</span></pre>
|
|
</div>
|
|
<p>Let's look at what each option does.</p>
|
|
<ul>
|
|
<li><strong><code>-DCPPHTTPLIB_OPENSSL_SUPPORT</code></strong> — Defines the macro that enables TLS support</li>
|
|
<li><strong><code>-lssl -lcrypto</code></strong> — Links the OpenSSL libraries</li>
|
|
<li><strong><code>-I</code> / <code>-L</code></strong> (macOS only) — Points to the Homebrew OpenSSL paths</li>
|
|
<li><strong><code>-framework CoreFoundation -framework Security</code></strong> (macOS only) — Needed to automatically load system certificates from the Keychain</li>
|
|
</ul>
|
|
<h2>Verifying the Setup</h2>
|
|
<p>Let's make sure everything works. Here's a simple program that passes an HTTPS URL to <code>httplib::Client</code>.</p>
|
|
<div class="code-dark"><pre style="background-color:#2d2d2d;">
|
|
<span style="color:#cc99cc;">#define </span><span style="color:#d3d0c8;">CPPHTTPLIB_OPENSSL_SUPPORT
|
|
</span><span style="color:#cc99cc;">#include </span><span style="color:#d3d0c8;">"</span><span style="color:#99cc99;">httplib.h</span><span style="color:#d3d0c8;">"
|
|
</span><span style="color:#cc99cc;">#include </span><span style="color:#d3d0c8;"><</span><span style="color:#99cc99;">iostream</span><span style="color:#d3d0c8;">>
|
|
</span><span style="color:#d3d0c8;">
|
|
</span><span style="color:#cc99cc;">int </span><span style="color:#6699cc;">main</span><span style="color:#d3d0c8;">() {
|
|
</span><span style="color:#d3d0c8;"> httplib::Client </span><span style="color:#6699cc;">cli</span><span style="color:#d3d0c8;">("</span><span style="color:#99cc99;">https://www.google.com</span><span style="color:#d3d0c8;">");
|
|
</span><span style="color:#d3d0c8;">
|
|
</span><span style="color:#d3d0c8;"> </span><span style="color:#cc99cc;">auto</span><span style="color:#d3d0c8;"> res = cli.</span><span style="color:#6699cc;">Get</span><span style="color:#d3d0c8;">("</span><span style="color:#99cc99;">/</span><span style="color:#d3d0c8;">");
|
|
</span><span style="color:#d3d0c8;"> </span><span style="color:#cc99cc;">if </span><span style="color:#d3d0c8;">(res) {
|
|
</span><span style="color:#d3d0c8;"> std::cout << "</span><span style="color:#99cc99;">Status: </span><span style="color:#d3d0c8;">" << res-></span><span style="color:#f2777a;">status </span><span style="color:#d3d0c8;"><< std::endl;
|
|
</span><span style="color:#d3d0c8;"> } </span><span style="color:#cc99cc;">else </span><span style="color:#d3d0c8;">{
|
|
</span><span style="color:#d3d0c8;"> std::cout << "</span><span style="color:#99cc99;">Error: </span><span style="color:#d3d0c8;">" << </span><span style="color:#6699cc;">httplib::to_string</span><span style="color:#d3d0c8;">(res.</span><span style="color:#6699cc;">error</span><span style="color:#d3d0c8;">()) << std::endl;
|
|
</span><span style="color:#d3d0c8;"> }
|
|
</span><span style="color:#d3d0c8;">}
|
|
</span></pre>
|
|
</div><div class="code-light"><pre style="background-color:#eff1f5;">
|
|
<span style="color:#b48ead;">#define </span><span style="color:#4f5b66;">CPPHTTPLIB_OPENSSL_SUPPORT
|
|
</span><span style="color:#b48ead;">#include </span><span style="color:#4f5b66;">"</span><span style="color:#a3be8c;">httplib.h</span><span style="color:#4f5b66;">"
|
|
</span><span style="color:#b48ead;">#include </span><span style="color:#4f5b66;"><</span><span style="color:#a3be8c;">iostream</span><span style="color:#4f5b66;">>
|
|
</span><span style="color:#4f5b66;">
|
|
</span><span style="color:#b48ead;">int </span><span style="color:#8fa1b3;">main</span><span style="color:#4f5b66;">() {
|
|
</span><span style="color:#4f5b66;"> httplib::Client </span><span style="color:#8fa1b3;">cli</span><span style="color:#4f5b66;">("</span><span style="color:#a3be8c;">https://www.google.com</span><span style="color:#4f5b66;">");
|
|
</span><span style="color:#4f5b66;">
|
|
</span><span style="color:#4f5b66;"> </span><span style="color:#b48ead;">auto</span><span style="color:#4f5b66;"> res = cli.</span><span style="color:#8fa1b3;">Get</span><span style="color:#4f5b66;">("</span><span style="color:#a3be8c;">/</span><span style="color:#4f5b66;">");
|
|
</span><span style="color:#4f5b66;"> </span><span style="color:#b48ead;">if </span><span style="color:#4f5b66;">(res) {
|
|
</span><span style="color:#4f5b66;"> std::cout << "</span><span style="color:#a3be8c;">Status: </span><span style="color:#4f5b66;">" << res-></span><span style="color:#bf616a;">status </span><span style="color:#4f5b66;"><< std::endl;
|
|
</span><span style="color:#4f5b66;"> } </span><span style="color:#b48ead;">else </span><span style="color:#4f5b66;">{
|
|
</span><span style="color:#4f5b66;"> std::cout << "</span><span style="color:#a3be8c;">Error: </span><span style="color:#4f5b66;">" << </span><span style="color:#8fa1b3;">httplib::to_string</span><span style="color:#4f5b66;">(res.</span><span style="color:#8fa1b3;">error</span><span style="color:#4f5b66;">()) << std::endl;
|
|
</span><span style="color:#4f5b66;"> }
|
|
</span><span style="color:#4f5b66;">}
|
|
</span></pre>
|
|
</div>
|
|
<p>Compile and run it. If you see <code>Status: 200</code>, your setup is complete.</p>
|
|
<h2>Other TLS Backends</h2>
|
|
<p>cpp-httplib also supports Mbed TLS and wolfSSL in addition to OpenSSL. You can switch between them just by changing the macro definition and linked libraries.</p>
|
|
<table><thead><tr><th style="text-align: left">Backend</th><th style="text-align: left">Macro</th><th style="text-align: left">Libraries to link</th></tr></thead><tbody>
|
|
<tr><td style="text-align: left">OpenSSL</td><td style="text-align: left"><code>CPPHTTPLIB_OPENSSL_SUPPORT</code></td><td style="text-align: left"><code>libssl</code>, <code>libcrypto</code></td></tr>
|
|
<tr><td style="text-align: left">Mbed TLS</td><td style="text-align: left"><code>CPPHTTPLIB_MBEDTLS_SUPPORT</code></td><td style="text-align: left"><code>libmbedtls</code>, <code>libmbedx509</code>, <code>libmbedcrypto</code></td></tr>
|
|
<tr><td style="text-align: left">wolfSSL</td><td style="text-align: left"><code>CPPHTTPLIB_WOLFSSL_SUPPORT</code></td><td style="text-align: left"><code>libwolfssl</code></td></tr>
|
|
</tbody></table>
|
|
<p>This tour assumes OpenSSL, but the API is the same regardless of which backend you choose.</p>
|
|
<h2>Next Step</h2>
|
|
<p>You're all set with TLS. Next, let's send a request to an HTTPS site.</p>
|
|
<p><strong>Next:</strong> <a href="../06-https-client">HTTPS Client</a></p>
|
|
|
|
</article>
|
|
</main>
|
|
|
|
</div>
|
|
|
|
<footer class="footer">
|
|
© 2025 yhirose. All rights reserved.
|
|
</footer>
|
|
|
|
<script src="/js/main.js"></script>
|
|
</body>
|
|
</html>
|