pages-data.json 17 KB

1
  1. [{"title":"Cookbook","url":"/cpp-httplib/en/cookbook/","lang":"en","section":"cookbook","body":"This section is under construction. Check back soon for a collection of recipes organized by topic."},{"title":"cpp-httplib","url":"/cpp-httplib/en/","lang":"en","section":"","body":"cpp-httplib is an HTTP/HTTPS library for C++. Just copy a single header file, httplib.h , and you're ready to go. When you need a quick HTTP server or client in C++, you want something that just works. That's exactly why I built cpp-httplib. You can start writing both servers and clients in just a few lines of code. The API uses a lambda-based design that feels natural. It runs anywhere you have a C++11 or later compiler. Windows, macOS, Linux — use whatever environment you already have. HTTPS w"},{"title":"Static File Server","url":"/cpp-httplib/en/tour/04-static-file-server/","lang":"en","section":"tour","body":"cpp-httplib can serve static files too — HTML, CSS, images, you name it. No complicated configuration required. One call to set_mount_point() is all it takes. The basics of set_mount_point Let's jump right in. set_mount_point() maps a URL path to a local directory. #include " httplib.h " #include < iostream > int main () { httplib::Server svr; svr. set_mount_point (" / ", " ./html "); std::cout << " Listening on port 8080... " << std::end"},{"title":"Getting Started","url":"/cpp-httplib/en/tour/01-getting-started/","lang":"en","section":"tour","body":"All you need to get started with cpp-httplib is httplib.h and a C++ compiler. Let's download the file and get a Hello World server running. Getting httplib.h You can download it directly from GitHub. Always use the latest version. curl -LO https://github.com/yhirose/cpp-httplib/raw/refs/tags/latest/httplib.h curl -LO https://github.com/yhirose/cpp-httplib/raw/refs/tags/latest/httplib.h Place the downloaded httplib.h in your project directory and you're good to go. Setting Up Your Compiler OS Dev"},{"title":"Basic Server","url":"/cpp-httplib/en/tour/03-basic-server/","lang":"en","section":"tour","body":"In the previous chapter, you sent requests from a client to a test server. Now let's walk through how that server actually works. Starting the Server Once you've registered your routes, call svr.listen() to start the server. svr. listen (" 0.0.0.0 ", 8080 ); svr. listen (" 0.0.0.0 ", 8080 ); The first argument is the host, and the second is the port. \"0.0.0.0\" listens on all network interfaces. Use \"127.0.0.1\" if you want to accept connections from your own machine only. list"},{"title":"Basic Client","url":"/cpp-httplib/en/tour/02-basic-client/","lang":"en","section":"tour","body":"cpp-httplib isn't just for servers -- it also comes with a full HTTP client. Let's use httplib::Client to send GET and POST requests. Preparing a Test Server To try out the client, you need a server that accepts requests. Save the following code, then compile and run it the same way you did in the previous chapter. We'll cover the server details in the next chapter. #include " httplib.h " #include < iostream > int main () { httplib::Server svr; svr. Get (" /hi ", []( co"},{"title":"HTTPS Client","url":"/cpp-httplib/en/tour/06-https-client/","lang":"en","section":"tour","body":"In the previous chapter, you set up OpenSSL. Now let's put it to use with an HTTPS client. You can use the same httplib::Client from Chapter 2. Just pass a URL with the https:// scheme to the constructor. GET Request Let's try accessing a real HTTPS site. #define CPPHTTPLIB_OPENSSL_SUPPORT #include " httplib.h " #include < iostream > int main () { httplib::Client cli (" https://nghttp2.org "); auto res = cli. Get (" / "); if (res) { std::cout << res->"},{"title":"A Tour of cpp-httplib","url":"/cpp-httplib/en/tour/","lang":"en","section":"tour","body":"This is a step-by-step tutorial that walks you through the basics of cpp-httplib. Each chapter builds on the previous one, so please read them in order starting from Chapter 1. Getting Started — Get httplib.h and build a Hello World server Basic Client — Send GET/POST requests and use path parameters Basic Server — Routing, path parameters, and building responses Static File Server — Serve static files TLS Setup — Set up OpenSSL / mbedTLS HTTPS Client — Make requests to HTTPS sites HTTPS Server "},{"title":"What's Next","url":"/cpp-httplib/en/tour/09-whats-next/","lang":"en","section":"tour","body":"Great job finishing the Tour! You now have a solid grasp of the cpp-httplib basics. But there's a lot more to explore. Here's a quick overview of features we didn't cover in the Tour, organized by category. Streaming API When you're working with LLM streaming responses or downloading large files, you don't want to load the entire response into memory. Use stream::Get() to process data chunk by chunk. httplib::Client cli (" http://localhost:11434 "); auto result = httplib::stream::Get ("},{"title":"TLS Setup","url":"/cpp-httplib/en/tour/05-tls-setup/","lang":"en","section":"tour","body":"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. In this tour, we'll use OpenSSL. It's the most widely used option, and you'll find plenty of resources online. Installing OpenSSL Install it for your OS. OS How to install macOS Homebrew ( brew install openssl ) Ubuntu / Debian sudo apt install libssl-dev Windows vcpkg ( vcpkg install openssl ) Compile Options To enable TLS, define the CPPHTTPLIB_OPENSSL_SUPPORT ma"},{"title":"HTTPS Server","url":"/cpp-httplib/en/tour/07-https-server/","lang":"en","section":"tour","body":"In the previous chapter, you used an HTTPS client. Now let's set up your own HTTPS server. Just swap httplib::Server from Chapter 3 with httplib::SSLServer . A TLS server needs a server certificate and a private key, though. Let's get those ready first. Creating a Self-Signed Certificate For development and testing, a self-signed certificate works just fine. You can generate one quickly with an OpenSSL command. openssl req -x509 -noenc -keyout key.pem -out cert.pem -subj /CN=localhost openssl re"},{"title":"WebSocket","url":"/cpp-httplib/en/tour/08-websocket/","lang":"en","section":"tour","body":"cpp-httplib supports WebSocket as well. Unlike HTTP request/response, WebSocket lets the server and client exchange messages in both directions. It's great for chat apps and real-time notifications. Let's build an echo server and client right away. Echo Server Here's an echo server that sends back whatever message it receives. #include " httplib.h " #include < iostream > int main () { httplib::Server svr; svr. WebSocket (" /ws ", []( const httplib::Request &, httpli"},{"title":"Cookbook","url":"/cpp-httplib/ja/cookbook/","lang":"ja","section":"cookbook","body":"This section is under construction. Check back soon for a collection of recipes organized by topic."},{"title":"cpp-httplib","url":"/cpp-httplib/ja/","lang":"ja","section":"","body":"cpp-httplib は、C++用のHTTP/HTTPSライブラリです。 httplib.h というヘッダーファイルを1枚コピーするだけで使えます。 C++でちょっとしたHTTPサーバーやクライアントが必要になったとき、すぐに動くものが欲しいですよね。cpp-httplibはまさにそのために作られました。サーバーもクライアントも、数行のコードで書き始められます。 APIはラムダ式をベースにした直感的な設計で、C++11以降のコンパイラーがあればどこでも動きます。Windows、macOS、Linux — お使いの環境をそのまま使えます。 HTTPSも使えます。OpenSSLやmbedTLSをリンクするだけで、サーバー・クライアントの両方がTLSに対応します。Content-Encoding(gzip, brotli等)、ファイルアップロードなど、実際の開発で必要になる機能もひと通り揃っています。WebSocketもサポートしています。 内部的にはブロッキングI/Oとスレッドプールを使っています。大量の同時接続を捌くような用途には向きませんが、APIサーバーやツールの組み込みHTTP、"},{"title":"Static File Server","url":"/cpp-httplib/ja/tour/04-static-file-server/","lang":"ja","section":"tour","body":"cpp-httplibは、HTMLやCSS、画像ファイルなどの静的ファイルも配信できます。面倒な設定は要りません。 set_mount_point() を1行呼ぶだけです。 set_mount_point の基本 さっそくやってみましょう。 set_mount_point() は、URLのパスとローカルディレクトリを紐づけます。 #include " httplib.h " #include < iostream > int main () { httplib::Server svr; svr. set_mount_point (" / ", " ./html "); std::cout << " Listening on port 8080... " << std::endl; svr. listen (" 0.0.0.0 ", 8080 ); } #include " httplib.h " #include < iost"},{"title":"Getting Started","url":"/cpp-httplib/ja/tour/01-getting-started/","lang":"ja","section":"tour","body":"cpp-httplibを始めるのに必要なのは、 httplib.h とC++コンパイラーだけです。ファイルをダウンロードして、Hello Worldサーバーを動かすところまでやってみましょう。 httplib.h の入手 GitHubから直接ダウンロードできます。常に最新版を使ってください。 curl -LO https://github.com/yhirose/cpp-httplib/raw/refs/tags/latest/httplib.h curl -LO https://github.com/yhirose/cpp-httplib/raw/refs/tags/latest/httplib.h ダウンロードした httplib.h をプロジェクトのディレクトリに置けば、準備完了です。 コンパイラーの準備 OS 開発環境 セットアップ macOS Apple Clang Xcode Command Line Tools ( xcode-select --install ) Ubuntu clang++ または g++ apt install clang または apt insta"},{"title":"Basic Server","url":"/cpp-httplib/ja/tour/03-basic-server/","lang":"ja","section":"tour","body":"前章ではクライアントからリクエストを送りました。そのとき、テスト用サーバーを用意しましたね。この章では、あのサーバーの仕組みをひとつずつ紐解いていきます。 サーバーの起動 ルーティングを登録したら、最後に svr.listen() を呼んでサーバーを起動します。 svr. listen (" 0.0.0.0 ", 8080 ); svr. listen (" 0.0.0.0 ", 8080 ); 第1引数はホスト、第2引数はポート番号です。 \"0.0.0.0\" を指定すると、すべてのネットワークインターフェースでリクエストを受け付けます。自分のマシンからのアクセスだけに限定したいときは \"127.0.0.1\" を使います。 listen() はブロッキング呼び出しです。サーバーが停止するまで、この行から先には進みません。ターミナルで Ctrl+C を押すか、別スレッドから svr.stop() を呼ぶまでサーバーは動き続けます。 ルーティング サーバーの核になるのは「ルーティング」です。どのURLに、どのHTTPメソッドでアクセスされたら、何をす"},{"title":"Basic Client","url":"/cpp-httplib/ja/tour/02-basic-client/","lang":"ja","section":"tour","body":"cpp-httplibはサーバーだけでなく、HTTPクライアント機能も備えています。 httplib::Client を使って、GETやPOSTリクエストを送ってみましょう。 テスト用サーバーの準備 クライアントの動作を確認するために、リクエストを受け付けるサーバーを用意します。次のコードを保存し、前章と同じ手順でコンパイル・実行してください。サーバーの詳しい解説は次章で行います。 #include " httplib.h " #include < iostream > int main () { httplib::Server svr; svr. Get (" /hi ", []( const auto &, auto &res) { res. set_content (" Hello! ", " text/plain "); }); svr. Get (" /search ", []( const auto &req, auto &res)"},{"title":"HTTPS Client","url":"/cpp-httplib/ja/tour/06-https-client/","lang":"ja","section":"tour","body":"前章でOpenSSLのセットアップが済んだので、さっそくHTTPSクライアントを使ってみましょう。2章で使った httplib::Client がそのまま使えます。コンストラクタに https:// 付きのURLを渡すだけです。 GETリクエスト 実在するHTTPSサイトにアクセスしてみましょう。 #define CPPHTTPLIB_OPENSSL_SUPPORT #include " httplib.h " #include < iostream > int main () { httplib::Client cli (" https://nghttp2.org "); auto res = cli. Get (" / "); if (res) { std::cout << res-> status << std::endl; // 200 std::cout << res-> body . substr ( 0 , 100 ) << std::end"},{"title":"A Tour of cpp-httplib","url":"/cpp-httplib/ja/tour/","lang":"ja","section":"tour","body":"cpp-httplibの基本を、順番に学んでいくチュートリアルです。各章は前の章の内容を踏まえて進む構成なので、1章から順に読んでください。 Getting Started — httplib.h の入手とHello Worldサーバー Basic Client — GET/POST・パスパラメーターのリクエスト送信 Basic Server — ルーティング、パスパラメーター、レスポンスの組み立て Static File Server — 静的ファイルの配信 TLS Setup — OpenSSL / mbedTLS のセットアップ HTTPS Client — HTTPSサイトへのリクエスト HTTPS Server — HTTPSサーバーの構築 WebSocket — WebSocket通信の基本 What's Next — さらなる機能の紹介"},{"title":"What's Next","url":"/cpp-httplib/ja/tour/09-whats-next/","lang":"ja","section":"tour","body":"Tourお疲れさまでした! cpp-httplibの基本はひと通り押さえましたね。でも、まだまだ便利な機能があります。Tourで取り上げなかった機能をカテゴリー別に紹介します。 Streaming API LLMのストリーミング応答や大きなファイルのダウンロードでは、レスポンス全体をメモリに載せたくないですよね。 stream::Get() を使えば、データをチャンクごとに処理できます。 httplib::Client cli (" http://localhost:11434 "); auto result = httplib::stream::Get (cli, " /api/generate "); if (result) { while (result. next ()) { std::cout. write (result. data (), result. size ()); } } httplib::Client cli (" http://localhost:11434 "); auto result = ht"},{"title":"TLS Setup","url":"/cpp-httplib/ja/tour/05-tls-setup/","lang":"ja","section":"tour","body":"ここまでは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 "},{"title":"HTTPS Server","url":"/cpp-httplib/ja/tour/07-https-server/","lang":"ja","section":"tour","body":"前章ではHTTPSクライアントを使いました。今度は自分でHTTPSサーバーを立ててみましょう。3章の httplib::Server を httplib::SSLServer に置き換えるだけです。 ただし、TLSサーバーにはサーバー証明書と秘密鍵が必要です。まずはそこから準備しましょう。 自己署名証明書の作成 開発やテスト用なら、自己署名証明書(いわゆるオレオレ証明書)で十分です。OpenSSLのコマンドでサクッと作れます。 openssl req -x509 -noenc -keyout key.pem -out cert.pem -subj /CN=localhost openssl req -x509 -noenc -keyout key.pem -out cert.pem -subj /CN=localhost これで2つのファイルができます。 cert.pem — サーバー証明書 key.pem — 秘密鍵 最小のHTTPSサーバー 証明書ができたら、さっそくサーバーを書いてみましょう。 #define CPPHTTPLIB_OPENSSL_SUPPORT #include"},{"title":"WebSocket","url":"/cpp-httplib/ja/tour/08-websocket/","lang":"ja","section":"tour","body":"cpp-httplibはWebSocketにも対応しています。HTTPのリクエスト/レスポンスと違い、WebSocketはサーバーとクライアントが双方向にメッセージをやり取りできます。チャットやリアルタイム通知に便利です。 さっそく、エコーサーバーとクライアントを作ってみましょう。 エコーサーバー 受け取ったメッセージをそのまま返すエコーサーバーです。 #include " httplib.h " #include < iostream > int main () { httplib::Server svr; svr. WebSocket (" /ws ", []( const httplib::Request &, httplib::ws::WebSocket &ws) { std::string msg; while (ws. read (msg)) { ws. send (msg); // 受け取ったメッセージをそのまま返す } }); std::cout << " Listening on"}]