index.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <!DOCTYPE html>
  2. <html lang="en" data-base-path="&#x2F;cpp-httplib">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>HTTPS Server - cpp-httplib</title>
  7. <link rel="icon" type="image/svg+xml" href="&#x2F;cpp-httplib/favicon.svg">
  8. <link rel="stylesheet" href="&#x2F;cpp-httplib/css/main.css">
  9. <script>
  10. (function() {
  11. var t = localStorage.getItem('preferred-theme');
  12. if (!t) t = window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
  13. if (t === 'light') document.documentElement.setAttribute('data-theme', 'light');
  14. })();
  15. </script>
  16. </head>
  17. <body>
  18. <header class="header">
  19. <div class="header-inner">
  20. <a href="&#x2F;cpp-httplib&#x2F;en/" class="header-title">cpp-httplib <span style="font-size:0.75em;font-weight:normal;margin-left:4px">v0.36.0</span></a>
  21. <div class="header-spacer"></div>
  22. <nav class="header-nav">
  23. <a href="&#x2F;cpp-httplib&#x2F;en/">
  24. <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><polyline points="9 22 9 12 15 12 15 22"/></svg>
  25. Home
  26. </a>
  27. <a href="&#x2F;cpp-httplib&#x2F;en/tour&#x2F;">
  28. <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><polygon points="16.24 7.76 14.12 14.12 7.76 16.24 9.88 9.88 16.24 7.76"/></svg>
  29. Tour
  30. </a>
  31. <a href="https:&#x2F;&#x2F;github.com&#x2F;yhirose&#x2F;cpp-httplib" aria-label="GitHub">
  32. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22"/></svg>
  33. GitHub
  34. </a>
  35. </nav>
  36. <div class="header-tools">
  37. <button class="search-btn" aria-label="Search (⌘K)">
  38. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
  39. </button>
  40. <button class="theme-toggle" aria-label="Toggle theme"></button>
  41. <div class="lang-selector">
  42. <button class="lang-btn" aria-label="Language">
  43. <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/></svg>
  44. EN
  45. </button>
  46. <ul class="lang-popup">
  47. <li><a href="#" data-lang="en">EN</a></li>
  48. <li><a href="#" data-lang="ja">JA</a></li>
  49. </ul>
  50. </div>
  51. </div>
  52. <button class="sidebar-toggle" aria-label="Menu">&#9776;</button>
  53. </div>
  54. </header>
  55. <div class="layout has-sidebar">
  56. <aside class="sidebar">
  57. <nav class="sidebar-nav">
  58. <div class="nav-section">
  59. <a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;" class="nav-section-title active">A Tour of cpp-httplib</a>
  60. <ul class="nav-list">
  61. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;01-getting-started&#x2F;" class="">Getting Started</a></li>
  62. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;02-basic-client&#x2F;" class="">Basic Client</a></li>
  63. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;03-basic-server&#x2F;" class="">Basic Server</a></li>
  64. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;04-static-file-server&#x2F;" class="">Static File Server</a></li>
  65. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;05-tls-setup&#x2F;" class="">TLS Setup</a></li>
  66. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;06-https-client&#x2F;" class="">HTTPS Client</a></li>
  67. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;07-https-server&#x2F;" class="active">HTTPS Server</a></li>
  68. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;08-websocket&#x2F;" class="">WebSocket</a></li>
  69. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;09-whats-next&#x2F;" class="">What&#x27;s Next</a></li>
  70. </ul>
  71. </div>
  72. </nav>
  73. </aside>
  74. <main class="content">
  75. <article>
  76. <h1>HTTPS Server</h1>
  77. <p>In the previous chapter, you used an HTTPS client. Now let's set up your own HTTPS server. Just swap <code>httplib::Server</code> from Chapter 3 with <code>httplib::SSLServer</code>.</p>
  78. <p>A TLS server needs a server certificate and a private key, though. Let's get those ready first.</p>
  79. <h2>Creating a Self-Signed Certificate</h2>
  80. <p>For development and testing, a self-signed certificate works just fine. You can generate one quickly with an OpenSSL command.</p>
  81. <div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
  82. <span style="color:#8fa1b3;">openssl</span><span style="color:#c0c5ce;"> req</span><span style="color:#bf616a;"> -x509 -noenc -keyout</span><span style="color:#c0c5ce;"> key.pem</span><span style="color:#bf616a;"> -out</span><span style="color:#c0c5ce;"> cert.pem</span><span style="color:#bf616a;"> -subj</span><span style="color:#c0c5ce;"> /CN=localhost
  83. </span></pre>
  84. </div><div data-code-theme="light"><pre style="background-color:#ffffff;">
  85. <span style="color:#323232;">openssl req -x509 -noenc -keyout key.pem -out cert.pem -subj /CN=localhost
  86. </span></pre>
  87. </div></div>
  88. <p>This creates two files:</p>
  89. <ul>
  90. <li><strong><code>cert.pem</code></strong> — Server certificate</li>
  91. <li><strong><code>key.pem</code></strong> — Private key</li>
  92. </ul>
  93. <h2>A Minimal HTTPS Server</h2>
  94. <p>Once you have your certificate, let's write the server.</p>
  95. <div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
  96. <span style="color:#b48ead;">#define </span><span style="color:#c0c5ce;">CPPHTTPLIB_OPENSSL_SUPPORT
  97. </span><span style="color:#b48ead;">#include </span><span style="color:#c0c5ce;">&quot;</span><span style="color:#a3be8c;">httplib.h</span><span style="color:#c0c5ce;">&quot;
  98. </span><span style="color:#b48ead;">#include </span><span style="color:#c0c5ce;">&lt;</span><span style="color:#a3be8c;">iostream</span><span style="color:#c0c5ce;">&gt;
  99. </span><span style="color:#c0c5ce;">
  100. </span><span style="color:#b48ead;">int </span><span style="color:#8fa1b3;">main</span><span style="color:#c0c5ce;">() {
  101. </span><span style="color:#c0c5ce;"> httplib::SSLServer </span><span style="color:#8fa1b3;">svr</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">cert.pem</span><span style="color:#c0c5ce;">&quot;, &quot;</span><span style="color:#a3be8c;">key.pem</span><span style="color:#c0c5ce;">&quot;);
  102. </span><span style="color:#c0c5ce;">
  103. </span><span style="color:#c0c5ce;"> svr.</span><span style="color:#8fa1b3;">Get</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">/</span><span style="color:#c0c5ce;">&quot;, [](</span><span style="color:#b48ead;">const auto </span><span style="color:#c0c5ce;">&amp;, </span><span style="color:#b48ead;">auto </span><span style="color:#c0c5ce;">&amp;res) {
  104. </span><span style="color:#c0c5ce;"> res.</span><span style="color:#8fa1b3;">set_content</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">Hello, HTTPS!</span><span style="color:#c0c5ce;">&quot;, &quot;</span><span style="color:#a3be8c;">text/plain</span><span style="color:#c0c5ce;">&quot;);
  105. </span><span style="color:#c0c5ce;"> });
  106. </span><span style="color:#c0c5ce;">
  107. </span><span style="color:#c0c5ce;"> std::cout &lt;&lt; &quot;</span><span style="color:#a3be8c;">Listening on https://localhost:8443</span><span style="color:#c0c5ce;">&quot; &lt;&lt; std::endl;
  108. </span><span style="color:#c0c5ce;"> svr.</span><span style="color:#8fa1b3;">listen</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">0.0.0.0</span><span style="color:#c0c5ce;">&quot;, </span><span style="color:#d08770;">8443</span><span style="color:#c0c5ce;">);
  109. </span><span style="color:#c0c5ce;">}
  110. </span></pre>
  111. </div><div data-code-theme="light"><pre style="background-color:#ffffff;">
  112. <span style="font-weight:bold;color:#a71d5d;">#define </span><span style="color:#323232;">CPPHTTPLIB_OPENSSL_SUPPORT
  113. </span><span style="font-weight:bold;color:#a71d5d;">#include </span><span style="color:#183691;">&quot;httplib.h&quot;
  114. </span><span style="font-weight:bold;color:#a71d5d;">#include </span><span style="color:#183691;">&lt;iostream&gt;
  115. </span><span style="color:#323232;">
  116. </span><span style="font-weight:bold;color:#a71d5d;">int </span><span style="font-weight:bold;color:#795da3;">main</span><span style="color:#323232;">() {
  117. </span><span style="color:#323232;"> httplib::SSLServer svr(</span><span style="color:#183691;">&quot;cert.pem&quot;</span><span style="color:#323232;">, </span><span style="color:#183691;">&quot;key.pem&quot;</span><span style="color:#323232;">);
  118. </span><span style="color:#323232;">
  119. </span><span style="color:#323232;"> svr.Get(</span><span style="color:#183691;">&quot;/&quot;</span><span style="color:#323232;">, [](</span><span style="font-weight:bold;color:#a71d5d;">const auto &amp;</span><span style="color:#323232;">, </span><span style="font-weight:bold;color:#a71d5d;">auto &amp;</span><span style="color:#323232;">res) {
  120. </span><span style="color:#323232;"> res.set_content(</span><span style="color:#183691;">&quot;Hello, HTTPS!&quot;</span><span style="color:#323232;">, </span><span style="color:#183691;">&quot;text/plain&quot;</span><span style="color:#323232;">);
  121. </span><span style="color:#323232;"> });
  122. </span><span style="color:#323232;">
  123. </span><span style="color:#323232;"> std::cout </span><span style="font-weight:bold;color:#a71d5d;">&lt;&lt; </span><span style="color:#183691;">&quot;Listening on https://localhost:8443&quot; </span><span style="font-weight:bold;color:#a71d5d;">&lt;&lt;</span><span style="color:#323232;"> std::endl;
  124. </span><span style="color:#323232;"> svr.listen(</span><span style="color:#183691;">&quot;0.0.0.0&quot;</span><span style="color:#323232;">, </span><span style="color:#0086b3;">8443</span><span style="color:#323232;">);
  125. </span><span style="color:#323232;">}
  126. </span></pre>
  127. </div></div>
  128. <p>Just pass the certificate and private key paths to the <code>httplib::SSLServer</code> constructor. The routing API is exactly the same as <code>httplib::Server</code> from Chapter 3.</p>
  129. <p>Compile and start it up.</p>
  130. <h2>Testing It Out</h2>
  131. <p>With the server running, try accessing it with <code>curl</code>. Since we're using a self-signed certificate, add the <code>-k</code> option to skip certificate verification.</p>
  132. <div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
  133. <span style="color:#8fa1b3;">curl</span><span style="color:#bf616a;"> -k</span><span style="color:#c0c5ce;"> https://localhost:8443/
  134. </span><span style="color:#65737e;"># Hello, HTTPS!
  135. </span></pre>
  136. </div><div data-code-theme="light"><pre style="background-color:#ffffff;">
  137. <span style="color:#323232;">curl -k https://localhost:8443/
  138. </span><span style="font-style:italic;color:#969896;"># Hello, HTTPS!
  139. </span></pre>
  140. </div></div>
  141. <p>If you open <code>https://localhost:8443</code> in a browser, you'll see a "This connection is not secure" warning. That's expected with a self-signed certificate. Just proceed past it.</p>
  142. <h2>Connecting from a Client</h2>
  143. <p>Let's connect using <code>httplib::Client</code> from the previous chapter. There are two ways to connect to a server with a self-signed certificate.</p>
  144. <h3>Option 1: Disable Certificate Verification</h3>
  145. <p>This is the quick and easy approach for development.</p>
  146. <div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
  147. <span style="color:#b48ead;">#define </span><span style="color:#c0c5ce;">CPPHTTPLIB_OPENSSL_SUPPORT
  148. </span><span style="color:#b48ead;">#include </span><span style="color:#c0c5ce;">&quot;</span><span style="color:#a3be8c;">httplib.h</span><span style="color:#c0c5ce;">&quot;
  149. </span><span style="color:#b48ead;">#include </span><span style="color:#c0c5ce;">&lt;</span><span style="color:#a3be8c;">iostream</span><span style="color:#c0c5ce;">&gt;
  150. </span><span style="color:#c0c5ce;">
  151. </span><span style="color:#b48ead;">int </span><span style="color:#8fa1b3;">main</span><span style="color:#c0c5ce;">() {
  152. </span><span style="color:#c0c5ce;"> httplib::Client </span><span style="color:#8fa1b3;">cli</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">https://localhost:8443</span><span style="color:#c0c5ce;">&quot;);
  153. </span><span style="color:#c0c5ce;"> cli.</span><span style="color:#8fa1b3;">enable_server_certificate_verification</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">false</span><span style="color:#c0c5ce;">);
  154. </span><span style="color:#c0c5ce;">
  155. </span><span style="color:#c0c5ce;"> </span><span style="color:#b48ead;">auto</span><span style="color:#c0c5ce;"> res = cli.</span><span style="color:#8fa1b3;">Get</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">/</span><span style="color:#c0c5ce;">&quot;);
  156. </span><span style="color:#c0c5ce;"> </span><span style="color:#b48ead;">if </span><span style="color:#c0c5ce;">(res) {
  157. </span><span style="color:#c0c5ce;"> std::cout &lt;&lt; res-&gt;</span><span style="color:#bf616a;">body </span><span style="color:#c0c5ce;">&lt;&lt; std::endl; </span><span style="color:#65737e;">// Hello, HTTPS!
  158. </span><span style="color:#c0c5ce;"> }
  159. </span><span style="color:#c0c5ce;">}
  160. </span></pre>
  161. </div><div data-code-theme="light"><pre style="background-color:#ffffff;">
  162. <span style="font-weight:bold;color:#a71d5d;">#define </span><span style="color:#323232;">CPPHTTPLIB_OPENSSL_SUPPORT
  163. </span><span style="font-weight:bold;color:#a71d5d;">#include </span><span style="color:#183691;">&quot;httplib.h&quot;
  164. </span><span style="font-weight:bold;color:#a71d5d;">#include </span><span style="color:#183691;">&lt;iostream&gt;
  165. </span><span style="color:#323232;">
  166. </span><span style="font-weight:bold;color:#a71d5d;">int </span><span style="font-weight:bold;color:#795da3;">main</span><span style="color:#323232;">() {
  167. </span><span style="color:#323232;"> httplib::Client cli(</span><span style="color:#183691;">&quot;https://localhost:8443&quot;</span><span style="color:#323232;">);
  168. </span><span style="color:#323232;"> cli.enable_server_certificate_verification(</span><span style="color:#0086b3;">false</span><span style="color:#323232;">);
  169. </span><span style="color:#323232;">
  170. </span><span style="color:#323232;"> </span><span style="font-weight:bold;color:#a71d5d;">auto</span><span style="color:#323232;"> res </span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#323232;"> cli.Get(</span><span style="color:#183691;">&quot;/&quot;</span><span style="color:#323232;">);
  171. </span><span style="color:#323232;"> </span><span style="font-weight:bold;color:#a71d5d;">if </span><span style="color:#323232;">(res) {
  172. </span><span style="color:#323232;"> std::cout </span><span style="font-weight:bold;color:#a71d5d;">&lt;&lt;</span><span style="color:#323232;"> res-&gt;body </span><span style="font-weight:bold;color:#a71d5d;">&lt;&lt;</span><span style="color:#323232;"> std::endl; </span><span style="font-style:italic;color:#969896;">// Hello, HTTPS!
  173. </span><span style="color:#323232;"> }
  174. </span><span style="color:#323232;">}
  175. </span></pre>
  176. </div></div>
  177. <h3>Option 2: Specify the Self-Signed Certificate as a CA Certificate</h3>
  178. <p>This is the safer approach. You tell the client to trust <code>cert.pem</code> as a CA certificate.</p>
  179. <div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
  180. <span style="color:#b48ead;">#define </span><span style="color:#c0c5ce;">CPPHTTPLIB_OPENSSL_SUPPORT
  181. </span><span style="color:#b48ead;">#include </span><span style="color:#c0c5ce;">&quot;</span><span style="color:#a3be8c;">httplib.h</span><span style="color:#c0c5ce;">&quot;
  182. </span><span style="color:#b48ead;">#include </span><span style="color:#c0c5ce;">&lt;</span><span style="color:#a3be8c;">iostream</span><span style="color:#c0c5ce;">&gt;
  183. </span><span style="color:#c0c5ce;">
  184. </span><span style="color:#b48ead;">int </span><span style="color:#8fa1b3;">main</span><span style="color:#c0c5ce;">() {
  185. </span><span style="color:#c0c5ce;"> httplib::Client </span><span style="color:#8fa1b3;">cli</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">https://localhost:8443</span><span style="color:#c0c5ce;">&quot;);
  186. </span><span style="color:#c0c5ce;"> cli.</span><span style="color:#8fa1b3;">set_ca_cert_path</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">cert.pem</span><span style="color:#c0c5ce;">&quot;);
  187. </span><span style="color:#c0c5ce;">
  188. </span><span style="color:#c0c5ce;"> </span><span style="color:#b48ead;">auto</span><span style="color:#c0c5ce;"> res = cli.</span><span style="color:#8fa1b3;">Get</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">/</span><span style="color:#c0c5ce;">&quot;);
  189. </span><span style="color:#c0c5ce;"> </span><span style="color:#b48ead;">if </span><span style="color:#c0c5ce;">(res) {
  190. </span><span style="color:#c0c5ce;"> std::cout &lt;&lt; res-&gt;</span><span style="color:#bf616a;">body </span><span style="color:#c0c5ce;">&lt;&lt; std::endl; </span><span style="color:#65737e;">// Hello, HTTPS!
  191. </span><span style="color:#c0c5ce;"> }
  192. </span><span style="color:#c0c5ce;">}
  193. </span></pre>
  194. </div><div data-code-theme="light"><pre style="background-color:#ffffff;">
  195. <span style="font-weight:bold;color:#a71d5d;">#define </span><span style="color:#323232;">CPPHTTPLIB_OPENSSL_SUPPORT
  196. </span><span style="font-weight:bold;color:#a71d5d;">#include </span><span style="color:#183691;">&quot;httplib.h&quot;
  197. </span><span style="font-weight:bold;color:#a71d5d;">#include </span><span style="color:#183691;">&lt;iostream&gt;
  198. </span><span style="color:#323232;">
  199. </span><span style="font-weight:bold;color:#a71d5d;">int </span><span style="font-weight:bold;color:#795da3;">main</span><span style="color:#323232;">() {
  200. </span><span style="color:#323232;"> httplib::Client cli(</span><span style="color:#183691;">&quot;https://localhost:8443&quot;</span><span style="color:#323232;">);
  201. </span><span style="color:#323232;"> cli.set_ca_cert_path(</span><span style="color:#183691;">&quot;cert.pem&quot;</span><span style="color:#323232;">);
  202. </span><span style="color:#323232;">
  203. </span><span style="color:#323232;"> </span><span style="font-weight:bold;color:#a71d5d;">auto</span><span style="color:#323232;"> res </span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#323232;"> cli.Get(</span><span style="color:#183691;">&quot;/&quot;</span><span style="color:#323232;">);
  204. </span><span style="color:#323232;"> </span><span style="font-weight:bold;color:#a71d5d;">if </span><span style="color:#323232;">(res) {
  205. </span><span style="color:#323232;"> std::cout </span><span style="font-weight:bold;color:#a71d5d;">&lt;&lt;</span><span style="color:#323232;"> res-&gt;body </span><span style="font-weight:bold;color:#a71d5d;">&lt;&lt;</span><span style="color:#323232;"> std::endl; </span><span style="font-style:italic;color:#969896;">// Hello, HTTPS!
  206. </span><span style="color:#323232;"> }
  207. </span><span style="color:#323232;">}
  208. </span></pre>
  209. </div></div>
  210. <p>This way, only connections to the server with that specific certificate are allowed, preventing impersonation. Use this approach whenever possible, even in test environments.</p>
  211. <h2>Comparing Server and SSLServer</h2>
  212. <p>The <code>httplib::Server</code> API you learned in Chapter 3 works exactly the same with <code>httplib::SSLServer</code>. The only difference is the constructor.</p>
  213. <table><thead><tr><th></th><th><code>httplib::Server</code></th><th><code>httplib::SSLServer</code></th></tr></thead><tbody>
  214. <tr><td>Constructor</td><td>No arguments</td><td>Certificate and private key paths</td></tr>
  215. <tr><td>Protocol</td><td>HTTP</td><td>HTTPS</td></tr>
  216. <tr><td>Port (convention)</td><td>8080</td><td>8443</td></tr>
  217. <tr><td>Routing</td><td>Same</td><td>Same</td></tr>
  218. </tbody></table>
  219. <p>To switch an HTTP server to HTTPS, just change the constructor.</p>
  220. <h2>Next Steps</h2>
  221. <p>Your HTTPS server is up and running. You now have the basics of both HTTP/HTTPS clients and servers covered.</p>
  222. <p>Next, let's look at the WebSocket support that was recently added to cpp-httplib.</p>
  223. <p><strong>Next:</strong> <a href="../08-websocket">WebSocket</a></p>
  224. </article>
  225. </main>
  226. </div>
  227. <footer class="footer">
  228. © 2026 Yuji Hirose. All rights reserved.
  229. </footer>
  230. <!-- Search modal -->
  231. <div class="search-overlay" id="search-overlay">
  232. <div class="search-modal">
  233. <div class="search-input-wrap">
  234. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
  235. <input type="text" id="search-input" placeholder="Search..." autocomplete="off" spellcheck="false">
  236. <kbd class="search-esc">ESC</kbd>
  237. </div>
  238. <ul class="search-results" id="search-results"></ul>
  239. </div>
  240. </div>
  241. <script src="&#x2F;cpp-httplib/js/main.js"></script>
  242. </body>
  243. </html>