mirror of
https://github.com/yhirose/cpp-httplib
synced 2026-06-08 18:30:49 +00:00
Compare commits
1136 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a77284a634 | |||
| 315a87520d | |||
| 703abbb53b | |||
| cb8365349f | |||
| 3792ce0da7 | |||
| 7178f451a4 | |||
| c2bdb1c5c1 | |||
| 45820de332 | |||
| dd8071a7d4 | |||
| c59ef98b3b | |||
| 1c6b3ea5a0 | |||
| 4a1e9443ee | |||
| 6f2717e623 | |||
| 257b266190 | |||
| ba0d0b82db | |||
| 5ecba74a99 | |||
| ec1ffbc27d | |||
| 4978f26f86 | |||
| bb7c7ab075 | |||
| 1c3d35f83c | |||
| b1bb2b7ecc | |||
| f6ed5fc60f | |||
| 69d468f4d9 | |||
| 2e61fd3e6e | |||
| 3ad4a4243a | |||
| 511e3ef9e5 | |||
| f787f31b87 | |||
| 43a54a3e3d | |||
| 83e98a28dd | |||
| 4d7c9a788d | |||
| 1cd0347ace | |||
| b3a8af80b9 | |||
| 1e97c28e36 | |||
| d279eff4db | |||
| 188035fb6d | |||
| 125272f34b | |||
| 9ced2f614d | |||
| 68fa9bce0f | |||
| 1f34c541b0 | |||
| e41ec36274 | |||
| 7489fd3a8b | |||
| ab3098f18b | |||
| 0d3a3d805c | |||
| a058e31a3a | |||
| c87d442aac | |||
| de296af3eb | |||
| 41899acece | |||
| bb22553087 | |||
| 8bffbe3ff2 | |||
| 0e4f104141 | |||
| 74807d24a7 | |||
| 2e124cde02 | |||
| fdb589d97e | |||
| 843ad379c7 | |||
| cda9680cdc | |||
| e906c31a79 | |||
| 2d669c3636 | |||
| b2d76658fc | |||
| 7444646627 | |||
| 82a61a6b60 | |||
| 63ede29db1 | |||
| ae64a5ee90 | |||
| f441fc33fb | |||
| a9fc935919 | |||
| b6cd71d4ff | |||
| bda599bfb4 | |||
| 797758a742 | |||
| 85b18a9c64 | |||
| 0853ce7753 | |||
| c99d7472b5 | |||
| defd907c74 | |||
| c6c75e4c69 | |||
| c2002f6e06 | |||
| 7c33fd47bf | |||
| 21243b3c9e | |||
| e068da4f6b | |||
| f6aec98145 | |||
| f29bb15f9d | |||
| c53d93d145 | |||
| b4d16a582d | |||
| e61a8bcec7 | |||
| 33dbe00cce | |||
| f1f8ff53d5 | |||
| 9b9bda6b6e | |||
| 2280f1d191 | |||
| 43cf1822c6 | |||
| 17f301877f | |||
| 4f17fbaa03 | |||
| 0d5bf55c73 | |||
| 718d7d92b9 | |||
| c41c5fb8a9 | |||
| ab96f72b96 | |||
| b2430249d2 | |||
| 464867a9ce | |||
| d4180e923f | |||
| 8d225afc8c | |||
| ed5c5d325b | |||
| c1ee85d89e | |||
| 14e37bd75b | |||
| c0adbb4b20 | |||
| f80864ca03 | |||
| 4e75a84b39 | |||
| 77d945d3b6 | |||
| f69737a838 | |||
| a188913b02 | |||
| 02e4c53685 | |||
| 8c4370247a | |||
| 1f1a799d13 | |||
| a875292153 | |||
| f0b7d4161d | |||
| 2867b74f13 | |||
| 4e14bc8948 | |||
| 5d717e6d91 | |||
| 8b4146324f | |||
| 94b5038eb3 | |||
| 9248ce3bfe | |||
| 4639b696ab | |||
| 5ead179b8e | |||
| 1942e0ef01 | |||
| 6be32a540d | |||
| dc6faf5c17 | |||
| 71bb17fb0e | |||
| 094bf112bb | |||
| fbec2a3466 | |||
| c3fa06112b | |||
| f73e694f0c | |||
| 191bfb2ea4 | |||
| ad5839f0d1 | |||
| 02dfb97fd6 | |||
| a38a076571 | |||
| 0e1b52b23e | |||
| c0469eba96 | |||
| 7dec57d1eb | |||
| b85aa76bd2 | |||
| cea018f2cd | |||
| 1111219f17 | |||
| a7e1d14b15 | |||
| 6eff49e1fb | |||
| bd95e67c23 | |||
| 2e2e47bab1 | |||
| 59905c7f0d | |||
| 8d03ef1615 | |||
| 23a1d79a66 | |||
| 781c55f120 | |||
| 40f7985e02 | |||
| f85f30a637 | |||
| 6da7f0c61c | |||
| 2ba0e7a797 | |||
| ded82448aa | |||
| 98048a033a | |||
| 7ae794a6bf | |||
| 385adefb11 | |||
| b7c2f04318 | |||
| d23cf77cd0 | |||
| 5304464a53 | |||
| db98efee5a | |||
| cdf0d33258 | |||
| 25688258ad | |||
| f0990ca96d | |||
| 0461cb770c | |||
| 51b704b902 | |||
| 7eb03e81fc | |||
| 6a6d4161d1 | |||
| 63b07ada43 | |||
| 2de4c59bc2 | |||
| b7097f1386 | |||
| 681d388247 | |||
| ae94d64f67 | |||
| 3401877d3d | |||
| bce08e62f9 | |||
| f4ecb96e54 | |||
| c23764269d | |||
| f441cd2a44 | |||
| c3613c6977 | |||
| 87c2b4e584 | |||
| c795ad1c32 | |||
| 3e0fa33559 | |||
| 27b73f050e | |||
| dbd5ca4bf2 | |||
| 143019a38c | |||
| 1d36013fc3 | |||
| 8bba34eebc | |||
| 0a9102ff6b | |||
| c1fa5e1710 | |||
| 84796738fc | |||
| adf58bf474 | |||
| 337fbb0793 | |||
| 9e7861b0b4 | |||
| 27ee115a60 | |||
| 59882752aa | |||
| 61e9f7ce8f | |||
| 1acf18876f | |||
| 4b2b851dbb | |||
| 551f96d4a2 | |||
| eacc1ca98e | |||
| ac9ebb0ee3 | |||
| 11eed05ce7 | |||
| f3bba0646a | |||
| 2da189f88c | |||
| 6e0f211cff | |||
| 318a3fe425 | |||
| 2d8d524178 | |||
| afa88dbe70 | |||
| 08133b593b | |||
| 8aedbf4547 | |||
| cde29362ef | |||
| bae40fcdf2 | |||
| db561f5552 | |||
| 35c52c1ab9 | |||
| 23ff9a5605 | |||
| 41be1e24e3 | |||
| 6e52d0a057 | |||
| f72b4582e6 | |||
| 89c932f313 | |||
| eb5a65e0df | |||
| 7a3b92bbd9 | |||
| eb11032797 | |||
| 54e75dc8ef | |||
| b20b5fdd1f | |||
| f4cc542d4b | |||
| 4285d33992 | |||
| 92b4f53012 | |||
| b8e21eac89 | |||
| 3fae5f1473 | |||
| fe7fe15d2e | |||
| fbd6ce7a3f | |||
| dffce89514 | |||
| 3f44c80fd3 | |||
| a2bb6f6c1e | |||
| 7012e765e1 | |||
| b1c1fa2dc6 | |||
| fbee136dca | |||
| 70cca55caf | |||
| cdaed14925 | |||
| b52d7d8411 | |||
| acf28a362d | |||
| 0b3758ec36 | |||
| a5d4c143e5 | |||
| c0c36f021d | |||
| 8e8a23e3d2 | |||
| 890a2dd85d | |||
| ca5fe354fb | |||
| dd98d2a24d | |||
| 1f110b54d8 | |||
| 7b6867bcdf | |||
| 8b28577ec6 | |||
| 55b38907dc | |||
| 53ea9e8bb4 | |||
| ecfd84c171 | |||
| b5b2a1d1c8 | |||
| 17ba303889 | |||
| 802743264c | |||
| 9dbaed75ef | |||
| c551e97297 | |||
| 4ff7a1c858 | |||
| 082acacd45 | |||
| 52163ed982 | |||
| af7a69bcf6 | |||
| 145fc8b021 | |||
| af73377611 | |||
| a3f5569196 | |||
| a636a094bf | |||
| cb85e573de | |||
| 120405beac | |||
| ceff2c1154 | |||
| 0c08c378d7 | |||
| 9a0571513e | |||
| cfb56c0b78 | |||
| 083fe43ad3 | |||
| 9e36247665 | |||
| fd03483237 | |||
| d5409ab541 | |||
| 0b875e0747 | |||
| 292f9a6c55 | |||
| 7c303bb871 | |||
| ea850cbfa7 | |||
| e6ff3d7ac2 | |||
| e1ab5a604b | |||
| 696c02f2bc | |||
| a183dba9fc | |||
| d37373a983 | |||
| b2bf172393 | |||
| 1729aa8c1f | |||
| aabd0634ae | |||
| de5a255ac6 | |||
| b6c55c6030 | |||
| 28dcf379e8 | |||
| 91e79e9a63 | |||
| 27879c4874 | |||
| 08a0452fb2 | |||
| 3a1f379e75 | |||
| fd8da4d8e4 | |||
| 365cbe37fa | |||
| 4a7aae5469 | |||
| fd324e1412 | |||
| 366eda72dc | |||
| c216dc94d2 | |||
| 61893a00a4 | |||
| 3af7f2c161 | |||
| a0de42ebc4 | |||
| 7b752106ac | |||
| 9589519d58 | |||
| caf7c55785 | |||
| 9e4aed482e | |||
| 65d6316d65 | |||
| 3e3a8cc02f | |||
| b7e33b08f1 | |||
| 0dbe8ba144 | |||
| dbc4af819a | |||
| 7dbf5471ce | |||
| 72b35befb2 | |||
| 65ce51aed7 | |||
| 787a34ad7f | |||
| 7a212cfe40 | |||
| 0be0526085 | |||
| 87a5ae64a4 | |||
| 33acccb346 | |||
| c765584e6b | |||
| 0bda3a7d1a | |||
| 2eaa2ea64f | |||
| 94a4028821 | |||
| a8d6172250 | |||
| 2f39723d08 | |||
| a9ba0a4dff | |||
| 37399af996 | |||
| 48084d55f2 | |||
| 5a1ecc3958 | |||
| 85b5cdd78d | |||
| f2928d7152 | |||
| ee0bee3907 | |||
| 71ba7e7b1b | |||
| ebe7efa1cc | |||
| 22d90c29b4 | |||
| b944f942ee | |||
| 550f728165 | |||
| a4b2c61a65 | |||
| 5c0135fa5d | |||
| 2b5d1eea8d | |||
| d274c0abe5 | |||
| dda2e007a0 | |||
| 321a86d9f2 | |||
| ada97046a2 | |||
| 6e73a63153 | |||
| cdc223019a | |||
| 574f5ce93e | |||
| 2996cecee0 | |||
| 32bf5c9c09 | |||
| 735e5930eb | |||
| 748f47b377 | |||
| 4cb8ff9f90 | |||
| 985cd9f6a2 | |||
| 233f0fb1b8 | |||
| 03cf43ebaa | |||
| 3c4b96024f | |||
| d74e4a7c9c | |||
| bfa2f735f2 | |||
| b6ab8435d7 | |||
| 39a64fb4e7 | |||
| d7c14b6f3a | |||
| 1880693aef | |||
| dd20342825 | |||
| a268d65c4f | |||
| b397c768e4 | |||
| 8e22a7676a | |||
| 8a7c536ad5 | |||
| 8aad481c69 | |||
| 5814e121df | |||
| 7adbccbaf7 | |||
| eb10c22db1 | |||
| 708f860e3a | |||
| eb30f15363 | |||
| 4941d5b56b | |||
| 9bbb4741b4 | |||
| 282f2feb77 | |||
| 60a1f00618 | |||
| 9104054ca5 | |||
| d69f144a99 | |||
| 929dfbd348 | |||
| 3047183fd9 | |||
| ef5e4044f1 | |||
| 3779800322 | |||
| 986a20fb7d | |||
| 8311e1105f | |||
| ba6845925d | |||
| 343a0fc073 | |||
| 54f8a4d0f3 | |||
| 9c36aae4b7 | |||
| b766025a83 | |||
| 9b5f76f833 | |||
| d647f484a4 | |||
| 8794792baa | |||
| b85768c1f3 | |||
| e6d71bd702 | |||
| 258992a160 | |||
| a7bc00e330 | |||
| 11a40584e9 | |||
| 3e86bdb4d8 | |||
| c817d65695 | |||
| 51dee793fe | |||
| 457fc4306e | |||
| 4f5b003e76 | |||
| 5421e27106 | |||
| fe07660f40 | |||
| da2f9e476e | |||
| 1a7a7ed1c3 | |||
| 413994912d | |||
| 01dcf1d0ad | |||
| 8e378779c2 | |||
| 970b52897c | |||
| 412ba04d19 | |||
| bfef4b3e9b | |||
| 7bd316f3d0 | |||
| 26208363ee | |||
| b1b4bb8850 | |||
| 9dd565b6e3 | |||
| 924f214303 | |||
| 5c1a34e766 | |||
| fa90d06dd5 | |||
| d869054318 | |||
| 0cc1ca9a8d | |||
| 3701195033 | |||
| f884a56258 | |||
| d79633ff52 | |||
| e0ebc431dc | |||
| 131bc6c674 | |||
| 10d68cff50 | |||
| 996acc5253 | |||
| 7c4799d0cf | |||
| c239087332 | |||
| 7018e9263d | |||
| 4990b4b4b7 | |||
| 5064373c23 | |||
| 6c93aea59a | |||
| 6553cdedab | |||
| a61b2427b0 | |||
| af4ece3d5f | |||
| e64379c3d7 | |||
| 5053912534 | |||
| 932b1cbc32 | |||
| de36ea7755 | |||
| 9f8db2c230 | |||
| 3f00e1b321 | |||
| 7ab9c119ef | |||
| 3f2922b3fa | |||
| 509f583dca | |||
| 2d01e71286 | |||
| e612154694 | |||
| 82fcbe3901 | |||
| dbd2465b56 | |||
| ea79494b29 | |||
| f35aff84c2 | |||
| 7b18ae6f16 | |||
| a79c56d06b | |||
| 3d6e315a4c | |||
| 4c27f9c6ef | |||
| d173a37d17 | |||
| 7fd346a2ca | |||
| c673d502b9 | |||
| c43c51362a | |||
| 3e86d93d13 | |||
| f6e4e2d0f3 | |||
| 01a52aa8bd | |||
| 8415bf0823 | |||
| 327ff263f5 | |||
| 61c418048d | |||
| 9720ef8c34 | |||
| 978a4f6345 | |||
| 80fb03628b | |||
| 2480c0342c | |||
| eb6f610a45 | |||
| cb74e4191b | |||
| dfa641ca41 | |||
| 969a9f99d5 | |||
| c099b42ba3 | |||
| b8315278cb | |||
| 485f8f2411 | |||
| 953e4f3841 | |||
| adf65cfe61 | |||
| 12c829f6d3 | |||
| 913314f1b1 | |||
| ef63f97afe | |||
| bda74db01d | |||
| 9ff3ff9446 | |||
| c75d071615 | |||
| b4989130da | |||
| 4fc0303bda | |||
| 3d9cc51851 | |||
| f69587656f | |||
| d5fc340c30 | |||
| d79a547dc9 | |||
| bd1da4346a | |||
| 4c2a608a0c | |||
| ee4eb8deaa | |||
| 7196ac8a07 | |||
| c88b09bc6b | |||
| 87fab847b8 | |||
| 4e6055f084 | |||
| 975cf0dae5 | |||
| 4854a694cd | |||
| b1f8e986bf | |||
| c5ee208775 | |||
| ddfdacfa49 | |||
| 2514ebc20f | |||
| 4f9c6540b2 | |||
| 21c9a6a1ff | |||
| 7f6d413ddd | |||
| 88277139e7 | |||
| 6cdd3493a1 | |||
| 9c91b6f4a6 | |||
| cee838e335 | |||
| d82c82db2c | |||
| ba638ff38e | |||
| da0c6579fa | |||
| 52a18c78a5 | |||
| 048edec9ed | |||
| af56b7ec0b | |||
| 6c3e8482f7 | |||
| 390f2c41f6 | |||
| aa04feebb4 | |||
| 45f3694f82 | |||
| c5c54b31e2 | |||
| 69c84c9597 | |||
| ae63b89cbf | |||
| ff038f98b7 | |||
| e00fd06355 | |||
| 521529d24d | |||
| ed0719f2bc | |||
| 6a848b1a16 | |||
| c8bcaf8a91 | |||
| 8cd0ed0509 | |||
| 177d8420a1 | |||
| 388a8c007c | |||
| bdefdce1ae | |||
| 9e4f93d87e | |||
| 0b657d28cf | |||
| c1a09daf15 | |||
| 8438df4a95 | |||
| 67fd7e3d09 | |||
| d44031615d | |||
| 98cc1ec344 | |||
| 25b1e0d906 | |||
| 05f9f83240 | |||
| fb739dbaec | |||
| 50fce538c6 | |||
| 3b6597bba9 | |||
| f10720ed69 | |||
| 2bc550b2f0 | |||
| ce36b8a6e5 | |||
| 560854a961 | |||
| 2064462c35 | |||
| 07288888ad | |||
| 34d392cf3d | |||
| 825c3fbbb1 | |||
| 00bdf73ec6 | |||
| f44ab9b3da | |||
| a61f2b89be | |||
| b8bafbc291 | |||
| 548dfff0ae | |||
| 4dd2f3d03d | |||
| 6791a8364d | |||
| d1a1c8a158 | |||
| b4d26badf2 | |||
| c5a0673c93 | |||
| ad40bd6a00 | |||
| 5c00bbf36b | |||
| 9d6f5372a3 | |||
| f06fd934f6 | |||
| 80c0cc445e | |||
| 762024b890 | |||
| 82a90a2325 | |||
| b7cac4f4b8 | |||
| e323374d2a | |||
| ffc294d37e | |||
| fceada9ef4 | |||
| 5f0f73fad9 | |||
| 530d6ee098 | |||
| 420c9759c6 | |||
| 2ce7c22218 | |||
| 4ef9ed80cd | |||
| 44b3fe6277 | |||
| 449801990f | |||
| af2928d316 | |||
| d948e38820 | |||
| 65218ce222 | |||
| 55e99c4030 | |||
| b63d50671d | |||
| eba980846b | |||
| 374d058de7 | |||
| 31cdcc3c3a | |||
| ad9f6423e2 | |||
| cbca63f091 | |||
| b4748a226c | |||
| 5b943d9bb8 | |||
| c86f69a105 | |||
| d39fda0657 | |||
| 37f8dc4382 | |||
| 3a8adda381 | |||
| 8aa38aecaf | |||
| f1dec77f46 | |||
| cddaedaff8 | |||
| cefb5a8822 | |||
| e426a38c3e | |||
| f14accb7b6 | |||
| c5c704cb3b | |||
| 115a786581 | |||
| 5ef4cfd263 | |||
| 03fecb2f78 | |||
| 7fc8682a0a | |||
| f1431311a4 | |||
| 1d14e051a5 | |||
| 97ae6733ed | |||
| 1d6b22b5f0 | |||
| 1a49076b5b | |||
| d0e4cb3f07 | |||
| e2813d9d4d | |||
| 20a7f088ce | |||
| f63ba7d013 | |||
| 0a629d7391 | |||
| a609330e4c | |||
| c029597a5a | |||
| 30b7732565 | |||
| 6650632e7f | |||
| afe627e7af | |||
| 67f6ff7fa9 | |||
| c7ed1796a7 | |||
| 44c62d838e | |||
| 6bb580cda8 | |||
| 00a8cb8e5d | |||
| 2e34a39673 | |||
| 01b90829bc | |||
| e699bd0730 | |||
| 961a9379d5 | |||
| ec87b04aff | |||
| aabf752a51 | |||
| afb0674ccb | |||
| ee625232a4 | |||
| 52d8dd41f1 | |||
| be07d2d7a9 | |||
| 0f1b62c2b3 | |||
| c30906a541 | |||
| 3533503323 | |||
| 82acdca638 | |||
| a1e56a567b | |||
| 0c17d172a2 | |||
| 5d8e7c761f | |||
| 17fc522b75 | |||
| f1daa5b88b | |||
| fe9a1949a6 | |||
| 50cba6db9f | |||
| 18592e7f98 | |||
| bd9612b81e | |||
| 7ab5fb65b2 | |||
| 8df5fedc35 | |||
| 4a61f68fa4 | |||
| 067890133c | |||
| d3076f5a70 | |||
| ed129f057f | |||
| eab5ea01d7 | |||
| 3e287b3a26 | |||
| 4f33637b43 | |||
| 698a1e51ec | |||
| 27c0e1186c | |||
| c54c71a3e5 | |||
| 3409c00e6f | |||
| f8ef5fab64 | |||
| 5b397d455d | |||
| f977558a28 | |||
| c2e156e0e0 | |||
| 7aba2938d3 | |||
| d587548250 | |||
| 21f9c51556 | |||
| 985ceba525 | |||
| e62a4b02e5 | |||
| ff34749572 | |||
| e5804d4a50 | |||
| 3956a2b790 | |||
| b33aa52dc2 | |||
| 76230db97f | |||
| a66a013ed7 | |||
| f4b02dfdc1 | |||
| 4cf218643e | |||
| 8f96b69a25 | |||
| d262033ded | |||
| 5745eabe69 | |||
| 5f18642271 | |||
| 88a9278872 | |||
| 9bb3ca8169 | |||
| f2f4728489 | |||
| d1b616286f | |||
| df74526f91 | |||
| 9f7ae0737a | |||
| 1ebb8412c5 | |||
| 7b69999c37 | |||
| c7e959a948 | |||
| ba5884e779 | |||
| cdaa5c48db | |||
| bab5c0e907 | |||
| 016838fd10 | |||
| 75053bf855 | |||
| ae3a6dd2a9 | |||
| 6d963fbe8d | |||
| 88f6245c84 | |||
| 0e7d2f9f93 | |||
| 4e6ded1f36 | |||
| d663588491 | |||
| 439caf5b79 | |||
| c4ba43ca6f | |||
| 20cba2ecd9 | |||
| 0ff2e16d69 | |||
| 51607ec752 | |||
| 7992b14896 | |||
| 7e420aeed3 | |||
| 227d2c2050 | |||
| 93e53c91f7 | |||
| 58cffd3223 | |||
| 8f32271e8c | |||
| c8c1c3d376 | |||
| 9f512acb42 | |||
| c0b461a3b7 | |||
| 74fe5a5029 | |||
| 9d0a9d4e23 | |||
| 5758769ad3 | |||
| e7eadc3605 | |||
| 07c6e58951 | |||
| 87994811a1 | |||
| 42feb7e8be | |||
| 26196b70af | |||
| 93a51979c4 | |||
| ad7edc7b27 | |||
| 27cd4e6ffe | |||
| cae5a8be1c | |||
| 8e10d4e8e7 | |||
| b57f79f438 | |||
| a9cf097951 | |||
| 5c3624e1af | |||
| cba9ef8c0b | |||
| 4f8407a3a7 | |||
| 656e936f49 | |||
| d92c314466 | |||
| b747fb111d | |||
| 7e0a0b2d0c | |||
| 362d064afa | |||
| 1bd88de2e5 | |||
| 0b541ffebc | |||
| 106be19c3e | |||
| 25d72bf881 | |||
| 9d5b5297cc | |||
| 462884bebb | |||
| b1cc24b795 | |||
| f0eb55b327 | |||
| 6dc285b5ca | |||
| 07e614eef7 | |||
| 916b2a8fd3 | |||
| 869f5bb279 | |||
| 3e21338f82 | |||
| 37bb3c6a77 | |||
| d4ab2fa0e6 | |||
| 72d3f4896a | |||
| 5e6f973b99 | |||
| 7ed77b02ad | |||
| 127a64d5a0 | |||
| caa31aafda | |||
| dae318495f | |||
| 305a7abcb9 | |||
| 219d13b718 | |||
| df20c27696 | |||
| a5a62768c0 | |||
| 4001637beb | |||
| 47044c05a8 | |||
| a449d82723 | |||
| fee8e97b4e | |||
| 72d9ed4056 | |||
| 1be1b3a86d | |||
| 9452c0a4b6 | |||
| 307b729549 | |||
| 696239d6e1 | |||
| 6929d90353 | |||
| 348d032029 | |||
| d1d3fcdfd5 | |||
| abf3a67dd0 | |||
| d87abeecf0 | |||
| 4e28e4f741 | |||
| 80a55cedeb | |||
| d05c343602 | |||
| 33f67386fe | |||
| 56d8168dc4 | |||
| 5d87cc0558 | |||
| cb41947eb4 | |||
| 0857eba17b | |||
| 020b0db090 | |||
| bf0760fde4 | |||
| bb8e45383e | |||
| a1df576e4f | |||
| 7fb0254794 | |||
| c82d1e52cc | |||
| 846151b605 | |||
| e44e31dd5b | |||
| f7b9501662 | |||
| e12fe4cbbb | |||
| 49d2e1f135 | |||
| 8191fd8e6c | |||
| d73395e1dc | |||
| 64d001162b | |||
| bb00a23116 | |||
| 63d6e9b91b | |||
| 66eed5681a | |||
| 8ecdb11979 | |||
| 894fcc8e02 | |||
| 7f43f0f3ff | |||
| 87e03dd1ce | |||
| e5cacb465d | |||
| ee8371f753 | |||
| 081723f983 | |||
| b61f36579c | |||
| 33f53aa458 | |||
| 412ab5f063 | |||
| 11e02e901c | |||
| 65a8f4cf44 | |||
| 27d128bbb4 | |||
| 070f9bec58 | |||
| f817032513 | |||
| 17abe221c0 | |||
| 4a7a81e039 | |||
| 37fd4eb643 | |||
| 865b0e4c03 | |||
| b324921c1a | |||
| 63f72caf30 | |||
| 99ac17b90a | |||
| 4b0ed9ee88 | |||
| 20056f6cda | |||
| 3b35279b16 | |||
| 27deb44df5 | |||
| 24a3ef949b | |||
| bc3e098964 | |||
| c247dcdd7b | |||
| 793ae9855e | |||
| 9fa426d51b | |||
| cec6288a99 | |||
| 9639578c2a | |||
| 743ecbd365 | |||
| 084c643973 | |||
| 824e7682e4 | |||
| f9074684dd | |||
| ddff782133 | |||
| 3051152103 | |||
| 06026bb47d | |||
| 226388ae27 | |||
| ea7548b4cc | |||
| c7486ead96 | |||
| 90a291214c | |||
| c111c42a86 | |||
| 6fb5b63018 | |||
| ec56dfa35e | |||
| 943cd51b67 | |||
| 301faa074c | |||
| dc0481e832 | |||
| 4f8fcdbaf7 | |||
| b80aa7fee3 | |||
| c384be02c9 | |||
| d17ac3bb40 | |||
| c7554ccac2 | |||
| 35ef1c7bae | |||
| d87d0672a8 | |||
| 3da42fd1e8 | |||
| 503aa61325 | |||
| e4c276d0c2 | |||
| e07f7691a8 | |||
| 623ab4a96e | |||
| e1efa337a2 | |||
| 549cdf2f7d | |||
| 3c522386e9 | |||
| c202aa9ce9 | |||
| e3e28c6231 | |||
| 4e05368086 | |||
| e1afe74fe2 | |||
| 461acb02f5 | |||
| 415edc237c | |||
| e20ecd2574 | |||
| ab477b5631 | |||
| 0823d5c7f2 | |||
| 1cc6930363 | |||
| 4297500928 | |||
| a58f042614 | |||
| 469c6bc2b6 | |||
| 887074efd2 | |||
| 9c2c15ca45 | |||
| 1b3b098329 | |||
| 6f7075e3aa | |||
| ccbddd8842 | |||
| 879dd261c2 | |||
| 52f5eb5980 | |||
| ea2f69a0d7 | |||
| 9f2064a8ed | |||
| e3750d9ddf | |||
| c1eee3012e | |||
| 6b08babbd2 | |||
| 215b81342e | |||
| 06bfa7e08b | |||
| 3d83cbb872 | |||
| 8a803b30f6 | |||
| 80be649de7 | |||
| 9648f950f5 | |||
| 6b9ffc8bec | |||
| d903053faf | |||
| 676f1b5a26 | |||
| b8dec12f15 | |||
| fc9b223acc | |||
| ba824089d7 | |||
| 1a2faf09e0 | |||
| 5a43bb8149 | |||
| 0104614656 | |||
| 77a77f6d2d | |||
| 089b9daa1c | |||
| ba34ea4ee8 | |||
| 2917b8a005 | |||
| dcf24d45a2 | |||
| 75fdb06696 | |||
| e00ad37580 | |||
| 5cfb70c2b4 | |||
| 2a70c45697 | |||
| c58b00580e | |||
| 7c60e69c33 | |||
| 33e94891ee | |||
| 73e0729f63 | |||
| 21c529229c | |||
| 63643e6386 | |||
| 6cc2edce99 | |||
| d122ff3ca8 | |||
| 14c6d526b4 | |||
| 28e07bca16 | |||
| faa5f1d802 | |||
| 9d3365df54 | |||
| 6ff84d34d1 | |||
| b845425cd0 | |||
| 89519c88e2 | |||
| ff813bf99d | |||
| cf475bcb50 | |||
| bc80d7c789 | |||
| b7566f6961 | |||
| 0542fdb8e4 | |||
| 78c474c744 | |||
| 88411a1f52 | |||
| ae6cf70bc4 | |||
| 68d1281759 | |||
| 0308d60cb2 | |||
| 59f5fdbb33 | |||
| 13184f5f80 | |||
| 8d9a477edb | |||
| 85b4abbf16 | |||
| e42a358da8 | |||
| f008fe4539 | |||
| ddf41d29ef | |||
| 3f88a46c4a | |||
| 242706ea34 | |||
| a9f5f8683f | |||
| 60c2213893 | |||
| eb2d28bca2 | |||
| 86f637a246 | |||
| 2c07ec4600 | |||
| 871d8d67b0 | |||
| 7299713195 | |||
| 96afa7e108 | |||
| 55f57af0b9 | |||
| 6b35cd0116 | |||
| 99f2229e48 | |||
| b9641048fc | |||
| e9c6c6e609 | |||
| 40db42108f | |||
| 24bb1387d6 | |||
| d0bd4afb0b | |||
| 78ea786abd | |||
| 9cac2c9ceb | |||
| 0cff3245df | |||
| 0e3925db3f | |||
| c9a13d214b | |||
| 0954af2d4c | |||
| 7c1c952f5a | |||
| a6edfc730a | |||
| c1264bfedc | |||
| 90a5b6ceb0 | |||
| eb240ad2e5 | |||
| 88c961f37e | |||
| b952376968 | |||
| 5dd605d3a2 | |||
| 9c0c98b1ed | |||
| 615867322d | |||
| 02d3cd5909 | |||
| 47e5af15ea | |||
| a5c239c174 | |||
| c2afc5ca44 | |||
| cee062d4c9 | |||
| b21dc8cbe0 | |||
| e1133a2dcb | |||
| e273fec93c | |||
| 95d0b073bd | |||
| 9c7d841b37 | |||
| f086bf5310 | |||
| 6613d7b7ad | |||
| 6adf130bf3 | |||
| b6b2eaf5bc | |||
| eb4b7c70a9 | |||
| 84661ea6ed | |||
| 041122908c | |||
| 401de608df | |||
| 726c64cf10 | |||
| e1f781a21a | |||
| 72b81badad | |||
| 17428a8fbf | |||
| 6e1879dfae | |||
| eb1d2e04bc | |||
| c909ffa758 | |||
| ff5677ad19 | |||
| 8b1b31ac20 | |||
| 953600c177 | |||
| 536e7eb7f2 | |||
| 6d66721ba1 | |||
| 3b29cd0bdc | |||
| 109b624dfe | |||
| 0ed70c4d9f | |||
| a50b7591ca | |||
| bf8fc11b53 | |||
| bc4a613b6d | |||
| 4bb001351c | |||
| e155ba44bb | |||
| a4a9637738 | |||
| 5292142046 | |||
| cc5147ad72 | |||
| fffbf1a669 | |||
| d37bc0fb4d | |||
| 6d60dc8839 | |||
| b713a3a651 | |||
| 7e8db1dc68 | |||
| 316add860b | |||
| 79ce6f1745 | |||
| 09fdf4eacd | |||
| 143b2dd15a | |||
| e2c4e9d95c | |||
| d87082f04b | |||
| cc14855ba0 | |||
| 56c418745f | |||
| 4ce9911837 | |||
| 559c407552 | |||
| a2f4e29a7b | |||
| b8cf739d27 | |||
| aec2f9521d | |||
| 7b55ecdc59 | |||
| e9575bcb78 | |||
| 308aeb187b | |||
| 05d18f2bc5 | |||
| 3da4a0ac69 | |||
| 9d12b3f20e | |||
| 852a374748 | |||
| 3b5bab3308 | |||
| 69e75f4a67 | |||
| b0fd4befb1 | |||
| 3e80666a74 | |||
| 3e4567bae8 | |||
| db075d8cf9 | |||
| 16df0ef37e | |||
| f1a2ac5108 | |||
| e5743b358d | |||
| 1184bbe4cb | |||
| 9dcffda7ae | |||
| e5903635e2 | |||
| 510b4eaaae | |||
| d7e63b4316 | |||
| e5dd410256 | |||
| 951e46929e | |||
| c8adac30f4 | |||
| b2b4f7635f | |||
| 649b1d2172 | |||
| dc5f9ba164 | |||
| cf084e1db1 | |||
| e0e5898601 | |||
| dfec2de5b5 | |||
| 04002d57bd | |||
| 38a7706c8b | |||
| abaf875c42 | |||
| 5f76cb01c7 | |||
| ae54e833ea | |||
| 0dd3659de5 | |||
| 999f6abd2c | |||
| 48da75dd35 | |||
| 4f84eeb298 | |||
| a5b4cfadb9 | |||
| 3e906a9b8c | |||
| 110393eadb | |||
| 98caa8c058 | |||
| ef65f09608 | |||
| e130cf3a3b | |||
| 8a348f17fd | |||
| 797d1f27e8 | |||
| 6cde600922 | |||
| 342c3ab293 | |||
| 6cce7951fc | |||
| e9058e5639 | |||
| 2538a85486 | |||
| 8c501022b3 | |||
| 9f5db2d1aa | |||
| 12540fe8d3 | |||
| 29a06f852a | |||
| 0e9cfd9f49 | |||
| 90da199aba | |||
| 366d073490 | |||
| 9ca1fa8b18 | |||
| 15c4106a36 | |||
| 72ce293fed | |||
| b476b55771 | |||
| 0db9d21eb0 | |||
| 5ddaf949d0 | |||
| 457a5a7501 | |||
| 2ce080c2cb | |||
| 6ad25b6cf0 | |||
| 3dff60eb16 | |||
| 5038314b21 | |||
| 6e1297cab0 | |||
| 7de743c962 | |||
| 964fb5e5ca | |||
| c4f3f9529b | |||
| 887def9490 | |||
| bad6b2d22f | |||
| 3d47a51430 | |||
| 0a2cb20223 | |||
| ce502a73e1 | |||
| 010e4479f4 | |||
| 70e193374a | |||
| 6b22409217 | |||
| 969cccd52a | |||
| 4a9c048bbc | |||
| bfabbec8c7 | |||
| 3e9c06cf79 | |||
| 29677540ae | |||
| 71fcfeb912 | |||
| c7d22e451f | |||
| 42f9f9107f | |||
| 7cd25fbd63 |
@@ -0,0 +1,2 @@
|
||||
/test/www*/dir/*.html text eol=lf
|
||||
/test/www*/dir/*.txt text eol=lf
|
||||
@@ -0,0 +1,70 @@
|
||||
# SPDX-FileCopyrightText: 2025 Andrea Pappacoda <andrea@pappacoda.it>
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
name: abidiff
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref || github.run_id }}
|
||||
cancel-in-progress: true
|
||||
|
||||
defaults:
|
||||
run:
|
||||
shell: sh
|
||||
|
||||
jobs:
|
||||
abi:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
|
||||
container:
|
||||
image: debian:testing
|
||||
|
||||
steps:
|
||||
- name: Install dependencies
|
||||
run: apt -y --update install --no-install-recommends
|
||||
abigail-tools
|
||||
ca-certificates
|
||||
g++
|
||||
git
|
||||
libbrotli-dev
|
||||
libssl-dev
|
||||
libzstd-dev
|
||||
meson
|
||||
pkg-config
|
||||
python3
|
||||
zlib1g-dev
|
||||
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
path: current
|
||||
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
path: previous
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Checkout previous
|
||||
working-directory: previous
|
||||
run: |
|
||||
git switch master
|
||||
git describe --tags --abbrev=0 master | xargs git checkout
|
||||
|
||||
- name: Build current
|
||||
working-directory: current
|
||||
run: |
|
||||
meson setup --buildtype=debug -Dcpp-httplib_compile=true build
|
||||
ninja -C build
|
||||
|
||||
- name: Build previous
|
||||
working-directory: previous
|
||||
run: |
|
||||
meson setup --buildtype=debug -Dcpp-httplib_compile=true build
|
||||
ninja -C build
|
||||
|
||||
- name: Run abidiff
|
||||
run: abidiff
|
||||
--headers-dir1 previous/build
|
||||
--headers-dir2 current/build
|
||||
previous/build/libcpp-httplib.so
|
||||
current/build/libcpp-httplib.so
|
||||
@@ -0,0 +1,32 @@
|
||||
name: CIFuzz
|
||||
|
||||
on: [pull_request]
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref || github.run_id }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
Fuzzing:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Build Fuzzers
|
||||
id: build
|
||||
uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master
|
||||
with:
|
||||
oss-fuzz-project-name: 'cpp-httplib'
|
||||
dry-run: false
|
||||
language: c++
|
||||
- name: Run Fuzzers
|
||||
uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master
|
||||
with:
|
||||
oss-fuzz-project-name: 'cpp-httplib'
|
||||
fuzz-seconds: 600
|
||||
dry-run: false
|
||||
language: c++
|
||||
- name: Upload Crash
|
||||
uses: actions/upload-artifact@v4
|
||||
if: failure() && steps.build.outcome == 'success'
|
||||
with:
|
||||
name: artifacts
|
||||
path: ./out/artifacts
|
||||
@@ -0,0 +1,33 @@
|
||||
name: docs
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
paths:
|
||||
- 'docs-src/**'
|
||||
permissions:
|
||||
contents: read
|
||||
pages: write
|
||||
id-token: write
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: ubuntu-latest
|
||||
environment:
|
||||
name: github-pages
|
||||
url: ${{ steps.deployment.outputs.page_url }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Install docs-gen
|
||||
run: cargo install docs-gen
|
||||
- name: Build
|
||||
run: docs-gen build docs-src docs
|
||||
- uses: actions/configure-pages@v5
|
||||
- uses: actions/upload-pages-artifact@v3
|
||||
with:
|
||||
path: docs
|
||||
- id: deployment
|
||||
uses: actions/deploy-pages@v4
|
||||
@@ -0,0 +1,51 @@
|
||||
name: Release Docker Image
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build-and-push:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0 # Fetch all history and tags
|
||||
|
||||
- name: Extract tag (manual)
|
||||
if: github.event_name == 'workflow_dispatch'
|
||||
id: set_tag_manual
|
||||
run: |
|
||||
# Checkout the latest tag and set output
|
||||
git fetch --tags
|
||||
LATEST_TAG=$(git describe --tags --abbrev=0)
|
||||
git checkout $LATEST_TAG
|
||||
echo "tag=${LATEST_TAG#v}" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Extract tag (release)
|
||||
if: github.event_name == 'release'
|
||||
id: set_tag_release
|
||||
run: echo "tag=${GITHUB_REF_NAME#v}" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Log in to Docker Hub
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
|
||||
- name: Build and push Docker image
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
file: ./Dockerfile
|
||||
push: true
|
||||
platforms: linux/amd64,linux/arm64 # Build for both amd64 and arm64
|
||||
# Use extracted tag without leading 'v'
|
||||
tags: |
|
||||
yhirose4dockerhub/cpp-httplib-server:latest
|
||||
yhirose4dockerhub/cpp-httplib-server:${{ steps.set_tag_manual.outputs.tag || steps.set_tag_release.outputs.tag }}
|
||||
@@ -0,0 +1,36 @@
|
||||
name: 32-bit Build Test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
pull_request:
|
||||
branches: [master]
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref || github.run_id }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
test-win32:
|
||||
name: Windows 32-bit (MSVC x86)
|
||||
runs-on: windows-latest
|
||||
timeout-minutes: 10
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Build (Win32)
|
||||
shell: cmd
|
||||
run: |
|
||||
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x86
|
||||
cl /std:c++14 /EHsc /W4 /WX /c /Fo:NUL test\test_32bit_build.cpp
|
||||
|
||||
test-arm32:
|
||||
name: ARM 32-bit (cross-compile)
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 10
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install cross compiler
|
||||
run: sudo apt-get update && sudo apt-get install -y g++-arm-linux-gnueabihf
|
||||
- name: Build (ARM 32-bit)
|
||||
run: arm-linux-gnueabihf-g++ -std=c++11 -Wall -Wextra -Wno-psabi -Werror -c -o /dev/null test/test_32bit_build.cpp
|
||||
+246
-9
@@ -1,17 +1,254 @@
|
||||
name: test
|
||||
|
||||
on: [push, pull_request]
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
gtest_filter:
|
||||
description: 'Google Test filter'
|
||||
test_linux:
|
||||
description: 'Test on Linux'
|
||||
type: boolean
|
||||
default: true
|
||||
test_macos:
|
||||
description: 'Test on MacOS'
|
||||
type: boolean
|
||||
default: true
|
||||
test_windows:
|
||||
description: 'Test on Windows'
|
||||
type: boolean
|
||||
default: true
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref || github.run_id }}
|
||||
cancel-in-progress: true
|
||||
|
||||
env:
|
||||
GTEST_FILTER: ${{ github.event.inputs.gtest_filter || '*' }}
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ${{ matrix.os }}
|
||||
style-check:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
|
||||
continue-on-error: true
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: run style check
|
||||
run: |
|
||||
clang-format --version
|
||||
cd test && make style_check
|
||||
|
||||
build-and-test-on-32bit:
|
||||
runs-on: ubuntu-latest
|
||||
if: >
|
||||
(github.event_name == 'push') ||
|
||||
(github.event_name == 'pull_request' &&
|
||||
github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
|
||||
(github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true')
|
||||
strategy:
|
||||
matrix:
|
||||
os: [macOS-latest, ubuntu-latest]
|
||||
|
||||
config:
|
||||
- arch_flags: -m32
|
||||
arch_suffix: :i386
|
||||
name: (32-bit)
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v1
|
||||
- name: make
|
||||
run: cd test && make
|
||||
- name: checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: install libraries
|
||||
run: |
|
||||
sudo dpkg --add-architecture i386
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y libc6-dev${{ matrix.config.arch_suffix }} libstdc++-13-dev${{ matrix.config.arch_suffix }} \
|
||||
libssl-dev${{ matrix.config.arch_suffix }} libcurl4-openssl-dev${{ matrix.config.arch_suffix }} \
|
||||
zlib1g-dev${{ matrix.config.arch_suffix }} libbrotli-dev${{ matrix.config.arch_suffix }} \
|
||||
libzstd-dev${{ matrix.config.arch_suffix }}
|
||||
- name: build and run tests
|
||||
run: cd test && make test EXTRA_CXXFLAGS="${{ matrix.config.arch_flags }}"
|
||||
|
||||
ubuntu:
|
||||
runs-on: ubuntu-latest
|
||||
if: >
|
||||
(github.event_name == 'push') ||
|
||||
(github.event_name == 'pull_request' &&
|
||||
github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
|
||||
(github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true')
|
||||
strategy:
|
||||
matrix:
|
||||
tls_backend: [openssl, mbedtls, wolfssl]
|
||||
name: ubuntu (${{ matrix.tls_backend }})
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: install common libraries
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y libcurl4-openssl-dev zlib1g-dev libbrotli-dev libzstd-dev
|
||||
- name: install OpenSSL
|
||||
if: matrix.tls_backend == 'openssl'
|
||||
run: sudo apt-get install -y libssl-dev
|
||||
- name: install Mbed TLS
|
||||
if: matrix.tls_backend == 'mbedtls'
|
||||
run: sudo apt-get install -y libmbedtls-dev
|
||||
- name: install wolfSSL
|
||||
if: matrix.tls_backend == 'wolfssl'
|
||||
run: sudo apt-get install -y libwolfssl-dev
|
||||
- name: build and run tests (OpenSSL)
|
||||
if: matrix.tls_backend == 'openssl'
|
||||
run: cd test && make test_split && make test_openssl_parallel
|
||||
env:
|
||||
LSAN_OPTIONS: suppressions=lsan_suppressions.txt
|
||||
- name: build and run tests (Mbed TLS)
|
||||
if: matrix.tls_backend == 'mbedtls'
|
||||
run: cd test && make test_split_mbedtls && make test_mbedtls_parallel
|
||||
- name: build and run tests (wolfSSL)
|
||||
if: matrix.tls_backend == 'wolfssl'
|
||||
run: cd test && make test_split_wolfssl && make test_wolfssl_parallel
|
||||
- name: run fuzz test target
|
||||
if: matrix.tls_backend == 'openssl'
|
||||
run: cd test && make fuzz_test
|
||||
- name: build and run WebSocket heartbeat test
|
||||
if: matrix.tls_backend == 'openssl'
|
||||
run: cd test && make test_websocket_heartbeat && ./test_websocket_heartbeat
|
||||
- name: build and run ThreadPool test
|
||||
run: cd test && make test_thread_pool && ./test_thread_pool
|
||||
|
||||
macos:
|
||||
runs-on: macos-latest
|
||||
if: >
|
||||
(github.event_name == 'push') ||
|
||||
(github.event_name == 'pull_request' &&
|
||||
github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
|
||||
(github.event_name == 'workflow_dispatch' && github.event.inputs.test_macos == 'true')
|
||||
strategy:
|
||||
matrix:
|
||||
tls_backend: [openssl, mbedtls, wolfssl]
|
||||
name: macos (${{ matrix.tls_backend }})
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: install Mbed TLS
|
||||
if: matrix.tls_backend == 'mbedtls'
|
||||
run: brew install mbedtls@3
|
||||
- name: install wolfSSL
|
||||
if: matrix.tls_backend == 'wolfssl'
|
||||
run: brew install wolfssl
|
||||
- name: build and run tests (OpenSSL)
|
||||
if: matrix.tls_backend == 'openssl'
|
||||
run: cd test && make test_split && make test_openssl_parallel
|
||||
env:
|
||||
LSAN_OPTIONS: suppressions=lsan_suppressions.txt
|
||||
- name: build and run tests (Mbed TLS)
|
||||
if: matrix.tls_backend == 'mbedtls'
|
||||
run: cd test && make test_split_mbedtls && make test_mbedtls_parallel
|
||||
- name: build and run tests (wolfSSL)
|
||||
if: matrix.tls_backend == 'wolfssl'
|
||||
run: cd test && make test_split_wolfssl && make test_wolfssl_parallel
|
||||
- name: run fuzz test target
|
||||
if: matrix.tls_backend == 'openssl'
|
||||
run: cd test && make fuzz_test
|
||||
- name: build and run WebSocket heartbeat test
|
||||
if: matrix.tls_backend == 'openssl'
|
||||
run: cd test && make test_websocket_heartbeat && ./test_websocket_heartbeat
|
||||
- name: build and run ThreadPool test
|
||||
run: cd test && make test_thread_pool && ./test_thread_pool
|
||||
|
||||
windows:
|
||||
runs-on: windows-latest
|
||||
if: >
|
||||
(github.event_name == 'push') ||
|
||||
(github.event_name == 'pull_request' &&
|
||||
github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
|
||||
(github.event_name == 'workflow_dispatch' && github.event.inputs.test_windows == 'true')
|
||||
strategy:
|
||||
matrix:
|
||||
config:
|
||||
- with_ssl: false
|
||||
compiled: false
|
||||
run_tests: true
|
||||
name: without SSL
|
||||
- with_ssl: true
|
||||
compiled: false
|
||||
run_tests: true
|
||||
name: with SSL
|
||||
- with_ssl: false
|
||||
compiled: true
|
||||
run_tests: false
|
||||
name: compiled
|
||||
name: windows ${{ matrix.config.name }}
|
||||
steps:
|
||||
- name: Prepare Git for Checkout on Windows
|
||||
run: |
|
||||
git config --global core.autocrlf false
|
||||
git config --global core.eol lf
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: Export GitHub Actions cache environment variables
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
|
||||
core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');
|
||||
- name: Setup msbuild on windows
|
||||
uses: microsoft/setup-msbuild@v2
|
||||
- name: Cache vcpkg packages
|
||||
id: vcpkg-cache
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: C:/vcpkg/installed
|
||||
key: vcpkg-installed-windows-gtest-curl-zlib-brotli-zstd
|
||||
- name: Install vcpkg dependencies
|
||||
if: steps.vcpkg-cache.outputs.cache-hit != 'true'
|
||||
run: vcpkg install gtest curl zlib brotli zstd
|
||||
- name: Install OpenSSL
|
||||
if: ${{ matrix.config.with_ssl }}
|
||||
run: choco install openssl
|
||||
- name: Configure CMake ${{ matrix.config.name }}
|
||||
run: >
|
||||
cmake -B build -S .
|
||||
-DCMAKE_BUILD_TYPE=Release
|
||||
-DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake
|
||||
-DHTTPLIB_TEST=ON
|
||||
-DHTTPLIB_COMPILE=${{ matrix.config.compiled && 'ON' || 'OFF' }}
|
||||
-DHTTPLIB_USE_OPENSSL_IF_AVAILABLE=${{ matrix.config.with_ssl && 'ON' || 'OFF' }}
|
||||
-DHTTPLIB_REQUIRE_ZLIB=ON
|
||||
-DHTTPLIB_REQUIRE_BROTLI=ON
|
||||
-DHTTPLIB_REQUIRE_ZSTD=ON
|
||||
-DHTTPLIB_REQUIRE_OPENSSL=${{ matrix.config.with_ssl && 'ON' || 'OFF' }}
|
||||
- name: Build ${{ matrix.config.name }}
|
||||
run: cmake --build build --config Release -- /v:m /clp:ShowCommandLine
|
||||
- name: Run tests ${{ matrix.config.name }}
|
||||
if: ${{ matrix.config.run_tests }}
|
||||
shell: pwsh
|
||||
working-directory: build/test
|
||||
run: |
|
||||
$shards = 4
|
||||
$procs = @()
|
||||
for ($i = 0; $i -lt $shards; $i++) {
|
||||
$log = "shard_${i}.log"
|
||||
$procs += Start-Process -FilePath ./Release/httplib-test.exe `
|
||||
-ArgumentList "--gtest_color=yes","--gtest_filter=${{ github.event.inputs.gtest_filter || '*' }}" `
|
||||
-NoNewWindow -PassThru -RedirectStandardOutput $log -RedirectStandardError "${log}.err" `
|
||||
-Environment @{ GTEST_TOTAL_SHARDS="$shards"; GTEST_SHARD_INDEX="$i" }
|
||||
}
|
||||
$procs | Wait-Process
|
||||
$failed = $false
|
||||
for ($i = 0; $i -lt $shards; $i++) {
|
||||
$log = "shard_${i}.log"
|
||||
if (Select-String -Path $log -Pattern "\[ PASSED \]" -Quiet) {
|
||||
$passed = (Select-String -Path $log -Pattern "\[ PASSED \]").Line
|
||||
Write-Host "Shard ${i}: $passed"
|
||||
} else {
|
||||
Write-Host "=== Shard $i FAILED ==="
|
||||
Get-Content $log
|
||||
if (Test-Path "${log}.err") { Get-Content "${log}.err" }
|
||||
$failed = $true
|
||||
}
|
||||
}
|
||||
if ($failed) { exit 1 }
|
||||
Write-Host "All shards passed."
|
||||
|
||||
env:
|
||||
VCPKG_ROOT: "C:/vcpkg"
|
||||
VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
name: benchmark
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref || github.run_id }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
ubuntu:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: build and run
|
||||
run: cd test && make test_benchmark && ./test_benchmark
|
||||
|
||||
macos:
|
||||
runs-on: macos-latest
|
||||
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: build and run
|
||||
run: cd test && make test_benchmark && ./test_benchmark
|
||||
|
||||
windows:
|
||||
runs-on: windows-latest
|
||||
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
|
||||
steps:
|
||||
- name: Prepare Git for Checkout on Windows
|
||||
run: |
|
||||
git config --global core.autocrlf false
|
||||
git config --global core.eol lf
|
||||
- name: checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: Export GitHub Actions cache environment variables
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
|
||||
core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');
|
||||
- name: Cache vcpkg packages
|
||||
id: vcpkg-cache
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: C:/vcpkg/installed
|
||||
key: vcpkg-installed-windows-gtest
|
||||
- name: Install vcpkg dependencies
|
||||
if: steps.vcpkg-cache.outputs.cache-hit != 'true'
|
||||
run: vcpkg install gtest
|
||||
- name: Configure and build
|
||||
shell: pwsh
|
||||
run: |
|
||||
$cmake_content = @"
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(httplib-benchmark CXX)
|
||||
find_package(GTest REQUIRED)
|
||||
add_executable(httplib-benchmark test/test_benchmark.cc)
|
||||
target_include_directories(httplib-benchmark PRIVATE .)
|
||||
target_link_libraries(httplib-benchmark PRIVATE GTest::gtest_main)
|
||||
target_compile_options(httplib-benchmark PRIVATE "$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
|
||||
"@
|
||||
New-Item -ItemType Directory -Force -Path build_bench/test | Out-Null
|
||||
Set-Content -Path build_bench/CMakeLists.txt -Value $cmake_content
|
||||
Copy-Item -Path httplib.h -Destination build_bench/
|
||||
Copy-Item -Path test/test_benchmark.cc -Destination build_bench/test/
|
||||
cmake -B build_bench/build -S build_bench `
|
||||
-DCMAKE_TOOLCHAIN_FILE="$env:VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake"
|
||||
cmake --build build_bench/build --config Release
|
||||
- name: Run with retry
|
||||
run: ctest --output-on-failure --test-dir build_bench/build -C Release --repeat until-pass:5
|
||||
env:
|
||||
VCPKG_ROOT: "C:/vcpkg"
|
||||
VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
|
||||
@@ -0,0 +1,20 @@
|
||||
name: No Exceptions Test
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
test-no-exceptions:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y build-essential libssl-dev zlib1g-dev libcurl4-openssl-dev libbrotli-dev libzstd-dev
|
||||
|
||||
- name: Run tests with CPPHTTPLIB_NO_EXCEPTIONS
|
||||
run: |
|
||||
cd test && make test_split EXTRA_CXXFLAGS="-fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS" && make test_openssl_parallel EXTRA_CXXFLAGS="-fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS"
|
||||
@@ -0,0 +1,62 @@
|
||||
name: test_offline
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
test_linux:
|
||||
description: 'Test on Linux'
|
||||
type: boolean
|
||||
default: true
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref || github.run_id }}
|
||||
cancel-in-progress: true
|
||||
|
||||
env:
|
||||
GTEST_FILTER: "-*.*_Online"
|
||||
|
||||
jobs:
|
||||
ubuntu:
|
||||
runs-on: ubuntu-latest
|
||||
if: >
|
||||
(github.event_name == 'push') ||
|
||||
(github.event_name == 'pull_request' &&
|
||||
github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
|
||||
(github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true')
|
||||
strategy:
|
||||
matrix:
|
||||
tls_backend: [openssl, no-tls]
|
||||
name: ubuntu (${{ matrix.tls_backend }})
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: install common libraries
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y libcurl4-openssl-dev zlib1g-dev libbrotli-dev libzstd-dev
|
||||
- name: install OpenSSL
|
||||
if: matrix.tls_backend == 'openssl'
|
||||
run: sudo apt-get install -y libssl-dev
|
||||
- name: disable network
|
||||
run: |
|
||||
sudo iptables -A OUTPUT -o lo -j ACCEPT
|
||||
sudo iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
|
||||
sudo iptables -A OUTPUT -j REJECT
|
||||
sudo ip6tables -A OUTPUT -o lo -j ACCEPT
|
||||
sudo ip6tables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
|
||||
sudo ip6tables -A OUTPUT -j REJECT
|
||||
- name: build and run tests (OpenSSL)
|
||||
if: matrix.tls_backend == 'openssl'
|
||||
run: cd test && make test_split && make test_openssl_parallel
|
||||
env:
|
||||
LSAN_OPTIONS: suppressions=lsan_suppressions.txt
|
||||
- name: build and run tests (No TLS)
|
||||
if: matrix.tls_backend == 'no-tls'
|
||||
run: cd test && make test_no_tls_parallel
|
||||
- name: restore network
|
||||
if: always()
|
||||
run: |
|
||||
sudo iptables -F OUTPUT
|
||||
sudo ip6tables -F OUTPUT
|
||||
@@ -0,0 +1,37 @@
|
||||
name: Proxy Test
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
test-proxy:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
|
||||
strategy:
|
||||
matrix:
|
||||
tls_backend: [openssl, mbedtls]
|
||||
name: proxy (${{ matrix.tls_backend }})
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install common dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y build-essential zlib1g-dev libcurl4-openssl-dev libbrotli-dev libzstd-dev netcat-openbsd
|
||||
- name: Install OpenSSL
|
||||
if: matrix.tls_backend == 'openssl'
|
||||
run: sudo apt-get install -y libssl-dev
|
||||
- name: Install Mbed TLS
|
||||
if: matrix.tls_backend == 'mbedtls'
|
||||
run: sudo apt-get install -y libmbedtls-dev
|
||||
|
||||
- name: Run proxy tests (OpenSSL)
|
||||
if: matrix.tls_backend == 'openssl'
|
||||
run: cd test && make proxy
|
||||
env:
|
||||
COMPOSE_FILE: docker-compose.yml:docker-compose.ci.yml
|
||||
- name: Run proxy tests (Mbed TLS)
|
||||
if: matrix.tls_backend == 'mbedtls'
|
||||
run: cd test && make proxy_mbedtls
|
||||
env:
|
||||
COMPOSE_FILE: docker-compose.yml:docker-compose.ci.yml
|
||||
+50
-2
@@ -1,24 +1,70 @@
|
||||
tags
|
||||
AGENTS.md
|
||||
docs-src/pages/AGENTS.md
|
||||
plans/
|
||||
work/
|
||||
|
||||
# Ignore executables (no extension) but not source files
|
||||
example/server
|
||||
!example/server.*
|
||||
example/client
|
||||
!example/client.*
|
||||
example/hello
|
||||
!example/hello.*
|
||||
example/simplecli
|
||||
!example/simplecli.*
|
||||
example/simplesvr
|
||||
!example/simplesvr.*
|
||||
example/benchmark
|
||||
!example/benchmark.*
|
||||
example/redirect
|
||||
example/sse
|
||||
!example/redirect.*
|
||||
example/ssecli
|
||||
!example/ssecli.*
|
||||
example/ssecli-stream
|
||||
!example/ssecli-stream.*
|
||||
example/ssesvr
|
||||
!example/ssesvr.*
|
||||
example/upload
|
||||
!example/upload.*
|
||||
example/one_time_request
|
||||
!example/one_time_request.*
|
||||
example/server_and_client
|
||||
!example/server_and_client.*
|
||||
example/accept_header
|
||||
!example/accept_header.*
|
||||
example/wsecho
|
||||
!example/wsecho.*
|
||||
example/*.pem
|
||||
test/httplib.cc
|
||||
test/httplib.h
|
||||
test/test
|
||||
test/test_mbedtls
|
||||
test/test_wolfssl
|
||||
test/test_no_tls
|
||||
test/server_fuzzer
|
||||
test/test_proxy
|
||||
test/test_proxy_mbedtls
|
||||
test/test_proxy_wolfssl
|
||||
test/test_split
|
||||
test/test_split_mbedtls
|
||||
test/test_split_wolfssl
|
||||
test/test_split_no_tls
|
||||
test/test_websocket_heartbeat
|
||||
test/test_thread_pool
|
||||
test/test_benchmark
|
||||
test/test.xcodeproj/xcuser*
|
||||
test/test.xcodeproj/*/xcuser*
|
||||
test/*.o
|
||||
test/*.pem
|
||||
test/*.srl
|
||||
test/*.log
|
||||
test/_build_*
|
||||
benchmark/server*
|
||||
|
||||
*.swp
|
||||
|
||||
build/
|
||||
Debug
|
||||
Release
|
||||
*.vcxproj.user
|
||||
@@ -28,5 +74,7 @@ Release
|
||||
*.db
|
||||
ipch
|
||||
*.dSYM
|
||||
*.pyc
|
||||
.*
|
||||
!/.travis.yml
|
||||
!/.gitattributes
|
||||
!/.github
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/mirrors-clang-format
|
||||
rev: v18.1.8 # 最新バージョンを使用
|
||||
hooks:
|
||||
- id: clang-format
|
||||
files: \.(cpp|cc|h)$
|
||||
args: [-i] # インプレースで修正
|
||||
+305
-114
@@ -1,15 +1,32 @@
|
||||
#[[
|
||||
Build options:
|
||||
* Standard BUILD_SHARED_LIBS is supported and sets HTTPLIB_SHARED default value.
|
||||
* HTTPLIB_USE_OPENSSL_IF_AVAILABLE (default on)
|
||||
* HTTPLIB_USE_WOLFSSL_IF_AVAILABLE (default off)
|
||||
* HTTPLIB_USE_MBEDTLS_IF_AVAILABLE (default off)
|
||||
* HTTPLIB_USE_ZLIB_IF_AVAILABLE (default on)
|
||||
* HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on)
|
||||
* HTTPLIB_USE_ZSTD_IF_AVAILABLE (default on)
|
||||
* HTTPLIB_BUILD_MODULES (default off)
|
||||
* HTTPLIB_REQUIRE_OPENSSL (default off)
|
||||
* HTTPLIB_REQUIRE_WOLFSSL (default off)
|
||||
* HTTPLIB_REQUIRE_MBEDTLS (default off)
|
||||
* HTTPLIB_REQUIRE_ZLIB (default off)
|
||||
* HTTPLIB_REQUIRE_BROTLI (default off)
|
||||
* HTTPLIB_REQUIRE_ZSTD (default off)
|
||||
* HTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES (default off)
|
||||
* HTTPLIB_USE_NON_BLOCKING_GETADDRINFO (default on)
|
||||
* HTTPLIB_COMPILE (default off)
|
||||
* HTTPLIB_INSTALL (default on)
|
||||
* HTTPLIB_SHARED (default off) builds as a shared library (if HTTPLIB_COMPILE is ON)
|
||||
* HTTPLIB_TEST (default off)
|
||||
* BROTLI_USE_STATIC_LIBS - tells Cmake to use the static Brotli libs (only works if you have them installed).
|
||||
* OPENSSL_USE_STATIC_LIBS - tells Cmake to use the static OpenSSL libs (only works if you have them installed).
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
After installation with Cmake, a find_package(httplib) is available.
|
||||
This creates a httplib::httplib target (if found).
|
||||
After installation with Cmake, a find_package(httplib COMPONENTS OpenSSL wolfssl MbedTLS ZLIB Brotli zstd) is available.
|
||||
This creates a httplib::httplib target (if found and if listed components are supported).
|
||||
It can be linked like so:
|
||||
|
||||
target_link_libraries(your_exe httplib::httplib)
|
||||
@@ -35,11 +52,17 @@
|
||||
These variables are available after you run find_package(httplib)
|
||||
* HTTPLIB_HEADER_PATH - this is the full path to the installed header (e.g. /usr/include/httplib.h).
|
||||
* HTTPLIB_IS_USING_OPENSSL - a bool for if OpenSSL support is enabled.
|
||||
* HTTPLIB_IS_USING_WOLFSSL - a bool for if wolfSSL support is enabled.
|
||||
* HTTPLIB_IS_USING_MBEDTLS - a bool for if MbedTLS support is enabled.
|
||||
* HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled.
|
||||
* HTTPLIB_IS_USING_BROTLI - a bool for if Brotli support is enabled.
|
||||
* HTTPLIB_IS_USING_ZSTD - a bool for if ZSTD support is enabled.
|
||||
* HTTPLIB_IS_USING_MACOSX_AUTOMATIC_ROOT_CERTIFICATES - a bool for if support of loading system certs from the Apple Keychain is enabled.
|
||||
* HTTPLIB_IS_USING_NON_BLOCKING_GETADDRINFO - a bool for if nonblocking getaddrinfo is enabled.
|
||||
* HTTPLIB_IS_COMPILED - a bool for if the library is compiled, or otherwise header-only.
|
||||
* HTTPLIB_INCLUDE_DIR - the root path to httplib's header (e.g. /usr/include).
|
||||
* HTTPLIB_LIBRARY - the full path to the library if compiled (e.g. /usr/lib/libhttplib.so).
|
||||
* HTTPLIB_VERSION - the project's version string.
|
||||
* httplib_VERSION or HTTPLIB_VERSION - the project's version string.
|
||||
* HTTPLIB_FOUND - a bool for if the target was found.
|
||||
|
||||
Want to use precompiled headers (Cmake feature since v3.16)?
|
||||
@@ -49,65 +72,197 @@
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
FindPython3 requires Cmake v3.12
|
||||
ARCH_INDEPENDENT option of write_basic_package_version_file() requires Cmake v3.14
|
||||
]]
|
||||
cmake_minimum_required(VERSION 3.12.0 FATAL_ERROR)
|
||||
cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR)
|
||||
|
||||
# Gets the latest tag as a string like "v0.6.6"
|
||||
# Can silently fail if git isn't on the system
|
||||
execute_process(COMMAND git describe --tags --abbrev=0
|
||||
OUTPUT_VARIABLE _raw_version_string
|
||||
ERROR_VARIABLE _git_tag_error
|
||||
)
|
||||
# Get the CPPHTTPLIB_VERSION value and use it as a version
|
||||
# This gets the string with the CPPHTTPLIB_VERSION value from the header.
|
||||
# This is so the maintainer doesn't actually need to update this manually.
|
||||
file(STRINGS httplib.h _raw_version_string REGEX "CPPHTTPLIB_VERSION \"([0-9]+\\.[0-9]+\\.[0-9]+)\"")
|
||||
|
||||
# execute_process can fail silenty, so check for an error
|
||||
# if there was an error, just use the user agent as a version
|
||||
if(_git_tag_error)
|
||||
message(WARNING "cpp-httplib failed to find the latest git tag, falling back to using user agent as the version.")
|
||||
# Get the user agent and use it as a version
|
||||
# This gets the string with the user agent from the header.
|
||||
# This is so the maintainer doesn't actually need to update this manually.
|
||||
file(STRINGS httplib.h _raw_version_string REGEX "User\-Agent.*cpp\-httplib/([0-9]+\.?)+")
|
||||
endif()
|
||||
# Needed since git tags have "v" prefixing them.
|
||||
# Also used if the fallback to user agent string is being used.
|
||||
# Extracts just the version string itself from the whole string contained in _raw_version_string
|
||||
# since _raw_version_string would contain the entire line of code where it found the version string
|
||||
string(REGEX MATCH "([0-9]+\\.?)+" _httplib_version "${_raw_version_string}")
|
||||
|
||||
project(httplib VERSION ${_httplib_version} LANGUAGES CXX)
|
||||
project(httplib
|
||||
VERSION ${_httplib_version}
|
||||
LANGUAGES CXX
|
||||
DESCRIPTION "A C++ header-only HTTP/HTTPS server and client library."
|
||||
HOMEPAGE_URL "https://github.com/yhirose/cpp-httplib"
|
||||
)
|
||||
|
||||
# Change as needed to set an OpenSSL minimum version.
|
||||
# This is used in the installed Cmake config file.
|
||||
set(_HTTPLIB_OPENSSL_MIN_VER "1.1.1")
|
||||
set(_HTTPLIB_OPENSSL_MIN_VER "3.0.0")
|
||||
|
||||
# Lets you disable C++ exception during CMake configure time.
|
||||
# The value is used in the install CMake config file.
|
||||
option(HTTPLIB_NO_EXCEPTIONS "Disable the use of C++ exceptions" OFF)
|
||||
# Allow for a build to require OpenSSL to pass, instead of just being optional
|
||||
option(HTTPLIB_REQUIRE_OPENSSL "Requires OpenSSL to be found & linked, or fails build." OFF)
|
||||
option(HTTPLIB_REQUIRE_WOLFSSL "Requires wolfSSL to be found & linked, or fails build." OFF)
|
||||
option(HTTPLIB_REQUIRE_MBEDTLS "Requires MbedTLS to be found & linked, or fails build." OFF)
|
||||
option(HTTPLIB_REQUIRE_ZLIB "Requires ZLIB to be found & linked, or fails build." OFF)
|
||||
# Allow for a build to casually enable OpenSSL/ZLIB support, but silenty continue if not found.
|
||||
# Allow for a build to casually enable OpenSSL/ZLIB support, but silently continue if not found.
|
||||
# Make these options so their automatic use can be specifically disabled (as needed)
|
||||
option(HTTPLIB_USE_OPENSSL_IF_AVAILABLE "Uses OpenSSL (if available) to enable HTTPS support." ON)
|
||||
option(HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable compression support." ON)
|
||||
option(HTTPLIB_USE_WOLFSSL_IF_AVAILABLE "Uses wolfSSL (if available) to enable HTTPS support." OFF)
|
||||
option(HTTPLIB_USE_MBEDTLS_IF_AVAILABLE "Uses MbedTLS (if available) to enable HTTPS support." OFF)
|
||||
option(HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable Zlib compression support." ON)
|
||||
# Lets you compile the program as a regular library instead of header-only
|
||||
option(HTTPLIB_COMPILE "If ON, uses a Python script to split the header into a compilable header & source file (requires Python v3)." OFF)
|
||||
# Defaults to static library
|
||||
option(BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF)
|
||||
if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE)
|
||||
# Necessary for Windows if building shared libs
|
||||
# See https://stackoverflow.com/a/40743080
|
||||
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
|
||||
# Lets you disable the installation (useful when fetched from another CMake project)
|
||||
option(HTTPLIB_INSTALL "Enables the installation target" ON)
|
||||
option(HTTPLIB_TEST "Enables testing and builds tests" OFF)
|
||||
option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF)
|
||||
option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON)
|
||||
option(HTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES "Disable loading system certs from the Apple Keychain on macOS." OFF)
|
||||
option(HTTPLIB_USE_NON_BLOCKING_GETADDRINFO "Enables the non-blocking alternatives for getaddrinfo." ON)
|
||||
option(HTTPLIB_REQUIRE_ZSTD "Requires ZSTD to be found & linked, or fails build." OFF)
|
||||
option(HTTPLIB_USE_ZSTD_IF_AVAILABLE "Uses ZSTD (if available) to enable zstd support." ON)
|
||||
# C++20 modules support requires CMake 3.28 or later
|
||||
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.28")
|
||||
option(HTTPLIB_BUILD_MODULES "Build httplib modules (requires HTTPLIB_COMPILE to be ON)." OFF)
|
||||
else()
|
||||
set(HTTPLIB_BUILD_MODULES OFF CACHE INTERNAL "Build httplib modules disabled (requires CMake 3.28+)" FORCE)
|
||||
if(DEFINED CACHE{HTTPLIB_BUILD_MODULES} AND HTTPLIB_BUILD_MODULES)
|
||||
message(WARNING "HTTPLIB_BUILD_MODULES requires CMake 3.28 or later. Current version is ${CMAKE_VERSION}. Modules support has been disabled.")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Incompatibility between TLS libraries
|
||||
set(TLS_LIBRARIES_USED_TMP 0)
|
||||
|
||||
foreach(tls_library OPENSSL WOLFSSL MBEDTLS)
|
||||
set(TLS_REQUIRED ${HTTPLIB_REQUIRE_${tls_library}})
|
||||
set(TLS_IF_AVAILABLE ${HTTPLIB_USE_${tls_library}_IF_AVAILABLE})
|
||||
|
||||
if(TLS_REQUIRED OR TLS_IF_AVAILABLE)
|
||||
math(EXPR TLS_LIBRARIES_USED_TMP "${TLS_LIBRARIES_USED_TMP} + 1")
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
if(TLS_LIBRARIES_USED_TMP GREATER 1)
|
||||
message(FATAL_ERROR "TLS libraries are mutually exclusive.")
|
||||
endif()
|
||||
|
||||
# Defaults to static library but respects standard BUILD_SHARED_LIBS if set
|
||||
include(CMakeDependentOption)
|
||||
cmake_dependent_option(HTTPLIB_SHARED "Build the library as a shared library instead of static. Has no effect if using header-only."
|
||||
"${BUILD_SHARED_LIBS}" HTTPLIB_COMPILE OFF
|
||||
)
|
||||
if(HTTPLIB_SHARED)
|
||||
set(HTTPLIB_LIB_TYPE SHARED)
|
||||
if(WIN32)
|
||||
# Necessary for Windows if building shared libs
|
||||
# See https://stackoverflow.com/a/40743080
|
||||
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
|
||||
endif()
|
||||
else()
|
||||
set(HTTPLIB_LIB_TYPE STATIC)
|
||||
endif()
|
||||
|
||||
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
|
||||
if(CMAKE_SYSTEM_VERSION)
|
||||
if(${CMAKE_SYSTEM_VERSION} VERSION_LESS "10.0.0")
|
||||
message(WARNING "Windows ${CMAKE_SYSTEM_VERSION} or lower is not supported. Please use Windows 10 or later.")
|
||||
endif()
|
||||
else()
|
||||
set(CMAKE_SYSTEM_VERSION "10.0.19041.0")
|
||||
message(WARNING "The target is Windows but CMAKE_SYSTEM_VERSION is not set, the default system version is set to Windows 10.")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Set some variables that are used in-tree and while building based on our options
|
||||
set(HTTPLIB_IS_COMPILED ${HTTPLIB_COMPILE})
|
||||
set(HTTPLIB_IS_USING_MACOSX_AUTOMATIC_ROOT_CERTIFICATES TRUE)
|
||||
if(HTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES)
|
||||
set(HTTPLIB_IS_USING_MACOSX_AUTOMATIC_ROOT_CERTIFICATES FALSE)
|
||||
endif()
|
||||
set(HTTPLIB_IS_USING_NON_BLOCKING_GETADDRINFO ${HTTPLIB_USE_NON_BLOCKING_GETADDRINFO})
|
||||
|
||||
# Threads needed for <thread> on some systems, and for <pthread.h> on Linux
|
||||
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
|
||||
find_package(Threads REQUIRED)
|
||||
# Since Cmake v3.11, Crypto & SSL became optional when not specified as COMPONENTS.
|
||||
if(HTTPLIB_REQUIRE_OPENSSL)
|
||||
find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} COMPONENTS Crypto SSL REQUIRED)
|
||||
set(HTTPLIB_IS_USING_OPENSSL TRUE)
|
||||
elseif(HTTPLIB_USE_OPENSSL_IF_AVAILABLE)
|
||||
find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} COMPONENTS Crypto SSL QUIET)
|
||||
# Avoid a rare circumstance of not finding all components but the end-user did their
|
||||
# own call for OpenSSL, which might trick us into thinking we'd otherwise have what we wanted
|
||||
if (TARGET OpenSSL::SSL AND TARGET OpenSSL::Crypto)
|
||||
set(HTTPLIB_IS_USING_OPENSSL ${OPENSSL_FOUND})
|
||||
else()
|
||||
set(HTTPLIB_IS_USING_OPENSSL FALSE)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(HTTPLIB_REQUIRE_WOLFSSL)
|
||||
find_package(wolfssl REQUIRED)
|
||||
set(HTTPLIB_IS_USING_WOLFSSL TRUE)
|
||||
elseif(HTTPLIB_USE_WOLFSSL_IF_AVAILABLE)
|
||||
find_package(wolfssl QUIET)
|
||||
set(HTTPLIB_IS_USING_WOLFSSL ${wolfssl_FOUND})
|
||||
endif()
|
||||
|
||||
if(HTTPLIB_REQUIRE_MBEDTLS)
|
||||
find_package(MbedTLS REQUIRED)
|
||||
set(HTTPLIB_IS_USING_MBEDTLS TRUE)
|
||||
elseif(HTTPLIB_USE_MBEDTLS_IF_AVAILABLE)
|
||||
find_package(MbedTLS QUIET)
|
||||
set(HTTPLIB_IS_USING_MBEDTLS ${MbedTLS_FOUND})
|
||||
endif()
|
||||
|
||||
if(HTTPLIB_REQUIRE_ZLIB)
|
||||
find_package(ZLIB REQUIRED)
|
||||
set(HTTPLIB_IS_USING_ZLIB TRUE)
|
||||
elseif(HTTPLIB_USE_ZLIB_IF_AVAILABLE)
|
||||
find_package(ZLIB QUIET)
|
||||
# FindZLIB doesn't have a ZLIB_FOUND variable, so check the target.
|
||||
if(TARGET ZLIB::ZLIB)
|
||||
set(HTTPLIB_IS_USING_ZLIB TRUE)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Adds our cmake folder to the search path for find_package
|
||||
# This is so we can use our custom FindBrotli.cmake
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
||||
if(HTTPLIB_REQUIRE_BROTLI)
|
||||
find_package(Brotli COMPONENTS encoder decoder common REQUIRED)
|
||||
set(HTTPLIB_IS_USING_BROTLI TRUE)
|
||||
elseif(HTTPLIB_USE_BROTLI_IF_AVAILABLE)
|
||||
find_package(Brotli COMPONENTS encoder decoder common QUIET)
|
||||
set(HTTPLIB_IS_USING_BROTLI ${Brotli_FOUND})
|
||||
endif()
|
||||
|
||||
# NOTE:
|
||||
# zstd < 1.5.6 does not provide the CMake imported target `zstd::libzstd`.
|
||||
# Older versions must be consumed via their pkg-config file.
|
||||
if(HTTPLIB_REQUIRE_ZSTD)
|
||||
find_package(zstd 1.5.6 CONFIG)
|
||||
if(NOT zstd_FOUND)
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_check_modules(zstd REQUIRED IMPORTED_TARGET libzstd)
|
||||
add_library(zstd::libzstd ALIAS PkgConfig::zstd)
|
||||
endif()
|
||||
set(HTTPLIB_IS_USING_ZSTD TRUE)
|
||||
elseif(HTTPLIB_USE_ZSTD_IF_AVAILABLE)
|
||||
find_package(zstd 1.5.6 CONFIG QUIET)
|
||||
if(NOT zstd_FOUND)
|
||||
find_package(PkgConfig QUIET)
|
||||
if(PKG_CONFIG_FOUND)
|
||||
pkg_check_modules(zstd QUIET IMPORTED_TARGET libzstd)
|
||||
|
||||
if(TARGET PkgConfig::zstd)
|
||||
add_library(zstd::libzstd ALIAS PkgConfig::zstd)
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
# Both find_package and PkgConf set a XXX_FOUND var
|
||||
set(HTTPLIB_IS_USING_ZSTD ${zstd_FOUND})
|
||||
endif()
|
||||
|
||||
# Used for default, common dirs that the end-user can change (if needed)
|
||||
@@ -135,18 +290,46 @@ if(HTTPLIB_COMPILE)
|
||||
ERROR_VARIABLE _httplib_split_error
|
||||
)
|
||||
if(_httplib_split_error)
|
||||
message(FATAL_ERROR "Failed when trying to split Cpp-httplib with the Python script.\n${_httplib_split_error}")
|
||||
message(FATAL_ERROR "Failed when trying to split cpp-httplib with the Python script.\n${_httplib_split_error}")
|
||||
endif()
|
||||
|
||||
# If building modules, also generate the module file
|
||||
if(HTTPLIB_BUILD_MODULES)
|
||||
# Put the generate_module script into the build dir
|
||||
configure_file(generate_module.py "${CMAKE_CURRENT_BINARY_DIR}/generate_module.py"
|
||||
COPYONLY
|
||||
)
|
||||
# Generate the module file
|
||||
execute_process(COMMAND ${Python3_EXECUTABLE} "${CMAKE_CURRENT_BINARY_DIR}/generate_module.py"
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||
ERROR_VARIABLE _httplib_module_error
|
||||
)
|
||||
if(_httplib_module_error)
|
||||
message(FATAL_ERROR "Failed when trying to generate cpp-httplib module with the Python script.\n${_httplib_module_error}")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# split.py puts output in "out"
|
||||
set(_httplib_build_includedir "${CMAKE_CURRENT_BINARY_DIR}/out")
|
||||
# This will automatically be either static or shared based on the value of BUILD_SHARED_LIBS
|
||||
add_library(${PROJECT_NAME} "${_httplib_build_includedir}/httplib.cc")
|
||||
add_library(${PROJECT_NAME} ${HTTPLIB_LIB_TYPE} "${_httplib_build_includedir}/httplib.cc")
|
||||
target_sources(${PROJECT_NAME}
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${_httplib_build_includedir}/httplib.h>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/httplib.h>
|
||||
)
|
||||
|
||||
# Add C++20 module support if requested
|
||||
# Include from separate file to prevent parse errors on older CMake versions
|
||||
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.28")
|
||||
include(cmake/modules.cmake)
|
||||
endif()
|
||||
|
||||
set_target_properties(${PROJECT_NAME}
|
||||
PROPERTIES
|
||||
VERSION ${${PROJECT_NAME}_VERSION}
|
||||
SOVERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}"
|
||||
OUTPUT_NAME cpp-httplib
|
||||
)
|
||||
else()
|
||||
# This is for header-only.
|
||||
set(_INTERFACE_OR_PUBLIC INTERFACE)
|
||||
@@ -157,117 +340,125 @@ endif()
|
||||
# Only useful if building in-tree, versus using it from an installation.
|
||||
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
|
||||
|
||||
# Might be missing some, but this list is somewhat comprehensive
|
||||
target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
|
||||
cxx_std_11
|
||||
cxx_nullptr
|
||||
cxx_lambdas
|
||||
cxx_override
|
||||
cxx_defaulted_functions
|
||||
cxx_attribute_deprecated
|
||||
cxx_auto_type
|
||||
cxx_decltype
|
||||
cxx_deleted_functions
|
||||
cxx_range_for
|
||||
cxx_sizeof_member
|
||||
# Require C++11, or C++20 if modules are enabled
|
||||
if(HTTPLIB_BUILD_MODULES)
|
||||
target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} cxx_std_20)
|
||||
else()
|
||||
target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} cxx_std_11)
|
||||
endif()
|
||||
|
||||
target_include_directories(${PROJECT_NAME} SYSTEM ${_INTERFACE_OR_PUBLIC}
|
||||
$<BUILD_INTERFACE:${_httplib_build_includedir}>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
|
||||
$<BUILD_INTERFACE:${_httplib_build_includedir}>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
|
||||
# Always require threads
|
||||
target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
|
||||
# Always require threads
|
||||
Threads::Threads
|
||||
# Needed for Windows libs on Mingw, as the pragma comment(lib, "xyz") aren't triggered.
|
||||
$<$<PLATFORM_ID:Windows>:ws2_32>
|
||||
$<$<PLATFORM_ID:Windows>:crypt32>
|
||||
# Needed for API from MacOS Security framework
|
||||
"$<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>,$<BOOL:${HTTPLIB_IS_USING_MACOSX_AUTOMATIC_ROOT_CERTIFICATES}>>:-framework CFNetwork -framework CoreFoundation -framework Security>"
|
||||
# Needed for non-blocking getaddrinfo on MacOS
|
||||
"$<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_USE_NON_BLOCKING_GETADDRINFO}>>:-framework CFNetwork -framework CoreFoundation>"
|
||||
# Can't put multiple targets in a single generator expression or it bugs out.
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::common>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::encoder>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::decoder>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:ZLIB::ZLIB>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_ZSTD}>:zstd::libzstd>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:OpenSSL::SSL>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:OpenSSL::Crypto>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_WOLFSSL}>:wolfssl::wolfssl>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_MBEDTLS}>:MbedTLS::mbedtls>
|
||||
)
|
||||
|
||||
# We check for the target when using IF_AVAILABLE since it's possible we didn't find it.
|
||||
if(HTTPLIB_USE_OPENSSL_IF_AVAILABLE AND TARGET OpenSSL::SSL AND TARGET OpenSSL::Crypto OR HTTPLIB_REQUIRE_OPENSSL)
|
||||
target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
|
||||
OpenSSL::SSL OpenSSL::Crypto
|
||||
)
|
||||
target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
|
||||
CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
)
|
||||
set(HTTPLIB_IS_USING_OPENSSL TRUE)
|
||||
else()
|
||||
set(HTTPLIB_IS_USING_OPENSSL FALSE)
|
||||
endif()
|
||||
# Set the definitions to enable optional features
|
||||
target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
|
||||
$<$<BOOL:${HTTPLIB_NO_EXCEPTIONS}>:CPPHTTPLIB_NO_EXCEPTIONS>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:CPPHTTPLIB_BROTLI_SUPPORT>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:CPPHTTPLIB_ZLIB_SUPPORT>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_ZSTD}>:CPPHTTPLIB_ZSTD_SUPPORT>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:CPPHTTPLIB_OPENSSL_SUPPORT>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_WOLFSSL}>:CPPHTTPLIB_WOLFSSL_SUPPORT>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_MBEDTLS}>:CPPHTTPLIB_MBEDTLS_SUPPORT>
|
||||
$<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES}>>:CPPHTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES>
|
||||
$<$<BOOL:${HTTPLIB_USE_NON_BLOCKING_GETADDRINFO}>:CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO>
|
||||
)
|
||||
|
||||
# We check for the target when using IF_AVAILABLE since it's possible we didn't find it.
|
||||
if(HTTPLIB_USE_ZLIB_IF_AVAILABLE AND TARGET ZLIB::ZLIB OR HTTPLIB_REQUIRE_ZLIB)
|
||||
target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
|
||||
ZLIB::ZLIB
|
||||
)
|
||||
target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
|
||||
CPPHTTPLIB_ZLIB_SUPPORT
|
||||
)
|
||||
set(HTTPLIB_IS_USING_ZLIB TRUE)
|
||||
else()
|
||||
set(HTTPLIB_IS_USING_ZLIB FALSE)
|
||||
endif()
|
||||
|
||||
# Cmake's find_package search path is different based on the system
|
||||
# See https://cmake.org/cmake/help/latest/command/find_package.html for the list
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
|
||||
set(_TARGET_INSTALL_CMAKEDIR "${CMAKE_INSTALL_PREFIX}/cmake/${PROJECT_NAME}")
|
||||
else()
|
||||
# On Non-Windows, it should be /usr/lib/cmake/<name>/<name>Config.cmake
|
||||
# NOTE: This may or may not work for macOS...
|
||||
set(_TARGET_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
|
||||
endif()
|
||||
# CMake configuration files installation directory
|
||||
set(_TARGET_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
|
||||
|
||||
include(CMakePackageConfigHelpers)
|
||||
|
||||
# Configures the meta-file httplibConfig.cmake.in to replace variables with paths/values/etc.
|
||||
configure_package_config_file("${PROJECT_NAME}Config.cmake.in"
|
||||
configure_package_config_file("cmake/${PROJECT_NAME}Config.cmake.in"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
|
||||
INSTALL_DESTINATION "${_TARGET_INSTALL_CMAKEDIR}"
|
||||
# Passes the includedir install path
|
||||
PATH_VARS CMAKE_INSTALL_FULL_INCLUDEDIR
|
||||
# There aren't any components, so don't use the macro
|
||||
NO_CHECK_REQUIRED_COMPONENTS_MACRO
|
||||
)
|
||||
|
||||
if(HTTPLIB_COMPILE)
|
||||
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
|
||||
# Example: if you find_package(httplib 0.5.4)
|
||||
# then anything >= 0.5 and <= 1.0 is accepted
|
||||
COMPATIBILITY SameMajorVersion
|
||||
# then anything >= 0.5.4 and < 0.6 is accepted
|
||||
COMPATIBILITY SameMinorVersion
|
||||
)
|
||||
else()
|
||||
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
|
||||
# Example: if you find_package(httplib 0.5.4)
|
||||
# then anything >= 0.5 and <= 1.0 is accepted
|
||||
COMPATIBILITY SameMajorVersion
|
||||
# then anything >= 0.5.4 and < 0.6 is accepted
|
||||
COMPATIBILITY SameMinorVersion
|
||||
# Tells Cmake that it's a header-only lib
|
||||
# Mildly useful for end-users :)
|
||||
ARCH_INDEPENDENT
|
||||
)
|
||||
endif()
|
||||
|
||||
# Creates the export httplibTargets.cmake
|
||||
# This is strictly what holds compilation requirements
|
||||
# and linkage information (doesn't find deps though).
|
||||
install(TARGETS ${PROJECT_NAME}
|
||||
EXPORT httplibTargets
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
)
|
||||
if(HTTPLIB_INSTALL)
|
||||
# Creates the export httplibTargets.cmake
|
||||
# This is strictly what holds compilation requirements
|
||||
# and linkage information (doesn't find deps though).
|
||||
if(HTTPLIB_BUILD_MODULES)
|
||||
install(TARGETS ${PROJECT_NAME} EXPORT httplibTargets FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/httplib/modules CXX_MODULES_BMI DESTINATION ${CMAKE_INSTALL_LIBDIR}/httplib/modules)
|
||||
else()
|
||||
install(TARGETS ${PROJECT_NAME} EXPORT httplibTargets)
|
||||
endif()
|
||||
|
||||
install(FILES "${_httplib_build_includedir}/httplib.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||
install(FILES "${_httplib_build_includedir}/httplib.h" TYPE INCLUDE)
|
||||
|
||||
install(FILES
|
||||
install(FILES
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
|
||||
DESTINATION ${_TARGET_INSTALL_CMAKEDIR}
|
||||
)
|
||||
# Install it so it can be used later by the httplibConfig.cmake file.
|
||||
# Put it in the same dir as our config file instead of a global path so we don't potentially stomp on other packages.
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindBrotli.cmake"
|
||||
DESTINATION ${_TARGET_INSTALL_CMAKEDIR}
|
||||
)
|
||||
|
||||
# NOTE: This path changes depending on if it's on Windows or Linux
|
||||
install(EXPORT httplibTargets
|
||||
# Puts the targets into the httplib namespace
|
||||
# So this makes httplib::httplib linkable after doing find_package(httplib)
|
||||
NAMESPACE ${PROJECT_NAME}::
|
||||
DESTINATION ${_TARGET_INSTALL_CMAKEDIR}
|
||||
)
|
||||
# NOTE: This path changes depending on if it's on Windows or Linux
|
||||
install(EXPORT httplibTargets
|
||||
# Puts the targets into the httplib namespace
|
||||
# So this makes httplib::httplib linkable after doing find_package(httplib)
|
||||
NAMESPACE ${PROJECT_NAME}::
|
||||
DESTINATION ${_TARGET_INSTALL_CMAKEDIR}
|
||||
)
|
||||
|
||||
# Install documentation & license
|
||||
# ex: /usr/share/doc/httplib/README.md and /usr/share/licenses/httplib/LICENSE
|
||||
install(FILES "README.md" DESTINATION "${CMAKE_INSTALL_DOCDIR}")
|
||||
install(FILES "LICENSE" DESTINATION "${CMAKE_INSTALL_DATADIR}/licenses/${PROJECT_NAME}")
|
||||
|
||||
include(CPack)
|
||||
endif()
|
||||
|
||||
if(HTTPLIB_BUILD_MODULES AND NOT HTTPLIB_COMPILE)
|
||||
message(FATAL_ERROR "HTTPLIB_BUILD_MODULES requires HTTPLIB_COMPILE to be ON.")
|
||||
endif()
|
||||
|
||||
if(HTTPLIB_TEST)
|
||||
include(CTest)
|
||||
add_subdirectory(test)
|
||||
endif()
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
FROM yhirose4dockerhub/ubuntu-builder AS builder
|
||||
WORKDIR /build
|
||||
COPY httplib.h .
|
||||
COPY docker/main.cc .
|
||||
RUN g++ -std=c++23 -static -o server -O2 -I. main.cc && strip server
|
||||
|
||||
FROM scratch
|
||||
COPY --from=builder /build/server /server
|
||||
COPY docker/html/index.html /html/index.html
|
||||
EXPOSE 80
|
||||
|
||||
ENTRYPOINT ["/server"]
|
||||
CMD ["--host", "0.0.0.0", "--port", "80", "--mount", "/:./html"]
|
||||
+204
@@ -0,0 +1,204 @@
|
||||
# SSEClient - Server-Sent Events Client
|
||||
|
||||
A simple, EventSource-like SSE client for C++11.
|
||||
|
||||
## Features
|
||||
|
||||
- **Auto-reconnect**: Automatically reconnects on connection loss
|
||||
- **Last-Event-ID**: Sends last received ID on reconnect for resumption
|
||||
- **retry field**: Respects server's reconnect interval
|
||||
- **Event types**: Supports custom event types via `on_event()`
|
||||
- **Async support**: Run in background thread with `start_async()`
|
||||
- **C++11 compatible**: No C++14/17/20 features required
|
||||
|
||||
## Quick Start
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
httplib::sse::SSEClient sse(cli, "/events");
|
||||
|
||||
sse.on_message([](const httplib::sse::SSEMessage &msg) {
|
||||
std::cout << "Event: " << msg.event << std::endl;
|
||||
std::cout << "Data: " << msg.data << std::endl;
|
||||
});
|
||||
|
||||
sse.start(); // Blocking, with auto-reconnect
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### SSEMessage
|
||||
|
||||
```cpp
|
||||
struct SSEMessage {
|
||||
std::string event; // Event type (default: "message")
|
||||
std::string data; // Event payload
|
||||
std::string id; // Event ID
|
||||
};
|
||||
```
|
||||
|
||||
### SSEClient
|
||||
|
||||
#### Constructor
|
||||
|
||||
```cpp
|
||||
// Basic
|
||||
SSEClient(Client &client, const std::string &path);
|
||||
|
||||
// With custom headers
|
||||
SSEClient(Client &client, const std::string &path, const Headers &headers);
|
||||
```
|
||||
|
||||
#### Event Handlers
|
||||
|
||||
```cpp
|
||||
// Called for all events (or events without a specific handler)
|
||||
sse.on_message([](const SSEMessage &msg) { });
|
||||
|
||||
// Called for specific event types
|
||||
sse.on_event("update", [](const SSEMessage &msg) { });
|
||||
sse.on_event("delete", [](const SSEMessage &msg) { });
|
||||
|
||||
// Called when connection is established
|
||||
sse.on_open([]() { });
|
||||
|
||||
// Called on connection errors
|
||||
sse.on_error([](httplib::Error err) { });
|
||||
```
|
||||
|
||||
#### Configuration
|
||||
|
||||
```cpp
|
||||
// Set reconnect interval (default: 3000ms)
|
||||
sse.set_reconnect_interval(5000);
|
||||
|
||||
// Set max reconnect attempts (default: 0 = unlimited)
|
||||
sse.set_max_reconnect_attempts(10);
|
||||
|
||||
// Update headers at any time (thread-safe)
|
||||
sse.set_headers({{"Authorization", "Bearer new_token"}});
|
||||
```
|
||||
|
||||
#### Control
|
||||
|
||||
```cpp
|
||||
// Blocking start with auto-reconnect
|
||||
sse.start();
|
||||
|
||||
// Non-blocking start (runs in background thread)
|
||||
sse.start_async();
|
||||
|
||||
// Stop the client (thread-safe)
|
||||
sse.stop();
|
||||
```
|
||||
|
||||
#### State
|
||||
|
||||
```cpp
|
||||
bool connected = sse.is_connected();
|
||||
const std::string &id = sse.last_event_id();
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
httplib::sse::SSEClient sse(cli, "/events");
|
||||
|
||||
sse.on_message([](const httplib::sse::SSEMessage &msg) {
|
||||
std::cout << msg.data << std::endl;
|
||||
});
|
||||
|
||||
sse.start();
|
||||
```
|
||||
|
||||
### With Custom Event Types
|
||||
|
||||
```cpp
|
||||
httplib::sse::SSEClient sse(cli, "/events");
|
||||
|
||||
sse.on_event("notification", [](const httplib::sse::SSEMessage &msg) {
|
||||
std::cout << "Notification: " << msg.data << std::endl;
|
||||
});
|
||||
|
||||
sse.on_event("update", [](const httplib::sse::SSEMessage &msg) {
|
||||
std::cout << "Update: " << msg.data << std::endl;
|
||||
});
|
||||
|
||||
sse.start();
|
||||
```
|
||||
|
||||
### Async with Stop
|
||||
|
||||
```cpp
|
||||
httplib::sse::SSEClient sse(cli, "/events");
|
||||
|
||||
sse.on_message([](const httplib::sse::SSEMessage &msg) {
|
||||
std::cout << msg.data << std::endl;
|
||||
});
|
||||
|
||||
sse.start_async(); // Returns immediately
|
||||
|
||||
// ... do other work ...
|
||||
|
||||
sse.stop(); // Stop when done
|
||||
```
|
||||
|
||||
### With Custom Headers (e.g., Authentication)
|
||||
|
||||
```cpp
|
||||
httplib::Headers headers = {
|
||||
{"Authorization", "Bearer token123"}
|
||||
};
|
||||
|
||||
httplib::sse::SSEClient sse(cli, "/events", headers);
|
||||
sse.start();
|
||||
```
|
||||
|
||||
### Refreshing Auth Token on Reconnect
|
||||
|
||||
```cpp
|
||||
httplib::sse::SSEClient sse(cli, "/events",
|
||||
{{"Authorization", "Bearer " + get_token()}});
|
||||
|
||||
// Preemptively refresh token on each successful connection
|
||||
sse.on_open([&sse]() {
|
||||
sse.set_headers({{"Authorization", "Bearer " + get_token()}});
|
||||
});
|
||||
|
||||
// Or reactively refresh on auth failure (401 triggers reconnect)
|
||||
sse.on_error([&sse](httplib::Error) {
|
||||
sse.set_headers({{"Authorization", "Bearer " + refresh_token()}});
|
||||
});
|
||||
|
||||
sse.start();
|
||||
```
|
||||
|
||||
### Error Handling
|
||||
|
||||
```cpp
|
||||
sse.on_error([](httplib::Error err) {
|
||||
std::cerr << "Error: " << httplib::to_string(err) << std::endl;
|
||||
});
|
||||
|
||||
sse.set_reconnect_interval(1000);
|
||||
sse.set_max_reconnect_attempts(5);
|
||||
|
||||
sse.start();
|
||||
```
|
||||
|
||||
## SSE Protocol
|
||||
|
||||
The client parses SSE format according to the [W3C specification](https://html.spec.whatwg.org/multipage/server-sent-events.html):
|
||||
|
||||
```
|
||||
event: custom-type
|
||||
id: 123
|
||||
data: {"message": "hello"}
|
||||
|
||||
data: simple message
|
||||
|
||||
: this is a comment (ignored)
|
||||
```
|
||||
@@ -0,0 +1,317 @@
|
||||
# cpp-httplib Streaming API
|
||||
|
||||
This document describes the streaming extensions for cpp-httplib, providing an iterator-style API for handling HTTP responses incrementally with **true socket-level streaming**.
|
||||
|
||||
> **Important Notes**:
|
||||
>
|
||||
> - **No Keep-Alive**: Each `stream::Get()` call uses a dedicated connection that is closed after the response is fully read. For connection reuse, use `Client::Get()`.
|
||||
> - **Single iteration only**: The `next()` method can only iterate through the body once.
|
||||
> - **Result is not thread-safe**: While `stream::Get()` can be called from multiple threads simultaneously, the returned `stream::Result` must be used from a single thread only.
|
||||
|
||||
## Overview
|
||||
|
||||
The streaming API allows you to process HTTP response bodies chunk by chunk using an iterator-style pattern. Data is read directly from the network socket, enabling low-memory processing of large responses. This is particularly useful for:
|
||||
|
||||
- **LLM/AI streaming responses** (e.g., ChatGPT, Claude, Ollama)
|
||||
- **Server-Sent Events (SSE)**
|
||||
- **Large file downloads** with progress tracking
|
||||
- **Reverse proxy implementations**
|
||||
|
||||
## Quick Start
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
|
||||
// Get streaming response
|
||||
auto result = httplib::stream::Get(cli, "/stream");
|
||||
|
||||
if (result) {
|
||||
// Process response body in chunks
|
||||
while (result.next()) {
|
||||
std::cout.write(result.data(), result.size());
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
## API Layers
|
||||
|
||||
cpp-httplib provides multiple API layers for different use cases:
|
||||
|
||||
```text
|
||||
┌─────────────────────────────────────────────┐
|
||||
│ SSEClient │ ← SSE-specific, parsed events
|
||||
│ - on_message(), on_event() │
|
||||
│ - Auto-reconnect, Last-Event-ID │
|
||||
├─────────────────────────────────────────────┤
|
||||
│ stream::Get() / stream::Result │ ← Iterator-based streaming
|
||||
│ - while (result.next()) { ... } │
|
||||
├─────────────────────────────────────────────┤
|
||||
│ open_stream() / StreamHandle │ ← General-purpose streaming
|
||||
│ - handle.read(buf, len) │
|
||||
├─────────────────────────────────────────────┤
|
||||
│ Client::Get() │ ← Traditional, full buffering
|
||||
└─────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
| Use Case | Recommended API |
|
||||
|----------|----------------|
|
||||
| SSE with auto-reconnect | `SSEClient` (see [README-sse.md](README-sse.md)) |
|
||||
| LLM streaming (JSON Lines) | `stream::Get()` |
|
||||
| Large file download | `stream::Get()` or `open_stream()` |
|
||||
| Reverse proxy | `open_stream()` |
|
||||
| Small responses with Keep-Alive | `Client::Get()` |
|
||||
|
||||
## API Reference
|
||||
|
||||
### Low-Level API: `StreamHandle`
|
||||
|
||||
The `StreamHandle` struct provides direct control over streaming responses. It takes ownership of the socket connection and reads data directly from the network.
|
||||
|
||||
> **Note:** When using `open_stream()`, the connection is dedicated to streaming and **Keep-Alive is not supported**. For Keep-Alive connections, use `client.Get()` instead.
|
||||
|
||||
```cpp
|
||||
// Open a stream (takes ownership of socket)
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
auto handle = cli.open_stream("GET", "/path");
|
||||
|
||||
// Check validity
|
||||
if (handle.is_valid()) {
|
||||
// Access response headers immediately
|
||||
int status = handle.response->status;
|
||||
auto content_type = handle.response->get_header_value("Content-Type");
|
||||
|
||||
// Read body incrementally
|
||||
char buf[4096];
|
||||
ssize_t n;
|
||||
while ((n = handle.read(buf, sizeof(buf))) > 0) {
|
||||
process(buf, n);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### StreamHandle Members
|
||||
|
||||
| Member | Type | Description |
|
||||
|--------|------|-------------|
|
||||
| `response` | `std::unique_ptr<Response>` | HTTP response with headers |
|
||||
| `error` | `Error` | Error code if request failed |
|
||||
| `is_valid()` | `bool` | Returns true if response is valid |
|
||||
| `read(buf, len)` | `ssize_t` | Read up to `len` bytes directly from socket |
|
||||
| `get_read_error()` | `Error` | Get the last read error |
|
||||
| `has_read_error()` | `bool` | Check if a read error occurred |
|
||||
|
||||
### High-Level API: `stream::Get()` and `stream::Result`
|
||||
|
||||
The `httplib.h` header provides a more ergonomic iterator-style API.
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
cli.set_follow_location(true);
|
||||
...
|
||||
|
||||
// Simple GET
|
||||
auto result = httplib::stream::Get(cli, "/path");
|
||||
|
||||
// GET with custom headers
|
||||
httplib::Headers headers = {{"Authorization", "Bearer token"}};
|
||||
auto result = httplib::stream::Get(cli, "/path", headers);
|
||||
|
||||
// Process the response
|
||||
if (result) {
|
||||
while (result.next()) {
|
||||
process(result.data(), result.size());
|
||||
}
|
||||
}
|
||||
|
||||
// Or read entire body at once
|
||||
auto result2 = httplib::stream::Get(cli, "/path");
|
||||
if (result2) {
|
||||
std::string body = result2.read_all();
|
||||
}
|
||||
```
|
||||
|
||||
#### stream::Result Members
|
||||
|
||||
| Member | Type | Description |
|
||||
|--------|------|-------------|
|
||||
| `operator bool()` | `bool` | Returns true if response is valid |
|
||||
| `is_valid()` | `bool` | Same as `operator bool()` |
|
||||
| `status()` | `int` | HTTP status code |
|
||||
| `headers()` | `const Headers&` | Response headers |
|
||||
| `get_header_value(key, def)` | `std::string` | Get header value (with optional default) |
|
||||
| `has_header(key)` | `bool` | Check if header exists |
|
||||
| `next()` | `bool` | Read next chunk, returns false when done |
|
||||
| `data()` | `const char*` | Pointer to current chunk data |
|
||||
| `size()` | `size_t` | Size of current chunk |
|
||||
| `read_all()` | `std::string` | Read entire remaining body into string |
|
||||
| `error()` | `Error` | Get the connection/request error |
|
||||
| `read_error()` | `Error` | Get the last read error |
|
||||
| `has_read_error()` | `bool` | Check if a read error occurred |
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Example 1: SSE (Server-Sent Events) Client
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("http://localhost:1234");
|
||||
|
||||
auto result = httplib::stream::Get(cli, "/events");
|
||||
if (!result) { return 1; }
|
||||
|
||||
while (result.next()) {
|
||||
std::cout.write(result.data(), result.size());
|
||||
std::cout.flush();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
For a complete SSE client with auto-reconnection and event parsing, see `example/ssecli-stream.cc`.
|
||||
|
||||
### Example 2: LLM Streaming Response
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("http://localhost:11434"); // Ollama
|
||||
|
||||
auto result = httplib::stream::Get(cli, "/api/generate");
|
||||
|
||||
if (result && result.status() == 200) {
|
||||
while (result.next()) {
|
||||
std::cout.write(result.data(), result.size());
|
||||
std::cout.flush();
|
||||
}
|
||||
}
|
||||
|
||||
// Check for connection errors
|
||||
if (result.read_error() != httplib::Error::Success) {
|
||||
std::cerr << "Connection lost\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
### Example 3: Large File Download with Progress
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("http://example.com");
|
||||
auto result = httplib::stream::Get(cli, "/large-file.zip");
|
||||
|
||||
if (!result || result.status() != 200) {
|
||||
std::cerr << "Download failed\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::ofstream file("download.zip", std::ios::binary);
|
||||
size_t total = 0;
|
||||
|
||||
while (result.next()) {
|
||||
file.write(result.data(), result.size());
|
||||
total += result.size();
|
||||
std::cout << "\rDownloaded: " << (total / 1024) << " KB" << std::flush;
|
||||
}
|
||||
|
||||
std::cout << "\nComplete!\n";
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
### Example 4: Reverse Proxy Streaming
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
|
||||
httplib::Server svr;
|
||||
|
||||
svr.Get("/proxy/(.*)", [](const httplib::Request& req, httplib::Response& res) {
|
||||
httplib::Client upstream("http://backend:8080");
|
||||
auto handle = upstream.open_stream("/" + req.matches[1].str());
|
||||
|
||||
if (!handle.is_valid()) {
|
||||
res.status = 502;
|
||||
return;
|
||||
}
|
||||
|
||||
res.status = handle.response->status;
|
||||
res.set_chunked_content_provider(
|
||||
handle.response->get_header_value("Content-Type"),
|
||||
[handle = std::move(handle)](size_t, httplib::DataSink& sink) mutable {
|
||||
char buf[8192];
|
||||
auto n = handle.read(buf, sizeof(buf));
|
||||
if (n > 0) {
|
||||
sink.write(buf, static_cast<size_t>(n));
|
||||
return true;
|
||||
}
|
||||
sink.done();
|
||||
return true;
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
svr.listen("0.0.0.0", 3000);
|
||||
```
|
||||
|
||||
## Comparison with Existing APIs
|
||||
|
||||
| Feature | `Client::Get()` | `open_stream()` | `stream::Get()` |
|
||||
|---------|----------------|-----------------|----------------|
|
||||
| Headers available | After complete | Immediately | Immediately |
|
||||
| Body reading | All at once | Direct from socket | Iterator-based |
|
||||
| Memory usage | Full body in RAM | Minimal (controlled) | Minimal (controlled) |
|
||||
| Keep-Alive support | ✅ Yes | ❌ No | ❌ No |
|
||||
| Compression | Auto-handled | Auto-handled | Auto-handled |
|
||||
| Best for | Small responses, Keep-Alive | Low-level streaming | Easy streaming |
|
||||
|
||||
## Features
|
||||
|
||||
- **True socket-level streaming**: Data is read directly from the network socket
|
||||
- **Low memory footprint**: Only the current chunk is held in memory
|
||||
- **Compression support**: Automatic decompression for gzip, brotli, and zstd
|
||||
- **Chunked transfer**: Full support for chunked transfer encoding
|
||||
- **SSL/TLS support**: Works with HTTPS connections
|
||||
|
||||
## Important Notes
|
||||
|
||||
### Keep-Alive Behavior
|
||||
|
||||
The streaming API (`stream::Get()` / `open_stream()`) takes ownership of the socket connection for the duration of the stream. This means:
|
||||
|
||||
- **Keep-Alive is not supported** for streaming connections
|
||||
- The socket is closed when `StreamHandle` is destroyed
|
||||
- For Keep-Alive scenarios, use the standard `client.Get()` API instead
|
||||
|
||||
```cpp
|
||||
// Use for streaming (no Keep-Alive)
|
||||
auto result = httplib::stream::Get(cli, "/large-stream");
|
||||
while (result.next()) { /* ... */ }
|
||||
|
||||
// Use for Keep-Alive connections
|
||||
auto res = cli.Get("/api/data"); // Connection can be reused
|
||||
```
|
||||
|
||||
## Related
|
||||
|
||||
- [Issue #2269](https://github.com/yhirose/cpp-httplib/issues/2269) - Original feature request
|
||||
- [example/ssecli-stream.cc](./example/ssecli-stream.cc) - SSE client with auto-reconnection
|
||||
@@ -0,0 +1,411 @@
|
||||
# WebSocket - RFC 6455 WebSocket Support
|
||||
|
||||
A simple, blocking WebSocket implementation for C++11.
|
||||
|
||||
> [!IMPORTANT]
|
||||
> This is a blocking I/O WebSocket implementation using a thread-per-connection model. If you need high-concurrency WebSocket support with non-blocking/async I/O (e.g., thousands of simultaneous connections), this is not the one that you want.
|
||||
|
||||
## Features
|
||||
|
||||
- **RFC 6455 compliant**: Full WebSocket protocol support
|
||||
- **Server and Client**: Both sides included
|
||||
- **SSL/TLS support**: `wss://` scheme for secure connections
|
||||
- **Text and Binary**: Both message types supported
|
||||
- **Automatic heartbeat**: Periodic Ping/Pong keeps connections alive
|
||||
- **Subprotocol negotiation**: `Sec-WebSocket-Protocol` support for GraphQL, MQTT, etc.
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Server
|
||||
|
||||
```cpp
|
||||
httplib::Server svr;
|
||||
|
||||
svr.WebSocket("/ws", [](const httplib::Request &req, httplib::ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {
|
||||
ws.send("echo: " + msg);
|
||||
}
|
||||
});
|
||||
|
||||
svr.listen("localhost", 8080);
|
||||
```
|
||||
|
||||
### Client
|
||||
|
||||
```cpp
|
||||
httplib::ws::WebSocketClient ws("ws://localhost:8080/ws");
|
||||
|
||||
if (ws.connect()) {
|
||||
ws.send("hello");
|
||||
|
||||
std::string msg;
|
||||
if (ws.read(msg)) {
|
||||
std::cout << msg << std::endl; // "echo: hello"
|
||||
}
|
||||
ws.close();
|
||||
}
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### ReadResult
|
||||
|
||||
```cpp
|
||||
enum ReadResult : int {
|
||||
Fail = 0, // Connection closed or error
|
||||
Text = 1, // UTF-8 text message
|
||||
Binary = 2, // Binary message
|
||||
};
|
||||
```
|
||||
|
||||
Returned by `read()`. Since `Fail` is `0`, the result works naturally in boolean contexts — `while (ws.read(msg))` continues until the connection closes. When you need to distinguish text from binary, check the return value directly.
|
||||
|
||||
### CloseStatus
|
||||
|
||||
```cpp
|
||||
enum class CloseStatus : uint16_t {
|
||||
Normal = 1000,
|
||||
GoingAway = 1001,
|
||||
ProtocolError = 1002,
|
||||
UnsupportedData = 1003,
|
||||
NoStatus = 1005,
|
||||
Abnormal = 1006,
|
||||
InvalidPayload = 1007,
|
||||
PolicyViolation = 1008,
|
||||
MessageTooBig = 1009,
|
||||
MandatoryExtension = 1010,
|
||||
InternalError = 1011,
|
||||
};
|
||||
```
|
||||
|
||||
### Server Registration
|
||||
|
||||
```cpp
|
||||
// Basic handler
|
||||
Server &WebSocket(const std::string &pattern, WebSocketHandler handler);
|
||||
|
||||
// With subprotocol negotiation
|
||||
Server &WebSocket(const std::string &pattern, WebSocketHandler handler,
|
||||
SubProtocolSelector sub_protocol_selector);
|
||||
```
|
||||
|
||||
**Type aliases:**
|
||||
|
||||
```cpp
|
||||
using WebSocketHandler =
|
||||
std::function<void(const Request &, ws::WebSocket &)>;
|
||||
using SubProtocolSelector =
|
||||
std::function<std::string(const std::vector<std::string> &protocols)>;
|
||||
```
|
||||
|
||||
The `SubProtocolSelector` receives the list of subprotocols proposed by the client (from the `Sec-WebSocket-Protocol` header) and returns the selected one. Return an empty string to decline all proposed subprotocols.
|
||||
|
||||
### WebSocket (Server-side)
|
||||
|
||||
Passed to the handler registered with `Server::WebSocket()`. The handler runs in a dedicated thread per connection.
|
||||
|
||||
```cpp
|
||||
// Read next message (blocks until received, returns Fail/Text/Binary)
|
||||
ReadResult read(std::string &msg);
|
||||
|
||||
// Send messages
|
||||
bool send(const std::string &data); // Text
|
||||
bool send(const char *data, size_t len); // Binary
|
||||
|
||||
// Close the connection
|
||||
void close(CloseStatus status = CloseStatus::Normal,
|
||||
const std::string &reason = "");
|
||||
|
||||
// Access the original HTTP upgrade request
|
||||
const Request &request() const;
|
||||
|
||||
// Check if the connection is still open
|
||||
bool is_open() const;
|
||||
```
|
||||
|
||||
### WebSocketClient
|
||||
|
||||
```cpp
|
||||
// Constructor - accepts ws:// or wss:// URL
|
||||
explicit WebSocketClient(const std::string &scheme_host_port_path,
|
||||
const Headers &headers = {});
|
||||
|
||||
// Check if the URL was parsed successfully
|
||||
bool is_valid() const;
|
||||
|
||||
// Connect (performs HTTP upgrade handshake)
|
||||
bool connect();
|
||||
|
||||
// Get the subprotocol selected by the server (empty if none)
|
||||
const std::string &subprotocol() const;
|
||||
|
||||
// Read/Send/Close (same as server-side WebSocket)
|
||||
ReadResult read(std::string &msg);
|
||||
bool send(const std::string &data);
|
||||
bool send(const char *data, size_t len);
|
||||
void close(CloseStatus status = CloseStatus::Normal,
|
||||
const std::string &reason = "");
|
||||
bool is_open() const;
|
||||
|
||||
// Timeouts
|
||||
void set_read_timeout(time_t sec, time_t usec = 0);
|
||||
void set_write_timeout(time_t sec, time_t usec = 0);
|
||||
|
||||
// SSL configuration (wss:// only, requires CPPHTTPLIB_OPENSSL_SUPPORT)
|
||||
void set_ca_cert_path(const std::string &path);
|
||||
void set_ca_cert_store(tls::ca_store_t store);
|
||||
void enable_server_certificate_verification(bool enabled);
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### Echo Server with Connection Logging
|
||||
|
||||
```cpp
|
||||
httplib::Server svr;
|
||||
|
||||
svr.WebSocket("/ws", [](const httplib::Request &req, httplib::ws::WebSocket &ws) {
|
||||
std::cout << "Connected from " << req.remote_addr << std::endl;
|
||||
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {
|
||||
ws.send("echo: " + msg);
|
||||
}
|
||||
|
||||
std::cout << "Disconnected" << std::endl;
|
||||
});
|
||||
|
||||
svr.listen("localhost", 8080);
|
||||
```
|
||||
|
||||
### Client: Continuous Read Loop
|
||||
|
||||
```cpp
|
||||
httplib::ws::WebSocketClient ws("ws://localhost:8080/ws");
|
||||
|
||||
if (ws.connect()) {
|
||||
ws.send("hello");
|
||||
ws.send("world");
|
||||
|
||||
std::string msg;
|
||||
while (ws.read(msg)) { // blocks until a message arrives
|
||||
std::cout << msg << std::endl; // "echo: hello", "echo: world"
|
||||
}
|
||||
// read() returns false when the server closes the connection
|
||||
}
|
||||
```
|
||||
|
||||
### Text and Binary Messages
|
||||
|
||||
Check the `ReadResult` return value to distinguish between text and binary:
|
||||
|
||||
```cpp
|
||||
// Server
|
||||
svr.WebSocket("/ws", [](const httplib::Request &req, httplib::ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
httplib::ws::ReadResult ret;
|
||||
while ((ret = ws.read(msg))) {
|
||||
if (ret == httplib::ws::Text) {
|
||||
ws.send("echo: " + msg);
|
||||
} else {
|
||||
ws.send(msg.data(), msg.size()); // Binary echo
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Client
|
||||
httplib::ws::WebSocketClient ws("ws://localhost:8080/ws");
|
||||
if (ws.connect()) {
|
||||
// Send binary data
|
||||
const char binary[] = {0x00, 0x01, 0x02, 0x03};
|
||||
ws.send(binary, sizeof(binary));
|
||||
|
||||
// Receive and check the type
|
||||
std::string msg;
|
||||
if (ws.read(msg) == httplib::ws::Binary) {
|
||||
// Process binary data in msg
|
||||
}
|
||||
ws.close();
|
||||
}
|
||||
```
|
||||
|
||||
### SSL Client
|
||||
|
||||
```cpp
|
||||
httplib::ws::WebSocketClient ws("wss://echo.example.com/ws");
|
||||
|
||||
if (ws.connect()) {
|
||||
ws.send("hello over TLS");
|
||||
|
||||
std::string msg;
|
||||
if (ws.read(msg)) {
|
||||
std::cout << msg << std::endl;
|
||||
}
|
||||
ws.close();
|
||||
}
|
||||
```
|
||||
|
||||
### Close with Status
|
||||
|
||||
```cpp
|
||||
// Client-side: close with a specific status code and reason
|
||||
ws.close(httplib::ws::CloseStatus::GoingAway, "shutting down");
|
||||
|
||||
// Server-side: close with a policy violation status
|
||||
ws.close(httplib::ws::CloseStatus::PolicyViolation, "forbidden");
|
||||
```
|
||||
|
||||
### Accessing the Upgrade Request
|
||||
|
||||
```cpp
|
||||
svr.WebSocket("/ws", [](const httplib::Request &req, httplib::ws::WebSocket &ws) {
|
||||
// Access headers from the original HTTP upgrade request
|
||||
auto auth = req.get_header_value("Authorization");
|
||||
if (auth.empty()) {
|
||||
ws.close(httplib::ws::CloseStatus::PolicyViolation, "unauthorized");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {
|
||||
ws.send("echo: " + msg);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### Custom Headers and Timeouts
|
||||
|
||||
```cpp
|
||||
httplib::Headers headers = {
|
||||
{"Authorization", "Bearer token123"}
|
||||
};
|
||||
|
||||
httplib::ws::WebSocketClient ws("ws://localhost:8080/ws", headers);
|
||||
ws.set_read_timeout(30, 0); // 30 seconds
|
||||
ws.set_write_timeout(10, 0); // 10 seconds
|
||||
|
||||
if (ws.connect()) {
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {
|
||||
std::cout << msg << std::endl;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Subprotocol Negotiation
|
||||
|
||||
The server can negotiate a subprotocol with the client using `Sec-WebSocket-Protocol`. This is required for protocols like GraphQL over WebSocket (`graphql-ws`) and MQTT.
|
||||
|
||||
```cpp
|
||||
// Server: register a handler with a subprotocol selector
|
||||
svr.WebSocket(
|
||||
"/ws",
|
||||
[](const httplib::Request &req, httplib::ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {
|
||||
ws.send("echo: " + msg);
|
||||
}
|
||||
},
|
||||
[](const std::vector<std::string> &protocols) -> std::string {
|
||||
// The client proposed a list of subprotocols; pick one
|
||||
for (const auto &p : protocols) {
|
||||
if (p == "graphql-ws" || p == "graphql-transport-ws") {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return ""; // Decline all
|
||||
});
|
||||
|
||||
// Client: propose subprotocols via Sec-WebSocket-Protocol header
|
||||
httplib::Headers headers = {
|
||||
{"Sec-WebSocket-Protocol", "graphql-ws, graphql-transport-ws"}
|
||||
};
|
||||
httplib::ws::WebSocketClient ws("ws://localhost:8080/ws", headers);
|
||||
|
||||
if (ws.connect()) {
|
||||
// Check which subprotocol the server selected
|
||||
std::cout << "Subprotocol: " << ws.subprotocol() << std::endl;
|
||||
// => "graphql-ws"
|
||||
ws.close();
|
||||
}
|
||||
```
|
||||
|
||||
### SSL Client with Certificate Configuration
|
||||
|
||||
```cpp
|
||||
httplib::ws::WebSocketClient ws("wss://example.com/ws");
|
||||
ws.set_ca_cert_path("/path/to/ca-bundle.crt");
|
||||
ws.enable_server_certificate_verification(true);
|
||||
|
||||
if (ws.connect()) {
|
||||
ws.send("secure message");
|
||||
ws.close();
|
||||
}
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
| Macro | Default | Description |
|
||||
|---------------------------------------------|-------------------|----------------------------------------------------------|
|
||||
| `CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH` | `16777216` (16MB) | Maximum payload size per message |
|
||||
| `CPPHTTPLIB_WEBSOCKET_READ_TIMEOUT_SECOND` | `300` | Read timeout for WebSocket connections (seconds) |
|
||||
| `CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND` | `5` | Timeout for waiting peer's Close response (seconds) |
|
||||
| `CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND` | `30` | Automatic Ping interval for heartbeat (seconds) |
|
||||
|
||||
### Runtime Ping Interval
|
||||
|
||||
You can override the ping interval at runtime instead of changing the compile-time macro. Set it to `0` to disable automatic pings entirely.
|
||||
|
||||
```cpp
|
||||
// Server side
|
||||
httplib::Server svr;
|
||||
svr.set_websocket_ping_interval(10); // 10 seconds
|
||||
|
||||
// Or using std::chrono
|
||||
svr.set_websocket_ping_interval(std::chrono::seconds(10));
|
||||
|
||||
// Client side
|
||||
httplib::ws::WebSocketClient ws("ws://localhost:8080/ws");
|
||||
ws.set_websocket_ping_interval(10); // 10 seconds
|
||||
|
||||
// Disable automatic pings
|
||||
ws.set_websocket_ping_interval(0);
|
||||
```
|
||||
|
||||
## Threading Model
|
||||
|
||||
WebSocket connections share the same thread pool as HTTP requests. Each WebSocket connection occupies one thread for its entire lifetime.
|
||||
|
||||
The default thread pool uses dynamic scaling: it maintains a base thread count of `CPPHTTPLIB_THREAD_POOL_COUNT` (8 or `std::thread::hardware_concurrency() - 1`, whichever is greater) and can scale up to 4x that count under load (`CPPHTTPLIB_THREAD_POOL_MAX_COUNT`). When all base threads are busy, temporary threads are spawned automatically up to the maximum. These dynamic threads exit after an idle timeout (`CPPHTTPLIB_THREAD_POOL_IDLE_TIMEOUT`, default 3 seconds).
|
||||
|
||||
This dynamic scaling helps accommodate WebSocket connections alongside HTTP requests. However, if you expect many simultaneous WebSocket connections, you should configure the thread pool accordingly:
|
||||
|
||||
```cpp
|
||||
httplib::Server svr;
|
||||
|
||||
svr.new_task_queue = [] {
|
||||
return new httplib::ThreadPool(/*base_threads=*/8, /*max_threads=*/128);
|
||||
};
|
||||
```
|
||||
|
||||
Choose sizes that account for both your expected HTTP load and the maximum number of simultaneous WebSocket connections.
|
||||
|
||||
## Protocol
|
||||
|
||||
The implementation follows [RFC 6455](https://tools.ietf.org/html/rfc6455):
|
||||
|
||||
- Handshake via HTTP Upgrade with `Sec-WebSocket-Key` / `Sec-WebSocket-Accept`
|
||||
- Subprotocol negotiation via `Sec-WebSocket-Protocol`
|
||||
- Frame masking (client-to-server)
|
||||
- Control frames: Close, Ping, Pong
|
||||
- Message fragmentation and reassembly
|
||||
- Close handshake with status codes
|
||||
|
||||
## Browser Test
|
||||
|
||||
Run the echo server example and open `http://localhost:8080` in a browser:
|
||||
|
||||
```bash
|
||||
cd example && make wsecho && ./wsecho
|
||||
```
|
||||
@@ -1,14 +0,0 @@
|
||||
image:
|
||||
- Visual Studio 2019
|
||||
|
||||
platform:
|
||||
- x64
|
||||
|
||||
build_script:
|
||||
- cmd: >-
|
||||
cd test
|
||||
|
||||
msbuild.exe test.sln /verbosity:minimal /t:Build /p:Configuration=Release;Platform=%PLATFORM%
|
||||
|
||||
test_script:
|
||||
- cmd: x64\Release\test.exe
|
||||
@@ -0,0 +1,50 @@
|
||||
CXXFLAGS = -O2 -I..
|
||||
|
||||
CPPHTTPLIB_CXXFLAGS = -std=c++11
|
||||
CROW_CXXFLAGS = -std=c++17
|
||||
|
||||
CPPHTTPLIB_FLAGS = -DCPPHTTPLIB_THREAD_POOL_COUNT=16
|
||||
|
||||
BENCH = bombardier -c 10 -d 5s localhost:8080
|
||||
MONITOR = ali http://localhost:8080
|
||||
|
||||
# cpp-httplib
|
||||
bench: server
|
||||
@echo "--------------------\n cpp-httplib latest\n--------------------\n"
|
||||
@./server & export PID=$$!; $(BENCH); kill $${PID}
|
||||
@echo ""
|
||||
|
||||
monitor: server
|
||||
@./server & export PID=$$!; $(MONITOR); kill $${PID}
|
||||
|
||||
run : server
|
||||
@./server
|
||||
|
||||
server : cpp-httplib/main.cpp ../httplib.h
|
||||
@g++ -o $@ $(CXXFLAGS) $(CPPHTTPLIB_CXXFLAGS) $(CPPHTTPLIB_FLAGS) cpp-httplib/main.cpp
|
||||
|
||||
# crow
|
||||
bench-crow: server-crow
|
||||
@echo "-------------\n Crow v1.3.1\n-------------\n"
|
||||
@./server-crow & export PID=$$!; $(BENCH); kill $${PID}
|
||||
@echo ""
|
||||
|
||||
monitor-crow: server-crow
|
||||
@./server-crow & export PID=$$!; $(MONITOR); kill $${PID}
|
||||
|
||||
run-crow : server-crow
|
||||
@./server-crow
|
||||
|
||||
server-crow : crow/main.cpp crow/crow_all.h
|
||||
@g++ -o $@ $(CXXFLAGS) $(CROW_CXXFLAGS) crow/main.cpp
|
||||
|
||||
# misc
|
||||
build: server server-crow
|
||||
|
||||
bench-all: bench-crow bench
|
||||
|
||||
issue:
|
||||
bombardier -c 10 -d 30s localhost:8080
|
||||
|
||||
clean:
|
||||
rm -rf server*
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "httplib.h"
|
||||
using namespace httplib;
|
||||
|
||||
int main() {
|
||||
Server svr;
|
||||
|
||||
svr.Get("/", [](const Request &, Response &res) {
|
||||
res.set_content("Hello World!", "text/plain");
|
||||
});
|
||||
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,17 @@
|
||||
#include "crow_all.h"
|
||||
|
||||
class CustomLogger : public crow::ILogHandler {
|
||||
public:
|
||||
void log(const std::string &, crow::LogLevel) {}
|
||||
};
|
||||
|
||||
int main() {
|
||||
CustomLogger logger;
|
||||
crow::logger::setHandler(&logger);
|
||||
|
||||
crow::SimpleApp app;
|
||||
|
||||
CROW_ROUTE(app, "/")([]() { return "Hello world!"; });
|
||||
|
||||
app.port(8080).multithreaded().run();
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
# A simple FindBrotli package for Cmake's find_package function.
|
||||
# Note: This find package doesn't have version support, as the version file doesn't seem to be installed on most systems.
|
||||
#
|
||||
# If you want to find the static packages instead of shared (the default), define BROTLI_USE_STATIC_LIBS as TRUE.
|
||||
# The targets will have the same names, but it will use the static libs.
|
||||
#
|
||||
# Valid find_package COMPONENTS names: "decoder", "encoder", and "common"
|
||||
# Note that if you're requiring "decoder" or "encoder", then "common" will be automatically added as required.
|
||||
#
|
||||
# Defines the libraries (if found): Brotli::decoder, Brotli::encoder, Brotli::common
|
||||
# and the includes path variable: Brotli_INCLUDE_DIR
|
||||
#
|
||||
# If it's failing to find the libraries, try setting BROTLI_ROOT_DIR to the folder containing your library & include dir.
|
||||
|
||||
# If they asked for a specific version, warn/fail since we don't support it.
|
||||
# TODO: if they start distributing the version somewhere, implement finding it.
|
||||
# See https://github.com/google/brotli/issues/773#issuecomment-579133187
|
||||
if(Brotli_FIND_VERSION)
|
||||
set(_brotli_version_error_msg "FindBrotli.cmake doesn't have version support!")
|
||||
# If the package is required, throw a fatal error
|
||||
# Otherwise, if not running quietly, we throw a warning
|
||||
if(Brotli_FIND_REQUIRED)
|
||||
message(FATAL_ERROR "${_brotli_version_error_msg}")
|
||||
elseif(NOT Brotli_FIND_QUIETLY)
|
||||
message(WARNING "${_brotli_version_error_msg}")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Since both decoder & encoder require the common lib, force its requirement..
|
||||
# if the user is requiring either of those other libs.
|
||||
if(Brotli_FIND_REQUIRED_decoder OR Brotli_FIND_REQUIRED_encoder)
|
||||
set(Brotli_FIND_REQUIRED_common TRUE)
|
||||
endif()
|
||||
|
||||
# Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
|
||||
# Credit to FindOpenSSL.cmake for this
|
||||
if(BROTLI_USE_STATIC_LIBS)
|
||||
set(_brotli_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
|
||||
if(WIN32)
|
||||
set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
|
||||
else()
|
||||
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Make PkgConfig optional, since some users (mainly Windows) don't have it.
|
||||
# But it's a lot more clean than manually using find_library.
|
||||
find_package(PkgConfig QUIET)
|
||||
|
||||
# Only used if the PkgConfig libraries aren't used.
|
||||
find_path(Brotli_INCLUDE_DIR
|
||||
NAMES
|
||||
"brotli/decode.h"
|
||||
"brotli/encode.h"
|
||||
HINTS
|
||||
${BROTLI_ROOT_DIR}
|
||||
PATH_SUFFIXES
|
||||
"include"
|
||||
"includes"
|
||||
DOC "The path to Brotli's include directory."
|
||||
)
|
||||
# Hides this var from the GUI
|
||||
mark_as_advanced(Brotli_INCLUDE_DIR)
|
||||
|
||||
# Just used for PkgConfig stuff in the loop below
|
||||
set(_brotli_stat_str "")
|
||||
if(BROTLI_USE_STATIC_LIBS)
|
||||
set(_brotli_stat_str "_STATIC")
|
||||
endif()
|
||||
|
||||
# Each string here is "ComponentName;LiteralName" (the semi-colon is a delimiter)
|
||||
foreach(_listvar "common;common" "decoder;dec" "encoder;enc")
|
||||
# Split the component name and literal library name from the listvar
|
||||
list(GET _listvar 0 _component_name)
|
||||
list(GET _listvar 1 _libname)
|
||||
|
||||
# NOTE: We can't rely on PkgConf for static libs since the upstream static lib support is broken
|
||||
# See https://github.com/google/brotli/issues/795
|
||||
# TODO: whenever their issue is fixed upstream, remove this "AND NOT BROTLI_USE_STATIC_LIBS" check
|
||||
if(PKG_CONFIG_FOUND AND NOT BROTLI_USE_STATIC_LIBS)
|
||||
# These need to be GLOBAL for MinGW when making ALIAS libraries against them.
|
||||
# Have to postfix _STATIC on the name to tell PkgConfig to find the static libs.
|
||||
pkg_check_modules(Brotli_${_component_name}${_brotli_stat_str} QUIET GLOBAL IMPORTED_TARGET libbrotli${_libname})
|
||||
endif()
|
||||
|
||||
# Check if the target was already found by Pkgconf
|
||||
if(TARGET PkgConfig::Brotli_${_component_name}${_brotli_stat_str})
|
||||
# ALIAS since we don't want the PkgConfig namespace on the Cmake library (for end-users)
|
||||
if (NOT TARGET Brotli::${_component_name})
|
||||
add_library(Brotli::${_component_name} ALIAS PkgConfig::Brotli_${_component_name}${_brotli_stat_str})
|
||||
endif()
|
||||
|
||||
# Tells HANDLE_COMPONENTS we found the component
|
||||
set(Brotli_${_component_name}_FOUND TRUE)
|
||||
if(Brotli_FIND_REQUIRED_${_component_name})
|
||||
# If the lib is required, we can add its literal path as a required var for FindPackageHandleStandardArgs
|
||||
# Since it won't accept the PkgConfig targets
|
||||
if(BROTLI_USE_STATIC_LIBS)
|
||||
list(APPEND _brotli_req_vars "Brotli_${_component_name}_STATIC_LIBRARIES")
|
||||
else()
|
||||
list(APPEND _brotli_req_vars "Brotli_${_component_name}_LINK_LIBRARIES")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Skip searching for the libs with find_library since it was already found by Pkgconf
|
||||
continue()
|
||||
endif()
|
||||
|
||||
if(Brotli_FIND_REQUIRED_${_component_name})
|
||||
# If it's required, we can set the name used in find_library as a required var for FindPackageHandleStandardArgs
|
||||
list(APPEND _brotli_req_vars "Brotli_${_component_name}")
|
||||
endif()
|
||||
|
||||
list(APPEND _brotli_lib_names
|
||||
"brotli${_libname}"
|
||||
"libbrotli${_libname}"
|
||||
)
|
||||
if(BROTLI_USE_STATIC_LIBS)
|
||||
# Postfix "-static" to the libnames since we're looking for static libs
|
||||
list(TRANSFORM _brotli_lib_names APPEND "-static")
|
||||
endif()
|
||||
|
||||
find_library(Brotli_${_component_name}
|
||||
NAMES ${_brotli_lib_names}
|
||||
HINTS ${BROTLI_ROOT_DIR}
|
||||
PATH_SUFFIXES
|
||||
"lib"
|
||||
"lib64"
|
||||
"libs"
|
||||
"libs64"
|
||||
"lib/x86_64-linux-gnu"
|
||||
)
|
||||
# Hide the library variable from the Cmake GUI
|
||||
mark_as_advanced(Brotli_${_component_name})
|
||||
|
||||
# Unset since otherwise it'll stick around for the next loop and break things
|
||||
unset(_brotli_lib_names)
|
||||
|
||||
# Check if find_library found the library
|
||||
if(Brotli_${_component_name})
|
||||
# Tells HANDLE_COMPONENTS we found the component
|
||||
set(Brotli_${_component_name}_FOUND TRUE)
|
||||
|
||||
if (NOT TARGET Brotli::${_component_name})
|
||||
add_library("Brotli::${_component_name}" UNKNOWN IMPORTED)
|
||||
endif()
|
||||
|
||||
# Attach the literal library and include dir to the IMPORTED target for the end-user
|
||||
set_target_properties("Brotli::${_component_name}" PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${Brotli_INCLUDE_DIR}"
|
||||
IMPORTED_LOCATION "${Brotli_${_component_name}}"
|
||||
)
|
||||
else()
|
||||
# Tells HANDLE_COMPONENTS we found the component
|
||||
set(Brotli_${_component_name}_FOUND FALSE)
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
# Sets Brotli_FOUND, and fails the find_package(Brotli) call if it was REQUIRED but missing libs.
|
||||
find_package_handle_standard_args(Brotli
|
||||
FOUND_VAR
|
||||
Brotli_FOUND
|
||||
REQUIRED_VARS
|
||||
Brotli_INCLUDE_DIR
|
||||
${_brotli_req_vars}
|
||||
HANDLE_COMPONENTS
|
||||
)
|
||||
|
||||
# Restore the original find library ordering
|
||||
if(BROTLI_USE_STATIC_LIBS)
|
||||
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_brotli_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
|
||||
endif()
|
||||
@@ -4,26 +4,75 @@
|
||||
# Setting these here so they're accessible after install.
|
||||
# Might be useful for some users to check which settings were used.
|
||||
set(HTTPLIB_IS_USING_OPENSSL @HTTPLIB_IS_USING_OPENSSL@)
|
||||
set(HTTPLIB_IS_USING_WOLFSSL @HTTPLIB_IS_USING_WOLFSSL@)
|
||||
set(HTTPLIB_IS_USING_MBEDTLS @HTTPLIB_IS_USING_MBEDTLS@)
|
||||
set(HTTPLIB_IS_USING_ZLIB @HTTPLIB_IS_USING_ZLIB@)
|
||||
set(HTTPLIB_IS_COMPILED @HTTPLIB_COMPILE@)
|
||||
set(HTTPLIB_IS_USING_BROTLI @HTTPLIB_IS_USING_BROTLI@)
|
||||
set(HTTPLIB_IS_USING_NON_BLOCKING_GETADDRINFO @HTTPLIB_IS_USING_NON_BLOCKING_GETADDRINFO@)
|
||||
set(HTTPLIB_VERSION @PROJECT_VERSION@)
|
||||
|
||||
include(CMakeFindDependencyMacro)
|
||||
|
||||
# We add find_dependency calls here to not make the end-user have to call them.
|
||||
find_dependency(Threads REQUIRED)
|
||||
find_dependency(Threads)
|
||||
if(@HTTPLIB_IS_USING_OPENSSL@)
|
||||
# OpenSSL COMPONENTS were added in Cmake v3.11
|
||||
if(CMAKE_VERSION VERSION_LESS "3.11")
|
||||
find_dependency(OpenSSL @_HTTPLIB_OPENSSL_MIN_VER@ REQUIRED)
|
||||
find_dependency(OpenSSL @_HTTPLIB_OPENSSL_MIN_VER@)
|
||||
else()
|
||||
# Once the COMPONENTS were added, they were made optional when not specified.
|
||||
# Since we use both, we need to search for both.
|
||||
find_dependency(OpenSSL @_HTTPLIB_OPENSSL_MIN_VER@ COMPONENTS Crypto SSL REQUIRED)
|
||||
find_dependency(OpenSSL @_HTTPLIB_OPENSSL_MIN_VER@ COMPONENTS Crypto SSL)
|
||||
endif()
|
||||
set(httplib_OpenSSL_FOUND ${OpenSSL_FOUND})
|
||||
endif()
|
||||
|
||||
if(@HTTPLIB_IS_USING_ZLIB@)
|
||||
find_dependency(ZLIB REQUIRED)
|
||||
find_dependency(ZLIB)
|
||||
set(httplib_ZLIB_FOUND ${ZLIB_FOUND})
|
||||
endif()
|
||||
|
||||
if(@HTTPLIB_IS_USING_WOLFSSL@)
|
||||
find_dependency(wolfssl)
|
||||
set(httplib_wolfssl_FOUND ${wolfssl_FOUND})
|
||||
endif()
|
||||
|
||||
if(@HTTPLIB_IS_USING_MBEDTLS@)
|
||||
find_dependency(MbedTLS)
|
||||
set(httplib_MbedTLS_FOUND ${MbedTLS_FOUND})
|
||||
endif()
|
||||
|
||||
if(@HTTPLIB_IS_USING_BROTLI@)
|
||||
# Needed so we can use our own FindBrotli.cmake in this file.
|
||||
# Note that the FindBrotli.cmake file is installed in the same dir as this file.
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
|
||||
set(BROTLI_USE_STATIC_LIBS @BROTLI_USE_STATIC_LIBS@)
|
||||
find_dependency(Brotli COMPONENTS common encoder decoder)
|
||||
set(httplib_Brotli_FOUND ${Brotli_FOUND})
|
||||
endif()
|
||||
|
||||
if(@HTTPLIB_IS_USING_ZSTD@)
|
||||
set(httplib_fd_zstd_quiet_arg)
|
||||
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
|
||||
set(httplib_fd_zstd_quiet_arg QUIET)
|
||||
endif()
|
||||
set(httplib_fd_zstd_required_arg)
|
||||
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
|
||||
set(httplib_fd_zstd_required_arg REQUIRED)
|
||||
endif()
|
||||
find_package(zstd QUIET)
|
||||
if(NOT zstd_FOUND)
|
||||
find_package(PkgConfig ${httplib_fd_zstd_quiet_arg} ${httplib_fd_zstd_required_arg})
|
||||
if(PKG_CONFIG_FOUND)
|
||||
pkg_check_modules(zstd ${httplib_fd_zstd_quiet_arg} ${httplib_fd_zstd_required_arg} IMPORTED_TARGET libzstd)
|
||||
|
||||
if(TARGET PkgConfig::zstd)
|
||||
add_library(zstd::libzstd ALIAS PkgConfig::zstd)
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
set(httplib_zstd_FOUND ${zstd_FOUND})
|
||||
endif()
|
||||
|
||||
# Mildly useful for end-users
|
||||
@@ -33,10 +82,14 @@ set_and_check(HTTPLIB_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@")
|
||||
# This is helpful if you're using Cmake's pre-compiled header feature
|
||||
set_and_check(HTTPLIB_HEADER_PATH "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@/httplib.h")
|
||||
|
||||
# Brings in the target library
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/httplibTargets.cmake")
|
||||
check_required_components(httplib)
|
||||
|
||||
# Ouputs a "found httplib /usr/include/httplib.h" message when using find_package(httplib)
|
||||
# Brings in the target library, but only if all required components are found
|
||||
if(NOT DEFINED httplib_FOUND OR httplib_FOUND)
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/httplibTargets.cmake")
|
||||
endif()
|
||||
|
||||
# Outputs a "found httplib /usr/include/httplib.h" message when using find_package(httplib)
|
||||
include(FindPackageMessage)
|
||||
if(TARGET httplib::httplib)
|
||||
set(HTTPLIB_FOUND TRUE)
|
||||
@@ -0,0 +1,19 @@
|
||||
# This file contains C++20 module support requiring CMake 3.28+
|
||||
# Included conditionally to prevent parse errors on older CMake versions
|
||||
|
||||
if(HTTPLIB_BUILD_MODULES)
|
||||
if(POLICY CMP0155)
|
||||
cmake_policy(SET CMP0155 NEW)
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
|
||||
|
||||
target_sources(${PROJECT_NAME}
|
||||
PUBLIC
|
||||
FILE_SET CXX_MODULES
|
||||
BASE_DIRS
|
||||
"${_httplib_build_includedir}"
|
||||
FILES
|
||||
"${_httplib_build_includedir}/httplib.cppm"
|
||||
)
|
||||
endif()
|
||||
@@ -0,0 +1,7 @@
|
||||
services:
|
||||
http:
|
||||
build: .
|
||||
ports:
|
||||
- "8080:80"
|
||||
volumes:
|
||||
- ./docker/html:/html
|
||||
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Welcome to cpp-httplib!</title>
|
||||
<style>
|
||||
html { color-scheme: light dark; }
|
||||
body { width: 35em; margin: 0 auto;
|
||||
font-family: Tahoma, Verdana, Arial, sans-serif; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome to cpp-httplib!</h1>
|
||||
<p>If you see this page, the cpp-httplib web server is successfully installed and
|
||||
working. Further configuration is required.</p>
|
||||
|
||||
<p>For online documentation and support please refer to
|
||||
<a href="https://github.com/yhirose/cpp-httplib">github.com/yhirose/cpp-httplib</a>.<br/>
|
||||
|
||||
<p><em>Thank you for using cpp-httplib.</em></p>
|
||||
</body>
|
||||
</html>
|
||||
+303
@@ -0,0 +1,303 @@
|
||||
//
|
||||
// main.cc
|
||||
//
|
||||
// Copyright (c) 2026 Yuji Hirose. All rights reserved.
|
||||
// MIT License
|
||||
//
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <format>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <signal.h>
|
||||
#include <sstream>
|
||||
|
||||
#include <httplib.h>
|
||||
|
||||
using namespace httplib;
|
||||
|
||||
const auto SERVER_NAME =
|
||||
std::format("cpp-httplib-server/{}", CPPHTTPLIB_VERSION);
|
||||
|
||||
Server svr;
|
||||
|
||||
void signal_handler(int signal) {
|
||||
if (signal == SIGINT || signal == SIGTERM) {
|
||||
std::cout << "\nReceived signal, shutting down gracefully...\n";
|
||||
svr.stop();
|
||||
}
|
||||
}
|
||||
|
||||
std::string get_time_format() {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto time_t = std::chrono::system_clock::to_time_t(now);
|
||||
|
||||
std::stringstream ss;
|
||||
ss << std::put_time(std::localtime(&time_t), "%d/%b/%Y:%H:%M:%S %z");
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string get_error_time_format() {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto time_t = std::chrono::system_clock::to_time_t(now);
|
||||
|
||||
std::stringstream ss;
|
||||
ss << std::put_time(std::localtime(&time_t), "%Y/%m/%d %H:%M:%S");
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// NGINX Combined log format:
|
||||
// $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent
|
||||
// "$http_referer" "$http_user_agent"
|
||||
void nginx_access_logger(const Request &req, const Response &res) {
|
||||
std::string remote_user =
|
||||
"-"; // cpp-httplib doesn't have built-in auth user tracking
|
||||
auto time_local = get_time_format();
|
||||
auto request = std::format("{} {} {}", req.method, req.path, req.version);
|
||||
auto status = res.status;
|
||||
auto body_bytes_sent = res.body.size();
|
||||
auto http_referer = req.get_header_value("Referer");
|
||||
if (http_referer.empty()) http_referer = "-";
|
||||
auto http_user_agent = req.get_header_value("User-Agent");
|
||||
if (http_user_agent.empty()) http_user_agent = "-";
|
||||
|
||||
std::cout << std::format("{} - {} [{}] \"{}\" {} {} \"{}\" \"{}\"",
|
||||
req.remote_addr, remote_user, time_local, request,
|
||||
status, body_bytes_sent, http_referer,
|
||||
http_user_agent)
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
// NGINX Error log format:
|
||||
// YYYY/MM/DD HH:MM:SS [level] message, client: client_ip, request: "request",
|
||||
// host: "host"
|
||||
void nginx_error_logger(const Error &err, const Request *req) {
|
||||
auto time_local = get_error_time_format();
|
||||
std::string level = "error";
|
||||
|
||||
if (req) {
|
||||
auto request =
|
||||
std::format("{} {} {}", req->method, req->path, req->version);
|
||||
auto host = req->get_header_value("Host");
|
||||
if (host.empty()) host = "-";
|
||||
|
||||
std::cerr << std::format("{} [{}] {}, client: {}, request: "
|
||||
"\"{}\", host: \"{}\"",
|
||||
time_local, level, to_string(err),
|
||||
req->remote_addr, request, host)
|
||||
<< std::endl;
|
||||
} else {
|
||||
// If no request context, just log the error
|
||||
std::cerr << std::format("{} [{}] {}", time_local, level, to_string(err))
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void print_usage(const char *program_name) {
|
||||
std::cout << "Usage: " << program_name << " [OPTIONS]" << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "Options:" << std::endl;
|
||||
std::cout << " --host <hostname> Server hostname (default: localhost)"
|
||||
<< std::endl;
|
||||
std::cout << " --port <port> Server port (default: 8080)"
|
||||
<< std::endl;
|
||||
std::cout << " --mount <mount:path> Mount point and document root"
|
||||
<< std::endl;
|
||||
std::cout << " Format: mount_point:document_root"
|
||||
<< std::endl;
|
||||
std::cout << " (default: /:./html)" << std::endl;
|
||||
std::cout << " --trusted-proxy <ip> Add trusted proxy IP address"
|
||||
<< std::endl;
|
||||
std::cout << " (can be specified multiple times)"
|
||||
<< std::endl;
|
||||
std::cout << " --version Show version information"
|
||||
<< std::endl;
|
||||
std::cout << " --help Show this help message" << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "Examples:" << std::endl;
|
||||
std::cout << " " << program_name
|
||||
<< " --host localhost --port 8080 --mount /:./html" << std::endl;
|
||||
std::cout << " " << program_name
|
||||
<< " --host 0.0.0.0 --port 3000 --mount /api:./api" << std::endl;
|
||||
std::cout << " " << program_name
|
||||
<< " --trusted-proxy 192.168.1.100 --trusted-proxy 10.0.0.1"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
struct ServerConfig {
|
||||
std::string hostname = "localhost";
|
||||
int port = 8080;
|
||||
std::string mount_point = "/";
|
||||
std::string document_root = "./html";
|
||||
std::vector<std::string> trusted_proxies;
|
||||
};
|
||||
|
||||
enum class ParseResult { SUCCESS, HELP_REQUESTED, VERSION_REQUESTED, ERROR };
|
||||
|
||||
ParseResult parse_command_line(int argc, char *argv[], ServerConfig &config) {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
|
||||
print_usage(argv[0]);
|
||||
return ParseResult::HELP_REQUESTED;
|
||||
} else if (strcmp(argv[i], "--host") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
std::cerr << "Error: --host requires a hostname argument" << std::endl;
|
||||
print_usage(argv[0]);
|
||||
return ParseResult::ERROR;
|
||||
}
|
||||
config.hostname = argv[++i];
|
||||
} else if (strcmp(argv[i], "--port") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
std::cerr << "Error: --port requires a port number argument"
|
||||
<< std::endl;
|
||||
print_usage(argv[0]);
|
||||
return ParseResult::ERROR;
|
||||
}
|
||||
config.port = std::atoi(argv[++i]);
|
||||
if (config.port <= 0 || config.port > 65535) {
|
||||
std::cerr << "Error: Invalid port number. Must be between 1 and 65535"
|
||||
<< std::endl;
|
||||
return ParseResult::ERROR;
|
||||
}
|
||||
} else if (strcmp(argv[i], "--mount") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
std::cerr
|
||||
<< "Error: --mount requires mount_point:document_root argument"
|
||||
<< std::endl;
|
||||
print_usage(argv[0]);
|
||||
return ParseResult::ERROR;
|
||||
}
|
||||
std::string mount_arg = argv[++i];
|
||||
auto colon_pos = mount_arg.find(':');
|
||||
if (colon_pos == std::string::npos) {
|
||||
std::cerr << "Error: --mount argument must be in format "
|
||||
"mount_point:document_root"
|
||||
<< std::endl;
|
||||
print_usage(argv[0]);
|
||||
return ParseResult::ERROR;
|
||||
}
|
||||
config.mount_point = mount_arg.substr(0, colon_pos);
|
||||
config.document_root = mount_arg.substr(colon_pos + 1);
|
||||
|
||||
if (config.mount_point.empty() || config.document_root.empty()) {
|
||||
std::cerr
|
||||
<< "Error: Both mount_point and document_root must be non-empty"
|
||||
<< std::endl;
|
||||
return ParseResult::ERROR;
|
||||
}
|
||||
} else if (strcmp(argv[i], "--version") == 0) {
|
||||
std::cout << CPPHTTPLIB_VERSION << std::endl;
|
||||
return ParseResult::VERSION_REQUESTED;
|
||||
} else if (strcmp(argv[i], "--trusted-proxy") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
std::cerr << "Error: --trusted-proxy requires an IP address argument"
|
||||
<< std::endl;
|
||||
print_usage(argv[0]);
|
||||
return ParseResult::ERROR;
|
||||
}
|
||||
config.trusted_proxies.push_back(argv[++i]);
|
||||
} else {
|
||||
std::cerr << "Error: Unknown option '" << argv[i] << "'" << std::endl;
|
||||
print_usage(argv[0]);
|
||||
return ParseResult::ERROR;
|
||||
}
|
||||
}
|
||||
return ParseResult::SUCCESS;
|
||||
}
|
||||
|
||||
bool setup_server(Server &svr, const ServerConfig &config) {
|
||||
svr.set_logger(nginx_access_logger);
|
||||
svr.set_error_logger(nginx_error_logger);
|
||||
|
||||
// Set trusted proxies if specified
|
||||
if (!config.trusted_proxies.empty()) {
|
||||
svr.set_trusted_proxies(config.trusted_proxies);
|
||||
}
|
||||
|
||||
auto ret = svr.set_mount_point(config.mount_point, config.document_root);
|
||||
if (!ret) {
|
||||
std::cerr
|
||||
<< std::format(
|
||||
"Error: Cannot mount '{}' to '{}'. Directory may not exist.",
|
||||
config.mount_point, config.document_root)
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
svr.set_file_extension_and_mimetype_mapping("html", "text/html");
|
||||
svr.set_file_extension_and_mimetype_mapping("htm", "text/html");
|
||||
svr.set_file_extension_and_mimetype_mapping("css", "text/css");
|
||||
svr.set_file_extension_and_mimetype_mapping("js", "text/javascript");
|
||||
svr.set_file_extension_and_mimetype_mapping("json", "application/json");
|
||||
svr.set_file_extension_and_mimetype_mapping("xml", "application/xml");
|
||||
svr.set_file_extension_and_mimetype_mapping("png", "image/png");
|
||||
svr.set_file_extension_and_mimetype_mapping("jpg", "image/jpeg");
|
||||
svr.set_file_extension_and_mimetype_mapping("jpeg", "image/jpeg");
|
||||
svr.set_file_extension_and_mimetype_mapping("gif", "image/gif");
|
||||
svr.set_file_extension_and_mimetype_mapping("svg", "image/svg+xml");
|
||||
svr.set_file_extension_and_mimetype_mapping("ico", "image/x-icon");
|
||||
svr.set_file_extension_and_mimetype_mapping("pdf", "application/pdf");
|
||||
svr.set_file_extension_and_mimetype_mapping("zip", "application/zip");
|
||||
svr.set_file_extension_and_mimetype_mapping("txt", "text/plain");
|
||||
|
||||
svr.set_error_handler([](const Request & /*req*/, Response &res) {
|
||||
if (res.status == 404) {
|
||||
res.set_content(
|
||||
std::format(
|
||||
"<html><head><title>404 Not Found</title></head>"
|
||||
"<body><h1>404 Not Found</h1>"
|
||||
"<p>The requested resource was not found on this server.</p>"
|
||||
"<hr><p>{}</p></body></html>",
|
||||
SERVER_NAME),
|
||||
"text/html");
|
||||
}
|
||||
});
|
||||
|
||||
svr.set_pre_routing_handler([](const Request & /*req*/, Response &res) {
|
||||
res.set_header("Server", SERVER_NAME);
|
||||
return Server::HandlerResponse::Unhandled;
|
||||
});
|
||||
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ServerConfig config;
|
||||
|
||||
auto result = parse_command_line(argc, argv, config);
|
||||
switch (result) {
|
||||
case ParseResult::HELP_REQUESTED:
|
||||
case ParseResult::VERSION_REQUESTED: return 0;
|
||||
case ParseResult::ERROR: return 1;
|
||||
case ParseResult::SUCCESS: break;
|
||||
}
|
||||
|
||||
if (!setup_server(svr, config)) { return 1; }
|
||||
|
||||
std::cout << "Serving HTTP on " << config.hostname << ":" << config.port
|
||||
<< std::endl;
|
||||
std::cout << "Mount point: " << config.mount_point << " -> "
|
||||
<< config.document_root << std::endl;
|
||||
|
||||
if (!config.trusted_proxies.empty()) {
|
||||
std::cout << "Trusted proxies: ";
|
||||
for (size_t i = 0; i < config.trusted_proxies.size(); ++i) {
|
||||
if (i > 0) std::cout << ", ";
|
||||
std::cout << config.trusted_proxies[i];
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Press Ctrl+C to shutdown gracefully..." << std::endl;
|
||||
|
||||
auto ret = svr.listen(config.hostname, config.port);
|
||||
|
||||
std::cout << "Server has been shut down." << std::endl;
|
||||
|
||||
return ret ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
[system]
|
||||
theme = "monotone"
|
||||
langs = ["en", "ja"]
|
||||
|
||||
[site]
|
||||
title = "cpp-httplib"
|
||||
version = "0.39.0"
|
||||
hostname = "https://yhirose.github.io"
|
||||
base_path = "/cpp-httplib"
|
||||
footer_message = "© 2026 Yuji Hirose. All rights reserved."
|
||||
|
||||
[[nav]]
|
||||
label = "Tour"
|
||||
path = "tour/"
|
||||
icon_svg = '<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>'
|
||||
|
||||
[[nav]]
|
||||
label = "Cookbook"
|
||||
path = "cookbook/"
|
||||
icon_svg = '<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="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"/><path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"/></svg>'
|
||||
|
||||
[[nav]]
|
||||
label = "LLM App"
|
||||
path = "llm-app/"
|
||||
icon_svg = '<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="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"/><path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"/></svg>'
|
||||
|
||||
[[nav]]
|
||||
label = "GitHub"
|
||||
url = "https://github.com/yhirose/cpp-httplib"
|
||||
icon_svg = '<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>'
|
||||
@@ -0,0 +1,96 @@
|
||||
---
|
||||
title: "Cookbook"
|
||||
order: 0
|
||||
status: "draft"
|
||||
---
|
||||
|
||||
A collection of recipes that answer "How do I...?" questions. Each recipe is self-contained — read only what you need.
|
||||
|
||||
## Client
|
||||
|
||||
### Basics
|
||||
- Get the response body as a string / save to a file
|
||||
- Send and receive JSON
|
||||
- Set default headers (`set_default_headers`)
|
||||
- Follow redirects (`set_follow_location`)
|
||||
|
||||
### Authentication
|
||||
- Use Basic authentication (`set_basic_auth`)
|
||||
- Call an API with a Bearer token
|
||||
|
||||
### File Upload
|
||||
- Upload a file as multipart form data (`make_file_provider`)
|
||||
- POST a file as raw binary (`make_file_body`)
|
||||
- Send the body with chunked transfer (Content Provider)
|
||||
|
||||
### Streaming & Progress
|
||||
- Receive a response as a stream
|
||||
- Use the progress callback (`DownloadProgress` / `UploadProgress`)
|
||||
|
||||
### Connection & Performance
|
||||
- Set timeouts (`set_connection_timeout` / `set_read_timeout`)
|
||||
- Set an overall timeout (`set_max_timeout`)
|
||||
- Understand connection reuse and Keep-Alive behavior
|
||||
- Enable compression (`set_compress` / `set_decompress`)
|
||||
- Send requests through a proxy (`set_proxy`)
|
||||
|
||||
### Error Handling & Debugging
|
||||
- Handle error codes (`Result::error()`)
|
||||
- Handle SSL errors (`ssl_error()` / `ssl_backend_error()`)
|
||||
- Set up client logging (`set_logger` / `set_error_logger`)
|
||||
|
||||
## Server
|
||||
|
||||
### Basics
|
||||
- Register GET / POST / PUT / DELETE handlers
|
||||
- Receive JSON requests and return JSON responses
|
||||
- Use path parameters (`/users/:id`)
|
||||
- Set up a static file server (`set_mount_point`)
|
||||
|
||||
### Streaming & Files
|
||||
- Stream a large file in the response (`ContentProvider`)
|
||||
- Return a file download response (`Content-Disposition`)
|
||||
- Receive multipart data as a stream (`ContentReader`)
|
||||
- Compress responses (gzip)
|
||||
|
||||
### Handler Chain
|
||||
- Add pre-processing to all routes (Pre-routing handler)
|
||||
- Add response headers with a Post-routing handler (CORS, etc.)
|
||||
- Authenticate per route with a Pre-request handler (`matched_route`)
|
||||
- Pass data between handlers with `res.user_data`
|
||||
|
||||
### Error Handling & Debugging
|
||||
- Return custom error pages (`set_error_handler`)
|
||||
- Catch exceptions (`set_exception_handler`)
|
||||
- Log requests (Logger)
|
||||
- Detect client disconnection (`req.is_connection_closed()`)
|
||||
|
||||
### Operations & Tuning
|
||||
- Assign a port dynamically (`bind_to_any_port`)
|
||||
- Control startup order with `listen_after_bind`
|
||||
- Shut down gracefully (`stop()` and signal handling)
|
||||
- Tune Keep-Alive (`set_keep_alive_max_count` / `set_keep_alive_timeout`)
|
||||
- Configure the thread pool (`new_task_queue`)
|
||||
|
||||
## TLS / Security
|
||||
|
||||
- Choosing between OpenSSL, mbedTLS, and wolfSSL (build-time `#define` differences)
|
||||
- Control SSL certificate verification (disable, custom CA, custom callback)
|
||||
- Use a custom certificate verification callback (`set_server_certificate_verifier`)
|
||||
- Set up an SSL/TLS server (certificate and private key)
|
||||
- Configure mTLS (mutual TLS with client certificates)
|
||||
- Access the peer certificate on the server (`req.peer_cert()` / SNI)
|
||||
|
||||
## SSE
|
||||
|
||||
- Implement an SSE server
|
||||
- Use event names to distinguish event types
|
||||
- Handle reconnection (`Last-Event-ID`)
|
||||
- Receive SSE events on the client
|
||||
|
||||
## WebSocket
|
||||
|
||||
- Implement a WebSocket echo server and client
|
||||
- Configure heartbeats (`set_websocket_ping_interval` / `CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND`)
|
||||
- Handle connection close
|
||||
- Send and receive binary frames
|
||||
@@ -0,0 +1,25 @@
|
||||
---
|
||||
title: "cpp-httplib"
|
||||
order: 0
|
||||
---
|
||||
|
||||
[cpp-httplib](https://github.com/yhirose/cpp-httplib) is an HTTP/HTTPS library for C++. Just copy a single header file, [`httplib.h`](https://github.com/yhirose/cpp-httplib/raw/refs/tags/latest/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 works too. Just link OpenSSL or mbedTLS, and both server and client gain TLS support. Content-Encoding (gzip, Brotli, etc.), file uploads, and other features you actually need in real-world development are all included. WebSocket is also supported.
|
||||
|
||||
Under the hood, it uses blocking I/O with a thread pool. It's not built for handling massive numbers of simultaneous connections. But for API servers, embedded HTTP in tools, mock servers for testing, and many other use cases, it delivers solid performance.
|
||||
|
||||
"Solve today's problem, today." That's the kind of simplicity cpp-httplib aims for.
|
||||
|
||||
## Documentation
|
||||
|
||||
- [A Tour of cpp-httplib](tour/) — A step-by-step tutorial covering the basics. Start here if you're new
|
||||
- [Building a Desktop LLM App](llm-app/) — A hands-on guide to building a desktop app with llama.cpp, step by step
|
||||
|
||||
## Stay Tuned
|
||||
|
||||
- [Cookbook](cookbook/) — A collection of recipes organized by topic. Jump to whatever you need
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 120 KiB |
@@ -0,0 +1,236 @@
|
||||
---
|
||||
title: "1. Setting Up the Project Environment"
|
||||
order: 1
|
||||
|
||||
---
|
||||
|
||||
Let's incrementally build a text translation REST API server using llama.cpp as the inference engine. By the end, a request like this will return a translation result.
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8080/translate \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"text": "The weather is nice today. Shall we go for a walk?", "target_lang": "ja"}'
|
||||
```
|
||||
|
||||
```json
|
||||
{
|
||||
"translation": "今日はいい天気ですね。散歩に行きましょうか?"
|
||||
}
|
||||
```
|
||||
|
||||
The "Translation API" is just one example. By swapping out the prompt, you can adapt this to any LLM application you like, such as summarization, code generation, or a chatbot.
|
||||
|
||||
Here's the full list of APIs the server will provide.
|
||||
|
||||
| Method | Path | Description | Chapter |
|
||||
| -------- | ---- | ---- | -- |
|
||||
| `GET` | `/health` | Returns server status | 1 |
|
||||
| `POST` | `/translate` | Translates text and returns JSON | 2 |
|
||||
| `POST` | `/translate/stream` | SSE streaming on a per-token basis | 3 |
|
||||
| `GET` | `/models` | Model list (available / downloaded / selected) | 4 |
|
||||
| `POST` | `/models/select` | Select a model (automatically downloads if not yet downloaded) | 4 |
|
||||
|
||||
In this chapter, let's set up the project environment. We'll fetch the dependency libraries, create the directory structure, configure the build settings, and grab the model file, so that we're ready to start writing code in the next chapter.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- A C++20-compatible compiler (GCC 10+, Clang 10+, MSVC 2019 16.8+)
|
||||
- CMake 3.20 or later
|
||||
- OpenSSL (used for the HTTPS client in Chapter 4. macOS: `brew install openssl`, Ubuntu: `sudo apt install libssl-dev`)
|
||||
- Sufficient disk space (model files can be several GB)
|
||||
|
||||
## 1.1 What We Will Use
|
||||
|
||||
Here are the libraries we'll use.
|
||||
|
||||
| Library | Role |
|
||||
| ----------- | ------ |
|
||||
| [cpp-httplib](https://github.com/yhirose/cpp-httplib) | HTTP server/client |
|
||||
| [nlohmann/json](https://github.com/nlohmann/json) | JSON parser |
|
||||
| [cpp-llamalib](https://github.com/yhirose/cpp-llamalib) | llama.cpp wrapper |
|
||||
| [llama.cpp](https://github.com/ggml-org/llama.cpp) | LLM inference engine |
|
||||
| [webview/webview](https://github.com/webview/webview) | Desktop WebView (used in Chapter 6) |
|
||||
|
||||
cpp-httplib, nlohmann/json, and cpp-llamalib are header-only libraries. You could just download a single header file with `curl` and `#include` it, but in this book we use CMake's `FetchContent` to fetch them automatically. Declare them in `CMakeLists.txt`, and `cmake -B build` downloads and builds everything for you. webview is used in Chapter 6, so you don't need to worry about it for now.
|
||||
|
||||
## 1.2 Directory Structure
|
||||
|
||||
The final structure will look like this.
|
||||
|
||||
```ascii
|
||||
translate-app/
|
||||
├── CMakeLists.txt
|
||||
├── models/
|
||||
│ └── (GGUF files)
|
||||
└── src/
|
||||
└── main.cpp
|
||||
```
|
||||
|
||||
We don't include library source code in the project. CMake's `FetchContent` fetches them automatically at build time, so all you need is your own code.
|
||||
|
||||
Let's create the project directory and initialize a git repository.
|
||||
|
||||
```bash
|
||||
mkdir translate-app && cd translate-app
|
||||
mkdir src models
|
||||
git init
|
||||
```
|
||||
|
||||
## 1.3 Obtaining the GGUF Model File
|
||||
|
||||
You need a model file for LLM inference. GGUF is the model format used by llama.cpp, and you can find many models on Hugging Face.
|
||||
|
||||
Let's start by trying a small model. The quantized version of Google's Gemma 2 2B (~1.6 GB) is a good starting point. It's lightweight but supports multiple languages and works well for translation tasks.
|
||||
|
||||
```bash
|
||||
curl -L -o models/gemma-2-2b-it-Q4_K_M.gguf \
|
||||
https://huggingface.co/bartowski/gemma-2-2b-it-GGUF/resolve/main/gemma-2-2b-it-Q4_K_M.gguf
|
||||
```
|
||||
|
||||
In Chapter 4, we'll add the ability to download models from within the app using cpp-httplib's client functionality.
|
||||
|
||||
## 1.4 CMakeLists.txt
|
||||
|
||||
Create a `CMakeLists.txt` in the project root. By declaring dependencies with `FetchContent`, CMake will automatically download and build them for you.
|
||||
|
||||
<!-- data-file="CMakeLists.txt" -->
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(translate-server CXX)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
include(FetchContent)
|
||||
|
||||
# llama.cpp (LLM inference engine)
|
||||
FetchContent_Declare(llama
|
||||
GIT_REPOSITORY https://github.com/ggml-org/llama.cpp
|
||||
GIT_TAG master
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(llama)
|
||||
|
||||
# cpp-httplib (HTTP server/client)
|
||||
FetchContent_Declare(httplib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-httplib
|
||||
GIT_TAG master
|
||||
)
|
||||
FetchContent_MakeAvailable(httplib)
|
||||
|
||||
# nlohmann/json (JSON parser)
|
||||
FetchContent_Declare(json
|
||||
URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
|
||||
)
|
||||
FetchContent_MakeAvailable(json)
|
||||
|
||||
# cpp-llamalib (header-only llama.cpp wrapper)
|
||||
FetchContent_Declare(cpp_llamalib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-llamalib
|
||||
GIT_TAG main
|
||||
)
|
||||
FetchContent_MakeAvailable(cpp_llamalib)
|
||||
|
||||
add_executable(translate-server src/main.cpp)
|
||||
|
||||
target_link_libraries(translate-server PRIVATE
|
||||
httplib::httplib
|
||||
nlohmann_json::nlohmann_json
|
||||
cpp-llamalib
|
||||
)
|
||||
```
|
||||
|
||||
`FetchContent_Declare` tells CMake where to find each library, and `FetchContent_MakeAvailable` fetches and builds them. The first `cmake -B build` will take some time because it downloads all libraries and builds llama.cpp, but subsequent runs will use the cache.
|
||||
|
||||
Just link with `target_link_libraries`, and each library's CMake configuration sets up include paths and build settings for you.
|
||||
|
||||
## 1.5 Creating the Skeleton Code
|
||||
|
||||
We'll use this skeleton code as a base and add functionality chapter by chapter.
|
||||
|
||||
<!-- data-file="main.cpp" -->
|
||||
```cpp
|
||||
// src/main.cpp
|
||||
#include <httplib.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
httplib::Server svr;
|
||||
|
||||
// Graceful shutdown on `Ctrl+C`
|
||||
void signal_handler(int sig) {
|
||||
if (sig == SIGINT || sig == SIGTERM) {
|
||||
std::cout << "\nReceived signal, shutting down gracefully...\n";
|
||||
svr.stop();
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Log requests and responses
|
||||
svr.set_logger([](const auto &req, const auto &res) {
|
||||
std::cout << req.method << " " << req.path << " -> " << res.status
|
||||
<< std::endl;
|
||||
});
|
||||
|
||||
// Health check
|
||||
svr.Get("/health", [](const auto &, auto &res) {
|
||||
res.set_content(json{{"status", "ok"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// Stub implementations for each endpoint (replaced with real ones in later chapters)
|
||||
svr.Post("/translate",
|
||||
[](const auto &req, auto &res) {
|
||||
res.set_content(json{{"translation", "TODO"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
svr.Post("/translate/stream",
|
||||
[](const auto &req, auto &res) {
|
||||
res.set_content("data: \"TODO\"\n\ndata: [DONE]\n\n", "text/event-stream");
|
||||
});
|
||||
|
||||
svr.Get("/models",
|
||||
[](const auto &req, auto &res) {
|
||||
res.set_content(json{{"models", json::array()}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
svr.Post("/models/select",
|
||||
[](const auto &req, auto &res) {
|
||||
res.set_content(json{{"status", "TODO"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// Allow the server to be stopped with `Ctrl+C` (`SIGINT`) or `kill` (`SIGTERM`)
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
||||
// Start the server
|
||||
std::cout << "Listening on http://127.0.0.1:8080" << std::endl;
|
||||
svr.listen("127.0.0.1", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
## 1.6 Building and Verifying
|
||||
|
||||
Build the project, start the server, and verify that requests work with curl.
|
||||
|
||||
```bash
|
||||
cmake -B build
|
||||
cmake --build build -j
|
||||
./build/translate-server
|
||||
```
|
||||
|
||||
From another terminal, try it with curl.
|
||||
|
||||
```bash
|
||||
curl http://localhost:8080/health
|
||||
# => {"status":"ok"}
|
||||
```
|
||||
|
||||
If you see JSON come back, the setup is complete.
|
||||
|
||||
## Next Chapter
|
||||
|
||||
Now that the environment is set up, in the next chapter we'll implement the translation REST API on top of this skeleton. We'll run inference with llama.cpp and expose it as an HTTP endpoint with cpp-httplib.
|
||||
|
||||
**Next:** [Integrating llama.cpp to Build a REST API](../ch02-rest-api)
|
||||
@@ -0,0 +1,212 @@
|
||||
---
|
||||
title: "2. Integrating llama.cpp to Build a REST API"
|
||||
order: 2
|
||||
|
||||
---
|
||||
|
||||
In the skeleton from Chapter 1, `/translate` simply returned `"TODO"`. In this chapter we integrate llama.cpp inference and turn it into an API that actually returns translation results.
|
||||
|
||||
Calling the llama.cpp API directly makes the code quite long, so we use a thin wrapper library called [cpp-llamalib](https://github.com/yhirose/cpp-llamalib). It lets you load a model and run inference in just a few lines, keeping the focus on cpp-httplib.
|
||||
|
||||
## 2.1 Initializing the LLM
|
||||
|
||||
Simply pass the path to a model file to `llamalib::Llama`, and model loading, context creation, and sampler configuration are all taken care of. If you downloaded a different model in Chapter 1, adjust the path accordingly.
|
||||
|
||||
```cpp
|
||||
#include <cpp-llamalib.h>
|
||||
|
||||
int main() {
|
||||
auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf"};
|
||||
|
||||
// LLM inference takes time, so set a longer timeout (default is 5 seconds)
|
||||
svr.set_read_timeout(300);
|
||||
svr.set_write_timeout(300);
|
||||
|
||||
// ... Build and start the HTTP server ...
|
||||
}
|
||||
```
|
||||
|
||||
If you want to change the number of GPU layers, context length, or other settings, you can specify them via `llamalib::Options`.
|
||||
|
||||
```cpp
|
||||
auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf", {
|
||||
.n_gpu_layers = 0, // CPU only
|
||||
.n_ctx = 4096,
|
||||
}};
|
||||
```
|
||||
|
||||
## 2.2 The `/translate` Handler
|
||||
|
||||
We replace the handler that returned dummy JSON in Chapter 1 with actual inference.
|
||||
|
||||
```cpp
|
||||
svr.Post("/translate",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
// Parse JSON (3rd arg `false`: don't throw on failure, check with `is_discarded()`)
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate required fields
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja"); // Default is Japanese
|
||||
|
||||
// Build the prompt and run inference
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
try {
|
||||
auto translation = llm.chat(prompt);
|
||||
res.set_content(json{{"translation", translation}}.dump(),
|
||||
"application/json");
|
||||
} catch (const std::exception &e) {
|
||||
res.status = 500;
|
||||
res.set_content(json{{"error", e.what()}}.dump(), "application/json");
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
`llm.chat()` can throw exceptions during inference (for example, when the context length is exceeded). By catching them with `try/catch` and returning the error as JSON, we prevent the server from crashing.
|
||||
|
||||
## 2.3 Complete Code
|
||||
|
||||
Here is the finished code with all the changes so far.
|
||||
|
||||
<details>
|
||||
<summary data-file="main.cpp">Complete code (main.cpp)</summary>
|
||||
|
||||
```cpp
|
||||
#include <httplib.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <cpp-llamalib.h>
|
||||
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
httplib::Server svr;
|
||||
|
||||
// Graceful shutdown on `Ctrl+C`
|
||||
void signal_handler(int sig) {
|
||||
if (sig == SIGINT || sig == SIGTERM) {
|
||||
std::cout << "\nReceived signal, shutting down gracefully...\n";
|
||||
svr.stop();
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Load the model downloaded in Chapter 1
|
||||
auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf"};
|
||||
|
||||
// LLM inference takes time, so set a longer timeout (default is 5 seconds)
|
||||
svr.set_read_timeout(300);
|
||||
svr.set_write_timeout(300);
|
||||
|
||||
// Log requests and responses
|
||||
svr.set_logger([](const auto &req, const auto &res) {
|
||||
std::cout << req.method << " " << req.path << " -> " << res.status
|
||||
<< std::endl;
|
||||
});
|
||||
|
||||
svr.Get("/health", [](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"status", "ok"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
svr.Post("/translate",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
// Parse JSON (3rd arg `false`: don't throw on failure, check with `is_discarded()`)
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate required fields
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja"); // Default is Japanese
|
||||
|
||||
// Build the prompt and run inference
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
try {
|
||||
auto translation = llm.chat(prompt);
|
||||
res.set_content(json{{"translation", translation}}.dump(),
|
||||
"application/json");
|
||||
} catch (const std::exception &e) {
|
||||
res.status = 500;
|
||||
res.set_content(json{{"error", e.what()}}.dump(), "application/json");
|
||||
}
|
||||
});
|
||||
|
||||
// Dummy implementations to be replaced with real ones in later chapters
|
||||
svr.Get("/models",
|
||||
[](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"models", json::array()}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
svr.Post("/models/select",
|
||||
[](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"status", "TODO"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// Allow the server to be stopped with `Ctrl+C` (`SIGINT`) or `kill` (`SIGTERM`)
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
||||
// Start the server (blocks until `stop()` is called)
|
||||
std::cout << "Listening on http://127.0.0.1:8080" << std::endl;
|
||||
svr.listen("127.0.0.1", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
## 2.4 Testing It Out
|
||||
|
||||
Rebuild and start the server, then verify that it now returns actual translation results.
|
||||
|
||||
```bash
|
||||
cmake --build build -j
|
||||
./build/translate-server
|
||||
```
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8080/translate \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"text": "I had a great time visiting Tokyo last spring. The cherry blossoms were beautiful.", "target_lang": "ja"}'
|
||||
# => {"translation":"去年の春に東京を訪れた。桜が綺麗だった。"}
|
||||
```
|
||||
|
||||
In Chapter 1 the response was `"TODO"`, but now you get an actual translation back.
|
||||
|
||||
## Next Chapter
|
||||
|
||||
The REST API we built in this chapter waits for the entire translation to complete before sending the response, so for long texts the user has to wait with no indication of progress.
|
||||
|
||||
In the next chapter, we use SSE (Server-Sent Events) to stream tokens back in real time as they are generated.
|
||||
|
||||
**Next:** [Adding Token Streaming with SSE](../ch03-sse-streaming)
|
||||
@@ -0,0 +1,264 @@
|
||||
---
|
||||
title: "3. Adding Token Streaming with SSE"
|
||||
order: 3
|
||||
|
||||
---
|
||||
|
||||
The `/translate` endpoint from Chapter 2 returned the entire translation at once after completion. This is fine for short sentences, but for longer text the user has to wait several seconds with nothing displayed.
|
||||
|
||||
In this chapter, we add a `/translate/stream` endpoint that uses SSE (Server-Sent Events) to return tokens in real time as they are generated. This is the same approach used by the ChatGPT and Claude APIs.
|
||||
|
||||
## 3.1 What is SSE?
|
||||
|
||||
SSE is a way to send HTTP responses as a stream. When a client sends a request, the server keeps the connection open and gradually returns events. The format is simple text.
|
||||
|
||||
```text
|
||||
data: "去年の"
|
||||
data: "春に"
|
||||
data: "東京を"
|
||||
data: [DONE]
|
||||
```
|
||||
|
||||
Each line starts with `data:` and events are separated by blank lines. The Content-Type is `text/event-stream`. Tokens are sent as escaped JSON strings, so they appear enclosed in double quotes (we implement this in Section 3.3).
|
||||
|
||||
## 3.2 Streaming with cpp-httplib
|
||||
|
||||
In cpp-httplib, you can use `set_chunked_content_provider` to send responses incrementally. Each time you write to `sink.os` inside the callback, data is sent to the client.
|
||||
|
||||
```cpp
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[](size_t offset, httplib::DataSink &sink) {
|
||||
sink.os << "data: hello\n\n";
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
```
|
||||
|
||||
Calling `sink.done()` ends the stream. If the client disconnects mid-stream, writing to `sink.os` will fail and `sink.os.fail()` will return `true`. You can use this to detect disconnection and abort unnecessary inference.
|
||||
|
||||
## 3.3 The `/translate/stream` Handler
|
||||
|
||||
JSON parsing and validation are the same as the `/translate` endpoint from Chapter 2. The only difference is how the response is returned. We combine the streaming callback of `llm.chat()` with `set_chunked_content_provider`.
|
||||
|
||||
```cpp
|
||||
svr.Post("/translate/stream",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
// ... JSON parsing and validation same as /translate ...
|
||||
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, prompt](size_t, httplib::DataSink &sink) {
|
||||
try {
|
||||
llm.chat(prompt, [&](std::string_view token) {
|
||||
sink.os << "data: "
|
||||
<< json(std::string(token)).dump(
|
||||
-1, ' ', false, json::error_handler_t::replace)
|
||||
<< "\n\n";
|
||||
return sink.os.good(); // Abort inference on disconnect
|
||||
});
|
||||
sink.os << "data: [DONE]\n\n";
|
||||
} catch (const std::exception &e) {
|
||||
sink.os << "data: " << json({{"error", e.what()}}).dump() << "\n\n";
|
||||
}
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
A few key points:
|
||||
|
||||
- When you pass a callback to `llm.chat()`, it is called each time a token is generated. If the callback returns `false`, generation is aborted
|
||||
- After writing to `sink.os`, you can check whether the client is still connected with `sink.os.good()`. If the client has disconnected, it returns `false` to stop inference
|
||||
- Each token is escaped as a JSON string using `json(token).dump()` before sending. This is safe even for tokens containing newlines or quotes
|
||||
- The first three arguments of `dump(-1, ' ', false, ...)` are the defaults. What matters is the fourth argument, `json::error_handler_t::replace`. Since the LLM returns tokens at the subword level, multi-byte characters (such as Japanese) can be split mid-character across tokens. Passing an incomplete UTF-8 byte sequence directly to `dump()` would throw an exception, so `replace` safely substitutes them. The browser reassembles the bytes on its end, so everything displays correctly
|
||||
- The entire lambda is wrapped in `try/catch`. `llm.chat()` can throw exceptions for reasons such as exceeding the context window. If an exception goes uncaught inside the lambda, the server will crash, so we return the error as an SSE event instead
|
||||
- `data: [DONE]` follows the OpenAI API convention to signal the end of the stream to the client
|
||||
|
||||
## 3.4 Complete Code
|
||||
|
||||
Here is the complete code with the `/translate/stream` endpoint added to the code from Chapter 2.
|
||||
|
||||
<details>
|
||||
<summary data-file="main.cpp">Complete code (main.cpp)</summary>
|
||||
|
||||
```cpp
|
||||
#include <httplib.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <cpp-llamalib.h>
|
||||
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
httplib::Server svr;
|
||||
|
||||
// Graceful shutdown on `Ctrl+C`
|
||||
void signal_handler(int sig) {
|
||||
if (sig == SIGINT || sig == SIGTERM) {
|
||||
std::cout << "\nReceived signal, shutting down gracefully...\n";
|
||||
svr.stop();
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Load the GGUF model
|
||||
auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf"};
|
||||
|
||||
// LLM inference takes time, so set a longer timeout (default is 5 seconds)
|
||||
svr.set_read_timeout(300);
|
||||
svr.set_write_timeout(300);
|
||||
|
||||
// Log requests and responses
|
||||
svr.set_logger([](const auto &req, const auto &res) {
|
||||
std::cout << req.method << " " << req.path << " -> " << res.status
|
||||
<< std::endl;
|
||||
});
|
||||
|
||||
svr.Get("/health", [](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"status", "ok"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// Standard translation endpoint from Chapter 2
|
||||
svr.Post("/translate",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
// JSON parsing and validation (see Chapter 2 for details)
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja");
|
||||
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
try {
|
||||
auto translation = llm.chat(prompt);
|
||||
res.set_content(json{{"translation", translation}}.dump(),
|
||||
"application/json");
|
||||
} catch (const std::exception &e) {
|
||||
res.status = 500;
|
||||
res.set_content(json{{"error", e.what()}}.dump(), "application/json");
|
||||
}
|
||||
});
|
||||
|
||||
// SSE streaming translation endpoint
|
||||
svr.Post("/translate/stream",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
// JSON parsing and validation (same as /translate)
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja");
|
||||
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, prompt](size_t, httplib::DataSink &sink) {
|
||||
try {
|
||||
llm.chat(prompt, [&](std::string_view token) {
|
||||
sink.os << "data: "
|
||||
<< json(std::string(token)).dump(
|
||||
-1, ' ', false, json::error_handler_t::replace)
|
||||
<< "\n\n";
|
||||
return sink.os.good(); // Abort inference on disconnect
|
||||
});
|
||||
sink.os << "data: [DONE]\n\n";
|
||||
} catch (const std::exception &e) {
|
||||
sink.os << "data: " << json({{"error", e.what()}}).dump() << "\n\n";
|
||||
}
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// Dummy implementations to be replaced in later chapters
|
||||
svr.Get("/models",
|
||||
[](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"models", json::array()}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
svr.Post("/models/select",
|
||||
[](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"status", "TODO"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// Allow the server to be stopped with `Ctrl+C` (`SIGINT`) or `kill` (`SIGTERM`)
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
||||
// Start the server (blocks until `stop()` is called)
|
||||
std::cout << "Listening on http://127.0.0.1:8080" << std::endl;
|
||||
svr.listen("127.0.0.1", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
## 3.5 Testing It Out
|
||||
|
||||
Build and start the server.
|
||||
|
||||
```bash
|
||||
cmake --build build -j
|
||||
./build/translate-server
|
||||
```
|
||||
|
||||
Using curl's `-N` option to disable buffering, you can see tokens displayed in real time as they arrive.
|
||||
|
||||
```bash
|
||||
curl -N -X POST http://localhost:8080/translate/stream \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"text": "I had a great time visiting Tokyo last spring. The cherry blossoms were beautiful.", "target_lang": "ja"}'
|
||||
```
|
||||
|
||||
```text
|
||||
data: "去年の"
|
||||
data: "春に"
|
||||
data: "東京を"
|
||||
data: "訪れた"
|
||||
data: "。"
|
||||
data: "桜が"
|
||||
data: "綺麗だった"
|
||||
data: "。"
|
||||
data: [DONE]
|
||||
```
|
||||
|
||||
You should see tokens streaming in one by one. The `/translate` endpoint from Chapter 2 continues to work as well.
|
||||
|
||||
## Next Chapter
|
||||
|
||||
The server's translation functionality is now complete. In the next chapter, we use cpp-httplib's client functionality to add the ability to fetch and manage models from Hugging Face.
|
||||
|
||||
**Next:** [Adding Model Download and Management](../ch04-model-management)
|
||||
@@ -0,0 +1,788 @@
|
||||
---
|
||||
title: "4. Adding Model Download and Management"
|
||||
order: 4
|
||||
|
||||
---
|
||||
|
||||
By the end of Chapter 3, the server's translation functionality was fully in place. However, the only model file available is the one we manually downloaded in Chapter 1. In this chapter, we'll use cpp-httplib's **client functionality** to enable downloading and switching Hugging Face models from within the app.
|
||||
|
||||
Once complete, you'll be able to manage models with requests like these:
|
||||
|
||||
```bash
|
||||
# Get the list of available models
|
||||
curl http://localhost:8080/models
|
||||
```
|
||||
|
||||
```json
|
||||
{
|
||||
"models": [
|
||||
{"name": "gemma-2-2b-it", "params": "2B", "size": "1.6 GB", "downloaded": true, "selected": true},
|
||||
{"name": "gemma-2-9b-it", "params": "9B", "size": "5.8 GB", "downloaded": false, "selected": false},
|
||||
{"name": "Llama-3.1-8B-Instruct", "params": "8B", "size": "4.9 GB", "downloaded": false, "selected": false}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
```bash
|
||||
# Select a different model (automatically downloads if not yet available)
|
||||
curl -N -X POST http://localhost:8080/models/select \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"model": "gemma-2-9b-it"}'
|
||||
```
|
||||
|
||||
```text
|
||||
data: {"status":"downloading","progress":0}
|
||||
data: {"status":"downloading","progress":12}
|
||||
...
|
||||
data: {"status":"downloading","progress":100}
|
||||
data: {"status":"loading"}
|
||||
data: {"status":"ready"}
|
||||
```
|
||||
|
||||
## 4.1 httplib::Client Basics
|
||||
|
||||
So far we've only used `httplib::Server`, but cpp-httplib also provides client functionality. Since Hugging Face uses HTTPS, we need a TLS-capable client.
|
||||
|
||||
```cpp
|
||||
#include <httplib.h>
|
||||
|
||||
// Including the URL scheme automatically uses SSLClient
|
||||
httplib::Client cli("https://huggingface.co");
|
||||
|
||||
// Automatically follow redirects (Hugging Face redirects to a CDN)
|
||||
cli.set_follow_location(true);
|
||||
|
||||
auto res = cli.Get("/api/models");
|
||||
if (res && res->status == 200) {
|
||||
std::cout << res->body << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
To use HTTPS, you need to enable OpenSSL at build time. Add the following to your `CMakeLists.txt`:
|
||||
|
||||
```cmake
|
||||
find_package(OpenSSL REQUIRED)
|
||||
|
||||
target_link_libraries(translate-server PRIVATE OpenSSL::SSL OpenSSL::Crypto)
|
||||
target_compile_definitions(translate-server PRIVATE CPPHTTPLIB_OPENSSL_SUPPORT)
|
||||
|
||||
# macOS: required for loading system certificates
|
||||
if(APPLE)
|
||||
target_link_libraries(translate-server PRIVATE "-framework CoreFoundation" "-framework Security")
|
||||
endif()
|
||||
```
|
||||
|
||||
Defining `CPPHTTPLIB_OPENSSL_SUPPORT` enables `httplib::Client("https://...")` to make TLS connections. On macOS, you also need to link the CoreFoundation and Security frameworks to access the system certificate store. See Section 4.8 for the complete `CMakeLists.txt`.
|
||||
|
||||
## 4.2 Defining the Model List
|
||||
|
||||
Let's define the list of models that the app can handle. Here are four models we've verified for translation tasks.
|
||||
|
||||
```cpp
|
||||
struct ModelInfo {
|
||||
std::string name; // Display name
|
||||
std::string params; // Parameter count
|
||||
std::string size; // GGUF Q4 size
|
||||
std::string repo; // Hugging Face repository
|
||||
std::string filename; // GGUF filename
|
||||
};
|
||||
|
||||
const std::vector<ModelInfo> MODELS = {
|
||||
{
|
||||
.name = "gemma-2-2b-it",
|
||||
.params = "2B",
|
||||
.size = "1.6 GB",
|
||||
.repo = "bartowski/gemma-2-2b-it-GGUF",
|
||||
.filename = "gemma-2-2b-it-Q4_K_M.gguf",
|
||||
},
|
||||
{
|
||||
.name = "gemma-2-9b-it",
|
||||
.params = "9B",
|
||||
.size = "5.8 GB",
|
||||
.repo = "bartowski/gemma-2-9b-it-GGUF",
|
||||
.filename = "gemma-2-9b-it-Q4_K_M.gguf",
|
||||
},
|
||||
{
|
||||
.name = "Llama-3.1-8B-Instruct",
|
||||
.params = "8B",
|
||||
.size = "4.9 GB",
|
||||
.repo = "bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
|
||||
.filename = "Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
## 4.3 Model Storage Location
|
||||
|
||||
Up through Chapter 3, we stored models in the `models/` directory within the project. However, when managing multiple models, a dedicated app directory makes more sense. On macOS/Linux we use `~/.translate-app/models/`, and on Windows we use `%APPDATA%\translate-app\models\`.
|
||||
|
||||
```cpp
|
||||
std::filesystem::path get_models_dir() {
|
||||
#ifdef _WIN32
|
||||
auto env = std::getenv("APPDATA");
|
||||
auto base = env ? std::filesystem::path(env) : std::filesystem::path(".");
|
||||
return base / "translate-app" / "models";
|
||||
#else
|
||||
auto env = std::getenv("HOME");
|
||||
auto base = env ? std::filesystem::path(env) : std::filesystem::path(".");
|
||||
return base / ".translate-app" / "models";
|
||||
#endif
|
||||
}
|
||||
```
|
||||
|
||||
If the environment variable isn't set, it falls back to the current directory. The app creates this directory at startup (`create_directories` won't error even if it already exists).
|
||||
|
||||
## 4.4 Rewriting Model Initialization
|
||||
|
||||
We rewrite the model initialization at the beginning of `main()`. In Chapter 1 we hardcoded the path, but from here on we support model switching. We track the currently loaded filename in `selected_model` and load the first entry in `MODELS` at startup. The `GET /models` and `POST /models/select` handlers reference and update this variable.
|
||||
|
||||
Since cpp-httplib runs handlers concurrently on a thread pool, reassigning `llm` while another thread is calling `llm.chat()` would crash. We add a `std::mutex` to protect against this.
|
||||
|
||||
```cpp
|
||||
int main() {
|
||||
auto models_dir = get_models_dir();
|
||||
std::filesystem::create_directories(models_dir);
|
||||
|
||||
std::string selected_model = MODELS[0].filename;
|
||||
auto path = models_dir / selected_model;
|
||||
|
||||
// Automatically download the default model if not yet present
|
||||
if (!std::filesystem::exists(path)) {
|
||||
std::cout << "Downloading " << selected_model << "..." << std::endl;
|
||||
if (!download_model(MODELS[0], [](int pct) {
|
||||
std::cout << "\r" << pct << "%" << std::flush;
|
||||
return true;
|
||||
})) {
|
||||
std::cerr << "\nFailed to download model." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
auto llm = llamalib::Llama{path};
|
||||
std::mutex llm_mutex; // Protect access during model switching
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
This ensures that users don't need to manually download models with curl on first launch. It uses the `download_model` function from Section 4.6 and displays progress on the console.
|
||||
|
||||
## 4.5 The `GET /models` Handler
|
||||
|
||||
This returns the model list with information about whether each model has been downloaded and whether it's currently selected.
|
||||
|
||||
```cpp
|
||||
svr.Get("/models",
|
||||
[&](const httplib::Request &, httplib::Response &res) {
|
||||
auto arr = json::array();
|
||||
for (const auto &m : MODELS) {
|
||||
auto path = get_models_dir() / m.filename;
|
||||
arr.push_back({
|
||||
{"name", m.name},
|
||||
{"params", m.params},
|
||||
{"size", m.size},
|
||||
{"downloaded", std::filesystem::exists(path)},
|
||||
{"selected", m.filename == selected_model},
|
||||
});
|
||||
}
|
||||
res.set_content(json{{"models", arr}}.dump(), "application/json");
|
||||
});
|
||||
```
|
||||
|
||||
## 4.6 Downloading Large Files
|
||||
|
||||
GGUF models are several gigabytes, so we can't load the entire file into memory. By passing callbacks to `httplib::Client::Get`, we can receive data chunk by chunk.
|
||||
|
||||
```cpp
|
||||
// content_receiver: callback that receives data chunks
|
||||
// progress: download progress callback
|
||||
cli.Get(url,
|
||||
[&](const char *data, size_t len) { // content_receiver
|
||||
ofs.write(data, len);
|
||||
return true; // returning false aborts the download
|
||||
},
|
||||
[&](size_t current, size_t total) { // progress
|
||||
int pct = total ? (int)(current * 100 / total) : 0;
|
||||
std::cout << pct << "%" << std::endl;
|
||||
return true; // returning false aborts the download
|
||||
});
|
||||
```
|
||||
|
||||
Let's use this to create a function that downloads models from Hugging Face.
|
||||
|
||||
```cpp
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
// Download a model and report progress via progress_cb.
|
||||
// If progress_cb returns false, the download is aborted.
|
||||
bool download_model(const ModelInfo &model,
|
||||
std::function<bool(int)> progress_cb) {
|
||||
httplib::Client cli("https://huggingface.co");
|
||||
cli.set_follow_location(true);
|
||||
cli.set_read_timeout(std::chrono::hours(1));
|
||||
|
||||
auto url = "/" + model.repo + "/resolve/main/" + model.filename;
|
||||
auto path = get_models_dir() / model.filename;
|
||||
auto tmp_path = std::filesystem::path(path).concat(".tmp");
|
||||
|
||||
std::ofstream ofs(tmp_path, std::ios::binary);
|
||||
if (!ofs) { return false; }
|
||||
|
||||
auto res = cli.Get(url,
|
||||
[&](const char *data, size_t len) {
|
||||
ofs.write(data, len);
|
||||
return ofs.good();
|
||||
},
|
||||
[&](size_t current, size_t total) {
|
||||
return progress_cb(total ? (int)(current * 100 / total) : 0);
|
||||
});
|
||||
|
||||
ofs.close();
|
||||
|
||||
if (!res || res->status != 200) {
|
||||
std::filesystem::remove(tmp_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Write to .tmp first, then rename, so that an incomplete file
|
||||
// is never mistaken for a usable model if the download is interrupted
|
||||
std::filesystem::rename(tmp_path, path);
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
## 4.7 The `/models/select` Handler
|
||||
|
||||
This handles model selection requests. We always respond with SSE, reporting status in sequence: download progress, loading, and ready.
|
||||
|
||||
```cpp
|
||||
svr.Post("/models/select",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded() || !input.contains("model")) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'model' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto name = input["model"].get<std::string>();
|
||||
|
||||
// Find the model in the list
|
||||
auto it = std::find_if(MODELS.begin(), MODELS.end(),
|
||||
[&](const ModelInfo &m) { return m.name == name; });
|
||||
|
||||
if (it == MODELS.end()) {
|
||||
res.status = 404;
|
||||
res.set_content(json{{"error", "Unknown model"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
const auto &model = *it;
|
||||
|
||||
// Always respond with SSE (same format whether already downloaded or not)
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, model](size_t, httplib::DataSink &sink) {
|
||||
// SSE event sending helper
|
||||
auto send = [&](const json &event) {
|
||||
sink.os << "data: " << event.dump() << "\n\n";
|
||||
};
|
||||
|
||||
// Download if not yet present (report progress via SSE)
|
||||
auto path = get_models_dir() / model.filename;
|
||||
if (!std::filesystem::exists(path)) {
|
||||
bool ok = download_model(model, [&](int pct) {
|
||||
send({{"status", "downloading"}, {"progress", pct}});
|
||||
return sink.os.good(); // Abort download on client disconnect
|
||||
});
|
||||
if (!ok) {
|
||||
send({{"status", "error"}, {"message", "Download failed"}});
|
||||
sink.done();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Load and switch to the model
|
||||
send({{"status", "loading"}});
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
llm = llamalib::Llama{path};
|
||||
selected_model = model.filename;
|
||||
}
|
||||
|
||||
send({{"status", "ready"}});
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
A few notes:
|
||||
|
||||
- We send SSE events directly from the `download_model` progress callback. This is an application of `set_chunked_content_provider` + `sink.os` from Chapter 3
|
||||
- Since the callback returns `sink.os.good()`, the download stops if the client disconnects. The cancel button we add in Chapter 5 uses this
|
||||
- When we update `selected_model`, it's reflected in the `selected` flag of `GET /models`
|
||||
- The `llm` reassignment is protected by `llm_mutex`. The `/translate` and `/translate/stream` handlers also lock the same mutex, so inference can't run during a model switch (see the complete code)
|
||||
|
||||
## 4.8 Complete Code
|
||||
|
||||
Here is the complete code with model management added to the Chapter 3 code.
|
||||
|
||||
<details>
|
||||
<summary data-file="CMakeLists.txt">Complete code (CMakeLists.txt)</summary>
|
||||
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(translate-server CXX)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
include(FetchContent)
|
||||
|
||||
# llama.cpp
|
||||
FetchContent_Declare(llama
|
||||
GIT_REPOSITORY https://github.com/ggml-org/llama.cpp
|
||||
GIT_TAG master
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(llama)
|
||||
|
||||
# cpp-httplib
|
||||
FetchContent_Declare(httplib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-httplib
|
||||
GIT_TAG master
|
||||
)
|
||||
FetchContent_MakeAvailable(httplib)
|
||||
|
||||
# nlohmann/json
|
||||
FetchContent_Declare(json
|
||||
URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
|
||||
)
|
||||
FetchContent_MakeAvailable(json)
|
||||
|
||||
# cpp-llamalib
|
||||
FetchContent_Declare(cpp_llamalib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-llamalib
|
||||
GIT_TAG main
|
||||
)
|
||||
FetchContent_MakeAvailable(cpp_llamalib)
|
||||
|
||||
find_package(OpenSSL REQUIRED)
|
||||
|
||||
add_executable(translate-server src/main.cpp)
|
||||
|
||||
target_link_libraries(translate-server PRIVATE
|
||||
httplib::httplib
|
||||
nlohmann_json::nlohmann_json
|
||||
cpp-llamalib
|
||||
OpenSSL::SSL OpenSSL::Crypto
|
||||
)
|
||||
|
||||
target_compile_definitions(translate-server PRIVATE CPPHTTPLIB_OPENSSL_SUPPORT)
|
||||
|
||||
if(APPLE)
|
||||
target_link_libraries(translate-server PRIVATE
|
||||
"-framework CoreFoundation"
|
||||
"-framework Security"
|
||||
)
|
||||
endif()
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary data-file="main.cpp">Complete code (main.cpp)</summary>
|
||||
|
||||
```cpp
|
||||
#include <httplib.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <cpp-llamalib.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <csignal>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Model definitions
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
struct ModelInfo {
|
||||
std::string name;
|
||||
std::string params;
|
||||
std::string size;
|
||||
std::string repo;
|
||||
std::string filename;
|
||||
};
|
||||
|
||||
const std::vector<ModelInfo> MODELS = {
|
||||
{
|
||||
.name = "gemma-2-2b-it",
|
||||
.params = "2B",
|
||||
.size = "1.6 GB",
|
||||
.repo = "bartowski/gemma-2-2b-it-GGUF",
|
||||
.filename = "gemma-2-2b-it-Q4_K_M.gguf",
|
||||
},
|
||||
{
|
||||
.name = "gemma-2-9b-it",
|
||||
.params = "9B",
|
||||
.size = "5.8 GB",
|
||||
.repo = "bartowski/gemma-2-9b-it-GGUF",
|
||||
.filename = "gemma-2-9b-it-Q4_K_M.gguf",
|
||||
},
|
||||
{
|
||||
.name = "Llama-3.1-8B-Instruct",
|
||||
.params = "8B",
|
||||
.size = "4.9 GB",
|
||||
.repo = "bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
|
||||
.filename = "Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
|
||||
},
|
||||
};
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Model storage directory
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
std::filesystem::path get_models_dir() {
|
||||
#ifdef _WIN32
|
||||
auto env = std::getenv("APPDATA");
|
||||
auto base = env ? std::filesystem::path(env) : std::filesystem::path(".");
|
||||
return base / "translate-app" / "models";
|
||||
#else
|
||||
auto env = std::getenv("HOME");
|
||||
auto base = env ? std::filesystem::path(env) : std::filesystem::path(".");
|
||||
return base / ".translate-app" / "models";
|
||||
#endif
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Model download
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// If progress_cb returns false, the download is aborted
|
||||
bool download_model(const ModelInfo &model,
|
||||
std::function<bool(int)> progress_cb) {
|
||||
httplib::Client cli("https://huggingface.co");
|
||||
cli.set_follow_location(true); // Hugging Face redirects to a CDN
|
||||
cli.set_read_timeout(std::chrono::hours(1)); // Set a long timeout for large models
|
||||
|
||||
auto url = "/" + model.repo + "/resolve/main/" + model.filename;
|
||||
auto path = get_models_dir() / model.filename;
|
||||
auto tmp_path = std::filesystem::path(path).concat(".tmp");
|
||||
|
||||
std::ofstream ofs(tmp_path, std::ios::binary);
|
||||
if (!ofs) { return false; }
|
||||
|
||||
auto res = cli.Get(url,
|
||||
// content_receiver: receive data chunk by chunk and write to file
|
||||
[&](const char *data, size_t len) {
|
||||
ofs.write(data, len);
|
||||
return ofs.good();
|
||||
},
|
||||
// progress: report download progress (returning false aborts)
|
||||
[&, last_pct = -1](size_t current, size_t total) mutable {
|
||||
int pct = total ? (int)(current * 100 / total) : 0;
|
||||
if (pct == last_pct) return true; // Skip if same value
|
||||
last_pct = pct;
|
||||
return progress_cb(pct);
|
||||
});
|
||||
|
||||
ofs.close();
|
||||
|
||||
if (!res || res->status != 200) {
|
||||
std::filesystem::remove(tmp_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Rename after download completes
|
||||
std::filesystem::rename(tmp_path, path);
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Server
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
httplib::Server svr;
|
||||
|
||||
void signal_handler(int sig) {
|
||||
if (sig == SIGINT || sig == SIGTERM) {
|
||||
std::cout << "\nReceived signal, shutting down gracefully...\n";
|
||||
svr.stop();
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Create the model storage directory
|
||||
auto models_dir = get_models_dir();
|
||||
std::filesystem::create_directories(models_dir);
|
||||
|
||||
// Automatically download the default model if not yet present
|
||||
std::string selected_model = MODELS[0].filename;
|
||||
auto path = models_dir / selected_model;
|
||||
if (!std::filesystem::exists(path)) {
|
||||
std::cout << "Downloading " << selected_model << "..." << std::endl;
|
||||
if (!download_model(MODELS[0], [](int pct) {
|
||||
std::cout << "\r" << pct << "%" << std::flush;
|
||||
return true;
|
||||
})) {
|
||||
std::cerr << "\nFailed to download model." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
auto llm = llamalib::Llama{path};
|
||||
std::mutex llm_mutex; // Protect access during model switching
|
||||
|
||||
// Set a long timeout since LLM inference takes time (default is 5 seconds)
|
||||
svr.set_read_timeout(300);
|
||||
svr.set_write_timeout(300);
|
||||
|
||||
svr.set_logger([](const auto &req, const auto &res) {
|
||||
std::cout << req.method << " " << req.path << " -> " << res.status
|
||||
<< std::endl;
|
||||
});
|
||||
|
||||
svr.Get("/health", [](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"status", "ok"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// --- Translation endpoint (Chapter 2) ------------------------------------
|
||||
|
||||
svr.Post("/translate",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
// JSON parsing and validation (see Chapter 2 for details)
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja");
|
||||
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
try {
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
auto translation = llm.chat(prompt);
|
||||
res.set_content(json{{"translation", translation}}.dump(),
|
||||
"application/json");
|
||||
} catch (const std::exception &e) {
|
||||
res.status = 500;
|
||||
res.set_content(json{{"error", e.what()}}.dump(), "application/json");
|
||||
}
|
||||
});
|
||||
|
||||
// --- SSE streaming translation (Chapter 3) -------------------------------
|
||||
|
||||
svr.Post("/translate/stream",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja");
|
||||
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, prompt](size_t, httplib::DataSink &sink) {
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
try {
|
||||
llm.chat(prompt, [&](std::string_view token) {
|
||||
sink.os << "data: "
|
||||
<< json(std::string(token)).dump(
|
||||
-1, ' ', false, json::error_handler_t::replace)
|
||||
<< "\n\n";
|
||||
return sink.os.good(); // Abort inference on disconnect
|
||||
});
|
||||
sink.os << "data: [DONE]\n\n";
|
||||
} catch (const std::exception &e) {
|
||||
sink.os << "data: " << json({{"error", e.what()}}).dump() << "\n\n";
|
||||
}
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// --- Model list (Chapter 4) ----------------------------------------------
|
||||
|
||||
svr.Get("/models",
|
||||
[&](const httplib::Request &, httplib::Response &res) {
|
||||
auto models_dir = get_models_dir();
|
||||
auto arr = json::array();
|
||||
for (const auto &m : MODELS) {
|
||||
auto path = models_dir / m.filename;
|
||||
arr.push_back({
|
||||
{"name", m.name},
|
||||
{"params", m.params},
|
||||
{"size", m.size},
|
||||
{"downloaded", std::filesystem::exists(path)},
|
||||
{"selected", m.filename == selected_model},
|
||||
});
|
||||
}
|
||||
res.set_content(json{{"models", arr}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// --- Model selection (Chapter 4) -----------------------------------------
|
||||
|
||||
svr.Post("/models/select",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded() || !input.contains("model")) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'model' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto name = input["model"].get<std::string>();
|
||||
|
||||
auto it = std::find_if(MODELS.begin(), MODELS.end(),
|
||||
[&](const ModelInfo &m) { return m.name == name; });
|
||||
|
||||
if (it == MODELS.end()) {
|
||||
res.status = 404;
|
||||
res.set_content(json{{"error", "Unknown model"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
const auto &model = *it;
|
||||
|
||||
// Always respond with SSE (same format whether already downloaded or not)
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, model](size_t, httplib::DataSink &sink) {
|
||||
// SSE event sending helper
|
||||
auto send = [&](const json &event) {
|
||||
sink.os << "data: " << event.dump() << "\n\n";
|
||||
};
|
||||
|
||||
// Download if not yet present (report progress via SSE)
|
||||
auto path = get_models_dir() / model.filename;
|
||||
if (!std::filesystem::exists(path)) {
|
||||
bool ok = download_model(model, [&](int pct) {
|
||||
send({{"status", "downloading"}, {"progress", pct}});
|
||||
return sink.os.good(); // Abort download on client disconnect
|
||||
});
|
||||
if (!ok) {
|
||||
send({{"status", "error"}, {"message", "Download failed"}});
|
||||
sink.done();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Load and switch to the model
|
||||
send({{"status", "loading"}});
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
llm = llamalib::Llama{path};
|
||||
selected_model = model.filename;
|
||||
}
|
||||
|
||||
send({{"status", "ready"}});
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// Allow the server to be stopped with `Ctrl+C` (`SIGINT`) or `kill` (`SIGTERM`)
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
||||
std::cout << "Listening on http://127.0.0.1:8080" << std::endl;
|
||||
svr.listen("127.0.0.1", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
## 4.9 Testing
|
||||
|
||||
Since we added OpenSSL configuration to CMakeLists.txt, we need to re-run CMake before building.
|
||||
|
||||
```bash
|
||||
cmake -B build
|
||||
cmake --build build -j
|
||||
./build/translate-server
|
||||
```
|
||||
|
||||
### Checking the Model List
|
||||
|
||||
```bash
|
||||
curl http://localhost:8080/models
|
||||
```
|
||||
|
||||
The gemma-2-2b-it model downloaded in Chapter 1 should show `downloaded: true` and `selected: true`.
|
||||
|
||||
### Switching to a Different Model
|
||||
|
||||
```bash
|
||||
curl -N -X POST http://localhost:8080/models/select \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"model": "gemma-2-9b-it"}'
|
||||
```
|
||||
|
||||
Download progress streams via SSE, and `"ready"` appears when it's done.
|
||||
|
||||
### Comparing Translations Across Models
|
||||
|
||||
Let's translate the same sentence with different models.
|
||||
|
||||
```bash
|
||||
# Translate with gemma-2-9b-it (the model we just switched to)
|
||||
curl -X POST http://localhost:8080/translate \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"text": "The quick brown fox jumps over the lazy dog.", "target_lang": "ja"}'
|
||||
|
||||
# Switch back to gemma-2-2b-it
|
||||
curl -N -X POST http://localhost:8080/models/select \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"model": "gemma-2-2b-it"}'
|
||||
|
||||
# Translate the same sentence
|
||||
curl -X POST http://localhost:8080/translate \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"text": "The quick brown fox jumps over the lazy dog.", "target_lang": "ja"}'
|
||||
```
|
||||
|
||||
Translation results vary depending on the model, even with the same code and the same prompt. Since cpp-llamalib automatically applies the appropriate chat template for each model, no code changes are needed.
|
||||
|
||||
## Next Chapter
|
||||
|
||||
The server's main features are now complete: REST API, SSE streaming, and model download and switching. In the next chapter, we'll add static file serving and build a Web UI you can use from a browser.
|
||||
|
||||
**Next:** [Adding a Web UI](../ch05-web-ui)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,724 @@
|
||||
---
|
||||
title: "6. Turning It into a Desktop App with WebView"
|
||||
order: 6
|
||||
|
||||
---
|
||||
|
||||
In Chapter 5, we completed a translation app you can use from a browser. But every time, you have to start the server, open the URL in a browser... Wouldn't it be nice to just double-click and start using it, like a normal app?
|
||||
|
||||
In this chapter, we'll do two things:
|
||||
|
||||
1. **WebView integration** — Use [webview/webview](https://github.com/webview/webview) to turn it into a desktop app that runs without a browser
|
||||
2. **Single binary packaging** — Use [cpp-embedlib](https://github.com/yhirose/cpp-embedlib) to embed HTML/CSS/JS into the binary, making the distributable a single file
|
||||
|
||||
When finished, you'll be able to just run `./translate-app` to open a window and start translating.
|
||||
|
||||

|
||||
|
||||
The model downloads automatically on first launch, so the only thing you need to give users is the single binary.
|
||||
|
||||
## 6.1 Introducing webview/webview
|
||||
|
||||
[webview/webview](https://github.com/webview/webview) is a library that lets you use the OS's native WebView component (WKWebView on macOS, WebKitGTK on Linux, WebView2 on Windows) from C/C++. Unlike Electron, it doesn't bundle its own browser, so the impact on binary size is negligible.
|
||||
|
||||
We'll fetch it with CMake. Add the following to your `CMakeLists.txt`:
|
||||
|
||||
```cmake
|
||||
# webview/webview
|
||||
FetchContent_Declare(webview
|
||||
GIT_REPOSITORY https://github.com/webview/webview
|
||||
GIT_TAG master
|
||||
)
|
||||
FetchContent_MakeAvailable(webview)
|
||||
```
|
||||
|
||||
This makes the `webview::core` CMake target available. When you link it with `target_link_libraries`, it automatically sets up include paths and platform-specific frameworks.
|
||||
|
||||
> **macOS**: No additional dependencies are needed. WKWebView is built into the system.
|
||||
>
|
||||
> **Linux**: WebKitGTK is required. Install it with `sudo apt install libwebkit2gtk-4.1-dev`.
|
||||
>
|
||||
> **Windows**: The WebView2 runtime is required. It comes pre-installed on Windows 11. For Windows 10, download it from the [official Microsoft website](https://developer.microsoft.com/en-us/microsoft-edge/webview2/).
|
||||
|
||||
## 6.2 Running the Server on a Background Thread
|
||||
|
||||
Up through Chapter 5, the server's `listen()` was blocking the main thread. To use WebView, we need to run the server on a separate thread and run the WebView event loop on the main thread.
|
||||
|
||||
```cpp
|
||||
#include "webview/webview.h"
|
||||
#include <thread>
|
||||
|
||||
int main() {
|
||||
// ... (server setup is the same as Chapter 5) ...
|
||||
|
||||
// Start the server on a background thread
|
||||
auto port = svr.bind_to_any_port("127.0.0.1");
|
||||
std::thread server_thread([&]() { svr.listen_after_bind(); });
|
||||
|
||||
std::cout << "Listening on http://127.0.0.1:" << port << std::endl;
|
||||
|
||||
// Display the UI with WebView
|
||||
webview::webview w(false, nullptr);
|
||||
w.set_title("Translate App");
|
||||
w.set_size(1024, 768, WEBVIEW_HINT_NONE);
|
||||
w.navigate("http://127.0.0.1:" + std::to_string(port));
|
||||
w.run(); // Block until the window is closed
|
||||
|
||||
// Stop the server when the window is closed
|
||||
svr.stop();
|
||||
server_thread.join();
|
||||
}
|
||||
```
|
||||
|
||||
Let's look at the key points:
|
||||
|
||||
- **`bind_to_any_port`** — Instead of `listen("127.0.0.1", 8080)`, we let the OS choose an available port. Since desktop apps can be launched multiple times, using a fixed port would cause conflicts
|
||||
- **`listen_after_bind`** — Starts accepting requests on the port reserved by `bind_to_any_port`. While `listen()` does bind and listen in one call, we need to know the port number first, so we split the operations
|
||||
- **Shutdown order** — When the WebView window is closed, we stop the server with `svr.stop()` and wait for the thread to finish with `server_thread.join()`. If we reversed the order, WebView would lose access to the server
|
||||
|
||||
The `signal_handler` from Chapter 5 is no longer needed. In a desktop app, closing the window means terminating the application.
|
||||
|
||||
## 6.3 Embedding Static Files with cpp-embedlib
|
||||
|
||||
In Chapter 5, we served files from the `public/` directory, so you'd need to distribute `public/` alongside the binary. With [cpp-embedlib](https://github.com/yhirose/cpp-embedlib), you can embed HTML, CSS, and JavaScript into the binary, packaging the distributable into a single file.
|
||||
|
||||
### CMakeLists.txt
|
||||
|
||||
Fetch cpp-embedlib and embed `public/`:
|
||||
|
||||
```cmake
|
||||
# cpp-embedlib
|
||||
FetchContent_Declare(cpp-embedlib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-embedlib
|
||||
GIT_TAG main
|
||||
)
|
||||
FetchContent_MakeAvailable(cpp-embedlib)
|
||||
|
||||
# Embed the public/ directory into the binary
|
||||
cpp_embedlib_add(WebAssets
|
||||
FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/public
|
||||
NAMESPACE Web
|
||||
)
|
||||
|
||||
target_link_libraries(translate-app PRIVATE
|
||||
WebAssets # Embedded files
|
||||
cpp-embedlib-httplib # cpp-httplib integration
|
||||
)
|
||||
```
|
||||
|
||||
`cpp_embedlib_add` converts the files under `public/` into binary data at compile time and creates a static library called `WebAssets`. When linked, you can access the embedded files through a `Web::FS` object. `cpp-embedlib-httplib` is a helper library that provides the `httplib::mount()` function.
|
||||
|
||||
### Replacing set_mount_point with httplib::mount
|
||||
|
||||
Simply replace Chapter 5's `set_mount_point` with cpp-embedlib's `httplib::mount`:
|
||||
|
||||
```cpp
|
||||
#include <cpp-embedlib-httplib.h>
|
||||
#include "WebAssets.h"
|
||||
|
||||
// Chapter 5:
|
||||
// svr.set_mount_point("/", "./public");
|
||||
|
||||
// Chapter 6:
|
||||
httplib::mount(svr, Web::FS);
|
||||
```
|
||||
|
||||
`httplib::mount` registers handlers that serve the files embedded in `Web::FS` over HTTP. MIME types are automatically determined from file extensions, so there's no need to manually set `Content-Type`.
|
||||
|
||||
The file contents are directly mapped to the binary's data segment, so no memory copies or heap allocations occur.
|
||||
|
||||
## 6.4 macOS: Adding the Edit Menu
|
||||
|
||||
If you try to paste text into the input field with `Cmd+V`, you'll find it doesn't work. On macOS, keyboard shortcuts like `Cmd+V` (paste) and `Cmd+C` (copy) are routed through the application's menu bar. Since webview/webview doesn't create one, these shortcuts never reach the WebView. We need to add a macOS Edit menu using the Objective-C runtime:
|
||||
|
||||
```cpp
|
||||
#ifdef __APPLE__
|
||||
#include <objc/objc-runtime.h>
|
||||
|
||||
void setup_macos_edit_menu() {
|
||||
auto cls = [](const char *n) { return (id)objc_getClass(n); };
|
||||
auto sel = sel_registerName;
|
||||
auto msg = reinterpret_cast<id (*)(id, SEL)>(objc_msgSend);
|
||||
auto msg_s = reinterpret_cast<id (*)(id, SEL, const char *)>(objc_msgSend);
|
||||
auto msg_id = reinterpret_cast<id (*)(id, SEL, id)>(objc_msgSend);
|
||||
auto msg_v = reinterpret_cast<void (*)(id, SEL, id)>(objc_msgSend);
|
||||
auto msg_mi = reinterpret_cast<id (*)(id, SEL, id, SEL, id)>(objc_msgSend);
|
||||
|
||||
auto str = [&](const char *s) {
|
||||
return msg_s(cls("NSString"), sel("stringWithUTF8String:"), s);
|
||||
};
|
||||
|
||||
id app = msg(cls("NSApplication"), sel("sharedApplication"));
|
||||
id mainMenu = msg(msg(cls("NSMenu"), sel("alloc")), sel("init"));
|
||||
id editItem = msg(msg(cls("NSMenuItem"), sel("alloc")), sel("init"));
|
||||
id editMenu = msg_id(msg(cls("NSMenu"), sel("alloc")),
|
||||
sel("initWithTitle:"), str("Edit"));
|
||||
|
||||
struct { const char *title; const char *action; const char *key; } items[] = {
|
||||
{"Undo", "undo:", "z"},
|
||||
{"Redo", "redo:", "Z"},
|
||||
{"Cut", "cut:", "x"},
|
||||
{"Copy", "copy:", "c"},
|
||||
{"Paste", "paste:", "v"},
|
||||
{"Select All", "selectAll:", "a"},
|
||||
};
|
||||
|
||||
for (auto &[title, action, key] : items) {
|
||||
id mi = msg_mi(msg(cls("NSMenuItem"), sel("alloc")),
|
||||
sel("initWithTitle:action:keyEquivalent:"),
|
||||
str(title), sel(action), str(key));
|
||||
msg_v(editMenu, sel("addItem:"), mi);
|
||||
}
|
||||
|
||||
msg_v(editItem, sel("setSubmenu:"), editMenu);
|
||||
msg_v(mainMenu, sel("addItem:"), editItem);
|
||||
msg_v(app, sel("setMainMenu:"), mainMenu);
|
||||
}
|
||||
#endif
|
||||
```
|
||||
|
||||
Call this before `w.run()`:
|
||||
|
||||
```cpp
|
||||
#ifdef __APPLE__
|
||||
setup_macos_edit_menu();
|
||||
#endif
|
||||
w.run();
|
||||
```
|
||||
|
||||
On Windows and Linux, keyboard shortcuts are delivered directly to the focused control without going through the menu bar, so this workaround is macOS-specific.
|
||||
|
||||
## 6.5 Complete Code
|
||||
|
||||
<details>
|
||||
<summary data-file="CMakeLists.txt">Complete code (CMakeLists.txt)</summary>
|
||||
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(translate-app CXX)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
include(FetchContent)
|
||||
|
||||
# llama.cpp
|
||||
FetchContent_Declare(llama
|
||||
GIT_REPOSITORY https://github.com/ggml-org/llama.cpp
|
||||
GIT_TAG master
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(llama)
|
||||
|
||||
# cpp-httplib
|
||||
FetchContent_Declare(httplib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-httplib
|
||||
GIT_TAG master
|
||||
)
|
||||
FetchContent_MakeAvailable(httplib)
|
||||
|
||||
# nlohmann/json
|
||||
FetchContent_Declare(json
|
||||
URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
|
||||
)
|
||||
FetchContent_MakeAvailable(json)
|
||||
|
||||
# cpp-llamalib
|
||||
FetchContent_Declare(cpp_llamalib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-llamalib
|
||||
GIT_TAG main
|
||||
)
|
||||
FetchContent_MakeAvailable(cpp_llamalib)
|
||||
|
||||
# webview/webview
|
||||
FetchContent_Declare(webview
|
||||
GIT_REPOSITORY https://github.com/webview/webview
|
||||
GIT_TAG master
|
||||
)
|
||||
FetchContent_MakeAvailable(webview)
|
||||
|
||||
# cpp-embedlib
|
||||
FetchContent_Declare(cpp-embedlib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-embedlib
|
||||
GIT_TAG main
|
||||
)
|
||||
FetchContent_MakeAvailable(cpp-embedlib)
|
||||
|
||||
# Embed the public/ directory into the binary
|
||||
cpp_embedlib_add(WebAssets
|
||||
FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/public
|
||||
NAMESPACE Web
|
||||
)
|
||||
|
||||
find_package(OpenSSL REQUIRED)
|
||||
|
||||
add_executable(translate-app src/main.cpp)
|
||||
|
||||
target_link_libraries(translate-app PRIVATE
|
||||
httplib::httplib
|
||||
nlohmann_json::nlohmann_json
|
||||
cpp-llamalib
|
||||
OpenSSL::SSL OpenSSL::Crypto
|
||||
WebAssets
|
||||
cpp-embedlib-httplib
|
||||
webview::core
|
||||
)
|
||||
|
||||
if(APPLE)
|
||||
target_link_libraries(translate-app PRIVATE
|
||||
"-framework CoreFoundation"
|
||||
"-framework Security"
|
||||
)
|
||||
endif()
|
||||
|
||||
target_compile_definitions(translate-app PRIVATE
|
||||
CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
)
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary data-file="main.cpp">Complete code (main.cpp)</summary>
|
||||
|
||||
```cpp
|
||||
#include <httplib.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <cpp-llamalib.h>
|
||||
#include <cpp-embedlib-httplib.h>
|
||||
#include "WebAssets.h"
|
||||
#include "webview/webview.h"
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <objc/objc-runtime.h>
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// macOS Edit menu (Cmd+C/V/X/A require an Edit menu on macOS)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
#ifdef __APPLE__
|
||||
void setup_macos_edit_menu() {
|
||||
auto cls = [](const char *n) { return (id)objc_getClass(n); };
|
||||
auto sel = sel_registerName;
|
||||
auto msg = reinterpret_cast<id (*)(id, SEL)>(objc_msgSend);
|
||||
auto msg_s = reinterpret_cast<id (*)(id, SEL, const char *)>(objc_msgSend);
|
||||
auto msg_id = reinterpret_cast<id (*)(id, SEL, id)>(objc_msgSend);
|
||||
auto msg_v = reinterpret_cast<void (*)(id, SEL, id)>(objc_msgSend);
|
||||
auto msg_mi = reinterpret_cast<id (*)(id, SEL, id, SEL, id)>(objc_msgSend);
|
||||
|
||||
auto str = [&](const char *s) {
|
||||
return msg_s(cls("NSString"), sel("stringWithUTF8String:"), s);
|
||||
};
|
||||
|
||||
id app = msg(cls("NSApplication"), sel("sharedApplication"));
|
||||
id mainMenu = msg(msg(cls("NSMenu"), sel("alloc")), sel("init"));
|
||||
id editItem = msg(msg(cls("NSMenuItem"), sel("alloc")), sel("init"));
|
||||
id editMenu = msg_id(msg(cls("NSMenu"), sel("alloc")),
|
||||
sel("initWithTitle:"), str("Edit"));
|
||||
|
||||
struct { const char *title; const char *action; const char *key; } items[] = {
|
||||
{"Undo", "undo:", "z"},
|
||||
{"Redo", "redo:", "Z"},
|
||||
{"Cut", "cut:", "x"},
|
||||
{"Copy", "copy:", "c"},
|
||||
{"Paste", "paste:", "v"},
|
||||
{"Select All", "selectAll:", "a"},
|
||||
};
|
||||
|
||||
for (auto &[title, action, key] : items) {
|
||||
id mi = msg_mi(msg(cls("NSMenuItem"), sel("alloc")),
|
||||
sel("initWithTitle:action:keyEquivalent:"),
|
||||
str(title), sel(action), str(key));
|
||||
msg_v(editMenu, sel("addItem:"), mi);
|
||||
}
|
||||
|
||||
msg_v(editItem, sel("setSubmenu:"), editMenu);
|
||||
msg_v(mainMenu, sel("addItem:"), editItem);
|
||||
msg_v(app, sel("setMainMenu:"), mainMenu);
|
||||
}
|
||||
#endif
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Model definitions
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
struct ModelInfo {
|
||||
std::string name;
|
||||
std::string params;
|
||||
std::string size;
|
||||
std::string repo;
|
||||
std::string filename;
|
||||
};
|
||||
|
||||
const std::vector<ModelInfo> MODELS = {
|
||||
{
|
||||
.name = "gemma-2-2b-it",
|
||||
.params = "2B",
|
||||
.size = "1.6 GB",
|
||||
.repo = "bartowski/gemma-2-2b-it-GGUF",
|
||||
.filename = "gemma-2-2b-it-Q4_K_M.gguf",
|
||||
},
|
||||
{
|
||||
.name = "gemma-2-9b-it",
|
||||
.params = "9B",
|
||||
.size = "5.8 GB",
|
||||
.repo = "bartowski/gemma-2-9b-it-GGUF",
|
||||
.filename = "gemma-2-9b-it-Q4_K_M.gguf",
|
||||
},
|
||||
{
|
||||
.name = "Llama-3.1-8B-Instruct",
|
||||
.params = "8B",
|
||||
.size = "4.9 GB",
|
||||
.repo = "bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
|
||||
.filename = "Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
|
||||
},
|
||||
};
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Model storage directory
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
std::filesystem::path get_models_dir() {
|
||||
#ifdef _WIN32
|
||||
auto env = std::getenv("APPDATA");
|
||||
auto base = env ? std::filesystem::path(env) : std::filesystem::path(".");
|
||||
return base / "translate-app" / "models";
|
||||
#else
|
||||
auto env = std::getenv("HOME");
|
||||
auto base = env ? std::filesystem::path(env) : std::filesystem::path(".");
|
||||
return base / ".translate-app" / "models";
|
||||
#endif
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Model download
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// Abort the download if progress_cb returns false
|
||||
bool download_model(const ModelInfo &model,
|
||||
std::function<bool(int)> progress_cb) {
|
||||
httplib::Client cli("https://huggingface.co");
|
||||
cli.set_follow_location(true); // Hugging Face redirects to a CDN
|
||||
cli.set_read_timeout(std::chrono::hours(1)); // Long timeout for large models
|
||||
|
||||
auto url = "/" + model.repo + "/resolve/main/" + model.filename;
|
||||
auto path = get_models_dir() / model.filename;
|
||||
auto tmp_path = std::filesystem::path(path).concat(".tmp");
|
||||
|
||||
std::ofstream ofs(tmp_path, std::ios::binary);
|
||||
if (!ofs) { return false; }
|
||||
|
||||
auto res = cli.Get(url,
|
||||
// content_receiver: Receive data chunk by chunk and write to file
|
||||
[&](const char *data, size_t len) {
|
||||
ofs.write(data, len);
|
||||
return ofs.good();
|
||||
},
|
||||
// progress: Report download progress (return false to abort)
|
||||
[&, last_pct = -1](size_t current, size_t total) mutable {
|
||||
int pct = total ? (int)(current * 100 / total) : 0;
|
||||
if (pct == last_pct) return true; // Skip if the value hasn't changed
|
||||
last_pct = pct;
|
||||
return progress_cb(pct);
|
||||
});
|
||||
|
||||
ofs.close();
|
||||
|
||||
if (!res || res->status != 200) {
|
||||
std::filesystem::remove(tmp_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Rename after download completes
|
||||
std::filesystem::rename(tmp_path, path);
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Server
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
int main() {
|
||||
httplib::Server svr;
|
||||
// Create the model storage directory
|
||||
auto models_dir = get_models_dir();
|
||||
std::filesystem::create_directories(models_dir);
|
||||
|
||||
// Auto-download the default model if not already present
|
||||
std::string selected_model = MODELS[0].filename;
|
||||
auto path = models_dir / selected_model;
|
||||
if (!std::filesystem::exists(path)) {
|
||||
std::cout << "Downloading " << selected_model << "..." << std::endl;
|
||||
if (!download_model(MODELS[0], [](int pct) {
|
||||
std::cout << "\r" << pct << "%" << std::flush;
|
||||
return true;
|
||||
})) {
|
||||
std::cerr << "\nFailed to download model." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
auto llm = llamalib::Llama{path};
|
||||
std::mutex llm_mutex; // Protect access during model switching
|
||||
|
||||
// Set a long timeout since LLM inference takes time (default is 5 seconds)
|
||||
svr.set_read_timeout(300);
|
||||
svr.set_write_timeout(300);
|
||||
|
||||
svr.set_logger([](const auto &req, const auto &res) {
|
||||
std::cout << req.method << " " << req.path << " -> " << res.status
|
||||
<< std::endl;
|
||||
});
|
||||
|
||||
svr.Get("/health", [](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"status", "ok"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// --- Translation endpoint (Chapter 2) ------------------------------------
|
||||
|
||||
svr.Post("/translate",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja");
|
||||
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
try {
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
auto translation = llm.chat(prompt);
|
||||
res.set_content(json{{"translation", translation}}.dump(),
|
||||
"application/json");
|
||||
} catch (const std::exception &e) {
|
||||
res.status = 500;
|
||||
res.set_content(json{{"error", e.what()}}.dump(), "application/json");
|
||||
}
|
||||
});
|
||||
|
||||
// --- SSE streaming translation (Chapter 3) -------------------------------
|
||||
|
||||
svr.Post("/translate/stream",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja");
|
||||
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, prompt](size_t, httplib::DataSink &sink) {
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
try {
|
||||
llm.chat(prompt, [&](std::string_view token) {
|
||||
sink.os << "data: "
|
||||
<< json(std::string(token)).dump(
|
||||
-1, ' ', false, json::error_handler_t::replace)
|
||||
<< "\n\n";
|
||||
return sink.os.good(); // Abort inference on disconnect
|
||||
});
|
||||
sink.os << "data: [DONE]\n\n";
|
||||
} catch (const std::exception &e) {
|
||||
sink.os << "data: " << json({{"error", e.what()}}).dump() << "\n\n";
|
||||
}
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// --- Model list (Chapter 4) ----------------------------------------------
|
||||
|
||||
svr.Get("/models",
|
||||
[&](const httplib::Request &, httplib::Response &res) {
|
||||
auto models_dir = get_models_dir();
|
||||
auto arr = json::array();
|
||||
for (const auto &m : MODELS) {
|
||||
auto path = models_dir / m.filename;
|
||||
arr.push_back({
|
||||
{"name", m.name},
|
||||
{"params", m.params},
|
||||
{"size", m.size},
|
||||
{"downloaded", std::filesystem::exists(path)},
|
||||
{"selected", m.filename == selected_model},
|
||||
});
|
||||
}
|
||||
res.set_content(json{{"models", arr}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// --- Model selection (Chapter 4) -----------------------------------------
|
||||
|
||||
svr.Post("/models/select",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded() || !input.contains("model")) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'model' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto name = input["model"].get<std::string>();
|
||||
|
||||
auto it = std::find_if(MODELS.begin(), MODELS.end(),
|
||||
[&](const ModelInfo &m) { return m.name == name; });
|
||||
|
||||
if (it == MODELS.end()) {
|
||||
res.status = 404;
|
||||
res.set_content(json{{"error", "Unknown model"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
const auto &model = *it;
|
||||
|
||||
// Always respond with SSE (same format whether downloaded or not)
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, model](size_t, httplib::DataSink &sink) {
|
||||
// SSE event sending helper
|
||||
auto send = [&](const json &event) {
|
||||
sink.os << "data: " << event.dump() << "\n\n";
|
||||
};
|
||||
|
||||
// Download if not yet downloaded (report progress via SSE)
|
||||
auto path = get_models_dir() / model.filename;
|
||||
if (!std::filesystem::exists(path)) {
|
||||
bool ok = download_model(model, [&](int pct) {
|
||||
send({{"status", "downloading"}, {"progress", pct}});
|
||||
return sink.os.good(); // Abort download on client disconnect
|
||||
});
|
||||
if (!ok) {
|
||||
send({{"status", "error"}, {"message", "Download failed"}});
|
||||
sink.done();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Load and switch to the model
|
||||
send({{"status", "loading"}});
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
llm = llamalib::Llama{path};
|
||||
selected_model = model.filename;
|
||||
}
|
||||
|
||||
send({{"status", "ready"}});
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// --- Embedded file serving (Chapter 6) ------------------------------------
|
||||
// Chapter 5: svr.set_mount_point("/", "./public");
|
||||
httplib::mount(svr, Web::FS);
|
||||
|
||||
// Start the server on a background thread
|
||||
auto port = svr.bind_to_any_port("127.0.0.1");
|
||||
std::thread server_thread([&]() { svr.listen_after_bind(); });
|
||||
|
||||
std::cout << "Listening on http://127.0.0.1:" << port << std::endl;
|
||||
|
||||
// Display the UI with WebView
|
||||
webview::webview w(false, nullptr);
|
||||
w.set_title("Translate App");
|
||||
w.set_size(1024, 768, WEBVIEW_HINT_NONE);
|
||||
w.navigate("http://127.0.0.1:" + std::to_string(port));
|
||||
|
||||
#ifdef __APPLE__
|
||||
setup_macos_edit_menu();
|
||||
#endif
|
||||
w.run(); // Block until the window is closed
|
||||
|
||||
// Stop the server when the window is closed
|
||||
svr.stop();
|
||||
server_thread.join();
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
To summarize the changes from Chapter 5:
|
||||
|
||||
- `#include <csignal>` replaced with `#include <thread>`, `<cpp-embedlib-httplib.h>`, `"WebAssets.h"`, `"webview/webview.h"`
|
||||
- Removed the `signal_handler` function
|
||||
- `svr.set_mount_point("/", "./public")` replaced with `httplib::mount(svr, Web::FS)`
|
||||
- `svr.listen("127.0.0.1", 8080)` replaced with `bind_to_any_port` + `listen_after_bind` + WebView event loop
|
||||
|
||||
Not a single line of handler code has changed. The REST API, SSE streaming, and model management built through Chapter 5 all work as-is.
|
||||
|
||||
## 6.6 Building and Testing
|
||||
|
||||
```bash
|
||||
cmake -B build
|
||||
cmake --build build -j
|
||||
```
|
||||
|
||||
Launch the app:
|
||||
|
||||
```bash
|
||||
./build/translate-app
|
||||
```
|
||||
|
||||
No browser is needed. A window opens automatically. The same UI from Chapter 5 appears as-is, and translation and model switching all work just the same.
|
||||
|
||||
When you close the window, the server shuts down automatically. There's no need for `Ctrl+C`.
|
||||
|
||||
### What Needs to Be Distributed
|
||||
|
||||
You only need to distribute:
|
||||
|
||||
- The single `translate-app` binary
|
||||
|
||||
That's it. You don't need the `public/` directory. HTML, CSS, and JavaScript are embedded in the binary. Model files download automatically on first launch, so there's no need to ask users to prepare anything in advance.
|
||||
|
||||
## Next Chapter
|
||||
|
||||
Congratulations! 🎉
|
||||
|
||||
In Chapter 1, `/health` just returned `{"status":"ok"}`. Now we have a desktop app where you type text and translations stream in real time, pick a different model from a dropdown and it downloads automatically, and closing the window cleanly shuts everything down — all in a single distributable binary.
|
||||
|
||||
What we changed in this chapter was just the static file serving and the server startup. Not a single line of handler code changed. The REST API, SSE streaming, and model management we built through Chapter 5 all work as a desktop app, as-is.
|
||||
|
||||
In the next chapter, we'll shift perspective and read through the code of llama.cpp's own `llama-server`. Let's compare our simple server with a production-quality one and see what design decisions differ and why.
|
||||
|
||||
**Next:** [Reading the llama.cpp Server Source Code](../ch07-code-reading)
|
||||
@@ -0,0 +1,154 @@
|
||||
---
|
||||
title: "7. Reading the llama.cpp Server Source Code"
|
||||
order: 7
|
||||
|
||||
---
|
||||
|
||||
Over the course of six chapters, we built a translation desktop app from scratch. We have a working product, but it's ultimately a "learning-oriented" implementation. So how does "production-quality" code differ? Let's read the source code of `llama-server`, the official server bundled with llama.cpp, and compare.
|
||||
|
||||
`llama-server` is located at `llama.cpp/tools/server/`. It uses the same cpp-httplib, so you can read the code the same way as in the previous chapters.
|
||||
|
||||
## 7.1 Source Code Location
|
||||
|
||||
```ascii
|
||||
llama.cpp/tools/server/
|
||||
├── server.cpp # Main server implementation
|
||||
├── httplib.h # cpp-httplib (bundled version)
|
||||
└── ...
|
||||
```
|
||||
|
||||
The code is contained in a single `server.cpp`. It runs to several thousand lines, but once you understand the structure, you can narrow down the parts worth reading.
|
||||
|
||||
## 7.2 OpenAI-Compatible API
|
||||
|
||||
The biggest difference between the server we built and `llama-server` is the API design.
|
||||
|
||||
**Our API:**
|
||||
|
||||
```text
|
||||
POST /translate → {"translation": "..."}
|
||||
POST /translate/stream → SSE: data: "token"
|
||||
```
|
||||
|
||||
**llama-server's API:**
|
||||
|
||||
```text
|
||||
POST /v1/chat/completions → OpenAI-compatible JSON
|
||||
POST /v1/completions → OpenAI-compatible JSON
|
||||
POST /v1/embeddings → Text embedding vectors
|
||||
```
|
||||
|
||||
`llama-server` conforms to [OpenAI's API specification](https://platform.openai.com/docs/api-reference). This means OpenAI's official client libraries (such as the Python `openai` package) work out of the box.
|
||||
|
||||
```python
|
||||
# Example of connecting to llama-server with the OpenAI client
|
||||
from openai import OpenAI
|
||||
client = OpenAI(base_url="http://localhost:8080/v1", api_key="dummy")
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model="local-model",
|
||||
messages=[{"role": "user", "content": "Hello!"}]
|
||||
)
|
||||
```
|
||||
|
||||
Compatibility with existing tools and libraries is a big design decision. We designed a simple translation-specific API, but if you're building a general-purpose server, OpenAI compatibility has become the de facto standard.
|
||||
|
||||
## 7.3 Concurrent Request Handling
|
||||
|
||||
Our server processes requests one at a time. If another request arrives while a translation is in progress, it waits until the previous inference finishes. This is fine for a desktop app used by one person, but it becomes a problem for a server shared by multiple users.
|
||||
|
||||
`llama-server` handles concurrent requests through a mechanism called **slots**.
|
||||
|
||||

|
||||
|
||||
The key point is that tokens from each slot are not inferred **one by one in sequence**, but rather **all at once in a single batch**. GPUs excel at parallel processing, so processing two users simultaneously takes almost the same time as processing one. This is called "continuous batching."
|
||||
|
||||
In our server, cpp-httplib's thread pool assigns one thread per request, but the inference itself runs single-threaded inside `llm.chat()`. `llama-server` consolidates this inference step into a shared batch processing loop.
|
||||
|
||||
## 7.4 Differences in SSE Format
|
||||
|
||||
The streaming mechanism itself is the same (`set_chunked_content_provider` + SSE), but the data format differs.
|
||||
|
||||
**Our format:**
|
||||
|
||||
```text
|
||||
data: "去年の"
|
||||
data: "春に"
|
||||
data: [DONE]
|
||||
```
|
||||
|
||||
**llama-server (OpenAI-compatible):**
|
||||
|
||||
```text
|
||||
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"去年の"}}]}
|
||||
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"春に"}}]}
|
||||
data: [DONE]
|
||||
```
|
||||
|
||||
Our format simply sends the tokens. Because `llama-server` follows the OpenAI specification, even a single token comes wrapped in JSON. It may look verbose, but it includes useful information for clients, like an `id` to identify the request and a `finish_reason` to indicate why generation stopped.
|
||||
|
||||
## 7.5 KV Cache Reuse
|
||||
|
||||
In our server, we process the entire prompt from scratch on every request. Our translation app's prompt is short ("Translate the following text to ja..." + input text), so this isn't a problem.
|
||||
|
||||
`llama-server` reuses the KV cache for the prefix portion when a request shares a common prompt prefix with a previous request.
|
||||
|
||||

|
||||
|
||||
For chatbots that send a long system prompt and few-shot examples with every request, this alone dramatically reduces response time. The difference is night and day: processing several thousand tokens of system prompt every time versus reading them from cache in an instant.
|
||||
|
||||
For our translation app, where the system prompt is just a single sentence, the benefit is limited. However, it's an optimization worth keeping in mind when applying this to your own applications.
|
||||
|
||||
## 7.6 Structured Output
|
||||
|
||||
Since our translation API returns plain text, there was no need to constrain the output format. But what if you want the LLM to respond in JSON?
|
||||
|
||||
```text
|
||||
Prompt: Analyze the sentiment of the following text and return it as JSON.
|
||||
LLM output (expected): {"sentiment": "positive", "score": 0.8}
|
||||
LLM output (reality): Here are the results of the sentiment analysis. {"sentiment": ...
|
||||
```
|
||||
|
||||
LLMs sometimes ignore instructions and add extraneous text. `llama-server` solves this problem with **grammar constraints**.
|
||||
|
||||
```bash
|
||||
curl http://localhost:8080/v1/chat/completions \
|
||||
-d '{
|
||||
"messages": [{"role": "user", "content": "Analyze sentiment..."}],
|
||||
"json_schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"sentiment": {"type": "string", "enum": ["positive", "negative", "neutral"]},
|
||||
"score": {"type": "number"}
|
||||
},
|
||||
"required": ["sentiment", "score"]
|
||||
}
|
||||
}'
|
||||
```
|
||||
|
||||
When you specify `json_schema`, tokens that don't conform to the grammar are excluded during token generation. This guarantees that the output is always valid JSON, so there's no need to worry about `json::parse` failing.
|
||||
|
||||
When embedding LLMs into applications, whether you can reliably parse the output directly impacts reliability. Grammar constraints are unnecessary for free-text output like translation, but they're essential for use cases where you need to return structured data as an API response.
|
||||
|
||||
## 7.7 Summary
|
||||
|
||||
Let's organize the differences we've covered.
|
||||
|
||||
| Aspect | Our Server | llama-server |
|
||||
|------|-------------|--------------|
|
||||
| API design | Translation-specific | OpenAI-compatible |
|
||||
| Concurrent requests | Sequential processing | Slots + continuous batching |
|
||||
| SSE format | Tokens only | OpenAI-compatible JSON |
|
||||
| KV cache | Cleared each time | Prefix reuse |
|
||||
| Structured output | None | JSON Schema / grammar constraints |
|
||||
| Code size | ~200 lines | Several thousand lines |
|
||||
|
||||
Our code is simple because of the assumption that "one person uses it as a desktop app." If you're building a server for multiple users or one that integrates with the existing ecosystem, `llama-server`'s design serves as a valuable reference.
|
||||
|
||||
Conversely, even 200 lines of code is enough to make a fully functional translation app. I hope this code reading exercise has also conveyed the value of "building only what you need."
|
||||
|
||||
## Next Chapter
|
||||
|
||||
In the next chapter, we'll cover the key points for swapping in your own library and customizing the app to make it truly yours.
|
||||
|
||||
**Next:** [Making It Your Own](../ch08-customization)
|
||||
@@ -0,0 +1,120 @@
|
||||
---
|
||||
title: "8. Making It Your Own"
|
||||
order: 8
|
||||
|
||||
---
|
||||
|
||||
Through Chapter 7, we've built a translation desktop app and studied how production-quality code differs. In this chapter, let's go over the key points for **turning this app into something entirely your own**.
|
||||
|
||||
The translation app was just a vehicle. Replace llama.cpp with your own library, and the same architecture works for any application.
|
||||
|
||||
## 8.1 Swapping Out the Build Configuration
|
||||
|
||||
First, replace the llama.cpp-related `FetchContent` entries in `CMakeLists.txt` with your own library.
|
||||
|
||||
```cmake
|
||||
# Remove: llama.cpp and cpp-llamalib FetchContent
|
||||
|
||||
# Add: your own library
|
||||
FetchContent_Declare(my_lib
|
||||
GIT_REPOSITORY https://github.com/yourname/my-lib
|
||||
GIT_TAG main
|
||||
)
|
||||
FetchContent_MakeAvailable(my_lib)
|
||||
|
||||
target_link_libraries(my-app PRIVATE
|
||||
httplib::httplib
|
||||
nlohmann_json::nlohmann_json
|
||||
my_lib # Your library instead of cpp-llamalib
|
||||
# ...
|
||||
)
|
||||
```
|
||||
|
||||
If your library doesn't support CMake, you can place the header and source files directly in `src/` and add them to `add_executable`. Keep cpp-httplib, nlohmann/json, and webview as they are.
|
||||
|
||||
## 8.2 Adapting the API to Your Task
|
||||
|
||||
Change the translation API's endpoints and parameters to match your task.
|
||||
|
||||
| Translation app | Your app (e.g., image processing) |
|
||||
|---|---|
|
||||
| `POST /translate` | `POST /process` |
|
||||
| `{"text": "...", "target_lang": "ja"}` | `{"image": "base64...", "filter": "blur"}` |
|
||||
| `POST /translate/stream` | `POST /process/stream` |
|
||||
| `GET /models` | `GET /filters` or `GET /presets` |
|
||||
|
||||
Then update each handler's implementation. For example, just replace the `llm.chat()` calls with your own library's API.
|
||||
|
||||
```cpp
|
||||
// Before: LLM translation
|
||||
auto translation = llm.chat(prompt);
|
||||
res.set_content(json{{"translation", translation}}.dump(), "application/json");
|
||||
|
||||
// After: e.g., an image processing library
|
||||
auto result = my_lib::process(input_image, options);
|
||||
res.set_content(json{{"result", result}}.dump(), "application/json");
|
||||
```
|
||||
|
||||
The same goes for SSE streaming. If your library has a function that reports progress via a callback, you can use the exact same pattern from Chapter 3 to send incremental responses. SSE isn't limited to LLMs — it's useful for any time-consuming task: image processing progress, data conversion steps, long-running computations.
|
||||
|
||||
## 8.3 Design Considerations
|
||||
|
||||
### Libraries with Expensive Initialization
|
||||
|
||||
In this book, we load the LLM model at the top of `main()` and keep it in a variable. This is intentional. Loading the model on every request would take several seconds, so we load it once at startup and reuse it. If your library has expensive initialization (loading large data files, acquiring GPU resources, etc.), the same approach works well.
|
||||
|
||||
### Thread Safety
|
||||
|
||||
cpp-httplib processes requests concurrently using a thread pool. In Chapter 4 we protected the `llm` object with a `std::mutex` to prevent crashes during model switching. The same pattern applies when integrating your own library. If your library isn't thread-safe or you need to swap objects at runtime, protect access with a `std::mutex`.
|
||||
|
||||
## 8.4 Customizing the UI
|
||||
|
||||
Edit the three files in `public/`.
|
||||
|
||||
- **`index.html`** — Change the input form layout. Swap `<textarea>` for `<input type="file">`, add parameter fields, etc.
|
||||
- **`style.css`** — Adjust the layout and colors. Keep the two-column design or switch to a single column
|
||||
- **`script.js`** — Update the `fetch()` target URLs, request bodies, and how responses are displayed
|
||||
|
||||
Even without changing any server code, just swapping the HTML makes the app look completely different. Since these are static files, you can iterate quickly — just reload the browser without restarting the server.
|
||||
|
||||
This book used plain HTML, CSS, and JavaScript, but combining them with a frontend framework like Vue or React, or a CSS framework, would let you build an even more polished app.
|
||||
|
||||
## 8.5 Distribution Considerations
|
||||
|
||||
### Licenses
|
||||
|
||||
Check the licenses of the libraries you're using. cpp-httplib (MIT), nlohmann/json (MIT), and webview (MIT) all allow commercial use. Don't forget to check the license of your own library and its dependencies too.
|
||||
|
||||
### Models and Data Files
|
||||
|
||||
The download mechanism we built in Chapter 4 isn't limited to LLM models. If your app needs large data files, the same pattern lets you auto-download them on first launch, keeping the binary small while sparing users the manual setup.
|
||||
|
||||
If the data is small, you can embed it directly into the binary with cpp-embedlib.
|
||||
|
||||
### Cross-Platform Builds
|
||||
|
||||
webview supports macOS, Linux, and Windows. When building for each platform:
|
||||
|
||||
- **macOS** — No additional dependencies
|
||||
- **Linux** — Requires `libwebkit2gtk-4.1-dev`
|
||||
- **Windows** — Requires the WebView2 runtime (pre-installed on Windows 11)
|
||||
|
||||
Consider setting up cross-platform builds in CI (e.g., GitHub Actions) too.
|
||||
|
||||
## Closing
|
||||
|
||||
Thank you so much for reading to the end. 🙏
|
||||
|
||||
This book started with `/health` returning `{"status":"ok"}` in Chapter 1. From there we built a REST API, added SSE streaming, downloaded models from Hugging Face, created a browser-based Web UI, and packaged it all into a single-binary desktop app. In Chapter 7 we read through `llama-server`'s code and learned how production-quality servers differ in their design. It's been quite a journey, and I'm truly grateful you stuck with it all the way through.
|
||||
|
||||
Looking back, we used several key cpp-httplib features hands-on:
|
||||
|
||||
- **Server**: routing, JSON responses, SSE streaming with `set_chunked_content_provider`, static file serving with `set_mount_point`
|
||||
- **Client**: HTTPS connections, redirect following, large downloads with content receivers, progress callbacks
|
||||
- **WebView integration**: `bind_to_any_port` + `listen_after_bind` for background threading
|
||||
|
||||
cpp-httplib offers many more features beyond what we covered here, including multipart file uploads, authentication, timeout control, compression, and range requests. See [A Tour of cpp-httplib](../../tour/) for details.
|
||||
|
||||
These patterns aren't limited to a translation app. If you want to add a web API to your C++ library, give it a browser UI, or ship it as an easy-to-distribute desktop app — I hope this book serves as a useful reference.
|
||||
|
||||
Take your own library, build your own app, and have fun with it. Happy hacking! 🚀
|
||||
@@ -0,0 +1,26 @@
|
||||
---
|
||||
title: "Building a Desktop LLM App with cpp-httplib"
|
||||
order: 0
|
||||
|
||||
---
|
||||
|
||||
Have you ever wanted to add a web API to your own C++ library, or quickly build an Electron-like desktop app? In Rust you might reach for "Tauri + axum," but in C++ it always seemed out of reach.
|
||||
|
||||
With [cpp-httplib](https://github.com/yhirose/cpp-httplib), [webview/webview](https://github.com/webview/webview), and [cpp-embedlib](https://github.com/yhirose/cpp-embedlib), you can take the same approach in pure C++ — and produce a small, easy-to-distribute single binary.
|
||||
|
||||
In this tutorial we build an LLM-powered translation app using [llama.cpp](https://github.com/ggml-org/llama.cpp), progressing step by step from "REST API" to "SSE streaming" to "Web UI" to "desktop app." Translation is just the vehicle — replace llama.cpp with your own library and the same architecture works for any application.
|
||||
|
||||

|
||||
|
||||
If you know basic C++17 and understand the basics of HTTP / REST APIs, you're ready to start.
|
||||
|
||||
## Chapters
|
||||
|
||||
1. **[Set up the project](ch01-setup)** — Fetch dependencies, configure the build, write scaffold code
|
||||
2. **[Embed llama.cpp and create a REST API](ch02-rest-api)** — Return translation results as JSON
|
||||
3. **[Add token streaming with SSE](ch03-sse-streaming)** — Stream responses token by token
|
||||
4. **[Add model discovery and management](ch04-model-management)** — Download and switch models from Hugging Face
|
||||
5. **[Add a Web UI](ch05-web-ui)** — A browser-based translation interface
|
||||
6. **[Turn it into a desktop app with WebView](ch06-desktop-app)** — A single-binary desktop application
|
||||
7. **[Reading the llama.cpp server source code](ch07-code-reading)** — Compare with production-quality code
|
||||
8. **[Making it your own](ch08-customization)** — Swap in your own library and customize
|
||||
@@ -0,0 +1,36 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 426 160" font-family="system-ui, sans-serif" font-size="14">
|
||||
<rect x="0" y="0" width="426" height="160" rx="8" fill="#f5f3ef"/>
|
||||
|
||||
<defs>
|
||||
<marker id="arrowhead" markerWidth="7" markerHeight="5" refX="7" refY="2.5" orient="auto">
|
||||
<polygon points="0,0 7,2.5 0,5" fill="#198754"/>
|
||||
</marker>
|
||||
</defs>
|
||||
|
||||
<!-- request 1 (y=16) -->
|
||||
<text x="94" y="35" fill="#333" font-weight="bold" text-anchor="end">Request 1:</text>
|
||||
<rect x="106" y="16" width="138" height="30" rx="4" fill="#d1e7dd" stroke="#198754" stroke-width="1"/>
|
||||
<text x="175" y="36" fill="#333" text-anchor="middle">System prompt</text>
|
||||
<text x="256" y="36" fill="#333" text-anchor="middle">+</text>
|
||||
<rect x="270" y="16" width="138" height="30" rx="4" fill="#cfe2ff" stroke="#0d6efd" stroke-width="1"/>
|
||||
<text x="339" y="36" fill="#333" text-anchor="middle">User question A</text>
|
||||
|
||||
<!-- annotation: cache save -->
|
||||
<text x="175" y="64" fill="#198754" font-size="11" text-anchor="middle">Saved to KV cache</text>
|
||||
|
||||
<!-- arrow -->
|
||||
<line x1="175" y1="70" x2="175" y2="90" stroke="#198754" stroke-width="1.2" marker-end="url(#arrowhead)"/>
|
||||
<text x="188" y="85" fill="#198754" font-size="11">Reuse</text>
|
||||
|
||||
<!-- request 2 (y=96) -->
|
||||
<text x="94" y="115" fill="#333" font-weight="bold" text-anchor="end">Request 2:</text>
|
||||
<rect x="106" y="96" width="138" height="30" rx="4" fill="#d1e7dd" stroke="#198754" stroke-width="1" stroke-dasharray="6,3"/>
|
||||
<text x="175" y="116" fill="#333" text-anchor="middle">System prompt</text>
|
||||
<text x="256" y="116" fill="#333" text-anchor="middle">+</text>
|
||||
<rect x="270" y="96" width="138" height="30" rx="4" fill="#cfe2ff" stroke="#0d6efd" stroke-width="1"/>
|
||||
<text x="339" y="116" fill="#333" text-anchor="middle">User question B</text>
|
||||
|
||||
<!-- bottom labels -->
|
||||
<text x="175" y="144" fill="#198754" font-size="11" text-anchor="middle">No recomputation</text>
|
||||
<text x="339" y="144" fill="#0d6efd" font-size="11" text-anchor="middle">Only this is computed</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 2.0 KiB |
@@ -0,0 +1,24 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 440 240" font-family="system-ui, sans-serif" font-size="14">
|
||||
<!-- outer box -->
|
||||
<rect x="0" y="0" width="440" height="240" rx="8" fill="#f5f3ef"/>
|
||||
<text x="20" y="28" font-weight="bold" font-size="16" fill="#333">llama-server</text>
|
||||
|
||||
<!-- slot 0 -->
|
||||
<rect x="20" y="46" width="400" height="32" rx="4" fill="#d1e7dd" stroke="#198754" stroke-width="1"/>
|
||||
<text x="32" y="67" fill="#333">Slot 0: User A's request</text>
|
||||
|
||||
<!-- slot 1 -->
|
||||
<rect x="20" y="86" width="400" height="32" rx="4" fill="#d1e7dd" stroke="#198754" stroke-width="1"/>
|
||||
<text x="32" y="107" fill="#333">Slot 1: User B's request</text>
|
||||
|
||||
<!-- slot 2 -->
|
||||
<rect x="20" y="126" width="400" height="32" rx="4" fill="#e9ecef" stroke="#adb5bd" stroke-width="1"/>
|
||||
<text x="32" y="147" fill="#999">Slot 2: (idle)</text>
|
||||
|
||||
<!-- slot 3 -->
|
||||
<rect x="20" y="166" width="400" height="32" rx="4" fill="#e9ecef" stroke="#adb5bd" stroke-width="1"/>
|
||||
<text x="32" y="187" fill="#999">Slot 3: (idle)</text>
|
||||
|
||||
<!-- arrow + label -->
|
||||
<text x="20" y="224" fill="#333" font-size="13">→ Active slots are inferred together in a single batch</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 1.2 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 130 KiB |
@@ -0,0 +1,88 @@
|
||||
---
|
||||
title: "Getting Started"
|
||||
order: 1
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
```sh
|
||||
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 | Development Environment | Setup |
|
||||
| -- | ----------------------- | ----- |
|
||||
| macOS | Apple Clang | Xcode Command Line Tools (`xcode-select --install`) |
|
||||
| Ubuntu | clang++ or g++ | `apt install clang` or `apt install g++` |
|
||||
| Windows | MSVC | Visual Studio 2022 or later (install with C++ components) |
|
||||
|
||||
## Hello World Server
|
||||
|
||||
Save the following code as `server.cpp`.
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
|
||||
int main() {
|
||||
httplib::Server svr;
|
||||
|
||||
svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
|
||||
res.set_content("Hello, World!", "text/plain");
|
||||
});
|
||||
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
In just a few lines, you have a server that responds to HTTP requests.
|
||||
|
||||
## Compiling and Running
|
||||
|
||||
The sample code in this tutorial is written in C++17 for cleaner, more concise code. cpp-httplib itself can compile with C++11 as well.
|
||||
|
||||
```sh
|
||||
# macOS
|
||||
clang++ -std=c++17 -o server server.cpp
|
||||
|
||||
# Linux
|
||||
# `-pthread`: cpp-httplib uses threads internally
|
||||
clang++ -std=c++17 -pthread -o server server.cpp
|
||||
|
||||
# Windows (Developer Command Prompt)
|
||||
# `/EHsc`: Enable C++ exception handling
|
||||
cl /EHsc /std:c++17 server.cpp
|
||||
```
|
||||
|
||||
Once it compiles, run it.
|
||||
|
||||
```sh
|
||||
# macOS / Linux
|
||||
./server
|
||||
|
||||
# Windows
|
||||
server.exe
|
||||
```
|
||||
|
||||
Open `http://localhost:8080` in your browser. If you see "Hello, World!", you're all set.
|
||||
|
||||
You can also verify with `curl`.
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080/
|
||||
# Hello, World!
|
||||
```
|
||||
|
||||
To stop the server, press `Ctrl+C` in your terminal.
|
||||
|
||||
## Next Steps
|
||||
|
||||
Now you know the basics of running a server. Next, let's look at the client side. cpp-httplib also comes with HTTP client functionality.
|
||||
|
||||
**Next:** [Basic Client](../02-basic-client)
|
||||
@@ -0,0 +1,266 @@
|
||||
---
|
||||
title: "Basic Client"
|
||||
order: 2
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
```cpp
|
||||
#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) {
|
||||
auto q = req.get_param_value("q");
|
||||
res.set_content("Query: " + q, "text/plain");
|
||||
});
|
||||
|
||||
svr.Post("/post", [](const auto &req, auto &res) {
|
||||
res.set_content(req.body, "text/plain");
|
||||
});
|
||||
|
||||
svr.Post("/submit", [](const auto &req, auto &res) {
|
||||
std::string result;
|
||||
for (auto &[key, val] : req.params) {
|
||||
result += key + " = " + val + "\n";
|
||||
}
|
||||
res.set_content(result, "text/plain");
|
||||
});
|
||||
|
||||
svr.Post("/upload", [](const auto &req, auto &res) {
|
||||
auto f = req.form.get_file("file");
|
||||
auto content = f.filename + " (" + std::to_string(f.content.size()) + " bytes)";
|
||||
res.set_content(content, "text/plain");
|
||||
});
|
||||
|
||||
svr.Get("/users/:id", [](const auto &req, auto &res) {
|
||||
auto id = req.path_params.at("id");
|
||||
res.set_content("User ID: " + id, "text/plain");
|
||||
});
|
||||
|
||||
svr.Get(R"(/files/(\d+))", [](const auto &req, auto &res) {
|
||||
auto id = req.matches[1];
|
||||
res.set_content("File ID: " + std::string(id), "text/plain");
|
||||
});
|
||||
|
||||
std::cout << "Listening on port 8080..." << std::endl;
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
## GET Request
|
||||
|
||||
Once the server is running, open a separate terminal and give it a try. Let's start with the simplest GET request.
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
|
||||
auto res = cli.Get("/hi");
|
||||
if (res) {
|
||||
std::cout << res->status << std::endl; // 200
|
||||
std::cout << res->body << std::endl; // Hello!
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Pass the server address to the `httplib::Client` constructor, then call `Get()` to send a request. You can retrieve the status code and body from the returned `res`.
|
||||
|
||||
Here's the equivalent `curl` command.
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080/hi
|
||||
# Hello!
|
||||
```
|
||||
|
||||
## Checking the Response
|
||||
|
||||
A response contains header information in addition to the status code and body.
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/hi");
|
||||
if (res) {
|
||||
// Status code
|
||||
std::cout << res->status << std::endl; // 200
|
||||
|
||||
// Body
|
||||
std::cout << res->body << std::endl; // Hello!
|
||||
|
||||
// Headers
|
||||
std::cout << res->get_header_value("Content-Type") << std::endl; // text/plain
|
||||
}
|
||||
```
|
||||
|
||||
`res->body` is a `std::string`, so if you want to parse a JSON response, you can pass it directly to a JSON library like [nlohmann/json](https://github.com/nlohmann/json).
|
||||
|
||||
## Query Parameters
|
||||
|
||||
To add query parameters to a GET request, you can either write them directly in the URL or use `httplib::Params`.
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/search", httplib::Params{{"q", "cpp-httplib"}});
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // Query: cpp-httplib
|
||||
}
|
||||
```
|
||||
|
||||
`httplib::Params` automatically URL-encodes special characters for you.
|
||||
|
||||
```sh
|
||||
curl "http://localhost:8080/search?q=cpp-httplib"
|
||||
# Query: cpp-httplib
|
||||
```
|
||||
|
||||
## Path Parameters
|
||||
|
||||
When values are embedded directly in the URL path, no special client API is needed. Just pass the path to `Get()` as-is.
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/users/42");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // User ID: 42
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080/users/42
|
||||
# User ID: 42
|
||||
```
|
||||
|
||||
The test server also has a `/files/(\d+)` route that uses a regex to accept numeric IDs only.
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/files/42");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // File ID: 42
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080/files/42
|
||||
# File ID: 42
|
||||
```
|
||||
|
||||
Pass a non-numeric ID like `/files/abc` and you'll get a 404. We'll cover how that works in the next chapter.
|
||||
|
||||
## Request Headers
|
||||
|
||||
To add custom HTTP headers, pass an `httplib::Headers` object. This works with both `Get()` and `Post()`.
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/hi", httplib::Headers{
|
||||
{"Authorization", "Bearer my-token"}
|
||||
});
|
||||
```
|
||||
|
||||
```sh
|
||||
curl -H "Authorization: Bearer my-token" http://localhost:8080/hi
|
||||
```
|
||||
|
||||
## POST Request
|
||||
|
||||
Let's POST some text data. Pass the body as the second argument to `Post()` and the Content-Type as the third.
|
||||
|
||||
```cpp
|
||||
auto res = cli.Post("/post", "Hello, Server!", "text/plain");
|
||||
if (res) {
|
||||
std::cout << res->status << std::endl; // 200
|
||||
std::cout << res->body << std::endl; // Hello, Server!
|
||||
}
|
||||
```
|
||||
|
||||
The test server's `/post` endpoint echoes the body back, so you get the same string you sent.
|
||||
|
||||
```sh
|
||||
curl -X POST -H "Content-Type: text/plain" -d "Hello, Server!" http://localhost:8080/post
|
||||
# Hello, Server!
|
||||
```
|
||||
|
||||
## Sending Form Data
|
||||
|
||||
You can send key-value pairs just like an HTML form. Use `httplib::Params` for this.
|
||||
|
||||
```cpp
|
||||
auto res = cli.Post("/submit", httplib::Params{
|
||||
{"name", "Alice"},
|
||||
{"age", "30"}
|
||||
});
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl;
|
||||
// age = 30
|
||||
// name = Alice
|
||||
}
|
||||
```
|
||||
|
||||
This sends the data in `application/x-www-form-urlencoded` format.
|
||||
|
||||
```sh
|
||||
curl -X POST -d "name=Alice&age=30" http://localhost:8080/submit
|
||||
```
|
||||
|
||||
## POSTing a File
|
||||
|
||||
To upload a file, use `httplib::UploadFormDataItems` to send it as multipart form data.
|
||||
|
||||
```cpp
|
||||
auto res = cli.Post("/upload", httplib::UploadFormDataItems{
|
||||
{"file", "Hello, File!", "hello.txt", "text/plain"}
|
||||
});
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // hello.txt (12 bytes)
|
||||
}
|
||||
```
|
||||
|
||||
Each element in `UploadFormDataItems` has four fields: `{name, content, filename, content_type}`.
|
||||
|
||||
```sh
|
||||
curl -F "file=Hello, File!;filename=hello.txt;type=text/plain" http://localhost:8080/upload
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
Network communication can fail -- the server might not be reachable. Always check whether `res` is valid.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:9999"); // Non-existent port
|
||||
auto res = cli.Get("/hi");
|
||||
|
||||
if (!res) {
|
||||
// Connection error
|
||||
std::cout << "Error: " << httplib::to_string(res.error()) << std::endl;
|
||||
// Error: Connection
|
||||
return 1;
|
||||
}
|
||||
|
||||
// If we reach here, we have a response
|
||||
if (res->status != 200) {
|
||||
std::cout << "HTTP Error: " << res->status << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << res->body << std::endl;
|
||||
```
|
||||
|
||||
There are two levels of errors.
|
||||
|
||||
- **Connection error**: The client couldn't reach the server. `res` evaluates to false, and you can call `res.error()` to find out what went wrong.
|
||||
- **HTTP error**: The server returned an error status (404, 500, etc.). `res` evaluates to true, but you need to check `res->status`.
|
||||
|
||||
## Next Steps
|
||||
|
||||
Now you know how to send requests from a client. Next, let's take a closer look at the server side. We'll dig into routing, path parameters, and more.
|
||||
|
||||
**Next:** [Basic Server](../03-basic-server)
|
||||
@@ -0,0 +1,280 @@
|
||||
---
|
||||
title: "Basic Server"
|
||||
order: 3
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
```cpp
|
||||
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.
|
||||
|
||||
`listen()` is a blocking call. It won't return until the server stops. The server keeps running until you press `Ctrl+C` in your terminal or call `svr.stop()` from another thread.
|
||||
|
||||
## Routing
|
||||
|
||||
Routing is the heart of any server. It's how you tell cpp-httplib: when a request comes in for this URL with this HTTP method, run this code.
|
||||
|
||||
```cpp
|
||||
httplib::Server svr;
|
||||
|
||||
svr.Get("/hi", [](const httplib::Request &req, httplib::Response &res) {
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`svr.Get()` registers a handler for GET requests. The first argument is the path, the second is the handler function. When a GET request arrives at `/hi`, your lambda runs.
|
||||
|
||||
There's a method for each HTTP verb.
|
||||
|
||||
```cpp
|
||||
svr.Get("/path", handler); // GET
|
||||
svr.Post("/path", handler); // POST
|
||||
svr.Put("/path", handler); // PUT
|
||||
svr.Delete("/path", handler); // DELETE
|
||||
```
|
||||
|
||||
The handler signature is `(const httplib::Request &req, httplib::Response &res)`. You can use `auto` to keep it short.
|
||||
|
||||
```cpp
|
||||
svr.Get("/hi", [](const auto &req, auto &res) {
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
The handler only runs when the path matches. Requests to unregistered paths automatically return 404.
|
||||
|
||||
## The Request Object
|
||||
|
||||
The first parameter `req` gives you everything the client sent.
|
||||
|
||||
### Body
|
||||
|
||||
`req.body` holds the request body as a `std::string`.
|
||||
|
||||
```cpp
|
||||
svr.Post("/post", [](const auto &req, auto &res) {
|
||||
// Echo the body back to the client
|
||||
res.set_content(req.body, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### Headers
|
||||
|
||||
Use `req.get_header_value()` to read a request header.
|
||||
|
||||
```cpp
|
||||
svr.Get("/check", [](const auto &req, auto &res) {
|
||||
auto auth = req.get_header_value("Authorization");
|
||||
res.set_content("Auth: " + auth, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### Query Parameters and Form Data
|
||||
|
||||
`req.get_param_value()` retrieves a parameter by name. It works for both GET query parameters and POST form data.
|
||||
|
||||
```cpp
|
||||
svr.Get("/search", [](const auto &req, auto &res) {
|
||||
auto q = req.get_param_value("q");
|
||||
res.set_content("Query: " + q, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
A request to `/search?q=cpp-httplib` gives you `"cpp-httplib"` for `q`.
|
||||
|
||||
To loop over all parameters, use `req.params`.
|
||||
|
||||
```cpp
|
||||
svr.Post("/submit", [](const auto &req, auto &res) {
|
||||
std::string result;
|
||||
for (auto &[key, val] : req.params) {
|
||||
result += key + " = " + val + "\n";
|
||||
}
|
||||
res.set_content(result, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### File Uploads
|
||||
|
||||
Files uploaded via multipart form data are available through `req.form.get_file()`.
|
||||
|
||||
```cpp
|
||||
svr.Post("/upload", [](const auto &req, auto &res) {
|
||||
auto f = req.form.get_file("file");
|
||||
auto content = f.filename + " (" + std::to_string(f.content.size()) + " bytes)";
|
||||
res.set_content(content, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`f.filename` gives you the filename, and `f.content` gives you the file data.
|
||||
|
||||
## Path Parameters
|
||||
|
||||
Sometimes you want to capture part of the URL as a variable -- for example, the `42` in `/users/42`. Use the `:param` syntax to do that.
|
||||
|
||||
```cpp
|
||||
svr.Get("/users/:id", [](const auto &req, auto &res) {
|
||||
auto id = req.path_params.at("id");
|
||||
res.set_content("User ID: " + id, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
A request to `/users/42` gives you `"42"` from `req.path_params.at("id")`. `/users/100` gives you `"100"`.
|
||||
|
||||
You can capture multiple segments at once.
|
||||
|
||||
```cpp
|
||||
svr.Get("/users/:user_id/posts/:post_id", [](const auto &req, auto &res) {
|
||||
auto user_id = req.path_params.at("user_id");
|
||||
auto post_id = req.path_params.at("post_id");
|
||||
res.set_content("User: " + user_id + ", Post: " + post_id, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### Regex Patterns
|
||||
|
||||
You can also write a regular expression directly in the path instead of `:param`. Capture group values are available via `req.matches`, which is a `std::smatch`.
|
||||
|
||||
```cpp
|
||||
// Only accept numeric IDs
|
||||
svr.Get(R"(/files/(\d+))", [](const auto &req, auto &res) {
|
||||
auto id = req.matches[1]; // First capture group
|
||||
res.set_content("File ID: " + std::string(id), "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`/files/42` matches, but `/files/abc` doesn't. This is handy when you want to constrain what values are accepted.
|
||||
|
||||
## Building a Response
|
||||
|
||||
The second parameter `res` is how you send data back to the client.
|
||||
|
||||
### Body and Content-Type
|
||||
|
||||
`res.set_content()` sets the body and Content-Type. That's all you need for a 200 response.
|
||||
|
||||
```cpp
|
||||
svr.Get("/hi", [](const auto &req, auto &res) {
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### Status Code
|
||||
|
||||
To return a different status code, assign to `res.status`.
|
||||
|
||||
```cpp
|
||||
svr.Get("/not-found", [](const auto &req, auto &res) {
|
||||
res.status = 404;
|
||||
res.set_content("Not found", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### Response Headers
|
||||
|
||||
Add response headers with `res.set_header()`.
|
||||
|
||||
```cpp
|
||||
svr.Get("/with-header", [](const auto &req, auto &res) {
|
||||
res.set_header("X-Custom", "my-value");
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
## Walking Through the Test Server
|
||||
|
||||
Now let's use what we've learned to read through the test server from the previous chapter.
|
||||
|
||||
### GET /hi
|
||||
|
||||
```cpp
|
||||
svr.Get("/hi", [](const auto &, auto &res) {
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
The simplest possible handler. We don't need any information from the request, so the `req` parameter is left unnamed. It just returns `"Hello!"`.
|
||||
|
||||
### GET /search
|
||||
|
||||
```cpp
|
||||
svr.Get("/search", [](const auto &req, auto &res) {
|
||||
auto q = req.get_param_value("q");
|
||||
res.set_content("Query: " + q, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`req.get_param_value("q")` pulls out the query parameter `q`. A request to `/search?q=cpp-httplib` returns `"Query: cpp-httplib"`.
|
||||
|
||||
### POST /post
|
||||
|
||||
```cpp
|
||||
svr.Post("/post", [](const auto &req, auto &res) {
|
||||
res.set_content(req.body, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
An echo server. Whatever body the client sends, `req.body` holds it, and we send it straight back.
|
||||
|
||||
### POST /submit
|
||||
|
||||
```cpp
|
||||
svr.Post("/submit", [](const auto &req, auto &res) {
|
||||
std::string result;
|
||||
for (auto &[key, val] : req.params) {
|
||||
result += key + " = " + val + "\n";
|
||||
}
|
||||
res.set_content(result, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
Loops over the form data in `req.params` using structured bindings (`auto &[key, val]`) to unpack each key-value pair.
|
||||
|
||||
### POST /upload
|
||||
|
||||
```cpp
|
||||
svr.Post("/upload", [](const auto &req, auto &res) {
|
||||
auto f = req.form.get_file("file");
|
||||
auto content = f.filename + " (" + std::to_string(f.content.size()) + " bytes)";
|
||||
res.set_content(content, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
Receives a file uploaded via multipart form data. `req.form.get_file("file")` fetches the field named `"file"`, and we respond with the filename and size.
|
||||
|
||||
### GET /users/:id
|
||||
|
||||
```cpp
|
||||
svr.Get("/users/:id", [](const auto &req, auto &res) {
|
||||
auto id = req.path_params.at("id");
|
||||
res.set_content("User ID: " + id, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`:id` is the path parameter. `req.path_params.at("id")` retrieves its value. `/users/42` gives you `"42"`, `/users/alice` gives you `"alice"`.
|
||||
|
||||
### GET /files/(\d+)
|
||||
|
||||
```cpp
|
||||
svr.Get(R"(/files/(\d+))", [](const auto &req, auto &res) {
|
||||
auto id = req.matches[1];
|
||||
res.set_content("File ID: " + std::string(id), "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
The regex `(\d+)` matches numeric IDs only. `/files/42` hits this handler, but `/files/abc` returns 404. `req.matches[1]` retrieves the first capture group.
|
||||
|
||||
## Next Steps
|
||||
|
||||
You now have the full picture of how a server works. Routing, reading requests, building responses -- that's enough to build a real API server.
|
||||
|
||||
Next, let's look at serving static files. We'll build a server that delivers HTML and CSS.
|
||||
|
||||
**Next:** [Static File Server](../04-static-file-server)
|
||||
@@ -0,0 +1,134 @@
|
||||
---
|
||||
title: "Static File Server"
|
||||
order: 4
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
```cpp
|
||||
#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);
|
||||
}
|
||||
```
|
||||
|
||||
The first argument is the URL mount point. The second is the local directory path. In this example, requests to `/` are served from the `./html` directory.
|
||||
|
||||
Let's try it out. First, create an `html` directory and add an `index.html` file.
|
||||
|
||||
```sh
|
||||
mkdir html
|
||||
```
|
||||
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head><title>My Page</title></head>
|
||||
<body>
|
||||
<h1>Hello from cpp-httplib!</h1>
|
||||
<p>This is a static file.</p>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
Compile and start the server.
|
||||
|
||||
```sh
|
||||
g++ -std=c++17 -o server server.cpp -pthread
|
||||
./server
|
||||
```
|
||||
|
||||
Open `http://localhost:8080` in your browser. You should see the contents of `html/index.html`. Visiting `http://localhost:8080/index.html` returns the same page.
|
||||
|
||||
You can also access it with the client code from the previous chapter, or with `curl`.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
auto res = cli.Get("/");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // HTML is displayed
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080
|
||||
```
|
||||
|
||||
## Multiple mount points
|
||||
|
||||
You can call `set_mount_point()` as many times as you like. Each URL path gets its own directory.
|
||||
|
||||
```cpp
|
||||
svr.set_mount_point("/", "./public");
|
||||
svr.set_mount_point("/assets", "./static/assets");
|
||||
svr.set_mount_point("/docs", "./documentation");
|
||||
```
|
||||
|
||||
A request to `/assets/style.css` serves `./static/assets/style.css`. A request to `/docs/guide.html` serves `./documentation/guide.html`.
|
||||
|
||||
## Combining with handlers
|
||||
|
||||
Static file serving and routing handlers — the kind you learned about in the previous chapter — work side by side.
|
||||
|
||||
```cpp
|
||||
httplib::Server svr;
|
||||
|
||||
// API endpoint
|
||||
svr.Get("/api/hello", [](const auto &, auto &res) {
|
||||
res.set_content(R"({"message":"Hello!"})", "application/json");
|
||||
});
|
||||
|
||||
// Static file serving
|
||||
svr.set_mount_point("/", "./public");
|
||||
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
```
|
||||
|
||||
Handlers take priority. The handler responds to `/api/hello`. For every other path, the server looks for a file in `./public`.
|
||||
|
||||
## Adding response headers
|
||||
|
||||
Pass headers as the third argument to `set_mount_point()` and they get attached to every static file response. This is great for cache control.
|
||||
|
||||
```cpp
|
||||
svr.set_mount_point("/", "./public", {
|
||||
{"Cache-Control", "max-age=3600"}
|
||||
});
|
||||
```
|
||||
|
||||
With this in place, the browser caches served files for one hour.
|
||||
|
||||
## A Dockerfile for your static file server
|
||||
|
||||
The cpp-httplib repository includes a `Dockerfile` built for static file serving. We also publish a pre-built image on Docker Hub, so you can get up and running with a single command.
|
||||
|
||||
```sh
|
||||
> docker run -p 8080:80 -v ./my-site:/html yhirose4dockerhub/cpp-httplib-server
|
||||
Serving HTTP on 0.0.0.0:80
|
||||
Mount point: / -> ./html
|
||||
Press Ctrl+C to shutdown gracefully...
|
||||
192.168.65.1 - - [22/Feb/2026:12:00:00 +0000] "GET / HTTP/1.1" 200 256 "-" "Mozilla/5.0 ..."
|
||||
192.168.65.1 - - [22/Feb/2026:12:00:00 +0000] "GET /style.css HTTP/1.1" 200 1024 "-" "Mozilla/5.0 ..."
|
||||
192.168.65.1 - - [22/Feb/2026:12:00:01 +0000] "GET /favicon.ico HTTP/1.1" 404 152 "-" "Mozilla/5.0 ..."
|
||||
```
|
||||
|
||||
Everything in your `./my-site` directory gets served on port 8080. The access log follows the same format as NGINX, so you can see exactly what's happening.
|
||||
|
||||
## What's next
|
||||
|
||||
You can now serve static files. A web server that delivers HTML, CSS, and JavaScript — built with this little code.
|
||||
|
||||
Next, let's encrypt your connections with HTTPS. We'll start by setting up a TLS library.
|
||||
|
||||
**Next:** [TLS Setup](../05-tls-setup)
|
||||
@@ -0,0 +1,88 @@
|
||||
---
|
||||
title: "TLS Setup"
|
||||
order: 5
|
||||
---
|
||||
|
||||
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](https://brew.sh/) (`brew install openssl`) |
|
||||
| Ubuntu / Debian | `sudo apt install libssl-dev` |
|
||||
| Windows | [vcpkg](https://vcpkg.io/) (`vcpkg install openssl`) |
|
||||
|
||||
## Compile Options
|
||||
|
||||
To enable TLS, define the `CPPHTTPLIB_OPENSSL_SUPPORT` macro when compiling. You'll need a few extra options compared to the previous chapters.
|
||||
|
||||
```sh
|
||||
# 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
|
||||
```
|
||||
|
||||
Let's look at what each option does.
|
||||
|
||||
- **`-DCPPHTTPLIB_OPENSSL_SUPPORT`** — Defines the macro that enables TLS support
|
||||
- **`-lssl -lcrypto`** — Links the OpenSSL libraries
|
||||
- **`-I` / `-L`** (macOS only) — Points to the Homebrew OpenSSL paths
|
||||
- **`-framework CoreFoundation -framework Security`** (macOS only) — Needed to automatically load system certificates from the Keychain
|
||||
|
||||
## Verifying the Setup
|
||||
|
||||
Let's make sure everything works. Here's a simple program that passes an HTTPS URL to `httplib::Client`.
|
||||
|
||||
```cpp
|
||||
#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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Compile and run it. If you see `Status: 200`, your setup is complete.
|
||||
|
||||
## Other TLS Backends
|
||||
|
||||
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.
|
||||
|
||||
| Backend | Macro | Libraries to link |
|
||||
| :--- | :--- | :--- |
|
||||
| OpenSSL | `CPPHTTPLIB_OPENSSL_SUPPORT` | `libssl`, `libcrypto` |
|
||||
| Mbed TLS | `CPPHTTPLIB_MBEDTLS_SUPPORT` | `libmbedtls`, `libmbedx509`, `libmbedcrypto` |
|
||||
| wolfSSL | `CPPHTTPLIB_WOLFSSL_SUPPORT` | `libwolfssl` |
|
||||
|
||||
This tour assumes OpenSSL, but the API is the same regardless of which backend you choose.
|
||||
|
||||
## Next Step
|
||||
|
||||
You're all set with TLS. Next, let's send a request to an HTTPS site.
|
||||
|
||||
**Next:** [HTTPS Client](../06-https-client)
|
||||
@@ -0,0 +1,122 @@
|
||||
---
|
||||
title: "HTTPS Client"
|
||||
order: 6
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
```cpp
|
||||
#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::endl; // First 100 chars of the HTML
|
||||
} else {
|
||||
std::cout << "Error: " << httplib::to_string(res.error()) << std::endl;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In Chapter 2, you wrote `httplib::Client cli("http://localhost:8080")`. All you need to change is the scheme to `https://`. Every API you learned in Chapter 2 -- `Get()`, `Post()`, and so on -- works exactly the same way.
|
||||
|
||||
```sh
|
||||
curl https://nghttp2.org/
|
||||
```
|
||||
|
||||
## Specifying a Port
|
||||
|
||||
The default port for HTTPS is 443. If you need a different port, include it in the URL.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://localhost:8443");
|
||||
```
|
||||
|
||||
## CA Certificate Verification
|
||||
|
||||
When connecting over HTTPS, `httplib::Client` verifies the server certificate by default. It only connects to servers whose certificate was issued by a trusted CA (Certificate Authority).
|
||||
|
||||
CA certificates are loaded automatically from the Keychain on macOS, the system CA certificate store on Linux, and the Windows certificate store on Windows. In most cases, no extra configuration is needed.
|
||||
|
||||
### Specifying a CA Certificate File
|
||||
|
||||
On some environments, the system CA certificates may not be found. In that case, use `set_ca_cert_path()` to specify the path directly.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://nghttp2.org");
|
||||
cli.set_ca_cert_path("/etc/ssl/certs/ca-certificates.crt");
|
||||
|
||||
auto res = cli.Get("/");
|
||||
```
|
||||
|
||||
```sh
|
||||
curl --cacert /etc/ssl/certs/ca-certificates.crt https://nghttp2.org/
|
||||
```
|
||||
|
||||
### Disabling Certificate Verification
|
||||
|
||||
During development, you might want to connect to a server with a self-signed certificate. You can disable verification for that.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://localhost:8443");
|
||||
cli.enable_server_certificate_verification(false);
|
||||
|
||||
auto res = cli.Get("/");
|
||||
```
|
||||
|
||||
```sh
|
||||
curl -k https://localhost:8443/
|
||||
```
|
||||
|
||||
Never disable this in production. It opens you up to man-in-the-middle attacks.
|
||||
|
||||
## Following Redirects
|
||||
|
||||
When accessing HTTPS sites, you'll often encounter redirects. For example, `http://` to `https://`, or a bare domain to `www`.
|
||||
|
||||
By default, redirects are not followed. You can check the redirect target in the `Location` header.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://nghttp2.org");
|
||||
|
||||
auto res = cli.Get("/httpbin/redirect/3");
|
||||
if (res) {
|
||||
std::cout << res->status << std::endl; // 302
|
||||
std::cout << res->get_header_value("Location") << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl https://nghttp2.org/httpbin/redirect/3
|
||||
```
|
||||
|
||||
Call `set_follow_location(true)` to automatically follow redirects and get the final response.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://nghttp2.org");
|
||||
cli.set_follow_location(true);
|
||||
|
||||
auto res = cli.Get("/httpbin/redirect/3");
|
||||
if (res) {
|
||||
std::cout << res->status << std::endl; // 200 (the final response)
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl -L https://nghttp2.org/httpbin/redirect/3
|
||||
```
|
||||
|
||||
## Next Steps
|
||||
|
||||
Now you know how to use the HTTPS client. Next, let's set up your own HTTPS server. We'll start with creating a self-signed certificate.
|
||||
|
||||
**Next:** [HTTPS Server](../07-https-server)
|
||||
@@ -0,0 +1,124 @@
|
||||
---
|
||||
title: "HTTPS Server"
|
||||
order: 7
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
```sh
|
||||
openssl req -x509 -noenc -keyout key.pem -out cert.pem -subj /CN=localhost
|
||||
```
|
||||
|
||||
This creates two files:
|
||||
|
||||
- **`cert.pem`** — Server certificate
|
||||
- **`key.pem`** — Private key
|
||||
|
||||
## A Minimal HTTPS Server
|
||||
|
||||
Once you have your certificate, let's write the server.
|
||||
|
||||
```cpp
|
||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::SSLServer svr("cert.pem", "key.pem");
|
||||
|
||||
svr.Get("/", [](const auto &, auto &res) {
|
||||
res.set_content("Hello, HTTPS!", "text/plain");
|
||||
});
|
||||
|
||||
std::cout << "Listening on https://localhost:8443" << std::endl;
|
||||
svr.listen("0.0.0.0", 8443);
|
||||
}
|
||||
```
|
||||
|
||||
Just pass the certificate and private key paths to the `httplib::SSLServer` constructor. The routing API is exactly the same as `httplib::Server` from Chapter 3.
|
||||
|
||||
Compile and start it up.
|
||||
|
||||
## Testing It Out
|
||||
|
||||
With the server running, try accessing it with `curl`. Since we're using a self-signed certificate, add the `-k` option to skip certificate verification.
|
||||
|
||||
```sh
|
||||
curl -k https://localhost:8443/
|
||||
# Hello, HTTPS!
|
||||
```
|
||||
|
||||
If you open `https://localhost:8443` 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.
|
||||
|
||||
## Connecting from a Client
|
||||
|
||||
Let's connect using `httplib::Client` from the previous chapter. There are two ways to connect to a server with a self-signed certificate.
|
||||
|
||||
### Option 1: Disable Certificate Verification
|
||||
|
||||
This is the quick and easy approach for development.
|
||||
|
||||
```cpp
|
||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("https://localhost:8443");
|
||||
cli.enable_server_certificate_verification(false);
|
||||
|
||||
auto res = cli.Get("/");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // Hello, HTTPS!
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Option 2: Specify the Self-Signed Certificate as a CA Certificate
|
||||
|
||||
This is the safer approach. You tell the client to trust `cert.pem` as a CA certificate.
|
||||
|
||||
```cpp
|
||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("https://localhost:8443");
|
||||
cli.set_ca_cert_path("cert.pem");
|
||||
|
||||
auto res = cli.Get("/");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // Hello, HTTPS!
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This way, only connections to the server with that specific certificate are allowed, preventing impersonation. Use this approach whenever possible, even in test environments.
|
||||
|
||||
## Comparing Server and SSLServer
|
||||
|
||||
The `httplib::Server` API you learned in Chapter 3 works exactly the same with `httplib::SSLServer`. The only difference is the constructor.
|
||||
|
||||
| | `httplib::Server` | `httplib::SSLServer` |
|
||||
| -- | ------------------ | -------------------- |
|
||||
| Constructor | No arguments | Certificate and private key paths |
|
||||
| Protocol | HTTP | HTTPS |
|
||||
| Port (convention) | 8080 | 8443 |
|
||||
| Routing | Same | Same |
|
||||
|
||||
To switch an HTTP server to HTTPS, just change the constructor.
|
||||
|
||||
## Next Steps
|
||||
|
||||
Your HTTPS server is up and running. You now have the basics of both HTTP/HTTPS clients and servers covered.
|
||||
|
||||
Next, let's look at the WebSocket support that was recently added to cpp-httplib.
|
||||
|
||||
**Next:** [WebSocket](../08-websocket)
|
||||
@@ -0,0 +1,139 @@
|
||||
---
|
||||
title: "WebSocket"
|
||||
order: 8
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
```cpp
|
||||
#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); // Send back the received message as-is
|
||||
}
|
||||
});
|
||||
|
||||
std::cout << "Listening on port 8080..." << std::endl;
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
You register a WebSocket handler with `svr.WebSocket()`. It works just like `svr.Get()` and `svr.Post()` from Chapter 3.
|
||||
|
||||
Inside the handler, `ws.read(msg)` waits for a message. When the connection closes, `read()` returns `false`, so the loop exits. `ws.send(msg)` sends a message back.
|
||||
|
||||
## Connecting from a Client
|
||||
|
||||
Let's connect to the server using `httplib::ws::WebSocketClient`.
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::ws::WebSocketClient client("ws://localhost:8080/ws");
|
||||
|
||||
if (!client.connect()) {
|
||||
std::cout << "Connection failed" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Send a message
|
||||
client.send("Hello, WebSocket!");
|
||||
|
||||
// Receive a response from the server
|
||||
std::string msg;
|
||||
if (client.read(msg)) {
|
||||
std::cout << msg << std::endl; // Hello, WebSocket!
|
||||
}
|
||||
|
||||
client.close();
|
||||
}
|
||||
```
|
||||
|
||||
Pass a URL in `ws://host:port/path` format to the constructor. Call `connect()` to start the connection, then use `send()` and `read()` to exchange messages.
|
||||
|
||||
## Text and Binary
|
||||
|
||||
WebSocket has two types of messages: text and binary. You can tell them apart by the return value of `read()`.
|
||||
|
||||
```cpp
|
||||
svr.WebSocket("/ws", [](const httplib::Request &, httplib::ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
httplib::ws::ReadResult ret;
|
||||
while ((ret = ws.read(msg))) {
|
||||
if (ret == httplib::ws::Binary) {
|
||||
ws.send(msg.data(), msg.size()); // Send as binary
|
||||
} else {
|
||||
ws.send(msg); // Send as text
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
- `ws.send(const std::string &)` — sends as a text message
|
||||
- `ws.send(const char *, size_t)` — sends as a binary message
|
||||
|
||||
The client-side API is the same.
|
||||
|
||||
## Accessing Request Information
|
||||
|
||||
You can read HTTP request information from the handshake through the first argument `req` in the handler. This is handy for checking authentication tokens.
|
||||
|
||||
```cpp
|
||||
svr.WebSocket("/ws", [](const httplib::Request &req, httplib::ws::WebSocket &ws) {
|
||||
auto token = req.get_header_value("Authorization");
|
||||
if (token.empty()) {
|
||||
ws.close(httplib::ws::CloseStatus::PolicyViolation, "unauthorized");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {
|
||||
ws.send(msg);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
## Using WSS
|
||||
|
||||
WebSocket over HTTPS (WSS) is also supported. On the server side, just register a WebSocket handler on `httplib::SSLServer`.
|
||||
|
||||
```cpp
|
||||
httplib::SSLServer svr("cert.pem", "key.pem");
|
||||
|
||||
svr.WebSocket("/ws", [](const httplib::Request &, httplib::ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {
|
||||
ws.send(msg);
|
||||
}
|
||||
});
|
||||
|
||||
svr.listen("0.0.0.0", 8443);
|
||||
```
|
||||
|
||||
On the client side, use the `wss://` scheme.
|
||||
|
||||
```cpp
|
||||
httplib::ws::WebSocketClient client("wss://localhost:8443/ws");
|
||||
```
|
||||
|
||||
## Next Steps
|
||||
|
||||
Now you know the basics of WebSocket. This wraps up the Tour.
|
||||
|
||||
The next page gives you a summary of features we didn't cover in the Tour.
|
||||
|
||||
**Next:** [What's Next](../09-whats-next)
|
||||
@@ -0,0 +1,228 @@
|
||||
---
|
||||
title: "What's Next"
|
||||
order: 9
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
```cpp
|
||||
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());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You can also pass a `content_receiver` callback to `Get()`. This approach works with Keep-Alive.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
|
||||
cli.Get("/stream", [](const char *data, size_t len) {
|
||||
std::cout.write(data, len);
|
||||
return true;
|
||||
});
|
||||
```
|
||||
|
||||
On the server side, you have `set_content_provider()` and `set_chunked_content_provider()`. Use the former when you know the size, and the latter when you don't.
|
||||
|
||||
```cpp
|
||||
// With known size (sets Content-Length)
|
||||
svr.Get("/file", [](const auto &, auto &res) {
|
||||
auto size = get_file_size("large.bin");
|
||||
res.set_content_provider(size, "application/octet-stream",
|
||||
[](size_t offset, size_t length, httplib::DataSink &sink) {
|
||||
// Send 'length' bytes starting from 'offset'
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// Unknown size (Chunked Transfer Encoding)
|
||||
svr.Get("/stream", [](const auto &, auto &res) {
|
||||
res.set_chunked_content_provider("text/plain",
|
||||
[](size_t offset, httplib::DataSink &sink) {
|
||||
sink.write("chunk\n", 6);
|
||||
return true; // Return false to finish
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
For uploading large files, `make_file_provider()` comes in handy. It streams the file instead of loading it all into memory.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
|
||||
auto res = cli.Post("/upload", {}, {}, {
|
||||
httplib::make_file_provider("file", "/path/to/large-file.zip")
|
||||
});
|
||||
```
|
||||
|
||||
## Server-Sent Events (SSE)
|
||||
|
||||
We provide an SSE client as well. It supports automatic reconnection and resuming via `Last-Event-ID`.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
httplib::sse::SSEClient sse(cli, "/events");
|
||||
|
||||
sse.on_message([](const httplib::sse::SSEMessage &msg) {
|
||||
std::cout << msg.event << ": " << msg.data << std::endl;
|
||||
});
|
||||
|
||||
sse.start(); // Blocking, with auto-reconnection
|
||||
```
|
||||
|
||||
You can also set separate handlers for each event type.
|
||||
|
||||
```cpp
|
||||
sse.on_event("update", [](const httplib::sse::SSEMessage &msg) {
|
||||
// Only handles "update" events
|
||||
});
|
||||
```
|
||||
|
||||
## Authentication
|
||||
|
||||
The client has helpers for Basic auth, Bearer Token auth, and Digest auth.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://api.example.com");
|
||||
cli.set_basic_auth("user", "password");
|
||||
cli.set_bearer_token_auth("my-token");
|
||||
```
|
||||
|
||||
## Compression
|
||||
|
||||
We support compression and decompression with gzip, Brotli, and Zstandard. Define the corresponding macro when you compile.
|
||||
|
||||
| Method | Macro |
|
||||
| -- | -- |
|
||||
| gzip | `CPPHTTPLIB_ZLIB_SUPPORT` |
|
||||
| Brotli | `CPPHTTPLIB_BROTLI_SUPPORT` |
|
||||
| Zstandard | `CPPHTTPLIB_ZSTD_SUPPORT` |
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://example.com");
|
||||
cli.set_compress(true); // Compress request body
|
||||
cli.set_decompress(true); // Decompress response body
|
||||
```
|
||||
|
||||
## Proxy
|
||||
|
||||
You can connect through an HTTP proxy.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://example.com");
|
||||
cli.set_proxy("proxy.example.com", 8080);
|
||||
cli.set_proxy_basic_auth("user", "password");
|
||||
```
|
||||
|
||||
## Timeouts
|
||||
|
||||
You can set connection, read, and write timeouts individually.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://example.com");
|
||||
cli.set_connection_timeout(5, 0); // 5 seconds
|
||||
cli.set_read_timeout(10, 0); // 10 seconds
|
||||
cli.set_write_timeout(10, 0); // 10 seconds
|
||||
```
|
||||
|
||||
## Keep-Alive
|
||||
|
||||
If you're making multiple requests to the same server, enable Keep-Alive. It reuses the TCP connection, which is much more efficient.
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://example.com");
|
||||
cli.set_keep_alive(true);
|
||||
```
|
||||
|
||||
## Server Middleware
|
||||
|
||||
You can hook into request processing before and after handlers run.
|
||||
|
||||
```cpp
|
||||
svr.set_pre_routing_handler([](const auto &req, auto &res) {
|
||||
// Runs before every request
|
||||
return httplib::Server::HandlerResponse::Unhandled; // Continue to normal routing
|
||||
});
|
||||
|
||||
svr.set_post_routing_handler([](const auto &req, auto &res) {
|
||||
// Runs after the response is sent
|
||||
res.set_header("X-Server", "cpp-httplib");
|
||||
});
|
||||
```
|
||||
|
||||
Use `res.user_data` to pass data from middleware to handlers. This is useful for sharing things like decoded auth tokens.
|
||||
|
||||
```cpp
|
||||
svr.set_pre_routing_handler([](const auto &req, auto &res) {
|
||||
res.user_data["auth_user"] = std::string("alice");
|
||||
return httplib::Server::HandlerResponse::Unhandled;
|
||||
});
|
||||
|
||||
svr.Get("/me", [](const auto &req, auto &res) {
|
||||
auto user = std::any_cast<std::string>(res.user_data.at("auth_user"));
|
||||
res.set_content("Hello, " + user, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
You can also customize error and exception handlers.
|
||||
|
||||
```cpp
|
||||
svr.set_error_handler([](const auto &req, auto &res) {
|
||||
res.set_content("Custom Error Page", "text/html");
|
||||
});
|
||||
|
||||
svr.set_exception_handler([](const auto &req, auto &res, std::exception_ptr ep) {
|
||||
res.status = 500;
|
||||
res.set_content("Internal Server Error", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
## Logging
|
||||
|
||||
You can set a logger on both the server and the client.
|
||||
|
||||
```cpp
|
||||
svr.set_logger([](const auto &req, const auto &res) {
|
||||
std::cout << req.method << " " << req.path << " " << res.status << std::endl;
|
||||
});
|
||||
```
|
||||
|
||||
## Unix Domain Socket
|
||||
|
||||
In addition to TCP, we support Unix Domain Sockets. You can use them for inter-process communication on the same machine.
|
||||
|
||||
```cpp
|
||||
// Server
|
||||
httplib::Server svr;
|
||||
svr.set_address_family(AF_UNIX);
|
||||
svr.listen("/tmp/httplib.sock", 0);
|
||||
```
|
||||
|
||||
```cpp
|
||||
// Client
|
||||
httplib::Client cli("http://localhost");
|
||||
cli.set_address_family(AF_UNIX);
|
||||
cli.set_hostname_addr_map({{"localhost", "/tmp/httplib.sock"}});
|
||||
|
||||
auto res = cli.Get("/");
|
||||
```
|
||||
|
||||
## Learn More
|
||||
|
||||
Want to dig deeper? Check out these resources.
|
||||
|
||||
- Cookbook — A collection of recipes for common use cases
|
||||
- [README](https://github.com/yhirose/cpp-httplib/blob/master/README.md) — Full API reference
|
||||
- [README-sse](https://github.com/yhirose/cpp-httplib/blob/master/README-sse.md) — How to use Server-Sent Events
|
||||
- [README-stream](https://github.com/yhirose/cpp-httplib/blob/master/README-stream.md) — How to use the Streaming API
|
||||
- [README-websocket](https://github.com/yhirose/cpp-httplib/blob/master/README-websocket.md) — How to use the WebSocket server
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
title: "A Tour of cpp-httplib"
|
||||
order: 0
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
1. [Getting Started](01-getting-started) — Get httplib.h and build a Hello World server
|
||||
2. [Basic Client](02-basic-client) — Send GET/POST requests and use path parameters
|
||||
3. [Basic Server](03-basic-server) — Routing, path parameters, and building responses
|
||||
4. [Static File Server](04-static-file-server) — Serve static files
|
||||
5. [TLS Setup](05-tls-setup) — Set up OpenSSL / mbedTLS
|
||||
6. [HTTPS Client](06-https-client) — Make requests to HTTPS sites
|
||||
7. [HTTPS Server](07-https-server) — Build an HTTPS server
|
||||
8. [WebSocket](08-websocket) — Learn the basics of WebSocket communication
|
||||
9. [What's Next](09-whats-next) — Explore more features
|
||||
@@ -0,0 +1,96 @@
|
||||
---
|
||||
title: "Cookbook"
|
||||
order: 0
|
||||
status: "draft"
|
||||
---
|
||||
|
||||
「〇〇をするには?」という問いに答えるレシピ集です。各レシピは独立しているので、必要なページだけ読めます。
|
||||
|
||||
## クライアント
|
||||
|
||||
### 基本
|
||||
- レスポンスボディを文字列で取得する / ファイルに保存する
|
||||
- JSON を送受信する
|
||||
- デフォルトヘッダーを設定する(`set_default_headers`)
|
||||
- リダイレクトを追従する(`set_follow_location`)
|
||||
|
||||
### 認証
|
||||
- Basic 認証を使う(`set_basic_auth`)
|
||||
- Bearer トークンで API を呼ぶ
|
||||
|
||||
### ファイル送信
|
||||
- ファイルをマルチパートフォームとしてアップロードする(`make_file_provider`)
|
||||
- ファイルを生バイナリとして POST する(`make_file_body`)
|
||||
- チャンク転送でボディを送る(Content Provider)
|
||||
|
||||
### ストリーミング・進捗
|
||||
- レスポンスをストリーミングで受信する
|
||||
- 進捗コールバックを使う(`DownloadProgress` / `UploadProgress`)
|
||||
|
||||
### 接続・パフォーマンス
|
||||
- タイムアウトを設定する(`set_connection_timeout` / `set_read_timeout`)
|
||||
- 全体タイムアウトを設定する(`set_max_timeout`)
|
||||
- 接続の再利用と Keep-Alive の挙動を理解する
|
||||
- 圧縮を有効にする(`set_compress` / `set_decompress`)
|
||||
- プロキシを経由してリクエストを送る(`set_proxy`)
|
||||
|
||||
### エラー処理・デバッグ
|
||||
- エラーコードをハンドリングする(`Result::error()`)
|
||||
- SSL エラーをハンドリングする(`ssl_error()` / `ssl_backend_error()`)
|
||||
- クライアントにログを設定する(`set_logger` / `set_error_logger`)
|
||||
|
||||
## サーバー
|
||||
|
||||
### 基本
|
||||
- GET / POST / PUT / DELETE ハンドラを登録する
|
||||
- JSON リクエストを受け取り JSON レスポンスを返す
|
||||
- パスパラメーターを使う(`/users/:id`)
|
||||
- 静的ファイルサーバーを設定する(`set_mount_point`)
|
||||
|
||||
### ストリーミング・ファイル
|
||||
- 大きなファイルをストリーミングで返す(`ContentProvider`)
|
||||
- ファイルダウンロードレスポンスを返す(`Content-Disposition`)
|
||||
- マルチパートデータをストリーミングで受け取る(`ContentReader`)
|
||||
- レスポンスを圧縮して返す(gzip)
|
||||
|
||||
### ハンドラチェーン
|
||||
- 全ルートに共通の前処理をする(Pre-routing handler)
|
||||
- Post-routing handler でレスポンスヘッダーを追加する(CORS など)
|
||||
- Pre-request handler でルート単位の認証を行う(`matched_route`)
|
||||
- `res.user_data` でハンドラ間データを渡す
|
||||
|
||||
### エラー処理・デバッグ
|
||||
- カスタムエラーページを返す(`set_error_handler`)
|
||||
- 例外をキャッチする(`set_exception_handler`)
|
||||
- リクエストをログに記録する(Logger)
|
||||
- クライアントが切断したか検出する(`req.is_connection_closed()`)
|
||||
|
||||
### 運用・チューニング
|
||||
- ポートを動的に割り当てる(`bind_to_any_port`)
|
||||
- `listen_after_bind` で起動順序を制御する
|
||||
- グレースフルシャットダウンする(`stop()` とシグナルハンドリング)
|
||||
- Keep-Alive を調整する(`set_keep_alive_max_count` / `set_keep_alive_timeout`)
|
||||
- マルチスレッド数を設定する(`new_task_queue`)
|
||||
|
||||
## TLS / セキュリティ
|
||||
|
||||
- OpenSSL・mbedTLS・wolfSSL の選択指針(ビルド時の `#define` の違い)
|
||||
- SSL 証明書の検証を制御する(証明書の無効化・カスタム CA・カスタムコールバック)
|
||||
- カスタム証明書検証コールバックを使う(`set_server_certificate_verifier`)
|
||||
- SSL/TLS サーバーを立ち上げる(証明書・秘密鍵の設定)
|
||||
- mTLS(クライアント証明書による相互認証)を設定する
|
||||
- サーバー側でピア証明書を参照する(`req.peer_cert()` / SNI)
|
||||
|
||||
## SSE
|
||||
|
||||
- SSE サーバーを実装する
|
||||
- SSE でイベント名を使い分ける
|
||||
- SSE の再接続を処理する(`Last-Event-ID`)
|
||||
- SSE をクライアントで受信する
|
||||
|
||||
## WebSocket
|
||||
|
||||
- WebSocket エコーサーバー/クライアントを実装する
|
||||
- ハートビートを設定する(`set_websocket_ping_interval` / `CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND`)
|
||||
- 接続クローズをハンドリングする
|
||||
- バイナリフレームを送受信する
|
||||
@@ -0,0 +1,25 @@
|
||||
---
|
||||
title: "cpp-httplib"
|
||||
order: 0
|
||||
---
|
||||
|
||||
[cpp-httplib](https://github.com/yhirose/cpp-httplib)は、C++用のHTTP/HTTPSライブラリです。[`httplib.h`](https://github.com/yhirose/cpp-httplib/raw/refs/tags/latest/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、テスト用のモックサーバーなど、多くのユースケースで十分な性能を発揮します。
|
||||
|
||||
「今日の課題を、今日中に解決する」— cpp-httplibが目指しているのは、そういうシンプルさです。
|
||||
|
||||
## ドキュメント
|
||||
|
||||
- [A Tour of cpp-httplib](tour/) — 基本を順を追って学べるチュートリアル。初めての方はここから
|
||||
- [Building a Desktop LLM App](llm-app/) — llama.cppを組み込んだデスクトップアプリを段階的に構築する実践ガイド
|
||||
|
||||
## お楽しみに
|
||||
|
||||
- [Cookbook](cookbook/) — 目的別のレシピ集。必要なトピックから読めます
|
||||
@@ -0,0 +1,236 @@
|
||||
---
|
||||
title: "1. プロジェクト環境を作る"
|
||||
order: 1
|
||||
|
||||
---
|
||||
|
||||
llama.cppを推論エンジンに使って、テキスト翻訳のREST APIサーバーを段階的に作っていきます。最終的にはこんなリクエストで翻訳結果が返ってくるようになります。
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8080/translate \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"text": "The weather is nice today. Shall we go for a walk?", "target_lang": "ja"}'
|
||||
```
|
||||
|
||||
```json
|
||||
{
|
||||
"translation": "今日はいい天気ですね。散歩に行きましょうか?"
|
||||
}
|
||||
```
|
||||
|
||||
「翻訳API」はあくまで一例です。プロンプトを差し替えれば、要約・コード生成・チャットボットなど、お好きなLLMアプリに応用できます。
|
||||
|
||||
最終的にサーバーが提供するAPIの一覧です。
|
||||
|
||||
| メソッド | パス | 説明 | 章 |
|
||||
| -------- | ---- | ---- | -- |
|
||||
| `GET` | `/health` | サーバーの状態を返す | 1 |
|
||||
| `POST` | `/translate` | テキストを翻訳してJSONで返す | 2 |
|
||||
| `POST` | `/translate/stream` | トークン単位でSSEストリーミング | 3 |
|
||||
| `GET` | `/models` | モデル一覧(available / downloaded / selected) | 4 |
|
||||
| `POST` | `/models/select` | モデルを選択(未ダウンロードなら自動取得) | 4 |
|
||||
|
||||
この章では、まずプロジェクトの環境を整えます。依存ライブラリの取得、ディレクトリ構成、ビルド設定、モデルファイルの入手まで済ませて、次の章ですぐにコードを書き始められるようにしましょう。
|
||||
|
||||
## 前提条件
|
||||
|
||||
- C++20対応コンパイラ(GCC 10+、Clang 10+、MSVC 2019 16.8+)
|
||||
- CMake 3.20以上
|
||||
- OpenSSL(4章でHTTPSクライアントに使用。macOS: `brew install openssl`、Ubuntu: `sudo apt install libssl-dev`)
|
||||
- 十分なディスク容量(モデルファイルが数GBになります)
|
||||
|
||||
## 1.1 何を使うか
|
||||
|
||||
使うライブラリはこちらです。
|
||||
|
||||
| ライブラリ | 役割 |
|
||||
| ----------- | ------ |
|
||||
| [cpp-httplib](https://github.com/yhirose/cpp-httplib) | HTTPサーバー/クライアント |
|
||||
| [nlohmann/json](https://github.com/nlohmann/json) | JSONパーサー |
|
||||
| [cpp-llamalib](https://github.com/yhirose/cpp-llamalib) | llama.cppラッパー |
|
||||
| [llama.cpp](https://github.com/ggml-org/llama.cpp) | LLM推論エンジン |
|
||||
| [webview/webview](https://github.com/webview/webview) | デスクトップWebView(6章で使用) |
|
||||
|
||||
cpp-httplib、nlohmann/json、cpp-llamalibはヘッダーオンリーライブラリです。`curl`でヘッダーファイルを1枚ダウンロードして`#include`するだけでも使えますが、本書ではCMakeの`FetchContent`で自動取得します。`CMakeLists.txt`に書いておけば、`cmake -B build`の時点で全ライブラリが自動でダウンロード・ビルドされるので、手作業の手順が減ります。`webview`は6章で使うので、今は気にしなくて大丈夫です。
|
||||
|
||||
## 1.2 ディレクトリ構成
|
||||
|
||||
最終的にこんな構成になります。
|
||||
|
||||
```ascii
|
||||
translate-app/
|
||||
├── CMakeLists.txt
|
||||
├── models/
|
||||
│ └── (GGUFファイル)
|
||||
└── src/
|
||||
└── main.cpp
|
||||
```
|
||||
|
||||
ライブラリのソースコードはプロジェクトに含めません。CMakeの`FetchContent`がビルド時に自動で取得するので、必要なのは自分のコードだけです。
|
||||
|
||||
プロジェクトディレクトリを作って、gitリポジトリにしましょう。
|
||||
|
||||
```bash
|
||||
mkdir translate-app && cd translate-app
|
||||
mkdir src models
|
||||
git init
|
||||
```
|
||||
|
||||
## 1.3 GGUFモデルファイルを入手する
|
||||
|
||||
LLMの推論にはモデルファイルが必要です。GGUFはllama.cppが使うモデル形式で、Hugging Faceにたくさんあります。
|
||||
|
||||
まずは小さいモデルで試してみましょう。GoogleのGemma 2 2Bの量子化版(約1.6GB)がおすすめです。軽量ですが多言語に対応していて、翻訳タスクにも向いています。
|
||||
|
||||
```bash
|
||||
curl -L -o models/gemma-2-2b-it-Q4_K_M.gguf \
|
||||
https://huggingface.co/bartowski/gemma-2-2b-it-GGUF/resolve/main/gemma-2-2b-it-Q4_K_M.gguf
|
||||
```
|
||||
|
||||
4章で、このダウンロード自体をアプリ内からcpp-httplibのクライアント機能で行えるようにします。
|
||||
|
||||
## 1.4 CMakeLists.txt
|
||||
|
||||
プロジェクトルートに`CMakeLists.txt`を作ります。`FetchContent`で依存ライブラリを宣言しておくと、CMakeが自動でダウンロード・ビルドしてくれます。
|
||||
|
||||
<!-- data-file="CMakeLists.txt" -->
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(translate-server CXX)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
include(FetchContent)
|
||||
|
||||
# llama.cpp(LLM推論エンジン)
|
||||
FetchContent_Declare(llama
|
||||
GIT_REPOSITORY https://github.com/ggml-org/llama.cpp
|
||||
GIT_TAG master
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(llama)
|
||||
|
||||
# cpp-httplib(HTTPサーバー/クライアント)
|
||||
FetchContent_Declare(httplib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-httplib
|
||||
GIT_TAG master
|
||||
)
|
||||
FetchContent_MakeAvailable(httplib)
|
||||
|
||||
# nlohmann/json(JSONパーサー)
|
||||
FetchContent_Declare(json
|
||||
URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
|
||||
)
|
||||
FetchContent_MakeAvailable(json)
|
||||
|
||||
# cpp-llamalib(llama.cppヘッダーオンリーラッパー)
|
||||
FetchContent_Declare(cpp_llamalib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-llamalib
|
||||
GIT_TAG main
|
||||
)
|
||||
FetchContent_MakeAvailable(cpp_llamalib)
|
||||
|
||||
add_executable(translate-server src/main.cpp)
|
||||
|
||||
target_link_libraries(translate-server PRIVATE
|
||||
httplib::httplib
|
||||
nlohmann_json::nlohmann_json
|
||||
cpp-llamalib
|
||||
)
|
||||
```
|
||||
|
||||
`FetchContent_Declare`でライブラリのソース取得先を宣言し、`FetchContent_MakeAvailable`で実際に取得・ビルドします。初回の`cmake -B build`は全ライブラリのダウンロードとllama.cppのビルドが走るので時間がかかりますが、2回目以降はキャッシュが効きます。
|
||||
|
||||
`target_link_libraries`でリンクするだけで、インクルードパスやビルド設定は各ライブラリのCMakeが自動で設定してくれます。
|
||||
|
||||
## 1.5 雛形コードの作成
|
||||
|
||||
この雛形コードをベースに、章ごとに機能を追加していきます。
|
||||
|
||||
<!-- data-file="main.cpp" -->
|
||||
```cpp
|
||||
// src/main.cpp
|
||||
#include <httplib.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
httplib::Server svr;
|
||||
|
||||
// `Ctrl+C`でgraceful shutdown
|
||||
void signal_handler(int sig) {
|
||||
if (sig == SIGINT || sig == SIGTERM) {
|
||||
std::cout << "\nReceived signal, shutting down gracefully...\n";
|
||||
svr.stop();
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// リクエストとレスポンスをログに記録
|
||||
svr.set_logger([](const auto &req, const auto &res) {
|
||||
std::cout << req.method << " " << req.path << " -> " << res.status
|
||||
<< std::endl;
|
||||
});
|
||||
|
||||
// ヘルスチェック
|
||||
svr.Get("/health", [](const auto &, auto &res) {
|
||||
res.set_content(json{{"status", "ok"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// 各エンドポイントのダミー実装(以降の章で本物に差し替えていく)
|
||||
svr.Post("/translate",
|
||||
[](const auto &req, auto &res) {
|
||||
res.set_content(json{{"translation", "TODO"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
svr.Post("/translate/stream",
|
||||
[](const auto &req, auto &res) {
|
||||
res.set_content("data: \"TODO\"\n\ndata: [DONE]\n\n", "text/event-stream");
|
||||
});
|
||||
|
||||
svr.Get("/models",
|
||||
[](const auto &req, auto &res) {
|
||||
res.set_content(json{{"models", json::array()}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
svr.Post("/models/select",
|
||||
[](const auto &req, auto &res) {
|
||||
res.set_content(json{{"status", "TODO"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// `Ctrl+C` (`SIGINT`)や`kill` (`SIGTERM`)でサーバーを停止できるようにする
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
||||
// サーバー起動
|
||||
std::cout << "Listening on http://127.0.0.1:8080" << std::endl;
|
||||
svr.listen("127.0.0.1", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
## 1.6 ビルドと動作確認
|
||||
|
||||
ビルドしてサーバーを起動し、curlでリクエストが通るか確かめます。
|
||||
|
||||
```bash
|
||||
cmake -B build
|
||||
cmake --build build -j
|
||||
./build/translate-server
|
||||
```
|
||||
|
||||
別のターミナルからcurlで確認してみましょう。
|
||||
|
||||
```bash
|
||||
curl http://localhost:8080/health
|
||||
# => {"status":"ok"}
|
||||
```
|
||||
|
||||
JSONが返ってくれば環境構築は完了です。
|
||||
|
||||
## 次の章へ
|
||||
|
||||
環境が整ったので、次の章ではこの雛形に翻訳REST APIを実装します。llama.cppで推論を行い、cpp-httplibでそれをHTTPエンドポイントとして公開します。
|
||||
|
||||
**Next:** [llama.cppを組み込んでREST APIを作る](../ch02-rest-api)
|
||||
@@ -0,0 +1,212 @@
|
||||
---
|
||||
title: "2. llama.cppを組み込んでREST APIを作る"
|
||||
order: 2
|
||||
|
||||
---
|
||||
|
||||
1章の雛形では`/translate`が`"TODO"`を返すだけでした。この章ではllama.cppの推論を組み込んで、実際に翻訳結果を返すAPIに仕上げます。
|
||||
|
||||
llama.cppのAPIを直接扱うとコードが長くなるので、薄いラッパーライブラリ[cpp-llamalib](https://github.com/yhirose/cpp-llamalib)を使います。モデルのロードから推論まで数行で書けるので、cpp-httplibの使い方に集中できます。
|
||||
|
||||
## 2.1 LLMの初期化
|
||||
|
||||
`llamalib::Llama`にモデルファイルのパスを渡すだけで、モデルのロード・コンテキスト作成・サンプラー設定がすべて済みます。1章で別のモデルをダウンロードした場合は、パスをそのモデルに合わせてください。
|
||||
|
||||
```cpp
|
||||
#include <cpp-llamalib.h>
|
||||
|
||||
int main() {
|
||||
auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf"};
|
||||
|
||||
// LLM推論は時間がかかるのでタイムアウトを長めに設定(デフォルトは5秒)
|
||||
svr.set_read_timeout(300);
|
||||
svr.set_write_timeout(300);
|
||||
|
||||
// ... HTTPサーバーの構築・起動 ...
|
||||
}
|
||||
```
|
||||
|
||||
GPU層数やコンテキスト長などを変えたい場合は`llamalib::Options`で指定できます。
|
||||
|
||||
```cpp
|
||||
auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf", {
|
||||
.n_gpu_layers = 0, // CPUのみ
|
||||
.n_ctx = 4096,
|
||||
}};
|
||||
```
|
||||
|
||||
## 2.2 `/translate`ハンドラ
|
||||
|
||||
1章ではダミーのJSONを返していたハンドラを、実際の推論に差し替えます。
|
||||
|
||||
```cpp
|
||||
svr.Post("/translate",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
// JSONパース(第3引数`false`: 失敗時に例外を投げず`is_discarded()`で判定)
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
// 必須フィールドの検証
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja"); // デフォルトは日本語
|
||||
|
||||
// プロンプトを組み立てて推論
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
try {
|
||||
auto translation = llm.chat(prompt);
|
||||
res.set_content(json{{"translation", translation}}.dump(),
|
||||
"application/json");
|
||||
} catch (const std::exception &e) {
|
||||
res.status = 500;
|
||||
res.set_content(json{{"error", e.what()}}.dump(), "application/json");
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
`llm.chat()`は推論中に例外を投げることがあります(コンテキスト長の超過など)。`try/catch`で捕捉してエラーをJSONで返すことで、サーバーがクラッシュするのを防ぎます。
|
||||
|
||||
## 2.3 全体のコード
|
||||
|
||||
ここまでの変更をまとめた完成形です。
|
||||
|
||||
<details>
|
||||
<summary data-file="main.cpp">全体のコード(main.cpp)</summary>
|
||||
|
||||
```cpp
|
||||
#include <httplib.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <cpp-llamalib.h>
|
||||
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
httplib::Server svr;
|
||||
|
||||
// `Ctrl+C`でgraceful shutdown
|
||||
void signal_handler(int sig) {
|
||||
if (sig == SIGINT || sig == SIGTERM) {
|
||||
std::cout << "\nReceived signal, shutting down gracefully...\n";
|
||||
svr.stop();
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// 1章でダウンロードしたモデルをロード
|
||||
auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf"};
|
||||
|
||||
// LLM推論は時間がかかるのでタイムアウトを長めに設定(デフォルトは5秒)
|
||||
svr.set_read_timeout(300);
|
||||
svr.set_write_timeout(300);
|
||||
|
||||
// リクエストとレスポンスをログに記録
|
||||
svr.set_logger([](const auto &req, const auto &res) {
|
||||
std::cout << req.method << " " << req.path << " -> " << res.status
|
||||
<< std::endl;
|
||||
});
|
||||
|
||||
svr.Get("/health", [](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"status", "ok"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
svr.Post("/translate",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
// JSONパース(第3引数`false`: 失敗時に例外を投げず`is_discarded()`で判定)
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
// 必須フィールドの検証
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja"); // デフォルトは日本語
|
||||
|
||||
// プロンプトを組み立てて推論
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
try {
|
||||
auto translation = llm.chat(prompt);
|
||||
res.set_content(json{{"translation", translation}}.dump(),
|
||||
"application/json");
|
||||
} catch (const std::exception &e) {
|
||||
res.status = 500;
|
||||
res.set_content(json{{"error", e.what()}}.dump(), "application/json");
|
||||
}
|
||||
});
|
||||
|
||||
// 以降の章で本物に差し替えるダミー実装
|
||||
svr.Get("/models",
|
||||
[](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"models", json::array()}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
svr.Post("/models/select",
|
||||
[](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"status", "TODO"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// `Ctrl+C` (`SIGINT`)や`kill` (`SIGTERM`)でサーバーを停止できるようにする
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
||||
// サーバー起動(`stop()`が呼ばれるまでブロック)
|
||||
std::cout << "Listening on http://127.0.0.1:8080" << std::endl;
|
||||
svr.listen("127.0.0.1", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
## 2.4 動作確認
|
||||
|
||||
ビルドし直してサーバーを起動し、今度は実際の翻訳結果が返ってくるか確かめましょう。
|
||||
|
||||
```bash
|
||||
cmake --build build -j
|
||||
./build/translate-server
|
||||
```
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8080/translate \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"text": "I had a great time visiting Tokyo last spring. The cherry blossoms were beautiful.", "target_lang": "ja"}'
|
||||
# => {"translation":"去年の春に東京を訪れた。桜が綺麗だった。"}
|
||||
```
|
||||
|
||||
1章では`"TODO"`が返ってきていましたが、今度は実際の翻訳結果が返ってきます。
|
||||
|
||||
## 次の章へ
|
||||
|
||||
この章で作ったREST APIは、翻訳が完了するまで全文を待つので、長いテキストだとユーザーは進捗がわからないまま待つことになります。
|
||||
|
||||
次の章ではSSE(Server-Sent Events)を使って、トークンが生成されるたびにリアルタイムで返す仕組みにします。
|
||||
|
||||
**Next:** [SSEでトークンストリーミングを追加する](../ch03-sse-streaming)
|
||||
@@ -0,0 +1,264 @@
|
||||
---
|
||||
title: "3. SSEでトークンストリーミングを追加する"
|
||||
order: 3
|
||||
|
||||
---
|
||||
|
||||
2章の`/translate`は、翻訳が完了してから結果をまとめて返していました。短い文なら問題ありませんが、長い文だとユーザーは何も表示されないまま何秒も待つことになります。
|
||||
|
||||
この章ではSSE(Server-Sent Events)を使って、トークンが生成されるたびにリアルタイムで返す`/translate/stream`エンドポイントを追加します。ChatGPTやClaudeのAPIでおなじみの方式です。
|
||||
|
||||
## 3.1 SSEとは
|
||||
|
||||
SSEはHTTPのレスポンスをストリームとして送る仕組みです。クライアントがリクエストを送ると、サーバーは接続を保ったままイベントを少しずつ返します。フォーマットはシンプルなテキストです。
|
||||
|
||||
```text
|
||||
data: "去年の"
|
||||
data: "春に"
|
||||
data: "東京を"
|
||||
data: [DONE]
|
||||
```
|
||||
|
||||
各行は`data:`で始まり、空行で区切ります。Content-Typeは`text/event-stream`です。トークンはJSON文字列としてエスケープして送るので、ダブルクォートで囲んだ形式になります(3.3節で実装します)。
|
||||
|
||||
## 3.2 cpp-httplibでのストリーミング
|
||||
|
||||
cpp-httplibでは`set_chunked_content_provider`を使うと、レスポンスを少しずつ送れます。コールバックの中で`sink.os`に書き込むたびにデータがクライアントに届きます。
|
||||
|
||||
```cpp
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[](size_t offset, httplib::DataSink &sink) {
|
||||
sink.os << "data: hello\n\n";
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
```
|
||||
|
||||
`sink.done()`を呼ぶとストリームが終了します。クライアントが途中で接続を切った場合、`sink.os`の書き込みが失敗して`sink.os.fail()`が`true`になります。これを使って切断を検知し、不要な推論を中断できます。
|
||||
|
||||
## 3.3 `/translate/stream`ハンドラ
|
||||
|
||||
JSONパースとバリデーションは2章の`/translate`と同じです。違うのはレスポンスの返し方だけ。`llm.chat()`のストリーミングコールバックと`set_chunked_content_provider`を組み合わせます。
|
||||
|
||||
```cpp
|
||||
svr.Post("/translate/stream",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
// ... JSONパース・バリデーションは/translateと同じ ...
|
||||
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, prompt](size_t, httplib::DataSink &sink) {
|
||||
try {
|
||||
llm.chat(prompt, [&](std::string_view token) {
|
||||
sink.os << "data: "
|
||||
<< json(std::string(token)).dump(
|
||||
-1, ' ', false, json::error_handler_t::replace)
|
||||
<< "\n\n";
|
||||
return sink.os.good(); // 切断されたらfalse→推論を中断
|
||||
});
|
||||
sink.os << "data: [DONE]\n\n";
|
||||
} catch (const std::exception &e) {
|
||||
sink.os << "data: " << json({{"error", e.what()}}).dump() << "\n\n";
|
||||
}
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
ポイントをいくつか。
|
||||
|
||||
- `llm.chat()`にコールバックを渡すと、トークンが生成されるたびに呼ばれます。コールバックが`false`を返すと生成を中断します
|
||||
- `sink.os`に書き込んだ後、`sink.os.good()`でクライアントがまだ接続しているかを確認できます。切断されていたら`false`を返して推論を止めます
|
||||
- 各トークンは`json(token).dump()`でJSON文字列としてエスケープしてから送ります。改行やクォートを含むトークンでも安全です
|
||||
- `dump(-1, ' ', false, ...)`の最初の3つの引数はデフォルトと同じです。重要なのは第4引数の`json::error_handler_t::replace`です。LLMはトークンをサブワード単位で返すため、マルチバイト文字(日本語など)の途中でトークンが切れることがあります。不完全なUTF-8バイト列をそのまま`dump()`に渡すと例外が飛ぶので、`replace`で安全に置換します。ブラウザ側で結合されるため、表示上の問題はありません
|
||||
- `try/catch`でラムダ全体を囲んでいます。`llm.chat()`はコンテキストウィンドウの超過などで例外を投げることがあります。ラムダ内で例外が未捕捉だとサーバーがクラッシュするので、エラーをSSEイベントとして返します
|
||||
- `data: [DONE]`はOpenAI APIと同じ慣習で、ストリームの終了をクライアントに伝えます
|
||||
|
||||
## 3.4 全体のコード
|
||||
|
||||
2章のコードに`/translate/stream`を追加した完成形です。
|
||||
|
||||
<details>
|
||||
<summary data-file="main.cpp">全体のコード(main.cpp)</summary>
|
||||
|
||||
```cpp
|
||||
#include <httplib.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <cpp-llamalib.h>
|
||||
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
httplib::Server svr;
|
||||
|
||||
// `Ctrl+C`でgraceful shutdown
|
||||
void signal_handler(int sig) {
|
||||
if (sig == SIGINT || sig == SIGTERM) {
|
||||
std::cout << "\nReceived signal, shutting down gracefully...\n";
|
||||
svr.stop();
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// GGUFモデルをロード
|
||||
auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf"};
|
||||
|
||||
// LLM推論は時間がかかるのでタイムアウトを長めに設定(デフォルトは5秒)
|
||||
svr.set_read_timeout(300);
|
||||
svr.set_write_timeout(300);
|
||||
|
||||
// リクエストとレスポンスをログに記録
|
||||
svr.set_logger([](const auto &req, const auto &res) {
|
||||
std::cout << req.method << " " << req.path << " -> " << res.status
|
||||
<< std::endl;
|
||||
});
|
||||
|
||||
svr.Get("/health", [](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"status", "ok"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// 2章で作った通常の翻訳エンドポイント
|
||||
svr.Post("/translate",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
// JSONパース・バリデーション(詳細は2章を参照)
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja");
|
||||
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
try {
|
||||
auto translation = llm.chat(prompt);
|
||||
res.set_content(json{{"translation", translation}}.dump(),
|
||||
"application/json");
|
||||
} catch (const std::exception &e) {
|
||||
res.status = 500;
|
||||
res.set_content(json{{"error", e.what()}}.dump(), "application/json");
|
||||
}
|
||||
});
|
||||
|
||||
// SSEストリーミング翻訳エンドポイント
|
||||
svr.Post("/translate/stream",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
// JSONパース・バリデーション(/translateと同じ)
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja");
|
||||
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, prompt](size_t, httplib::DataSink &sink) {
|
||||
try {
|
||||
llm.chat(prompt, [&](std::string_view token) {
|
||||
sink.os << "data: "
|
||||
<< json(std::string(token)).dump(
|
||||
-1, ' ', false, json::error_handler_t::replace)
|
||||
<< "\n\n";
|
||||
return sink.os.good(); // 切断されたら推論を中断
|
||||
});
|
||||
sink.os << "data: [DONE]\n\n";
|
||||
} catch (const std::exception &e) {
|
||||
sink.os << "data: " << json({{"error", e.what()}}).dump() << "\n\n";
|
||||
}
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// 以降の章で本物に差し替えるダミー実装
|
||||
svr.Get("/models",
|
||||
[](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"models", json::array()}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
svr.Post("/models/select",
|
||||
[](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"status", "TODO"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// `Ctrl+C` (`SIGINT`)や`kill` (`SIGTERM`)でサーバーを停止できるようにする
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
||||
// サーバー起動(`stop()`が呼ばれるまでブロック)
|
||||
std::cout << "Listening on http://127.0.0.1:8080" << std::endl;
|
||||
svr.listen("127.0.0.1", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
## 3.5 動作確認
|
||||
|
||||
ビルドしてサーバーを起動します。
|
||||
|
||||
```bash
|
||||
cmake --build build -j
|
||||
./build/translate-server
|
||||
```
|
||||
|
||||
curlの`-N`オプションでバッファリングを無効にすると、トークンが届くたびにリアルタイムで表示されます。
|
||||
|
||||
```bash
|
||||
curl -N -X POST http://localhost:8080/translate/stream \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"text": "I had a great time visiting Tokyo last spring. The cherry blossoms were beautiful.", "target_lang": "ja"}'
|
||||
```
|
||||
|
||||
```text
|
||||
data: "去年の"
|
||||
data: "春に"
|
||||
data: "東京を"
|
||||
data: "訪れた"
|
||||
data: "。"
|
||||
data: "桜が"
|
||||
data: "綺麗だった"
|
||||
data: "。"
|
||||
data: [DONE]
|
||||
```
|
||||
|
||||
トークンがひとつずつ流れてくるのが確認できるはずです。2章の`/translate`も引き続き使えます。
|
||||
|
||||
## 次の章へ
|
||||
|
||||
サーバーの翻訳機能はこれで一通り揃いました。次の章では、cpp-httplibのクライアント機能を使ってHugging Faceからモデルを取得・管理する機能を追加します。
|
||||
|
||||
**Next:** [モデルの取得・管理機能を追加する](../ch04-model-management)
|
||||
@@ -0,0 +1,788 @@
|
||||
---
|
||||
title: "4. モデルの取得・管理機能を追加する"
|
||||
order: 4
|
||||
|
||||
---
|
||||
|
||||
3章まででサーバーの翻訳機能は一通り揃いました。しかし、モデルファイルは1章で手動ダウンロードした1つだけです。この章ではcpp-httplibの**クライアント機能**を使い、アプリ内からHugging Faceのモデルをダウンロード・切り替えできるようにします。
|
||||
|
||||
完成すると、こんなリクエストでモデルを管理できるようになります。
|
||||
|
||||
```bash
|
||||
# 利用可能なモデル一覧を取得
|
||||
curl http://localhost:8080/models
|
||||
```
|
||||
|
||||
```json
|
||||
{
|
||||
"models": [
|
||||
{"name": "gemma-2-2b-it", "params": "2B", "size": "1.6 GB", "downloaded": true, "selected": true},
|
||||
{"name": "gemma-2-9b-it", "params": "9B", "size": "5.8 GB", "downloaded": false, "selected": false},
|
||||
{"name": "Llama-3.1-8B-Instruct", "params": "8B", "size": "4.9 GB", "downloaded": false, "selected": false}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
```bash
|
||||
# 別のモデルを選択(未ダウンロードなら自動で取得)
|
||||
curl -N -X POST http://localhost:8080/models/select \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"model": "gemma-2-9b-it"}'
|
||||
```
|
||||
|
||||
```text
|
||||
data: {"status":"downloading","progress":0}
|
||||
data: {"status":"downloading","progress":12}
|
||||
...
|
||||
data: {"status":"downloading","progress":100}
|
||||
data: {"status":"loading"}
|
||||
data: {"status":"ready"}
|
||||
```
|
||||
|
||||
## 4.1 httplib::Clientの基本
|
||||
|
||||
これまでは`httplib::Server`だけを使ってきましたが、cpp-httplibはクライアント機能も備えています。Hugging FaceはHTTPSなので、TLS対応のクライアントが必要です。
|
||||
|
||||
```cpp
|
||||
#include <httplib.h>
|
||||
|
||||
// URLスキームを含めると自動でSSLClientが使われる
|
||||
httplib::Client cli("https://huggingface.co");
|
||||
|
||||
// リダイレクト先を自動で追従(Hugging FaceはCDNにリダイレクトする)
|
||||
cli.set_follow_location(true);
|
||||
|
||||
auto res = cli.Get("/api/models");
|
||||
if (res && res->status == 200) {
|
||||
std::cout << res->body << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
HTTPSを使うには、ビルド時にOpenSSLを有効にする必要があります。`CMakeLists.txt`に以下を追加しましょう。
|
||||
|
||||
```cmake
|
||||
find_package(OpenSSL REQUIRED)
|
||||
|
||||
target_link_libraries(translate-server PRIVATE OpenSSL::SSL OpenSSL::Crypto)
|
||||
target_compile_definitions(translate-server PRIVATE CPPHTTPLIB_OPENSSL_SUPPORT)
|
||||
|
||||
# macOS: システム証明書の読み込みに必要
|
||||
if(APPLE)
|
||||
target_link_libraries(translate-server PRIVATE "-framework CoreFoundation" "-framework Security")
|
||||
endif()
|
||||
```
|
||||
|
||||
`CPPHTTPLIB_OPENSSL_SUPPORT`を定義すると、`httplib::Client("https://...")`がTLS接続を行います。macOSではシステム証明書ストアにアクセスするため、CoreFoundationとSecurityフレームワークのリンクも必要です。完全な`CMakeLists.txt`は4.8節にあります。
|
||||
|
||||
## 4.2 モデル一覧を定義する
|
||||
|
||||
アプリが扱えるモデルの一覧を定義します。翻訳タスクで検証済みの4モデルを用意しました。
|
||||
|
||||
```cpp
|
||||
struct ModelInfo {
|
||||
std::string name; // 表示名
|
||||
std::string params; // パラメータ数
|
||||
std::string size; // GGUF Q4サイズ
|
||||
std::string repo; // Hugging Faceリポジトリ
|
||||
std::string filename; // GGUFファイル名
|
||||
};
|
||||
|
||||
const std::vector<ModelInfo> MODELS = {
|
||||
{
|
||||
.name = "gemma-2-2b-it",
|
||||
.params = "2B",
|
||||
.size = "1.6 GB",
|
||||
.repo = "bartowski/gemma-2-2b-it-GGUF",
|
||||
.filename = "gemma-2-2b-it-Q4_K_M.gguf",
|
||||
},
|
||||
{
|
||||
.name = "gemma-2-9b-it",
|
||||
.params = "9B",
|
||||
.size = "5.8 GB",
|
||||
.repo = "bartowski/gemma-2-9b-it-GGUF",
|
||||
.filename = "gemma-2-9b-it-Q4_K_M.gguf",
|
||||
},
|
||||
{
|
||||
.name = "Llama-3.1-8B-Instruct",
|
||||
.params = "8B",
|
||||
.size = "4.9 GB",
|
||||
.repo = "bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
|
||||
.filename = "Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
## 4.3 モデルの保存場所
|
||||
|
||||
3章まではプロジェクトディレクトリ内の`models/`にモデルを置いていました。しかし複数モデルを管理するなら、アプリ専用のディレクトリに保存する方が適切です。macOS/Linuxでは`~/.translate-app/models/`、Windowsでは`%APPDATA%\translate-app\models\`を使います。
|
||||
|
||||
```cpp
|
||||
std::filesystem::path get_models_dir() {
|
||||
#ifdef _WIN32
|
||||
auto env = std::getenv("APPDATA");
|
||||
auto base = env ? std::filesystem::path(env) : std::filesystem::path(".");
|
||||
return base / "translate-app" / "models";
|
||||
#else
|
||||
auto env = std::getenv("HOME");
|
||||
auto base = env ? std::filesystem::path(env) : std::filesystem::path(".");
|
||||
return base / ".translate-app" / "models";
|
||||
#endif
|
||||
}
|
||||
```
|
||||
|
||||
環境変数が未設定の場合はカレントディレクトリにフォールバックします。このディレクトリはアプリ起動時に自動作成します(`create_directories`は既に存在していてもエラーになりません)。
|
||||
|
||||
## 4.4 モデルの初期化を書き換える
|
||||
|
||||
モデルの初期化を`main()`の先頭で書き換えます。1章ではパスをハードコードしていましたが、ここからはモデルの切り替えに対応します。現在ロード中のファイル名は`selected_model`変数で管理します。起動時は`MODELS`の先頭エントリーをロードします。この変数は`GET /models`や`POST /models/select`のハンドラから参照・更新します。
|
||||
|
||||
cpp-httplibはスレッドプールでハンドラを並行実行します。そのため、モデル切り替え中(`llm`の上書き中)に別スレッドで`llm.chat()`が走るとクラッシュします。`std::mutex`で排他制御を入れておきます。
|
||||
|
||||
```cpp
|
||||
int main() {
|
||||
auto models_dir = get_models_dir();
|
||||
std::filesystem::create_directories(models_dir);
|
||||
|
||||
std::string selected_model = MODELS[0].filename;
|
||||
auto path = models_dir / selected_model;
|
||||
|
||||
// デフォルトモデルが未ダウンロードなら起動時に自動取得
|
||||
if (!std::filesystem::exists(path)) {
|
||||
std::cout << "Downloading " << selected_model << "..." << std::endl;
|
||||
if (!download_model(MODELS[0], [](int pct) {
|
||||
std::cout << "\r" << pct << "%" << std::flush;
|
||||
return true;
|
||||
})) {
|
||||
std::cerr << "\nFailed to download model." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
auto llm = llamalib::Llama{path};
|
||||
std::mutex llm_mutex; // モデル切り替え中のアクセスを保護する
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
初回起動時にユーザーが`curl`で手動ダウンロードしなくても済むようにしています。4.6節の`download_model`関数を使い、進捗をコンソールに表示します。
|
||||
|
||||
## 4.5 `GET /models`ハンドラ
|
||||
|
||||
モデル一覧に「ダウンロード済みか」「選択中か」の情報を付けて返します。
|
||||
|
||||
```cpp
|
||||
svr.Get("/models",
|
||||
[&](const httplib::Request &, httplib::Response &res) {
|
||||
auto arr = json::array();
|
||||
for (const auto &m : MODELS) {
|
||||
auto path = get_models_dir() / m.filename;
|
||||
arr.push_back({
|
||||
{"name", m.name},
|
||||
{"params", m.params},
|
||||
{"size", m.size},
|
||||
{"downloaded", std::filesystem::exists(path)},
|
||||
{"selected", m.filename == selected_model},
|
||||
});
|
||||
}
|
||||
res.set_content(json{{"models", arr}}.dump(), "application/json");
|
||||
});
|
||||
```
|
||||
|
||||
## 4.6 大きなファイルをダウンロードする
|
||||
|
||||
GGUFモデルは数GBあるため、全体をメモリに載せるわけにはいきません。`httplib::Client::Get`にコールバックを渡すと、チャンクごとにデータを受け取れます。
|
||||
|
||||
```cpp
|
||||
// content_receiver: データチャンクを受け取るコールバック
|
||||
// progress: ダウンロード進捗コールバック
|
||||
cli.Get(url,
|
||||
[&](const char *data, size_t len) { // content_receiver
|
||||
ofs.write(data, len);
|
||||
return true; // falseを返すと中断
|
||||
},
|
||||
[&](size_t current, size_t total) { // progress
|
||||
int pct = total ? (int)(current * 100 / total) : 0;
|
||||
std::cout << pct << "%" << std::endl;
|
||||
return true; // falseを返すと中断
|
||||
});
|
||||
```
|
||||
|
||||
これを使ってHugging Faceからモデルをダウンロードする関数を作ります。
|
||||
|
||||
```cpp
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
// モデルをダウンロードし、進捗をprogress_cbで通知する
|
||||
// progress_cbがfalseを返すとダウンロードを中断する
|
||||
bool download_model(const ModelInfo &model,
|
||||
std::function<bool(int)> progress_cb) {
|
||||
httplib::Client cli("https://huggingface.co");
|
||||
cli.set_follow_location(true);
|
||||
cli.set_read_timeout(std::chrono::hours(1));
|
||||
|
||||
auto url = "/" + model.repo + "/resolve/main/" + model.filename;
|
||||
auto path = get_models_dir() / model.filename;
|
||||
auto tmp_path = std::filesystem::path(path).concat(".tmp");
|
||||
|
||||
std::ofstream ofs(tmp_path, std::ios::binary);
|
||||
if (!ofs) { return false; }
|
||||
|
||||
auto res = cli.Get(url,
|
||||
[&](const char *data, size_t len) {
|
||||
ofs.write(data, len);
|
||||
return ofs.good();
|
||||
},
|
||||
[&](size_t current, size_t total) {
|
||||
return progress_cb(total ? (int)(current * 100 / total) : 0);
|
||||
});
|
||||
|
||||
ofs.close();
|
||||
|
||||
if (!res || res->status != 200) {
|
||||
std::filesystem::remove(tmp_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
// .tmpに書いてからリネームすることで、DLが途中で止まっても
|
||||
// 不完全なファイルがモデルとして使われるのを防ぐ
|
||||
std::filesystem::rename(tmp_path, path);
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
## 4.7 `/models/select`ハンドラ
|
||||
|
||||
モデルの選択リクエストを処理します。レスポンスは常にSSEで返し、ダウンロード進捗 → ロード → 完了のステータスを順に通知します。
|
||||
|
||||
```cpp
|
||||
svr.Post("/models/select",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded() || !input.contains("model")) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'model' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto name = input["model"].get<std::string>();
|
||||
|
||||
// モデル一覧から探す
|
||||
auto it = std::find_if(MODELS.begin(), MODELS.end(),
|
||||
[&](const ModelInfo &m) { return m.name == name; });
|
||||
|
||||
if (it == MODELS.end()) {
|
||||
res.status = 404;
|
||||
res.set_content(json{{"error", "Unknown model"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
const auto &model = *it;
|
||||
|
||||
// 常にSSEで応答する(DL済みでも未DLでも同じ形式)
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, model](size_t, httplib::DataSink &sink) {
|
||||
// SSEイベント送信ヘルパー
|
||||
auto send = [&](const json &event) {
|
||||
sink.os << "data: " << event.dump() << "\n\n";
|
||||
};
|
||||
|
||||
// 未ダウンロードならダウンロード(進捗をSSEで通知)
|
||||
auto path = get_models_dir() / model.filename;
|
||||
if (!std::filesystem::exists(path)) {
|
||||
bool ok = download_model(model, [&](int pct) {
|
||||
send({{"status", "downloading"}, {"progress", pct}});
|
||||
return sink.os.good(); // クライアント切断時にダウンロードを中断
|
||||
});
|
||||
if (!ok) {
|
||||
send({{"status", "error"}, {"message", "Download failed"}});
|
||||
sink.done();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// モデルをロードして切り替え
|
||||
send({{"status", "loading"}});
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
llm = llamalib::Llama{path};
|
||||
selected_model = model.filename;
|
||||
}
|
||||
|
||||
send({{"status", "ready"}});
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
いくつか補足します。
|
||||
|
||||
- `download_model`の進捗コールバックから直接SSEイベントを送っています。3章の`set_chunked_content_provider` + `sink.os`の応用です
|
||||
- コールバックが`sink.os.good()`を返すので、クライアントが接続を切るとダウンロードも中断します。5章で追加するキャンセルボタンで使います
|
||||
- `selected_model`を更新すると、`GET /models`の`selected`フラグに反映されます
|
||||
- `llm`の上書きを`llm_mutex`で保護しています。`/translate`や`/translate/stream`のハンドラも同じ`mutex`でロックするので、モデル切り替え中に推論が走ることはありません(全体コードを参照)
|
||||
|
||||
## 4.8 全体のコード
|
||||
|
||||
3章のコードにモデル管理機能を追加した完成形です。
|
||||
|
||||
<details>
|
||||
<summary data-file="CMakeLists.txt">全体のコード(CMakeLists.txt)</summary>
|
||||
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(translate-server CXX)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
include(FetchContent)
|
||||
|
||||
# llama.cpp
|
||||
FetchContent_Declare(llama
|
||||
GIT_REPOSITORY https://github.com/ggml-org/llama.cpp
|
||||
GIT_TAG master
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(llama)
|
||||
|
||||
# cpp-httplib
|
||||
FetchContent_Declare(httplib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-httplib
|
||||
GIT_TAG master
|
||||
)
|
||||
FetchContent_MakeAvailable(httplib)
|
||||
|
||||
# nlohmann/json
|
||||
FetchContent_Declare(json
|
||||
URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
|
||||
)
|
||||
FetchContent_MakeAvailable(json)
|
||||
|
||||
# cpp-llamalib
|
||||
FetchContent_Declare(cpp_llamalib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-llamalib
|
||||
GIT_TAG main
|
||||
)
|
||||
FetchContent_MakeAvailable(cpp_llamalib)
|
||||
|
||||
find_package(OpenSSL REQUIRED)
|
||||
|
||||
add_executable(translate-server src/main.cpp)
|
||||
|
||||
target_link_libraries(translate-server PRIVATE
|
||||
httplib::httplib
|
||||
nlohmann_json::nlohmann_json
|
||||
cpp-llamalib
|
||||
OpenSSL::SSL OpenSSL::Crypto
|
||||
)
|
||||
|
||||
target_compile_definitions(translate-server PRIVATE CPPHTTPLIB_OPENSSL_SUPPORT)
|
||||
|
||||
if(APPLE)
|
||||
target_link_libraries(translate-server PRIVATE
|
||||
"-framework CoreFoundation"
|
||||
"-framework Security"
|
||||
)
|
||||
endif()
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary data-file="main.cpp">全体のコード(main.cpp)</summary>
|
||||
|
||||
```cpp
|
||||
#include <httplib.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <cpp-llamalib.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <csignal>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// モデル定義
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
struct ModelInfo {
|
||||
std::string name;
|
||||
std::string params;
|
||||
std::string size;
|
||||
std::string repo;
|
||||
std::string filename;
|
||||
};
|
||||
|
||||
const std::vector<ModelInfo> MODELS = {
|
||||
{
|
||||
.name = "gemma-2-2b-it",
|
||||
.params = "2B",
|
||||
.size = "1.6 GB",
|
||||
.repo = "bartowski/gemma-2-2b-it-GGUF",
|
||||
.filename = "gemma-2-2b-it-Q4_K_M.gguf",
|
||||
},
|
||||
{
|
||||
.name = "gemma-2-9b-it",
|
||||
.params = "9B",
|
||||
.size = "5.8 GB",
|
||||
.repo = "bartowski/gemma-2-9b-it-GGUF",
|
||||
.filename = "gemma-2-9b-it-Q4_K_M.gguf",
|
||||
},
|
||||
{
|
||||
.name = "Llama-3.1-8B-Instruct",
|
||||
.params = "8B",
|
||||
.size = "4.9 GB",
|
||||
.repo = "bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
|
||||
.filename = "Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
|
||||
},
|
||||
};
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// モデル保存ディレクトリ
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
std::filesystem::path get_models_dir() {
|
||||
#ifdef _WIN32
|
||||
auto env = std::getenv("APPDATA");
|
||||
auto base = env ? std::filesystem::path(env) : std::filesystem::path(".");
|
||||
return base / "translate-app" / "models";
|
||||
#else
|
||||
auto env = std::getenv("HOME");
|
||||
auto base = env ? std::filesystem::path(env) : std::filesystem::path(".");
|
||||
return base / ".translate-app" / "models";
|
||||
#endif
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// モデルダウンロード
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// progress_cbがfalseを返したらダウンロードを中断する
|
||||
bool download_model(const ModelInfo &model,
|
||||
std::function<bool(int)> progress_cb) {
|
||||
httplib::Client cli("https://huggingface.co");
|
||||
cli.set_follow_location(true); // Hugging FaceはCDNにリダイレクトする
|
||||
cli.set_read_timeout(std::chrono::hours(1)); // 大きなモデルに備えて長めに
|
||||
|
||||
auto url = "/" + model.repo + "/resolve/main/" + model.filename;
|
||||
auto path = get_models_dir() / model.filename;
|
||||
auto tmp_path = std::filesystem::path(path).concat(".tmp");
|
||||
|
||||
std::ofstream ofs(tmp_path, std::ios::binary);
|
||||
if (!ofs) { return false; }
|
||||
|
||||
auto res = cli.Get(url,
|
||||
// content_receiver: チャンクごとにデータを受け取ってファイルに書き込む
|
||||
[&](const char *data, size_t len) {
|
||||
ofs.write(data, len);
|
||||
return ofs.good();
|
||||
},
|
||||
// progress: ダウンロード進捗を通知(falseを返すと中断)
|
||||
[&, last_pct = -1](size_t current, size_t total) mutable {
|
||||
int pct = total ? (int)(current * 100 / total) : 0;
|
||||
if (pct == last_pct) return true; // 同じ値なら通知をスキップ
|
||||
last_pct = pct;
|
||||
return progress_cb(pct);
|
||||
});
|
||||
|
||||
ofs.close();
|
||||
|
||||
if (!res || res->status != 200) {
|
||||
std::filesystem::remove(tmp_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
// ダウンロード完了後にリネーム
|
||||
std::filesystem::rename(tmp_path, path);
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// サーバー
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
httplib::Server svr;
|
||||
|
||||
void signal_handler(int sig) {
|
||||
if (sig == SIGINT || sig == SIGTERM) {
|
||||
std::cout << "\nReceived signal, shutting down gracefully...\n";
|
||||
svr.stop();
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// モデル保存ディレクトリを作成
|
||||
auto models_dir = get_models_dir();
|
||||
std::filesystem::create_directories(models_dir);
|
||||
|
||||
// デフォルトモデルが未ダウンロードなら自動取得
|
||||
std::string selected_model = MODELS[0].filename;
|
||||
auto path = models_dir / selected_model;
|
||||
if (!std::filesystem::exists(path)) {
|
||||
std::cout << "Downloading " << selected_model << "..." << std::endl;
|
||||
if (!download_model(MODELS[0], [](int pct) {
|
||||
std::cout << "\r" << pct << "%" << std::flush;
|
||||
return true;
|
||||
})) {
|
||||
std::cerr << "\nFailed to download model." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
auto llm = llamalib::Llama{path};
|
||||
std::mutex llm_mutex; // モデル切り替え中のアクセスを保護する
|
||||
|
||||
// LLM推論は時間がかかるのでタイムアウトを長めに設定(デフォルトは5秒)
|
||||
svr.set_read_timeout(300);
|
||||
svr.set_write_timeout(300);
|
||||
|
||||
svr.set_logger([](const auto &req, const auto &res) {
|
||||
std::cout << req.method << " " << req.path << " -> " << res.status
|
||||
<< std::endl;
|
||||
});
|
||||
|
||||
svr.Get("/health", [](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"status", "ok"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// --- 翻訳エンドポイント(2章) -----------------------------------------
|
||||
|
||||
svr.Post("/translate",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
// JSONパース・バリデーション(詳細は2章を参照)
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja");
|
||||
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
try {
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
auto translation = llm.chat(prompt);
|
||||
res.set_content(json{{"translation", translation}}.dump(),
|
||||
"application/json");
|
||||
} catch (const std::exception &e) {
|
||||
res.status = 500;
|
||||
res.set_content(json{{"error", e.what()}}.dump(), "application/json");
|
||||
}
|
||||
});
|
||||
|
||||
// --- SSEストリーミング翻訳(3章)--------------------------------------
|
||||
|
||||
svr.Post("/translate/stream",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja");
|
||||
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, prompt](size_t, httplib::DataSink &sink) {
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
try {
|
||||
llm.chat(prompt, [&](std::string_view token) {
|
||||
sink.os << "data: "
|
||||
<< json(std::string(token)).dump(
|
||||
-1, ' ', false, json::error_handler_t::replace)
|
||||
<< "\n\n";
|
||||
return sink.os.good(); // 切断されたら推論を中断
|
||||
});
|
||||
sink.os << "data: [DONE]\n\n";
|
||||
} catch (const std::exception &e) {
|
||||
sink.os << "data: " << json({{"error", e.what()}}).dump() << "\n\n";
|
||||
}
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// --- モデル一覧(4章) -------------------------------------------------
|
||||
|
||||
svr.Get("/models",
|
||||
[&](const httplib::Request &, httplib::Response &res) {
|
||||
auto models_dir = get_models_dir();
|
||||
auto arr = json::array();
|
||||
for (const auto &m : MODELS) {
|
||||
auto path = models_dir / m.filename;
|
||||
arr.push_back({
|
||||
{"name", m.name},
|
||||
{"params", m.params},
|
||||
{"size", m.size},
|
||||
{"downloaded", std::filesystem::exists(path)},
|
||||
{"selected", m.filename == selected_model},
|
||||
});
|
||||
}
|
||||
res.set_content(json{{"models", arr}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// --- モデル選択(4章) -------------------------------------------------
|
||||
|
||||
svr.Post("/models/select",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded() || !input.contains("model")) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'model' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto name = input["model"].get<std::string>();
|
||||
|
||||
auto it = std::find_if(MODELS.begin(), MODELS.end(),
|
||||
[&](const ModelInfo &m) { return m.name == name; });
|
||||
|
||||
if (it == MODELS.end()) {
|
||||
res.status = 404;
|
||||
res.set_content(json{{"error", "Unknown model"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
const auto &model = *it;
|
||||
|
||||
// 常にSSEで応答する(DL済みでも未DLでも同じ形式)
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, model](size_t, httplib::DataSink &sink) {
|
||||
// SSEイベント送信ヘルパー
|
||||
auto send = [&](const json &event) {
|
||||
sink.os << "data: " << event.dump() << "\n\n";
|
||||
};
|
||||
|
||||
// 未ダウンロードならダウンロード(進捗をSSEで通知)
|
||||
auto path = get_models_dir() / model.filename;
|
||||
if (!std::filesystem::exists(path)) {
|
||||
bool ok = download_model(model, [&](int pct) {
|
||||
send({{"status", "downloading"}, {"progress", pct}});
|
||||
return sink.os.good(); // クライアント切断時にダウンロードを中断
|
||||
});
|
||||
if (!ok) {
|
||||
send({{"status", "error"}, {"message", "Download failed"}});
|
||||
sink.done();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// モデルをロードして切り替え
|
||||
send({{"status", "loading"}});
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
llm = llamalib::Llama{path};
|
||||
selected_model = model.filename;
|
||||
}
|
||||
|
||||
send({{"status", "ready"}});
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// `Ctrl+C` (`SIGINT`)や`kill` (`SIGTERM`)でサーバーを停止できるようにする
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
||||
std::cout << "Listening on http://127.0.0.1:8080" << std::endl;
|
||||
svr.listen("127.0.0.1", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
## 4.9 動作確認
|
||||
|
||||
CMakeLists.txtにOpenSSLの設定を追加したので、CMakeを再実行してからビルドします。
|
||||
|
||||
```bash
|
||||
cmake -B build
|
||||
cmake --build build -j
|
||||
./build/translate-server
|
||||
```
|
||||
|
||||
### モデル一覧の確認
|
||||
|
||||
```bash
|
||||
curl http://localhost:8080/models
|
||||
```
|
||||
|
||||
1章でダウンロードした`gemma-2-2b-it`が`downloaded: true`、`selected: true`になっているはずです。
|
||||
|
||||
### 別のモデルに切り替える
|
||||
|
||||
```bash
|
||||
curl -N -X POST http://localhost:8080/models/select \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"model": "gemma-2-9b-it"}'
|
||||
```
|
||||
|
||||
SSEでダウンロード進捗が流れ、完了すると`"ready"`が返ります。
|
||||
|
||||
### 複数モデルで翻訳を比較する
|
||||
|
||||
同じ例文を異なるモデルで翻訳してみましょう。
|
||||
|
||||
```bash
|
||||
# gemma-2-9b-itで翻訳(先ほど切り替えたモデル)
|
||||
curl -X POST http://localhost:8080/translate \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"text": "The quick brown fox jumps over the lazy dog.", "target_lang": "ja"}'
|
||||
|
||||
# gemma-2-2b-itに戻す
|
||||
curl -N -X POST http://localhost:8080/models/select \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"model": "gemma-2-2b-it"}'
|
||||
|
||||
# 同じ文を翻訳
|
||||
curl -X POST http://localhost:8080/translate \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"text": "The quick brown fox jumps over the lazy dog.", "target_lang": "ja"}'
|
||||
```
|
||||
|
||||
同じコード・同じプロンプトでもモデルによって翻訳結果が変わることがわかります。cpp-llamalibがモデルごとのチャットテンプレートを自動適用するので、コード側の変更は不要です。
|
||||
|
||||
## 次の章へ
|
||||
|
||||
これでサーバーの主要な機能が揃いました。REST API、SSEストリーミング、モデルのダウンロードと切り替え。次の章では静的ファイル配信を追加して、ブラウザから操作できるWeb UIを作ります。
|
||||
|
||||
**Next:** [Web UIを追加する](../ch05-web-ui)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,724 @@
|
||||
---
|
||||
title: "6. WebViewでデスクトップアプリ化する"
|
||||
order: 6
|
||||
|
||||
---
|
||||
|
||||
5章で、ブラウザから操作できる翻訳アプリが完成しました。でも使うたびに「サーバーを起動して、ブラウザでURLを開いて…」という手順が必要です。普通のアプリのように、ダブルクリックで起動してすぐ使えるようにしたいですよね。
|
||||
|
||||
この章では2つのことをやります。
|
||||
|
||||
1. **WebView化** — [webview/webview](https://github.com/webview/webview)でブラウザなしで動くデスクトップアプリにする
|
||||
2. **シングルバイナリ化** — [cpp-embedlib](https://github.com/yhirose/cpp-embedlib)でHTML/CSS/JSをバイナリに埋め込み、配布物を1ファイルにする
|
||||
|
||||
完成すると、`./translate-app`を実行するだけでウインドウが開き、翻訳が使えるようになります。
|
||||
|
||||

|
||||
|
||||
モデルは初回起動時に自動ダウンロードされるので、ユーザーに渡すのはバイナリ1つだけです。
|
||||
|
||||
## 6.1 webview/webview を導入する
|
||||
|
||||
[webview/webview](https://github.com/webview/webview)は、OS標準のWebViewコンポーネント(macOSならWKWebView、LinuxならWebKitGTK、WindowsならWebView2)をC/C++から使えるようにするライブラリです。Electronのように独自ブラウザを同梱するわけではないので、バイナリサイズへの影響はほぼありません。
|
||||
|
||||
CMakeで取得します。`CMakeLists.txt`に以下を追加してください。
|
||||
|
||||
```cmake
|
||||
# webview/webview
|
||||
FetchContent_Declare(webview
|
||||
GIT_REPOSITORY https://github.com/webview/webview
|
||||
GIT_TAG master
|
||||
)
|
||||
FetchContent_MakeAvailable(webview)
|
||||
```
|
||||
|
||||
これで`webview::core`というCMakeターゲットが使えるようになります。`target_link_libraries`でリンクすると、インクルードパスやプラットフォーム固有のフレームワークを自動で設定してくれます。
|
||||
|
||||
> **macOS**: 追加の依存は不要です。WKWebViewはシステムに組み込まれています。
|
||||
>
|
||||
> **Linux**: WebKitGTKが必要です。`sudo apt install libwebkit2gtk-4.1-dev`でインストールしてください。
|
||||
>
|
||||
> **Windows**: WebView2ランタイムが必要です。Windows 11には標準搭載されています。Windows 10の場合は[Microsoft公式サイト](https://developer.microsoft.com/en-us/microsoft-edge/webview2/)から入手してください。
|
||||
|
||||
## 6.2 サーバーをバックグラウンドスレッドで動かす
|
||||
|
||||
5章まではサーバーの`listen()`がメインスレッドをブロックしていました。WebViewを使うには、サーバーを別スレッドで動かし、メインスレッドでWebViewのイベントループを回す必要があります。
|
||||
|
||||
```cpp
|
||||
#include "webview/webview.h"
|
||||
#include <thread>
|
||||
|
||||
int main() {
|
||||
// ... (サーバーのセットアップは5章と同じ) ...
|
||||
|
||||
// サーバーをバックグラウンドスレッドで起動
|
||||
auto port = svr.bind_to_any_port("127.0.0.1");
|
||||
std::thread server_thread([&]() { svr.listen_after_bind(); });
|
||||
|
||||
std::cout << "Listening on http://127.0.0.1:" << port << std::endl;
|
||||
|
||||
// WebViewでUIを表示
|
||||
webview::webview w(false, nullptr);
|
||||
w.set_title("Translate App");
|
||||
w.set_size(1024, 768, WEBVIEW_HINT_NONE);
|
||||
w.navigate("http://127.0.0.1:" + std::to_string(port));
|
||||
w.run(); // ウインドウが閉じるまでブロック
|
||||
|
||||
// ウインドウが閉じたらサーバーも停止
|
||||
svr.stop();
|
||||
server_thread.join();
|
||||
}
|
||||
```
|
||||
|
||||
ポイントを見ていきましょう。
|
||||
|
||||
- **`bind_to_any_port`** — `listen("127.0.0.1", 8080)`の代わりに、OSに空いているポートを選んでもらいます。デスクトップアプリは複数起動されることがあるので、ポートを固定するとぶつかります
|
||||
- **`listen_after_bind`** — `bind_to_any_port`で確保したポートでリクエストの受付を開始します。`listen()`はbindとlistenを一度にやりますが、ポート番号を先に知る必要があるので分けています
|
||||
- **シャットダウン順序** — WebViewのウインドウが閉じたら`svr.stop()`でサーバーを止め、`server_thread.join()`でスレッドの終了を待ちます。逆順だとWebViewがサーバーにアクセスできなくなります
|
||||
|
||||
5章の`signal_handler`は不要になります。デスクトップアプリではウインドウを閉じることがアプリの終了を意味するからです。
|
||||
|
||||
## 6.3 cpp-embedlib で静的ファイルを埋め込む
|
||||
|
||||
5章では`public/`ディレクトリからファイルを配信していました。これだと配布時に`public/`も一緒に渡す必要があります。[cpp-embedlib](https://github.com/yhirose/cpp-embedlib)を使うと、HTML・CSS・JavaScriptをバイナリに埋め込んで、配布物をバイナリ1つにまとめられます。
|
||||
|
||||
### CMakeLists.txt
|
||||
|
||||
cpp-embedlibを取得し、`public/`を埋め込みます。
|
||||
|
||||
```cmake
|
||||
# cpp-embedlib
|
||||
FetchContent_Declare(cpp-embedlib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-embedlib
|
||||
GIT_TAG main
|
||||
)
|
||||
FetchContent_MakeAvailable(cpp-embedlib)
|
||||
|
||||
# public/ ディレクトリをバイナリに埋め込む
|
||||
cpp_embedlib_add(WebAssets
|
||||
FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/public
|
||||
NAMESPACE Web
|
||||
)
|
||||
|
||||
target_link_libraries(translate-app PRIVATE
|
||||
WebAssets # 埋め込みファイル
|
||||
cpp-embedlib-httplib # cpp-httplib連携
|
||||
)
|
||||
```
|
||||
|
||||
`cpp_embedlib_add`は、`public/`配下のファイルをコンパイル時にバイナリに変換し、`WebAssets`という静的ライブラリを作ります。リンクすると`Web::FS`というオブジェクトから埋め込みファイルにアクセスできます。`cpp-embedlib-httplib`は`httplib::mount()`関数を提供するヘルパーライブラリです。
|
||||
|
||||
### set_mount_point を httplib::mount に置き換える
|
||||
|
||||
5章の`set_mount_point`をcpp-embedlibの`httplib::mount`に置き換えるだけです。
|
||||
|
||||
```cpp
|
||||
#include <cpp-embedlib-httplib.h>
|
||||
#include "WebAssets.h"
|
||||
|
||||
// 5章:
|
||||
// svr.set_mount_point("/", "./public");
|
||||
|
||||
// 6章:
|
||||
httplib::mount(svr, Web::FS);
|
||||
```
|
||||
|
||||
`httplib::mount`は、`Web::FS`に埋め込まれたファイルをHTTPで配信するハンドラを登録します。MIMEタイプはファイルの拡張子から自動判定するので、`Content-Type`を手動で設定する必要はありません。
|
||||
|
||||
ファイルの中身はバイナリのデータセグメントに直接マップしているので、メモリコピーもヒープ割り当ても発生しません。
|
||||
|
||||
## 6.4 macOS: Editメニューの追加
|
||||
|
||||
入力欄に`Cmd+V`でテキストをペーストしようとすると、動かないことに気づくはずです。macOSでは、`Cmd+V`(ペースト)や`Cmd+C`(コピー)などのキーボードショートカットは、アプリケーションのメニューバーを経由してWebViewに届きます。webview/webviewはメニューバーを作らないので、これらのショートカットが効きません。Objective-CランタイムAPIを使ってEditメニューを追加する必要があります。
|
||||
|
||||
```cpp
|
||||
#ifdef __APPLE__
|
||||
#include <objc/objc-runtime.h>
|
||||
|
||||
void setup_macos_edit_menu() {
|
||||
auto cls = [](const char *n) { return (id)objc_getClass(n); };
|
||||
auto sel = sel_registerName;
|
||||
auto msg = reinterpret_cast<id (*)(id, SEL)>(objc_msgSend);
|
||||
auto msg_s = reinterpret_cast<id (*)(id, SEL, const char *)>(objc_msgSend);
|
||||
auto msg_id = reinterpret_cast<id (*)(id, SEL, id)>(objc_msgSend);
|
||||
auto msg_v = reinterpret_cast<void (*)(id, SEL, id)>(objc_msgSend);
|
||||
auto msg_mi = reinterpret_cast<id (*)(id, SEL, id, SEL, id)>(objc_msgSend);
|
||||
|
||||
auto str = [&](const char *s) {
|
||||
return msg_s(cls("NSString"), sel("stringWithUTF8String:"), s);
|
||||
};
|
||||
|
||||
id app = msg(cls("NSApplication"), sel("sharedApplication"));
|
||||
id mainMenu = msg(msg(cls("NSMenu"), sel("alloc")), sel("init"));
|
||||
id editItem = msg(msg(cls("NSMenuItem"), sel("alloc")), sel("init"));
|
||||
id editMenu = msg_id(msg(cls("NSMenu"), sel("alloc")),
|
||||
sel("initWithTitle:"), str("Edit"));
|
||||
|
||||
struct { const char *title; const char *action; const char *key; } items[] = {
|
||||
{"Undo", "undo:", "z"},
|
||||
{"Redo", "redo:", "Z"},
|
||||
{"Cut", "cut:", "x"},
|
||||
{"Copy", "copy:", "c"},
|
||||
{"Paste", "paste:", "v"},
|
||||
{"Select All", "selectAll:", "a"},
|
||||
};
|
||||
|
||||
for (auto &[title, action, key] : items) {
|
||||
id mi = msg_mi(msg(cls("NSMenuItem"), sel("alloc")),
|
||||
sel("initWithTitle:action:keyEquivalent:"),
|
||||
str(title), sel(action), str(key));
|
||||
msg_v(editMenu, sel("addItem:"), mi);
|
||||
}
|
||||
|
||||
msg_v(editItem, sel("setSubmenu:"), editMenu);
|
||||
msg_v(mainMenu, sel("addItem:"), editItem);
|
||||
msg_v(app, sel("setMainMenu:"), mainMenu);
|
||||
}
|
||||
#endif
|
||||
```
|
||||
|
||||
`w.run()`の前に呼び出します。
|
||||
|
||||
```cpp
|
||||
#ifdef __APPLE__
|
||||
setup_macos_edit_menu();
|
||||
#endif
|
||||
w.run();
|
||||
```
|
||||
|
||||
WindowsとLinuxでは、キーボードショートカットはメニューバーを介さずフォーカスのあるコントロールに直接届くので、この対処はmacOS固有です。
|
||||
|
||||
## 6.5 全体のコード
|
||||
|
||||
<details>
|
||||
<summary data-file="CMakeLists.txt">全体のコード(CMakeLists.txt)</summary>
|
||||
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(translate-app CXX)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
include(FetchContent)
|
||||
|
||||
# llama.cpp
|
||||
FetchContent_Declare(llama
|
||||
GIT_REPOSITORY https://github.com/ggml-org/llama.cpp
|
||||
GIT_TAG master
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(llama)
|
||||
|
||||
# cpp-httplib
|
||||
FetchContent_Declare(httplib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-httplib
|
||||
GIT_TAG master
|
||||
)
|
||||
FetchContent_MakeAvailable(httplib)
|
||||
|
||||
# nlohmann/json
|
||||
FetchContent_Declare(json
|
||||
URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
|
||||
)
|
||||
FetchContent_MakeAvailable(json)
|
||||
|
||||
# cpp-llamalib
|
||||
FetchContent_Declare(cpp_llamalib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-llamalib
|
||||
GIT_TAG main
|
||||
)
|
||||
FetchContent_MakeAvailable(cpp_llamalib)
|
||||
|
||||
# webview/webview
|
||||
FetchContent_Declare(webview
|
||||
GIT_REPOSITORY https://github.com/webview/webview
|
||||
GIT_TAG master
|
||||
)
|
||||
FetchContent_MakeAvailable(webview)
|
||||
|
||||
# cpp-embedlib
|
||||
FetchContent_Declare(cpp-embedlib
|
||||
GIT_REPOSITORY https://github.com/yhirose/cpp-embedlib
|
||||
GIT_TAG main
|
||||
)
|
||||
FetchContent_MakeAvailable(cpp-embedlib)
|
||||
|
||||
# public/ ディレクトリをバイナリに埋め込む
|
||||
cpp_embedlib_add(WebAssets
|
||||
FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/public
|
||||
NAMESPACE Web
|
||||
)
|
||||
|
||||
find_package(OpenSSL REQUIRED)
|
||||
|
||||
add_executable(translate-app src/main.cpp)
|
||||
|
||||
target_link_libraries(translate-app PRIVATE
|
||||
httplib::httplib
|
||||
nlohmann_json::nlohmann_json
|
||||
cpp-llamalib
|
||||
OpenSSL::SSL OpenSSL::Crypto
|
||||
WebAssets
|
||||
cpp-embedlib-httplib
|
||||
webview::core
|
||||
)
|
||||
|
||||
if(APPLE)
|
||||
target_link_libraries(translate-app PRIVATE
|
||||
"-framework CoreFoundation"
|
||||
"-framework Security"
|
||||
)
|
||||
endif()
|
||||
|
||||
target_compile_definitions(translate-app PRIVATE
|
||||
CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
)
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary data-file="main.cpp">全体のコード(main.cpp)</summary>
|
||||
|
||||
```cpp
|
||||
#include <httplib.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <cpp-llamalib.h>
|
||||
#include <cpp-embedlib-httplib.h>
|
||||
#include "WebAssets.h"
|
||||
#include "webview/webview.h"
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <objc/objc-runtime.h>
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// macOS Editメニュー(Cmd+C/V/X/AにはEditメニューが必要)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
#ifdef __APPLE__
|
||||
void setup_macos_edit_menu() {
|
||||
auto cls = [](const char *n) { return (id)objc_getClass(n); };
|
||||
auto sel = sel_registerName;
|
||||
auto msg = reinterpret_cast<id (*)(id, SEL)>(objc_msgSend);
|
||||
auto msg_s = reinterpret_cast<id (*)(id, SEL, const char *)>(objc_msgSend);
|
||||
auto msg_id = reinterpret_cast<id (*)(id, SEL, id)>(objc_msgSend);
|
||||
auto msg_v = reinterpret_cast<void (*)(id, SEL, id)>(objc_msgSend);
|
||||
auto msg_mi = reinterpret_cast<id (*)(id, SEL, id, SEL, id)>(objc_msgSend);
|
||||
|
||||
auto str = [&](const char *s) {
|
||||
return msg_s(cls("NSString"), sel("stringWithUTF8String:"), s);
|
||||
};
|
||||
|
||||
id app = msg(cls("NSApplication"), sel("sharedApplication"));
|
||||
id mainMenu = msg(msg(cls("NSMenu"), sel("alloc")), sel("init"));
|
||||
id editItem = msg(msg(cls("NSMenuItem"), sel("alloc")), sel("init"));
|
||||
id editMenu = msg_id(msg(cls("NSMenu"), sel("alloc")),
|
||||
sel("initWithTitle:"), str("Edit"));
|
||||
|
||||
struct { const char *title; const char *action; const char *key; } items[] = {
|
||||
{"Undo", "undo:", "z"},
|
||||
{"Redo", "redo:", "Z"},
|
||||
{"Cut", "cut:", "x"},
|
||||
{"Copy", "copy:", "c"},
|
||||
{"Paste", "paste:", "v"},
|
||||
{"Select All", "selectAll:", "a"},
|
||||
};
|
||||
|
||||
for (auto &[title, action, key] : items) {
|
||||
id mi = msg_mi(msg(cls("NSMenuItem"), sel("alloc")),
|
||||
sel("initWithTitle:action:keyEquivalent:"),
|
||||
str(title), sel(action), str(key));
|
||||
msg_v(editMenu, sel("addItem:"), mi);
|
||||
}
|
||||
|
||||
msg_v(editItem, sel("setSubmenu:"), editMenu);
|
||||
msg_v(mainMenu, sel("addItem:"), editItem);
|
||||
msg_v(app, sel("setMainMenu:"), mainMenu);
|
||||
}
|
||||
#endif
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// モデル定義
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
struct ModelInfo {
|
||||
std::string name;
|
||||
std::string params;
|
||||
std::string size;
|
||||
std::string repo;
|
||||
std::string filename;
|
||||
};
|
||||
|
||||
const std::vector<ModelInfo> MODELS = {
|
||||
{
|
||||
.name = "gemma-2-2b-it",
|
||||
.params = "2B",
|
||||
.size = "1.6 GB",
|
||||
.repo = "bartowski/gemma-2-2b-it-GGUF",
|
||||
.filename = "gemma-2-2b-it-Q4_K_M.gguf",
|
||||
},
|
||||
{
|
||||
.name = "gemma-2-9b-it",
|
||||
.params = "9B",
|
||||
.size = "5.8 GB",
|
||||
.repo = "bartowski/gemma-2-9b-it-GGUF",
|
||||
.filename = "gemma-2-9b-it-Q4_K_M.gguf",
|
||||
},
|
||||
{
|
||||
.name = "Llama-3.1-8B-Instruct",
|
||||
.params = "8B",
|
||||
.size = "4.9 GB",
|
||||
.repo = "bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
|
||||
.filename = "Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
|
||||
},
|
||||
};
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// モデル保存ディレクトリ
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
std::filesystem::path get_models_dir() {
|
||||
#ifdef _WIN32
|
||||
auto env = std::getenv("APPDATA");
|
||||
auto base = env ? std::filesystem::path(env) : std::filesystem::path(".");
|
||||
return base / "translate-app" / "models";
|
||||
#else
|
||||
auto env = std::getenv("HOME");
|
||||
auto base = env ? std::filesystem::path(env) : std::filesystem::path(".");
|
||||
return base / ".translate-app" / "models";
|
||||
#endif
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// モデルダウンロード
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// progress_cbがfalseを返したらダウンロードを中断する
|
||||
bool download_model(const ModelInfo &model,
|
||||
std::function<bool(int)> progress_cb) {
|
||||
httplib::Client cli("https://huggingface.co");
|
||||
cli.set_follow_location(true); // Hugging FaceはCDNにリダイレクトする
|
||||
cli.set_read_timeout(std::chrono::hours(1)); // 大きなモデルに備えて長めに
|
||||
|
||||
auto url = "/" + model.repo + "/resolve/main/" + model.filename;
|
||||
auto path = get_models_dir() / model.filename;
|
||||
auto tmp_path = std::filesystem::path(path).concat(".tmp");
|
||||
|
||||
std::ofstream ofs(tmp_path, std::ios::binary);
|
||||
if (!ofs) { return false; }
|
||||
|
||||
auto res = cli.Get(url,
|
||||
// content_receiver: チャンクごとにデータを受け取ってファイルに書き込む
|
||||
[&](const char *data, size_t len) {
|
||||
ofs.write(data, len);
|
||||
return ofs.good();
|
||||
},
|
||||
// progress: ダウンロード進捗を通知(falseを返すと中断)
|
||||
[&, last_pct = -1](size_t current, size_t total) mutable {
|
||||
int pct = total ? (int)(current * 100 / total) : 0;
|
||||
if (pct == last_pct) return true; // 同じ値なら通知をスキップ
|
||||
last_pct = pct;
|
||||
return progress_cb(pct);
|
||||
});
|
||||
|
||||
ofs.close();
|
||||
|
||||
if (!res || res->status != 200) {
|
||||
std::filesystem::remove(tmp_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
// ダウンロード完了後にリネーム
|
||||
std::filesystem::rename(tmp_path, path);
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// サーバー
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
int main() {
|
||||
httplib::Server svr;
|
||||
// モデル保存ディレクトリを作成
|
||||
auto models_dir = get_models_dir();
|
||||
std::filesystem::create_directories(models_dir);
|
||||
|
||||
// デフォルトモデルが未ダウンロードなら自動取得
|
||||
std::string selected_model = MODELS[0].filename;
|
||||
auto path = models_dir / selected_model;
|
||||
if (!std::filesystem::exists(path)) {
|
||||
std::cout << "Downloading " << selected_model << "..." << std::endl;
|
||||
if (!download_model(MODELS[0], [](int pct) {
|
||||
std::cout << "\r" << pct << "%" << std::flush;
|
||||
return true;
|
||||
})) {
|
||||
std::cerr << "\nFailed to download model." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
auto llm = llamalib::Llama{path};
|
||||
std::mutex llm_mutex; // モデル切り替え中のアクセスを保護する
|
||||
|
||||
// LLM推論は時間がかかるのでタイムアウトを長めに設定(デフォルトは5秒)
|
||||
svr.set_read_timeout(300);
|
||||
svr.set_write_timeout(300);
|
||||
|
||||
svr.set_logger([](const auto &req, const auto &res) {
|
||||
std::cout << req.method << " " << req.path << " -> " << res.status
|
||||
<< std::endl;
|
||||
});
|
||||
|
||||
svr.Get("/health", [](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_content(json{{"status", "ok"}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// --- 翻訳エンドポイント(2章) -----------------------------------------
|
||||
|
||||
svr.Post("/translate",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja");
|
||||
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
try {
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
auto translation = llm.chat(prompt);
|
||||
res.set_content(json{{"translation", translation}}.dump(),
|
||||
"application/json");
|
||||
} catch (const std::exception &e) {
|
||||
res.status = 500;
|
||||
res.set_content(json{{"error", e.what()}}.dump(), "application/json");
|
||||
}
|
||||
});
|
||||
|
||||
// --- SSEストリーミング翻訳(3章)--------------------------------------
|
||||
|
||||
svr.Post("/translate/stream",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "Invalid JSON"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!input.contains("text") || !input["text"].is_string() ||
|
||||
input["text"].get<std::string>().empty()) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'text' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto text = input["text"].get<std::string>();
|
||||
auto target_lang = input.value("target_lang", "ja");
|
||||
|
||||
auto prompt = "Translate the following text to " + target_lang +
|
||||
". Output only the translation, nothing else.\n\n" + text;
|
||||
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, prompt](size_t, httplib::DataSink &sink) {
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
try {
|
||||
llm.chat(prompt, [&](std::string_view token) {
|
||||
sink.os << "data: "
|
||||
<< json(std::string(token)).dump(
|
||||
-1, ' ', false, json::error_handler_t::replace)
|
||||
<< "\n\n";
|
||||
return sink.os.good(); // 切断されたら推論を中断
|
||||
});
|
||||
sink.os << "data: [DONE]\n\n";
|
||||
} catch (const std::exception &e) {
|
||||
sink.os << "data: " << json({{"error", e.what()}}).dump() << "\n\n";
|
||||
}
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// --- モデル一覧(4章) -------------------------------------------------
|
||||
|
||||
svr.Get("/models",
|
||||
[&](const httplib::Request &, httplib::Response &res) {
|
||||
auto models_dir = get_models_dir();
|
||||
auto arr = json::array();
|
||||
for (const auto &m : MODELS) {
|
||||
auto path = models_dir / m.filename;
|
||||
arr.push_back({
|
||||
{"name", m.name},
|
||||
{"params", m.params},
|
||||
{"size", m.size},
|
||||
{"downloaded", std::filesystem::exists(path)},
|
||||
{"selected", m.filename == selected_model},
|
||||
});
|
||||
}
|
||||
res.set_content(json{{"models", arr}}.dump(), "application/json");
|
||||
});
|
||||
|
||||
// --- モデル選択(4章) -------------------------------------------------
|
||||
|
||||
svr.Post("/models/select",
|
||||
[&](const httplib::Request &req, httplib::Response &res) {
|
||||
auto input = json::parse(req.body, nullptr, false);
|
||||
if (input.is_discarded() || !input.contains("model")) {
|
||||
res.status = 400;
|
||||
res.set_content(json{{"error", "'model' is required"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
auto name = input["model"].get<std::string>();
|
||||
|
||||
auto it = std::find_if(MODELS.begin(), MODELS.end(),
|
||||
[&](const ModelInfo &m) { return m.name == name; });
|
||||
|
||||
if (it == MODELS.end()) {
|
||||
res.status = 404;
|
||||
res.set_content(json{{"error", "Unknown model"}}.dump(),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
|
||||
const auto &model = *it;
|
||||
|
||||
// 常にSSEで応答する(DL済みでも未DLでも同じ形式)
|
||||
res.set_chunked_content_provider(
|
||||
"text/event-stream",
|
||||
[&, model](size_t, httplib::DataSink &sink) {
|
||||
// SSEイベント送信ヘルパー
|
||||
auto send = [&](const json &event) {
|
||||
sink.os << "data: " << event.dump() << "\n\n";
|
||||
};
|
||||
|
||||
// 未ダウンロードならダウンロード(進捗をSSEで通知)
|
||||
auto path = get_models_dir() / model.filename;
|
||||
if (!std::filesystem::exists(path)) {
|
||||
bool ok = download_model(model, [&](int pct) {
|
||||
send({{"status", "downloading"}, {"progress", pct}});
|
||||
return sink.os.good(); // クライアント切断時にダウンロードを中断
|
||||
});
|
||||
if (!ok) {
|
||||
send({{"status", "error"}, {"message", "Download failed"}});
|
||||
sink.done();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// モデルをロードして切り替え
|
||||
send({{"status", "loading"}});
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(llm_mutex);
|
||||
llm = llamalib::Llama{path};
|
||||
selected_model = model.filename;
|
||||
}
|
||||
|
||||
send({{"status", "ready"}});
|
||||
sink.done();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// --- 埋め込みファイル配信(6章) ---------------------------------------
|
||||
// 5章: svr.set_mount_point("/", "./public");
|
||||
httplib::mount(svr, Web::FS);
|
||||
|
||||
// サーバーをバックグラウンドスレッドで起動
|
||||
auto port = svr.bind_to_any_port("127.0.0.1");
|
||||
std::thread server_thread([&]() { svr.listen_after_bind(); });
|
||||
|
||||
std::cout << "Listening on http://127.0.0.1:" << port << std::endl;
|
||||
|
||||
// WebViewでUIを表示
|
||||
webview::webview w(false, nullptr);
|
||||
w.set_title("Translate App");
|
||||
w.set_size(1024, 768, WEBVIEW_HINT_NONE);
|
||||
w.navigate("http://127.0.0.1:" + std::to_string(port));
|
||||
|
||||
#ifdef __APPLE__
|
||||
setup_macos_edit_menu();
|
||||
#endif
|
||||
w.run(); // ウインドウが閉じるまでブロック
|
||||
|
||||
// ウインドウが閉じたらサーバーも停止
|
||||
svr.stop();
|
||||
server_thread.join();
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
5章からの変更点をまとめると:
|
||||
|
||||
- `#include <csignal>` → `#include <thread>`, `<cpp-embedlib-httplib.h>`, `"WebAssets.h"`, `"webview/webview.h"`
|
||||
- `signal_handler`関数を削除
|
||||
- `svr.set_mount_point("/", "./public")` → `httplib::mount(svr, Web::FS)`
|
||||
- `svr.listen("127.0.0.1", 8080)` → `bind_to_any_port` + `listen_after_bind` + WebViewのイベントループ
|
||||
|
||||
ハンドラのコードは1行も変わっていません。5章まで作ってきたREST API・SSEストリーミング・モデル管理がそのまま動きます。
|
||||
|
||||
## 6.6 ビルドと動作確認
|
||||
|
||||
```bash
|
||||
cmake -B build
|
||||
cmake --build build -j
|
||||
```
|
||||
|
||||
起動します。
|
||||
|
||||
```bash
|
||||
./build/translate-app
|
||||
```
|
||||
|
||||
ブラウザは不要です。ウインドウが自動で開きます。5章と同じUIがそのまま表示され、翻訳やモデル切り替えがすべてそのまま動きます。
|
||||
|
||||
ウインドウを閉じるとサーバーも自動で終了します。`Ctrl+C`は不要です。
|
||||
|
||||
### 何が配布に必要か
|
||||
|
||||
配布に必要なのは:
|
||||
|
||||
- `translate-app`バイナリ1つ
|
||||
|
||||
これだけです。`public/`ディレクトリは不要です。HTML・CSS・JavaScriptはバイナリに埋め込まれています。モデルファイルは初回起動時に自動ダウンロードするので、ユーザーに事前準備を求める必要もありません。
|
||||
|
||||
## 次の章へ
|
||||
|
||||
お疲れさまでした!🎉
|
||||
|
||||
1章では`/health`が`{"status":"ok"}`を返すだけでした。それが今、テキストを入力すればリアルタイムで翻訳が流れ、ドロップダウンからモデルを切り替えれば自動でダウンロードが始まり、ウインドウを閉じればサーバーも一緒に終了する―そんなデスクトップアプリになりました。しかもバイナリ1つで配れます。
|
||||
|
||||
6章で変えたのは、静的ファイルの配信方法とサーバーの起動方法だけです。ハンドラのコードは1行も変わっていません。5章までに積み上げてきたREST API・SSEストリーミング・モデル管理が、そのままデスクトップアプリとして動いています。
|
||||
|
||||
次の章では視点を変えて、llama.cpp本家の`llama-server`のコードを読みます。本書のシンプルなサーバーと、プロダクション品質のサーバーを比較して、設計判断の違いとその理由を学びましょう。
|
||||
|
||||
**Next:** [llama.cpp本家のサーバー実装をコードリーディング](../ch07-code-reading)
|
||||
@@ -0,0 +1,154 @@
|
||||
---
|
||||
title: "7. llama.cpp本家のサーバー実装をコードリーディング"
|
||||
order: 7
|
||||
|
||||
---
|
||||
|
||||
6章かけてゼロから翻訳デスクトップアプリを作りました。動くものは完成しましたが、あくまで「学習用」の実装です。では「プロダクション品質」のコードはどう違うのか? llama.cppに同梱されている公式サーバー`llama-server`のソースコードを読んで、比較してみましょう。
|
||||
|
||||
`llama-server`は`llama.cpp/tools/server/`にあります。同じcpp-httplibを使っているので、コードの読み方はこれまでの章と同じです。
|
||||
|
||||
## 7.1 ソースコードの場所
|
||||
|
||||
```ascii
|
||||
llama.cpp/tools/server/
|
||||
├── server.cpp # メインのサーバー実装
|
||||
├── httplib.h # cpp-httplib(同梱版)
|
||||
└── ...
|
||||
```
|
||||
|
||||
ファイルは1つの`server.cpp`にまとまっています。数千行ありますが、構造を知っていれば読むべき箇所は絞れます。
|
||||
|
||||
## 7.2 OpenAI互換API
|
||||
|
||||
ここまで作ってきたサーバーと`llama-server`の最も大きな違いはAPIの設計です。
|
||||
|
||||
**私たちのAPI:**
|
||||
|
||||
```text
|
||||
POST /translate → {"translation": "..."}
|
||||
POST /translate/stream → SSE: data: "token"
|
||||
```
|
||||
|
||||
**llama-serverのAPI:**
|
||||
|
||||
```text
|
||||
POST /v1/chat/completions → OpenAI互換のJSON
|
||||
POST /v1/completions → OpenAI互換のJSON
|
||||
POST /v1/embeddings → テキスト埋め込みベクトル
|
||||
```
|
||||
|
||||
`llama-server`は[OpenAIのAPI仕様](https://platform.openai.com/docs/api-reference)に合わせています。つまり、OpenAIの公式クライアントライブラリ(Pythonの`openai`パッケージなど)がそのまま動きます。
|
||||
|
||||
```python
|
||||
# OpenAIクライアントでllama-serverに接続する例
|
||||
from openai import OpenAI
|
||||
client = OpenAI(base_url="http://localhost:8080/v1", api_key="dummy")
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model="local-model",
|
||||
messages=[{"role": "user", "content": "Hello!"}]
|
||||
)
|
||||
```
|
||||
|
||||
既存のツールやライブラリとの互換性を持たせるかどうかは、大きな設計判断です。私たちは翻訳専用のAPIをシンプルに設計しましたが、汎用のサーバーを作るならOpenAI互換が事実上の標準になっています。
|
||||
|
||||
## 7.3 同時リクエスト処理
|
||||
|
||||
私たちのサーバーはリクエストを1つずつ処理します。翻訳中に別のリクエストが来ると、前の推論が終わるまで待ちます。1人で使うデスクトップアプリなら問題ありませんが、複数人で共有するサーバーでは困ります。
|
||||
|
||||
`llama-server`は**スロット**という仕組みで同時リクエストを処理します。
|
||||
|
||||

|
||||
|
||||
ポイントは、各スロットのトークンを**1つずつ順番に**ではなく、**まとめて1回のバッチ**で推論することです。GPUは並列処理が得意なので、2人分を同時に処理しても1人分とほとんど変わらない時間で済みます。これを「連続バッチ処理(continuous batching)」と呼びます。
|
||||
|
||||
私たちのサーバーではcpp-httplibのスレッドプールが各リクエストに1スレッドを割り当てますが、推論自体は`llm.chat()`の中でシングルスレッドです。`llama-server`はこの推論部分を共有のバッチ処理ループに集約しています。
|
||||
|
||||
## 7.4 SSEフォーマットの違い
|
||||
|
||||
ストリーミングの仕組み自体は同じ(`set_chunked_content_provider` + SSE)ですが、送るデータのフォーマットが違います。
|
||||
|
||||
**私たちの形式:**
|
||||
|
||||
```text
|
||||
data: "去年の"
|
||||
data: "春に"
|
||||
data: [DONE]
|
||||
```
|
||||
|
||||
**llama-server(OpenAI互換):**
|
||||
|
||||
```text
|
||||
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"去年の"}}]}
|
||||
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"春に"}}]}
|
||||
data: [DONE]
|
||||
```
|
||||
|
||||
私たちの形式はトークンだけを送るシンプルなものです。`llama-server`はOpenAI互換のため、1つのトークンにもJSONのラッパーが付きます。冗長に見えますが、`id`でリクエストを識別したり、`finish_reason`で停止理由を返せたりと、クライアントにとって便利な情報が含まれています。
|
||||
|
||||
## 7.5 KVキャッシュの再利用
|
||||
|
||||
私たちのサーバーでは、リクエストのたびにプロンプト全体をゼロから処理しています。翻訳アプリのプロンプトは短い("Translate the following text to ja..." + 入力テキスト)ので、これで問題ありません。
|
||||
|
||||
`llama-server`は、前のリクエストと共通するプロンプトのprefixがある場合、その部分のKVキャッシュを再利用します。
|
||||
|
||||

|
||||
|
||||
長いシステムプロンプトやfew-shot例を毎回送るチャットボットでは、これだけで応答時間が大幅に短縮されます。数千トークンのシステムプロンプトを毎回処理するのと、キャッシュから一瞬で読むのとでは、体感が全く違います。
|
||||
|
||||
翻訳アプリではシステムプロンプトが1文だけなので効果は限定的ですが、自分のアプリに応用するときは意識したい最適化です。
|
||||
|
||||
## 7.6 構造化出力
|
||||
|
||||
翻訳APIはプレーンテキストを返すので、出力形式を制約する必要がありませんでした。でも、LLMにJSONで返させたい場合はどうでしょう?
|
||||
|
||||
```text
|
||||
プロンプト: 以下の文の感情を分析してJSONで返してください。
|
||||
LLMの出力(期待): {"sentiment": "positive", "score": 0.8}
|
||||
LLMの出力(現実): 感情分析の結果は以下の通りです。{"sentiment": ...
|
||||
```
|
||||
|
||||
LLMは指示を無視して余計なテキストを付けることがあります。`llama-server`はこの問題を**文法制約(grammar)**で解決しています。
|
||||
|
||||
```bash
|
||||
curl http://localhost:8080/v1/chat/completions \
|
||||
-d '{
|
||||
"messages": [{"role": "user", "content": "Analyze sentiment..."}],
|
||||
"json_schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"sentiment": {"type": "string", "enum": ["positive", "negative", "neutral"]},
|
||||
"score": {"type": "number"}
|
||||
},
|
||||
"required": ["sentiment", "score"]
|
||||
}
|
||||
}'
|
||||
```
|
||||
|
||||
`json_schema`を指定すると、LLMのトークン生成時に文法に合わないトークンを除外します。出力が必ず有効なJSONになるので、`json::parse`が失敗する心配がありません。
|
||||
|
||||
LLMをアプリに組み込むとき、出力を確実にパースできるかどうかは信頼性に直結します。翻訳のようなフリーテキスト出力では不要ですが、APIのレスポンスとして構造化データを返す用途では必須の機能です。
|
||||
|
||||
## 7.7 まとめ
|
||||
|
||||
ここまでの違いを整理します。
|
||||
|
||||
| 観点 | 私たちのサーバー | llama-server |
|
||||
|------|-------------|--------------|
|
||||
| API設計 | 翻訳専用 | OpenAI互換 |
|
||||
| 同時リクエスト | 1つずつ処理 | スロット+連続バッチ |
|
||||
| SSEフォーマット | トークンのみ | OpenAI互換JSON |
|
||||
| KVキャッシュ | 毎回クリア | prefixを再利用 |
|
||||
| 構造化出力 | なし | JSON Schema/文法制約 |
|
||||
| コード量 | 約200行 | 数千行 |
|
||||
|
||||
私たちのコードがシンプルなのは、「デスクトップアプリで1人が使う」という前提があるからです。複数人に提供するサーバーや、既存のエコシステムと連携するサーバーを作るなら、`llama-server`の設計が参考になります。
|
||||
|
||||
逆に言えば、200行のコードでも翻訳アプリとしては十分に動きます。「必要な分だけ作る」ことの価値も、このコードリーディングから感じてもらえたら嬉しいです。
|
||||
|
||||
## 次の章へ
|
||||
|
||||
次の章では、ここまで作ったアプリを自分のライブラリに差し替えてカスタマイズするためのポイントをまとめます。
|
||||
|
||||
**Next:** [自分だけのアプリにカスタマイズする](../ch08-customization)
|
||||
@@ -0,0 +1,120 @@
|
||||
---
|
||||
title: "8. 自分だけのアプリにカスタマイズする"
|
||||
order: 8
|
||||
|
||||
---
|
||||
|
||||
7章までで翻訳デスクトップアプリが完成し、プロダクション品質のコードとの違いも学びました。この章では、ここまで作ったアプリを**自分だけのアプリに作り変える**ためのポイントをまとめます。
|
||||
|
||||
翻訳アプリはあくまで題材です。llama.cppを自分のライブラリに差し替えれば、同じ構成でどんなアプリでも作れます。
|
||||
|
||||
## 8.1 ビルド設定を差し替える
|
||||
|
||||
まず`CMakeLists.txt`で、llama.cpp関連の`FetchContent`を自分のライブラリに置き換えます。
|
||||
|
||||
```cmake
|
||||
# 削除: llama.cpp と cpp-llamalib の FetchContent
|
||||
|
||||
# 追加: 自分のライブラリ
|
||||
FetchContent_Declare(my_lib
|
||||
GIT_REPOSITORY https://github.com/yourname/my-lib
|
||||
GIT_TAG main
|
||||
)
|
||||
FetchContent_MakeAvailable(my_lib)
|
||||
|
||||
target_link_libraries(my-app PRIVATE
|
||||
httplib::httplib
|
||||
nlohmann_json::nlohmann_json
|
||||
my_lib # cpp-llamalib の代わりに自分のライブラリ
|
||||
# ...
|
||||
)
|
||||
```
|
||||
|
||||
ライブラリがCMakeに対応していない場合は、ヘッダーファイルとソースファイルを直接`src/`に置いて`add_executable`に追加すればOKです。cpp-httplibやnlohmann/json、webviewはそのまま残します。
|
||||
|
||||
## 8.2 APIを自分のタスクに合わせる
|
||||
|
||||
翻訳APIのエンドポイントとパラメータを、自分のタスクに合わせて変更します。
|
||||
|
||||
| 翻訳アプリ | 自分のアプリ(例: 画像処理) |
|
||||
|---|---|
|
||||
| `POST /translate` | `POST /process` |
|
||||
| `{"text": "...", "target_lang": "ja"}` | `{"image": "base64...", "filter": "blur"}` |
|
||||
| `POST /translate/stream` | `POST /process/stream` |
|
||||
| `GET /models` | `GET /filters`や`GET /presets` |
|
||||
|
||||
個々のハンドラの中身も書き換えます。例えば`llm.chat()`を呼んでいた箇所を、自分のライブラリのAPIに差し替えるだけです。
|
||||
|
||||
```cpp
|
||||
// Before: LLM翻訳
|
||||
auto translation = llm.chat(prompt);
|
||||
res.set_content(json{{"translation", translation}}.dump(), "application/json");
|
||||
|
||||
// After: 例えば画像処理ライブラリの場合
|
||||
auto result = my_lib::process(input_image, options);
|
||||
res.set_content(json{{"result", result}}.dump(), "application/json");
|
||||
```
|
||||
|
||||
SSEストリーミングも同じです。コールバックで進捗を返す関数があれば、3章と同じパターンで逐次レスポンスを返せます。LLMに限らず、処理に時間がかかるタスクならどれでも使えます。画像処理の進捗、データ変換のステップ、長時間の計算結果など、用途は様々です。
|
||||
|
||||
## 8.3 設計上の注意点
|
||||
|
||||
### 初期化コストが高いライブラリ
|
||||
|
||||
本書ではLLMモデルを`main()`の先頭でロードし、変数に保持しています。これは意図的な設計です。リクエストのたびにモデルをロードすると数秒かかるので、起動時に1回だけロードして使い回しています。大きなデータファイルの読み込みやGPUリソースの確保など、初期化が重いライブラリでも同じアプローチが使えます。
|
||||
|
||||
### スレッド安全性
|
||||
|
||||
cpp-httplibはスレッドプールでリクエストを並行処理します。4章ではモデル切り替え時に`llm`オブジェクトが上書きされる問題を`std::mutex`で保護しました。自分のライブラリを組み込む場合も同じパターンが使えます。ライブラリがスレッドセーフでない場合や、オブジェクトの差し替えが発生する場合は`std::mutex`で保護してください。
|
||||
|
||||
## 8.4 UIをカスタマイズする
|
||||
|
||||
`public/`の3ファイルを編集します。
|
||||
|
||||
- **`index.html`** — 入力フォームの構成を変えます。`<textarea>`を`<input type="file">`にしたり、パラメータの入力欄を追加したり
|
||||
- **`style.css`** — レイアウトやカラーを変更します。2カラムのままでも、1カラムに変えても
|
||||
- **`script.js`** — `fetch()`の送信先URLとリクエストボディ、レスポンスの表示方法を書き換えます
|
||||
|
||||
サーバー側のコードは変えなくても、HTMLを差し替えるだけで全く別のアプリに見えます。静的ファイルなのでサーバーの再起動なしにブラウザをリロードするだけで確認でき、試行錯誤しやすいです。
|
||||
|
||||
本書では素のHTML・CSS・JavaScriptで書きましたが、VueやReactなどのフロントエンドフレームワークやCSSフレームワークを組み合わせれば、さらに使い勝手の良いアプリに仕上げることができます。
|
||||
|
||||
## 8.5 配布するときの注意点
|
||||
|
||||
### ライセンス
|
||||
|
||||
使っているライブラリのライセンスを確認してください。cpp-httplib(MIT)、nlohmann/json(MIT)、webview(MIT)はいずれも商用利用可能です。自分のライブラリや、それが依存するライブラリのライセンスも忘れずに確認しましょう。
|
||||
|
||||
### モデルやデータファイル
|
||||
|
||||
4章で作ったダウンロード機能は、LLMモデルに限らず使えます。大きなデータファイルが必要なアプリなら、同じパターンで初回起動時に自動ダウンロードさせると、バイナリサイズを抑えつつユーザーの手間を省けます。
|
||||
|
||||
データが小さければ、cpp-embedlibでバイナリに埋め込んでしまうのも手です。
|
||||
|
||||
### クロスプラットフォームビルド
|
||||
|
||||
webviewはmacOS・Linux・Windowsに対応しています。各プラットフォーム向けにビルドする場合:
|
||||
|
||||
- **macOS** — 追加の依存なし
|
||||
- **Linux** — `libwebkit2gtk-4.1-dev`が必要
|
||||
- **Windows** — WebView2ランタイムが必要(Windows 11は標準搭載)
|
||||
|
||||
CI(GitHub Actionsなど)でクロスプラットフォームビルドを自動化するのもおすすめです。
|
||||
|
||||
## おわりに
|
||||
|
||||
最後まで読んでくださり、ありがとうございます。🙏
|
||||
|
||||
この本は、1章の`/health`が`{"status":"ok"}`を返すところから始まりました。そこからREST API、SSEストリーミング、Hugging Faceからのモデルダウンロード、ブラウザで動くWeb UI、そしてシングルバイナリのデスクトップアプリへ。7章では`llama-server`のコードを読んで、プロダクション品質のサーバーとの設計の違いを学びました。長い道のりでしたが、ここまで付き合ってくださったことに心から感謝します。
|
||||
|
||||
振り返ると、cpp-httplibのいくつかの主要な機能を実際に使いました。
|
||||
|
||||
- **サーバー**: ルーティング、JSONレスポンス、`set_chunked_content_provider`によるSSEストリーミング、`set_mount_point`による静的ファイル配信
|
||||
- **クライアント**: HTTPS接続、リダイレクト追従、コンテンツレシーバーによる大容量ダウンロード、進捗コールバック
|
||||
- **WebView連携**: `bind_to_any_port` + `listen_after_bind`でバックグラウンドスレッド化
|
||||
|
||||
cpp-httplibにはこの他にも、マルチパートによるファイルアップロード、認証、タイムアウト制御、圧縮、レンジリクエストなど便利な機能があります。詳しくは [A Tour of cpp-httplib](../../tour/) をご覧ください。
|
||||
|
||||
これらのパターンは翻訳アプリに限りません。自分のC++ライブラリにWeb APIを付けたい、ブラウザUIで操作できるようにしたい、配布しやすいデスクトップアプリにしたい―そんなときに、この本がリファレンスになれば嬉しいです。
|
||||
|
||||
あなたのライブラリで、あなただけのアプリを作ってみてください。Happy hacking! 🚀
|
||||
@@ -0,0 +1,26 @@
|
||||
---
|
||||
title: "Building a Desktop LLM App with cpp-httplib"
|
||||
order: 0
|
||||
|
||||
---
|
||||
|
||||
自分のC++ライブラリにWeb APIを追加したい、Electronライクなデスクトップアプリをサクッと作りたい―そう思ったことはありませんか? Rustなら「Tauri + axum」という選択肢がありますが、C++では難しいと諦めていませんか?
|
||||
|
||||
[cpp-httplib](https://github.com/yhirose/cpp-httplib)と[webview/webview](https://github.com/webview/webview)、そして[cpp-embedlib](https://github.com/yhirose/cpp-embedlib)を組み合わせれば、C++だけで同じアプローチが取れます。しかも配布しやすい、小さなシングルバイナリーのアプリケーションを作れます。
|
||||
|
||||
今回は、[llama.cpp](https://github.com/ggml-org/llama.cpp)を組み込んだLLM翻訳アプリを題材に、「REST API → SSEストリーミング → Web UI→デスクトップアプリ」と段階的に構築しながら、そのやり方を学んでいきましょう。もちろん、翻訳はあくまで題材です。llama.cppを自分のライブラリに置き換えれば、同じ構成で自分だけのアプリが作れます。
|
||||
|
||||

|
||||
|
||||
C++17の基本文法とHTTP(REST API)の基本がわかれば、すぐに始められます。🚀
|
||||
|
||||
## 目次
|
||||
|
||||
1. **[プロジェクト環境を作る](ch01-setup)** — 依存ライブラリの取得、ビルド設定、雛形コード
|
||||
2. **[llama.cppを組み込んでREST APIを作る](ch02-rest-api)** — JSONで翻訳結果を返すAPIの実装
|
||||
3. **[SSEでトークンストリーミングを追加する](ch03-sse-streaming)** — トークン単位の逐次レスポンス
|
||||
4. **[モデルの取得・管理機能を追加する](ch04-model-management)** — Hugging Faceからのダウンロードと切り替え
|
||||
5. **[Web UIを追加する](ch05-web-ui)** — ブラウザから操作できる翻訳画面
|
||||
6. **[WebViewでデスクトップアプリ化する](ch06-desktop-app)** — シングルバイナリのデスクトップアプリ
|
||||
7. **[llama.cpp本家のサーバー実装をコードリーディング](ch07-code-reading)** — プロダクション品質のコードとの比較
|
||||
8. **[自分だけのアプリにカスタマイズする](ch08-customization)** — 自分のライブラリへの差し替えと応用
|
||||
@@ -0,0 +1,36 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 426 160" font-family="system-ui, sans-serif" font-size="14">
|
||||
<rect x="0" y="0" width="426" height="160" rx="8" fill="#f5f3ef"/>
|
||||
|
||||
<defs>
|
||||
<marker id="arrowhead" markerWidth="7" markerHeight="5" refX="7" refY="2.5" orient="auto">
|
||||
<polygon points="0,0 7,2.5 0,5" fill="#198754"/>
|
||||
</marker>
|
||||
</defs>
|
||||
|
||||
<!-- request 1 (y=16) -->
|
||||
<text x="94" y="35" fill="#333" font-weight="bold" text-anchor="end">リクエスト1:</text>
|
||||
<rect x="106" y="16" width="138" height="30" rx="4" fill="#d1e7dd" stroke="#198754" stroke-width="1"/>
|
||||
<text x="175" y="36" fill="#333" text-anchor="middle">システムプロンプト</text>
|
||||
<text x="256" y="36" fill="#333" text-anchor="middle">+</text>
|
||||
<rect x="270" y="16" width="138" height="30" rx="4" fill="#cfe2ff" stroke="#0d6efd" stroke-width="1"/>
|
||||
<text x="339" y="36" fill="#333" text-anchor="middle">ユーザーの質問A</text>
|
||||
|
||||
<!-- annotation: cache save -->
|
||||
<text x="175" y="64" fill="#198754" font-size="11" text-anchor="middle">KV キャッシュに保存</text>
|
||||
|
||||
<!-- arrow -->
|
||||
<line x1="175" y1="70" x2="175" y2="90" stroke="#198754" stroke-width="1.2" marker-end="url(#arrowhead)"/>
|
||||
<text x="188" y="85" fill="#198754" font-size="11">再利用</text>
|
||||
|
||||
<!-- request 2 (y=96) -->
|
||||
<text x="94" y="115" fill="#333" font-weight="bold" text-anchor="end">リクエスト2:</text>
|
||||
<rect x="106" y="96" width="138" height="30" rx="4" fill="#d1e7dd" stroke="#198754" stroke-width="1" stroke-dasharray="6,3"/>
|
||||
<text x="175" y="116" fill="#333" text-anchor="middle">システムプロンプト</text>
|
||||
<text x="256" y="116" fill="#333" text-anchor="middle">+</text>
|
||||
<rect x="270" y="96" width="138" height="30" rx="4" fill="#cfe2ff" stroke="#0d6efd" stroke-width="1"/>
|
||||
<text x="339" y="116" fill="#333" text-anchor="middle">ユーザーの質問B</text>
|
||||
|
||||
<!-- bottom labels -->
|
||||
<text x="175" y="144" fill="#198754" font-size="11" text-anchor="middle">再計算しない</text>
|
||||
<text x="339" y="144" fill="#0d6efd" font-size="11" text-anchor="middle">ここだけ計算</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 2.1 KiB |
@@ -0,0 +1,24 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 440 240" font-family="system-ui, sans-serif" font-size="14">
|
||||
<!-- outer box -->
|
||||
<rect x="0" y="0" width="440" height="240" rx="8" fill="#f5f3ef"/>
|
||||
<text x="20" y="28" font-weight="bold" font-size="16" fill="#333">llama-server</text>
|
||||
|
||||
<!-- slot 0 -->
|
||||
<rect x="20" y="46" width="400" height="32" rx="4" fill="#d1e7dd" stroke="#198754" stroke-width="1"/>
|
||||
<text x="32" y="67" fill="#333">スロット0: ユーザーAのリクエスト</text>
|
||||
|
||||
<!-- slot 1 -->
|
||||
<rect x="20" y="86" width="400" height="32" rx="4" fill="#d1e7dd" stroke="#198754" stroke-width="1"/>
|
||||
<text x="32" y="107" fill="#333">スロット1: ユーザーBのリクエスト</text>
|
||||
|
||||
<!-- slot 2 -->
|
||||
<rect x="20" y="126" width="400" height="32" rx="4" fill="#e9ecef" stroke="#adb5bd" stroke-width="1"/>
|
||||
<text x="32" y="147" fill="#999">スロット2: (空き)</text>
|
||||
|
||||
<!-- slot 3 -->
|
||||
<rect x="20" y="166" width="400" height="32" rx="4" fill="#e9ecef" stroke="#adb5bd" stroke-width="1"/>
|
||||
<text x="32" y="187" fill="#999">スロット3: (空き)</text>
|
||||
|
||||
<!-- arrow + label -->
|
||||
<text x="20" y="224" fill="#333" font-size="13">→ アクティブなスロットをまとめて1回のバッチで推論</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 1.2 KiB |
@@ -0,0 +1,88 @@
|
||||
---
|
||||
title: "Getting Started"
|
||||
order: 1
|
||||
---
|
||||
|
||||
cpp-httplibを始めるのに必要なのは、`httplib.h`とC++コンパイラーだけです。ファイルをダウンロードして、Hello Worldサーバーを動かすところまでやってみましょう。
|
||||
|
||||
## httplib.h の入手
|
||||
|
||||
GitHubから直接ダウンロードできます。常に最新版を使ってください。
|
||||
|
||||
```sh
|
||||
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 install g++` |
|
||||
| Windows | MSVC | Visual Studio 2022 以降(C++ コンポーネントを含めてインストール) |
|
||||
|
||||
## Hello World サーバー
|
||||
|
||||
次のコードを `server.cpp` として保存しましょう。
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
|
||||
int main() {
|
||||
httplib::Server svr;
|
||||
|
||||
svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
|
||||
res.set_content("Hello, World!", "text/plain");
|
||||
});
|
||||
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
たった数行で、HTTPリクエストに応答するサーバーが書けます。
|
||||
|
||||
## コンパイルと実行
|
||||
|
||||
このチュートリアルのサンプルコードは、コードを簡潔に書けるC++17で書いています。cpp-httplib自体はC++11でもコンパイルできます。
|
||||
|
||||
```sh
|
||||
# macOS
|
||||
clang++ -std=c++17 -o server server.cpp
|
||||
|
||||
# Linux
|
||||
# `-pthread`: cpp-httplibは内部でスレッドを使用
|
||||
clang++ -std=c++17 -pthread -o server server.cpp
|
||||
|
||||
# Windows (Developer Command Prompt)
|
||||
# `/EHsc`: C++例外処理を有効化
|
||||
cl /EHsc /std:c++17 server.cpp
|
||||
```
|
||||
|
||||
コンパイルできたら実行します。
|
||||
|
||||
```sh
|
||||
# macOS / Linux
|
||||
./server
|
||||
|
||||
# Windows
|
||||
server.exe
|
||||
```
|
||||
|
||||
ブラウザで `http://localhost:8080` を開いてください。"Hello, World!" と表示されれば成功です。
|
||||
|
||||
`curl` でも確認できます。
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080/
|
||||
# Hello, World!
|
||||
```
|
||||
|
||||
サーバーを停止するには、ターミナルで `Ctrl+C` を押します。
|
||||
|
||||
## 次のステップ
|
||||
|
||||
サーバーの基本がわかりましたね。次は、クライアント側を見てみましょう。cpp-httplibはHTTPクライアント機能も備えています。
|
||||
|
||||
**次:** [Basic Client](../02-basic-client)
|
||||
@@ -0,0 +1,266 @@
|
||||
---
|
||||
title: "Basic Client"
|
||||
order: 2
|
||||
---
|
||||
|
||||
cpp-httplibはサーバーだけでなく、HTTPクライアント機能も備えています。`httplib::Client` を使って、GETやPOSTリクエストを送ってみましょう。
|
||||
|
||||
## テスト用サーバーの準備
|
||||
|
||||
クライアントの動作を確認するために、リクエストを受け付けるサーバーを用意します。次のコードを保存し、前章と同じ手順でコンパイル・実行してください。サーバーの詳しい解説は次章で行います。
|
||||
|
||||
```cpp
|
||||
#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) {
|
||||
auto q = req.get_param_value("q");
|
||||
res.set_content("Query: " + q, "text/plain");
|
||||
});
|
||||
|
||||
svr.Post("/post", [](const auto &req, auto &res) {
|
||||
res.set_content(req.body, "text/plain");
|
||||
});
|
||||
|
||||
svr.Post("/submit", [](const auto &req, auto &res) {
|
||||
std::string result;
|
||||
for (auto &[key, val] : req.params) {
|
||||
result += key + " = " + val + "\n";
|
||||
}
|
||||
res.set_content(result, "text/plain");
|
||||
});
|
||||
|
||||
svr.Post("/upload", [](const auto &req, auto &res) {
|
||||
auto f = req.form.get_file("file");
|
||||
auto content = f.filename + " (" + std::to_string(f.content.size()) + " bytes)";
|
||||
res.set_content(content, "text/plain");
|
||||
});
|
||||
|
||||
svr.Get("/users/:id", [](const auto &req, auto &res) {
|
||||
auto id = req.path_params.at("id");
|
||||
res.set_content("User ID: " + id, "text/plain");
|
||||
});
|
||||
|
||||
svr.Get(R"(/files/(\d+))", [](const auto &req, auto &res) {
|
||||
auto id = req.matches[1];
|
||||
res.set_content("File ID: " + std::string(id), "text/plain");
|
||||
});
|
||||
|
||||
std::cout << "Listening on port 8080..." << std::endl;
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
## GETリクエスト
|
||||
|
||||
サーバーが起動したら、別のターミナルを開いて試してみましょう。まず、最もシンプルなGETリクエストです。
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
|
||||
auto res = cli.Get("/hi");
|
||||
if (res) {
|
||||
std::cout << res->status << std::endl; // 200
|
||||
std::cout << res->body << std::endl; // Hello!
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`httplib::Client` のコンストラクターにサーバーのアドレスを渡し、`Get()` でリクエストを送ります。戻り値の `res` からステータスコードやボディを取得できます。
|
||||
|
||||
対応する `curl` コマンドはこうなります。
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080/hi
|
||||
# Hello!
|
||||
```
|
||||
|
||||
## レスポンスの確認
|
||||
|
||||
レスポンスには、ステータスコードとボディ以外にもヘッダー情報が含まれています。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/hi");
|
||||
if (res) {
|
||||
// ステータスコード
|
||||
std::cout << res->status << std::endl; // 200
|
||||
|
||||
// ボディ
|
||||
std::cout << res->body << std::endl; // Hello!
|
||||
|
||||
// ヘッダー
|
||||
std::cout << res->get_header_value("Content-Type") << std::endl; // text/plain
|
||||
}
|
||||
```
|
||||
|
||||
`res->body` は `std::string` なので、JSON レスポンスをパースしたい場合は [nlohmann/json](https://github.com/nlohmann/json) などの JSON ライブラリにそのまま渡せます。
|
||||
|
||||
## クエリパラメーター
|
||||
|
||||
GETリクエストにクエリパラメーターを付けるには、URLに直接書くか、`httplib::Params` を使います。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/search", httplib::Params{{"q", "cpp-httplib"}});
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // Query: cpp-httplib
|
||||
}
|
||||
```
|
||||
|
||||
`httplib::Params` を使うと、特殊文字のURLエンコードを自動で行ってくれます。
|
||||
|
||||
```sh
|
||||
curl "http://localhost:8080/search?q=cpp-httplib"
|
||||
# Query: cpp-httplib
|
||||
```
|
||||
|
||||
## パスパラメーター
|
||||
|
||||
URLのパスに値を直接埋め込む場合も、クライアント側は特別なAPIは不要です。パスをそのまま `Get()` に渡すだけです。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/users/42");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // User ID: 42
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080/users/42
|
||||
# User ID: 42
|
||||
```
|
||||
|
||||
テスト用サーバーには、正規表現でIDを数字のみに絞った `/files/(\d+)` もあります。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/files/42");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // File ID: 42
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080/files/42
|
||||
# File ID: 42
|
||||
```
|
||||
|
||||
`/files/abc` のように数字以外を渡すと404が返ります。仕組みは次章で解説します。
|
||||
|
||||
## リクエストヘッダー
|
||||
|
||||
カスタムHTTPヘッダーを付けるには、`httplib::Headers` を渡します。`Get()` や `Post()` のどちらでも使えます。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/hi", httplib::Headers{
|
||||
{"Authorization", "Bearer my-token"}
|
||||
});
|
||||
```
|
||||
|
||||
```sh
|
||||
curl -H "Authorization: Bearer my-token" http://localhost:8080/hi
|
||||
```
|
||||
|
||||
## POSTリクエスト
|
||||
|
||||
テキストデータをPOSTしてみましょう。`Post()` の第2引数にボディ、第3引数にContent-Typeを指定します。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Post("/post", "Hello, Server!", "text/plain");
|
||||
if (res) {
|
||||
std::cout << res->status << std::endl; // 200
|
||||
std::cout << res->body << std::endl; // Hello, Server!
|
||||
}
|
||||
```
|
||||
|
||||
テスト用サーバーの `/post` はボディをそのまま返すので、送った文字列がそのまま返ってきます。
|
||||
|
||||
```sh
|
||||
curl -X POST -H "Content-Type: text/plain" -d "Hello, Server!" http://localhost:8080/post
|
||||
# Hello, Server!
|
||||
```
|
||||
|
||||
## フォームデータの送信
|
||||
|
||||
HTMLフォームのように、キーと値のペアを送ることもできます。`httplib::Params` を使います。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Post("/submit", httplib::Params{
|
||||
{"name", "Alice"},
|
||||
{"age", "30"}
|
||||
});
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl;
|
||||
// age = 30
|
||||
// name = Alice
|
||||
}
|
||||
```
|
||||
|
||||
これは `application/x-www-form-urlencoded` 形式で送信されます。
|
||||
|
||||
```sh
|
||||
curl -X POST -d "name=Alice&age=30" http://localhost:8080/submit
|
||||
```
|
||||
|
||||
## ファイルのPOST
|
||||
|
||||
ファイルをアップロードするには、`httplib::UploadFormDataItems` を使ってマルチパートフォームデータとして送信します。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Post("/upload", httplib::UploadFormDataItems{
|
||||
{"file", "Hello, File!", "hello.txt", "text/plain"}
|
||||
});
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // hello.txt (12 bytes)
|
||||
}
|
||||
```
|
||||
|
||||
`UploadFormDataItems` の各要素は `{name, content, filename, content_type}` の4つのフィールドで構成されます。
|
||||
|
||||
```sh
|
||||
curl -F "file=Hello, File!;filename=hello.txt;type=text/plain" http://localhost:8080/upload
|
||||
```
|
||||
|
||||
## エラーハンドリング
|
||||
|
||||
ネットワーク通信では、サーバーに接続できない場合があります。`res` が有効かどうかを必ず確認しましょう。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:9999"); // 存在しないポート
|
||||
auto res = cli.Get("/hi");
|
||||
|
||||
if (!res) {
|
||||
// 接続エラー
|
||||
std::cout << "Error: " << httplib::to_string(res.error()) << std::endl;
|
||||
// Error: Connection
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ここに到達すればレスポンスを受信できている
|
||||
if (res->status != 200) {
|
||||
std::cout << "HTTP Error: " << res->status << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << res->body << std::endl;
|
||||
```
|
||||
|
||||
エラーには2つのレベルがあります。
|
||||
|
||||
- **接続エラー**: サーバーに到達できなかった場合。`res` が偽になり、`res.error()` でエラーの種類を取得できます
|
||||
- **HTTPエラー**: サーバーからエラーステータス(404、500など)が返ってきた場合。`res` は真ですが、`res->status` を確認する必要があります
|
||||
|
||||
## 次のステップ
|
||||
|
||||
クライアントからリクエストを送る方法がわかりました。次は、サーバー側をもっと詳しく見てみましょう。ルーティングやパスパラメータなど、サーバーの機能を掘り下げます。
|
||||
|
||||
**次:** [Basic Server](../03-basic-server)
|
||||
@@ -0,0 +1,280 @@
|
||||
---
|
||||
title: "Basic Server"
|
||||
order: 3
|
||||
---
|
||||
|
||||
前章ではクライアントからリクエストを送りました。そのとき、テスト用サーバーを用意しましたね。この章では、あのサーバーの仕組みをひとつずつ紐解いていきます。
|
||||
|
||||
## サーバーの起動
|
||||
|
||||
ルーティングを登録したら、最後に `svr.listen()` を呼んでサーバーを起動します。
|
||||
|
||||
```cpp
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
```
|
||||
|
||||
第1引数はホスト、第2引数はポート番号です。`"0.0.0.0"` を指定すると、すべてのネットワークインターフェースでリクエストを受け付けます。自分のマシンからのアクセスだけに限定したいときは `"127.0.0.1"` を使います。
|
||||
|
||||
`listen()` はブロッキング呼び出しです。サーバーが停止するまで、この行から先には進みません。ターミナルで `Ctrl+C` を押すか、別スレッドから `svr.stop()` を呼ぶまでサーバーは動き続けます。
|
||||
|
||||
## ルーティング
|
||||
|
||||
サーバーの核になるのは「ルーティング」です。どのURLに、どのHTTPメソッドでアクセスされたら、何をするか。それを登録する仕組みです。
|
||||
|
||||
```cpp
|
||||
httplib::Server svr;
|
||||
|
||||
svr.Get("/hi", [](const httplib::Request &req, httplib::Response &res) {
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`svr.Get()` は、GETリクエストに対するハンドラーを登録します。第1引数がパス、第2引数がハンドラー関数です。`/hi` にGETリクエストが来たら、このラムダが呼ばれます。
|
||||
|
||||
HTTPメソッドごとにメソッドが用意されています。
|
||||
|
||||
```cpp
|
||||
svr.Get("/path", handler); // GET
|
||||
svr.Post("/path", handler); // POST
|
||||
svr.Put("/path", handler); // PUT
|
||||
svr.Delete("/path", handler); // DELETE
|
||||
```
|
||||
|
||||
ハンドラーのシグネチャは `(const httplib::Request &req, httplib::Response &res)` です。`auto` を使って短く書くこともできます。
|
||||
|
||||
```cpp
|
||||
svr.Get("/hi", [](const auto &req, auto &res) {
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
パスが一致したときだけハンドラーが呼ばれます。登録されていないパスにアクセスすると、自動的に404が返ります。
|
||||
|
||||
## リクエストオブジェクト
|
||||
|
||||
ハンドラーの第1引数 `req` から、クライアントが送ってきた情報を読み取れます。
|
||||
|
||||
### ボディ
|
||||
|
||||
`req.body` でリクエストボディを取得できます。型は `std::string` です。
|
||||
|
||||
```cpp
|
||||
svr.Post("/post", [](const auto &req, auto &res) {
|
||||
// クライアントが送ったボディをそのまま返す
|
||||
res.set_content(req.body, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### ヘッダー
|
||||
|
||||
`req.get_header_value()` でリクエストヘッダーの値を取得できます。
|
||||
|
||||
```cpp
|
||||
svr.Get("/check", [](const auto &req, auto &res) {
|
||||
auto auth = req.get_header_value("Authorization");
|
||||
res.set_content("Auth: " + auth, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### クエリパラメーターとフォームデータ
|
||||
|
||||
`req.get_param_value()` でパラメーターを取得できます。GETのクエリパラメーターと、POSTのフォームデータの両方に使えます。
|
||||
|
||||
```cpp
|
||||
svr.Get("/search", [](const auto &req, auto &res) {
|
||||
auto q = req.get_param_value("q");
|
||||
res.set_content("Query: " + q, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`/search?q=cpp-httplib` にアクセスすると、`q` の値は `"cpp-httplib"` になります。
|
||||
|
||||
すべてのパラメーターをループで処理したいときは、`req.params` を使います。
|
||||
|
||||
```cpp
|
||||
svr.Post("/submit", [](const auto &req, auto &res) {
|
||||
std::string result;
|
||||
for (auto &[key, val] : req.params) {
|
||||
result += key + " = " + val + "\n";
|
||||
}
|
||||
res.set_content(result, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### ファイルアップロード
|
||||
|
||||
マルチパートフォームでアップロードされたファイルは、`req.form.get_file()` で取得します。
|
||||
|
||||
```cpp
|
||||
svr.Post("/upload", [](const auto &req, auto &res) {
|
||||
auto f = req.form.get_file("file");
|
||||
auto content = f.filename + " (" + std::to_string(f.content.size()) + " bytes)";
|
||||
res.set_content(content, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`f.filename` でファイル名、`f.content` でファイルの中身にアクセスできます。
|
||||
|
||||
## パスパラメーター
|
||||
|
||||
URLの一部を変数として受け取りたいことがあります。たとえば `/users/42` の `42` を取得したい場合です。`:param` 記法を使うと、URLの一部をキャプチャできます。
|
||||
|
||||
```cpp
|
||||
svr.Get("/users/:id", [](const auto &req, auto &res) {
|
||||
auto id = req.path_params.at("id");
|
||||
res.set_content("User ID: " + id, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`/users/42` にアクセスすると、`req.path_params.at("id")` は `"42"` を返します。`/users/100` なら `"100"` です。
|
||||
|
||||
複数のパスパラメーターも使えます。
|
||||
|
||||
```cpp
|
||||
svr.Get("/users/:user_id/posts/:post_id", [](const auto &req, auto &res) {
|
||||
auto user_id = req.path_params.at("user_id");
|
||||
auto post_id = req.path_params.at("post_id");
|
||||
res.set_content("User: " + user_id + ", Post: " + post_id, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### 正規表現パターン
|
||||
|
||||
`:param` の代わりに正規表現をパスに書くこともできます。キャプチャグループの値は `req.matches` で取得します。型は `std::smatch` です。
|
||||
|
||||
```cpp
|
||||
// 数字のみのIDを受け付ける
|
||||
svr.Get(R"(/files/(\d+))", [](const auto &req, auto &res) {
|
||||
auto id = req.matches[1]; // 最初のキャプチャグループ
|
||||
res.set_content("File ID: " + std::string(id), "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`/files/42` にはマッチしますが、`/files/abc` にはマッチしません。入力値を絞り込みたいときに便利です。
|
||||
|
||||
## レスポンスの組み立て
|
||||
|
||||
ハンドラーの第2引数 `res` を使って、クライアントに返すレスポンスを組み立てます。
|
||||
|
||||
### ボディとContent-Type
|
||||
|
||||
`res.set_content()` でボディとContent-Typeを設定します。これだけでステータスコード200のレスポンスが返ります。
|
||||
|
||||
```cpp
|
||||
svr.Get("/hi", [](const auto &req, auto &res) {
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### ステータスコード
|
||||
|
||||
ステータスコードを変えたいときは、`res.status` に代入します。
|
||||
|
||||
```cpp
|
||||
svr.Get("/not-found", [](const auto &req, auto &res) {
|
||||
res.status = 404;
|
||||
res.set_content("Not found", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### レスポンスヘッダー
|
||||
|
||||
`res.set_header()` でレスポンスヘッダーを追加できます。
|
||||
|
||||
```cpp
|
||||
svr.Get("/with-header", [](const auto &req, auto &res) {
|
||||
res.set_header("X-Custom", "my-value");
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
## 前章のサーバーを読み解く
|
||||
|
||||
ここまでの知識を使って、前章で用意したテスト用サーバーを改めて見てみましょう。
|
||||
|
||||
### GET /hi
|
||||
|
||||
```cpp
|
||||
svr.Get("/hi", [](const auto &, auto &res) {
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
最もシンプルなハンドラーです。リクエストの情報は使わないので、`req` の変数名を省略しています。`"Hello!"` というテキストをそのまま返します。
|
||||
|
||||
### GET /search
|
||||
|
||||
```cpp
|
||||
svr.Get("/search", [](const auto &req, auto &res) {
|
||||
auto q = req.get_param_value("q");
|
||||
res.set_content("Query: " + q, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`req.get_param_value("q")` でクエリパラメーター `q` の値を取り出します。`/search?q=cpp-httplib` なら、レスポンスは `"Query: cpp-httplib"` になります。
|
||||
|
||||
### POST /post
|
||||
|
||||
```cpp
|
||||
svr.Post("/post", [](const auto &req, auto &res) {
|
||||
res.set_content(req.body, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
クライアントが送ったリクエストボディを、そのままレスポンスとして返すエコーサーバーです。`req.body` にボディが丸ごと入っています。
|
||||
|
||||
### POST /submit
|
||||
|
||||
```cpp
|
||||
svr.Post("/submit", [](const auto &req, auto &res) {
|
||||
std::string result;
|
||||
for (auto &[key, val] : req.params) {
|
||||
result += key + " = " + val + "\n";
|
||||
}
|
||||
res.set_content(result, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
フォームデータとして送られたキーと値のペアを、`req.params` でループ処理しています。構造化束縛 `auto &[key, val]` を使って、各ペアを取り出しています。
|
||||
|
||||
### POST /upload
|
||||
|
||||
```cpp
|
||||
svr.Post("/upload", [](const auto &req, auto &res) {
|
||||
auto f = req.form.get_file("file");
|
||||
auto content = f.filename + " (" + std::to_string(f.content.size()) + " bytes)";
|
||||
res.set_content(content, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
マルチパートフォームで送られたファイルを受け取ります。`req.form.get_file("file")` で `"file"` という名前のフィールドを取得し、`f.filename` と `f.content.size()` でファイル名とサイズを返しています。
|
||||
|
||||
### GET /users/:id
|
||||
|
||||
```cpp
|
||||
svr.Get("/users/:id", [](const auto &req, auto &res) {
|
||||
auto id = req.path_params.at("id");
|
||||
res.set_content("User ID: " + id, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`:id` の部分がパスパラメーターです。`req.path_params.at("id")` で値を取り出しています。`/users/42` なら `"42"`、`/users/alice` なら `"alice"` が得られます。
|
||||
|
||||
### GET /files/(\d+)
|
||||
|
||||
```cpp
|
||||
svr.Get(R"(/files/(\d+))", [](const auto &req, auto &res) {
|
||||
auto id = req.matches[1];
|
||||
res.set_content("File ID: " + std::string(id), "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
正規表現 `(\d+)` で数字だけのIDにマッチします。`/files/42` にはマッチしますが、`/files/abc` は404になります。`req.matches[1]` で最初のキャプチャグループの値を取得しています。
|
||||
|
||||
## 次のステップ
|
||||
|
||||
サーバーの基本がわかりましたね。ルーティング、リクエストの読み取り、レスポンスの組み立て。これだけで、十分に実用的なAPIサーバーが作れます。
|
||||
|
||||
次は、静的ファイルの配信を見てみましょう。HTMLやCSSを配信するサーバーを作ります。
|
||||
|
||||
**次:** [Static File Server](../04-static-file-server)
|
||||
@@ -0,0 +1,134 @@
|
||||
---
|
||||
title: "Static File Server"
|
||||
order: 4
|
||||
---
|
||||
|
||||
cpp-httplibは、HTMLやCSS、画像ファイルなどの静的ファイルも配信できます。面倒な設定は要りません。`set_mount_point()` を1行呼ぶだけです。
|
||||
|
||||
## set_mount_point の基本
|
||||
|
||||
さっそくやってみましょう。`set_mount_point()` は、URLのパスとローカルディレクトリを紐づけます。
|
||||
|
||||
```cpp
|
||||
#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);
|
||||
}
|
||||
```
|
||||
|
||||
第1引数がURLのマウントポイント、第2引数がローカルのディレクトリパスです。この例だと、`/` へのリクエストを `./html` ディレクトリから配信します。
|
||||
|
||||
試してみましょう。まず `html` ディレクトリを作って、`index.html` を置きます。
|
||||
|
||||
```sh
|
||||
mkdir html
|
||||
```
|
||||
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head><title>My Page</title></head>
|
||||
<body>
|
||||
<h1>Hello from cpp-httplib!</h1>
|
||||
<p>This is a static file.</p>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
コンパイルして起動します。
|
||||
|
||||
```sh
|
||||
g++ -std=c++17 -o server server.cpp -pthread
|
||||
./server
|
||||
```
|
||||
|
||||
ブラウザで `http://localhost:8080` を開いてみてください。`html/index.html` の内容が表示されるはずです。`http://localhost:8080/index.html` でも同じページが返ります。
|
||||
|
||||
もちろん、前章のクライアントコードや `curl` でもアクセスできますよ。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
auto res = cli.Get("/");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // HTMLが表示される
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080
|
||||
```
|
||||
|
||||
## 複数のマウントポイント
|
||||
|
||||
`set_mount_point()` は何回でも呼べます。URLのパスごとに、別々のディレクトリを割り当てられます。
|
||||
|
||||
```cpp
|
||||
svr.set_mount_point("/", "./public");
|
||||
svr.set_mount_point("/assets", "./static/assets");
|
||||
svr.set_mount_point("/docs", "./documentation");
|
||||
```
|
||||
|
||||
`/assets/style.css` なら `./static/assets/style.css` を、`/docs/guide.html` なら `./documentation/guide.html` を配信します。
|
||||
|
||||
## ハンドラーとの組み合わせ
|
||||
|
||||
静的ファイルの配信と、前章で学んだルーティングハンドラーは共存できます。
|
||||
|
||||
```cpp
|
||||
httplib::Server svr;
|
||||
|
||||
// APIエンドポイント
|
||||
svr.Get("/api/hello", [](const auto &, auto &res) {
|
||||
res.set_content(R"({"message":"Hello!"})", "application/json");
|
||||
});
|
||||
|
||||
// 静的ファイル配信
|
||||
svr.set_mount_point("/", "./public");
|
||||
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
```
|
||||
|
||||
ハンドラーが先に評価されます。`/api/hello` にはハンドラーが応答し、それ以外のパスは `./public` ディレクトリからファイルを探します。
|
||||
|
||||
## レスポンスヘッダーの追加
|
||||
|
||||
`set_mount_point()` の第3引数にヘッダーを渡すと、静的ファイルのレスポンスにカスタムヘッダーを付けられます。キャッシュ制御に便利です。
|
||||
|
||||
```cpp
|
||||
svr.set_mount_point("/", "./public", {
|
||||
{"Cache-Control", "max-age=3600"}
|
||||
});
|
||||
```
|
||||
|
||||
こうすると、ブラウザは配信されたファイルを1時間キャッシュします。
|
||||
|
||||
## 静的ファイルサーバー用のDockerファイル
|
||||
|
||||
cpp-httplibのリポジトリには、静的ファイルサーバー用の `Dockerfile` が含まれています。Docker Hubにビルド済みイメージも公開しているので、1コマンドで起動できます。
|
||||
|
||||
```sh
|
||||
> docker run -p 8080:80 -v ./my-site:/html yhirose4dockerhub/cpp-httplib-server
|
||||
Serving HTTP on 0.0.0.0:80
|
||||
Mount point: / -> ./html
|
||||
Press Ctrl+C to shutdown gracefully...
|
||||
192.168.65.1 - - [22/Feb/2026:12:00:00 +0000] "GET / HTTP/1.1" 200 256 "-" "Mozilla/5.0 ..."
|
||||
192.168.65.1 - - [22/Feb/2026:12:00:00 +0000] "GET /style.css HTTP/1.1" 200 1024 "-" "Mozilla/5.0 ..."
|
||||
192.168.65.1 - - [22/Feb/2026:12:00:01 +0000] "GET /favicon.ico HTTP/1.1" 404 152 "-" "Mozilla/5.0 ..."
|
||||
```
|
||||
|
||||
`./my-site` ディレクトリの中身が、そのままポート8080で配信されます。NGINXと同じログ形式で、アクセスの様子を確認できますよ。
|
||||
|
||||
## 次のステップ
|
||||
|
||||
静的ファイルの配信ができるようになりましたね。HTMLやCSS、JavaScriptを配信するWebサーバーが、これだけのコードで作れます。
|
||||
|
||||
次は、HTTPSで暗号化通信をしてみましょう。まずはTLSライブラリのセットアップからです。
|
||||
|
||||
**次:** [TLS Setup](../05-tls-setup)
|
||||
@@ -0,0 +1,88 @@
|
||||
---
|
||||
title: "TLS Setup"
|
||||
order: 5
|
||||
---
|
||||
|
||||
ここまではHTTP(平文)でやってきましたが、実際のWebではHTTPS(暗号化通信)が当たり前ですよね。cpp-httplibでHTTPSを使うには、TLSライブラリが必要です。
|
||||
|
||||
このTourではOpenSSLを使います。最も広く使われていて、情報も豊富です。
|
||||
|
||||
## OpenSSLのインストール
|
||||
|
||||
お使いのOSに合わせてインストールしましょう。
|
||||
|
||||
| OS | インストール方法 |
|
||||
| -- | ---------------- |
|
||||
| macOS | [Homebrew](https://brew.sh/) (`brew install openssl`) |
|
||||
| Ubuntu / Debian | `sudo apt install libssl-dev` |
|
||||
| Windows | [vcpkg](https://vcpkg.io/) (`vcpkg install openssl`) |
|
||||
|
||||
## コンパイルオプション
|
||||
|
||||
TLS機能を有効にするには、`CPPHTTPLIB_OPENSSL_SUPPORT` マクロを定義してコンパイルします。前章までのコンパイルコマンドに、いくつかオプションが増えます。
|
||||
|
||||
```sh
|
||||
# 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` / `-L`**(macOSのみ)— Homebrew版OpenSSLのパスを指定
|
||||
- **`-framework CoreFoundation -framework Security`**(macOSのみ)— Keychainからシステム証明書を自動で読み込むために必要です
|
||||
|
||||
## 動作確認
|
||||
|
||||
ちゃんと動くか確認してみましょう。`httplib::Client` にHTTPSのURLを渡してアクセスするだけのプログラムです。
|
||||
|
||||
```cpp
|
||||
#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](../06-https-client)
|
||||
@@ -0,0 +1,122 @@
|
||||
---
|
||||
title: "HTTPS Client"
|
||||
order: 6
|
||||
---
|
||||
|
||||
前章でOpenSSLのセットアップが済んだので、さっそくHTTPSクライアントを使ってみましょう。2章で使った `httplib::Client` がそのまま使えます。コンストラクタに `https://` 付きのURLを渡すだけです。
|
||||
|
||||
## GETリクエスト
|
||||
|
||||
実在するHTTPSサイトにアクセスしてみましょう。
|
||||
|
||||
```cpp
|
||||
#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::endl; // HTMLの先頭部分
|
||||
} else {
|
||||
std::cout << "Error: " << httplib::to_string(res.error()) << std::endl;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
2章では `httplib::Client cli("http://localhost:8080")` と書きましたよね。スキームを `https://` に変えるだけです。`Get()` や `Post()` など、2章で学んだAPIはすべてそのまま使えます。
|
||||
|
||||
```sh
|
||||
curl https://nghttp2.org/
|
||||
```
|
||||
|
||||
## ポートの指定
|
||||
|
||||
HTTPSのデフォルトポートは443です。別のポートを使いたい場合は、URLにポートを含めます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://localhost:8443");
|
||||
```
|
||||
|
||||
## CA証明書の検証
|
||||
|
||||
`httplib::Client` はHTTPS接続時、デフォルトでサーバー証明書を検証します。信頼できるCA(認証局)が発行した証明書を持つサーバーにしか接続しません。
|
||||
|
||||
CA証明書は、macOSならKeychain、LinuxならシステムのCA証明書ストア、WindowsならWindowsの証明書ストアから自動で読み込みます。ほとんどの場合、追加の設定は要りません。
|
||||
|
||||
### CA証明書ファイルの指定
|
||||
|
||||
環境によってはシステムのCA証明書が見つからないこともあります。そのときは `set_ca_cert_path()` でパスを直接指定してください。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://nghttp2.org");
|
||||
cli.set_ca_cert_path("/etc/ssl/certs/ca-certificates.crt");
|
||||
|
||||
auto res = cli.Get("/");
|
||||
```
|
||||
|
||||
```sh
|
||||
curl --cacert /etc/ssl/certs/ca-certificates.crt https://nghttp2.org/
|
||||
```
|
||||
|
||||
### 証明書検証の無効化
|
||||
|
||||
開発中、自己署名証明書のサーバーに接続したいときは、検証を無効にできます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://localhost:8443");
|
||||
cli.enable_server_certificate_verification(false);
|
||||
|
||||
auto res = cli.Get("/");
|
||||
```
|
||||
|
||||
```sh
|
||||
curl -k https://localhost:8443/
|
||||
```
|
||||
|
||||
本番では絶対に無効にしないでください。中間者攻撃のリスクがあります。
|
||||
|
||||
## リダイレクトの追跡
|
||||
|
||||
HTTPSサイトへのアクセスでは、リダイレクトに遭遇することがよくあります。たとえば `http://` から `https://` へ、あるいは `www` なしから `www` ありへ転送されるケースです。
|
||||
|
||||
デフォルトではリダイレクトを追跡しません。リダイレクト先は `Location` ヘッダーで確認できます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://nghttp2.org");
|
||||
|
||||
auto res = cli.Get("/httpbin/redirect/3");
|
||||
if (res) {
|
||||
std::cout << res->status << std::endl; // 302
|
||||
std::cout << res->get_header_value("Location") << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl https://nghttp2.org/httpbin/redirect/3
|
||||
```
|
||||
|
||||
`set_follow_location(true)` を設定すると、リダイレクトを自動で追跡して、最終的なレスポンスを返してくれます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://nghttp2.org");
|
||||
cli.set_follow_location(true);
|
||||
|
||||
auto res = cli.Get("/httpbin/redirect/3");
|
||||
if (res) {
|
||||
std::cout << res->status << std::endl; // 200(最終的なレスポンス)
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl -L https://nghttp2.org/httpbin/redirect/3
|
||||
```
|
||||
|
||||
## 次のステップ
|
||||
|
||||
HTTPSクライアントの使い方がわかりましたね。次は自分でHTTPSサーバーを立ててみましょう。自己署名証明書の作り方から始めます。
|
||||
|
||||
**次:** [HTTPS Server](../07-https-server)
|
||||
@@ -0,0 +1,124 @@
|
||||
---
|
||||
title: "HTTPS Server"
|
||||
order: 7
|
||||
---
|
||||
|
||||
前章ではHTTPSクライアントを使いました。今度は自分でHTTPSサーバーを立ててみましょう。3章の `httplib::Server` を `httplib::SSLServer` に置き換えるだけです。
|
||||
|
||||
ただし、TLSサーバーにはサーバー証明書と秘密鍵が必要です。まずはそこから準備しましょう。
|
||||
|
||||
## 自己署名証明書の作成
|
||||
|
||||
開発やテスト用なら、自己署名証明書(いわゆるオレオレ証明書)で十分です。OpenSSLのコマンドでサクッと作れます。
|
||||
|
||||
```sh
|
||||
openssl req -x509 -noenc -keyout key.pem -out cert.pem -subj /CN=localhost
|
||||
```
|
||||
|
||||
これで2つのファイルができます。
|
||||
|
||||
- **`cert.pem`** — サーバー証明書
|
||||
- **`key.pem`** — 秘密鍵
|
||||
|
||||
## 最小のHTTPSサーバー
|
||||
|
||||
証明書ができたら、さっそくサーバーを書いてみましょう。
|
||||
|
||||
```cpp
|
||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::SSLServer svr("cert.pem", "key.pem");
|
||||
|
||||
svr.Get("/", [](const auto &, auto &res) {
|
||||
res.set_content("Hello, HTTPS!", "text/plain");
|
||||
});
|
||||
|
||||
std::cout << "Listening on https://localhost:8443" << std::endl;
|
||||
svr.listen("0.0.0.0", 8443);
|
||||
}
|
||||
```
|
||||
|
||||
`httplib::SSLServer` のコンストラクタに証明書と秘密鍵のパスを渡すだけです。ルーティングの書き方は3章の `httplib::Server` とまったく同じですよ。
|
||||
|
||||
コンパイルして起動しましょう。
|
||||
|
||||
## 動作確認
|
||||
|
||||
サーバーが起動したら、`curl` でアクセスしてみましょう。自己署名証明書なので、`-k` オプションで証明書検証をスキップします。
|
||||
|
||||
```sh
|
||||
curl -k https://localhost:8443/
|
||||
# Hello, HTTPS!
|
||||
```
|
||||
|
||||
ブラウザで `https://localhost:8443` を開くと、「この接続は安全ではありません」と警告が出ます。自己署名証明書なので正常です。気にせず進めてください。
|
||||
|
||||
## クライアントからの接続
|
||||
|
||||
前章の `httplib::Client` で接続してみましょう。自己署名証明書のサーバーに接続するには、2つの方法があります。
|
||||
|
||||
### 方法1: 証明書検証を無効にする
|
||||
|
||||
開発時の手軽な方法です。
|
||||
|
||||
```cpp
|
||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("https://localhost:8443");
|
||||
cli.enable_server_certificate_verification(false);
|
||||
|
||||
auto res = cli.Get("/");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // Hello, HTTPS!
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 方法2: 自己署名証明書をCA証明書として指定する
|
||||
|
||||
こちらのほうが安全です。`cert.pem` をCA証明書として信頼するよう指定します。
|
||||
|
||||
```cpp
|
||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("https://localhost:8443");
|
||||
cli.set_ca_cert_path("cert.pem");
|
||||
|
||||
auto res = cli.Get("/");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // Hello, HTTPS!
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
この方法なら、指定した証明書のサーバーにだけ接続を許可して、なりすましを防げます。テスト環境でもなるべくこちらを使いましょう。
|
||||
|
||||
## Server と SSLServer の比較
|
||||
|
||||
3章で学んだ `httplib::Server` のAPIは、`httplib::SSLServer` でもそのまま使えます。違いはコンストラクタだけです。
|
||||
|
||||
| | `httplib::Server` | `httplib::SSLServer` |
|
||||
| -- | ------------------ | -------------------- |
|
||||
| コンストラクタ | 引数なし | 証明書と秘密鍵のパス |
|
||||
| プロトコル | HTTP | HTTPS |
|
||||
| ポート(慣例) | 8080 | 8443 |
|
||||
| ルーティング | 共通 | 共通 |
|
||||
|
||||
HTTPサーバーをHTTPSに切り替えるには、コンストラクタを変えるだけです。
|
||||
|
||||
## 次のステップ
|
||||
|
||||
HTTPSサーバーが動きましたね。これでHTTP/HTTPSのクライアントとサーバー、両方の基本がそろいました。
|
||||
|
||||
次は、cpp-httplibに新しく加わったWebSocket機能を見てみましょう。
|
||||
|
||||
**次:** [WebSocket](../08-websocket)
|
||||
@@ -0,0 +1,139 @@
|
||||
---
|
||||
title: "WebSocket"
|
||||
order: 8
|
||||
---
|
||||
|
||||
cpp-httplibはWebSocketにも対応しています。HTTPのリクエスト/レスポンスと違い、WebSocketはサーバーとクライアントが双方向にメッセージをやり取りできます。チャットやリアルタイム通知に便利です。
|
||||
|
||||
さっそく、エコーサーバーとクライアントを作ってみましょう。
|
||||
|
||||
## エコーサーバー
|
||||
|
||||
受け取ったメッセージをそのまま返すエコーサーバーです。
|
||||
|
||||
```cpp
|
||||
#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 port 8080..." << std::endl;
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
`svr.WebSocket()` でWebSocketハンドラーを登録します。3章の `svr.Get()` や `svr.Post()` と同じ感覚ですね。
|
||||
|
||||
ハンドラーの中では、`ws.read(msg)` でメッセージを待ちます。接続が閉じられると `read()` が `false` を返すので、ループを抜けます。`ws.send(msg)` でメッセージを送り返します。
|
||||
|
||||
## クライアントからの接続
|
||||
|
||||
`httplib::ws::WebSocketClient` を使ってサーバーに接続してみましょう。
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::ws::WebSocketClient client("ws://localhost:8080/ws");
|
||||
|
||||
if (!client.connect()) {
|
||||
std::cout << "Connection failed" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// メッセージを送信
|
||||
client.send("Hello, WebSocket!");
|
||||
|
||||
// サーバーからの応答を受信
|
||||
std::string msg;
|
||||
if (client.read(msg)) {
|
||||
std::cout << msg << std::endl; // Hello, WebSocket!
|
||||
}
|
||||
|
||||
client.close();
|
||||
}
|
||||
```
|
||||
|
||||
コンストラクタには `ws://host:port/path` 形式のURLを渡します。`connect()` で接続を開始し、`send()` と `read()` でメッセージをやり取りします。
|
||||
|
||||
## テキストとバイナリ
|
||||
|
||||
WebSocketにはテキストとバイナリの2種類のメッセージがあります。`read()` の戻り値で区別できます。
|
||||
|
||||
```cpp
|
||||
svr.WebSocket("/ws", [](const httplib::Request &, httplib::ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
httplib::ws::ReadResult ret;
|
||||
while ((ret = ws.read(msg))) {
|
||||
if (ret == httplib::ws::Binary) {
|
||||
ws.send(msg.data(), msg.size()); // バイナリとして送信
|
||||
} else {
|
||||
ws.send(msg); // テキストとして送信
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
- `ws.send(const std::string &)` — テキストメッセージとして送信
|
||||
- `ws.send(const char *, size_t)` — バイナリメッセージとして送信
|
||||
|
||||
クライアント側も同じAPIです。
|
||||
|
||||
## リクエスト情報へのアクセス
|
||||
|
||||
ハンドラーの第1引数 `req` から、ハンドシェイク時のHTTPリクエスト情報を読み取れます。認証トークンの確認などに便利です。
|
||||
|
||||
```cpp
|
||||
svr.WebSocket("/ws", [](const httplib::Request &req, httplib::ws::WebSocket &ws) {
|
||||
auto token = req.get_header_value("Authorization");
|
||||
if (token.empty()) {
|
||||
ws.close(httplib::ws::CloseStatus::PolicyViolation, "unauthorized");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {
|
||||
ws.send(msg);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
## WSSで使う
|
||||
|
||||
HTTPS上のWebSocket(WSS)にも対応しています。サーバー側は `httplib::SSLServer` にWebSocketハンドラーを登録するだけです。
|
||||
|
||||
```cpp
|
||||
httplib::SSLServer svr("cert.pem", "key.pem");
|
||||
|
||||
svr.WebSocket("/ws", [](const httplib::Request &, httplib::ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {
|
||||
ws.send(msg);
|
||||
}
|
||||
});
|
||||
|
||||
svr.listen("0.0.0.0", 8443);
|
||||
```
|
||||
|
||||
クライアント側は `wss://` スキームを使います。
|
||||
|
||||
```cpp
|
||||
httplib::ws::WebSocketClient client("wss://localhost:8443/ws");
|
||||
```
|
||||
|
||||
## 次のステップ
|
||||
|
||||
WebSocketの基本がわかりましたね。ここまでで Tourは終わりです。
|
||||
|
||||
次のページでは、Tourで取り上げなかった機能をまとめて紹介します。
|
||||
|
||||
**次:** [What's Next](../09-whats-next)
|
||||
@@ -0,0 +1,228 @@
|
||||
---
|
||||
title: "What's Next"
|
||||
order: 9
|
||||
---
|
||||
|
||||
Tourお疲れさまでした! cpp-httplibの基本はひと通り押さえましたね。でも、まだまだ便利な機能があります。Tourで取り上げなかった機能をカテゴリー別に紹介します。
|
||||
|
||||
## Streaming API
|
||||
|
||||
LLMのストリーミング応答や大きなファイルのダウンロードでは、レスポンス全体をメモリに載せたくないですよね。`stream::Get()` を使えば、データをチャンクごとに処理できます。
|
||||
|
||||
```cpp
|
||||
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());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`Get()` に `content_receiver` コールバックを渡す方法もあります。こちらはKeep-Aliveと併用できます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
|
||||
cli.Get("/stream", [](const char *data, size_t len) {
|
||||
std::cout.write(data, len);
|
||||
return true;
|
||||
});
|
||||
```
|
||||
|
||||
サーバー側には `set_content_provider()` と `set_chunked_content_provider()` があります。サイズがわかっているなら前者、不明なら後者を使ってください。
|
||||
|
||||
```cpp
|
||||
// サイズ指定あり(Content-Length が設定される)
|
||||
svr.Get("/file", [](const auto &, auto &res) {
|
||||
auto size = get_file_size("large.bin");
|
||||
res.set_content_provider(size, "application/octet-stream",
|
||||
[](size_t offset, size_t length, httplib::DataSink &sink) {
|
||||
// offset から length バイト分を送る
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// サイズ不明(Chunked Transfer Encoding)
|
||||
svr.Get("/stream", [](const auto &, auto &res) {
|
||||
res.set_chunked_content_provider("text/plain",
|
||||
[](size_t offset, httplib::DataSink &sink) {
|
||||
sink.write("chunk\n", 6);
|
||||
return true; // falseを返すと終了
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
大きなファイルのアップロードには `make_file_provider()` が便利です。ファイルを全部メモリに読み込まず、ストリーミングで送れます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
|
||||
auto res = cli.Post("/upload", {}, {}, {
|
||||
httplib::make_file_provider("file", "/path/to/large-file.zip")
|
||||
});
|
||||
```
|
||||
|
||||
## Server-Sent Events (SSE)
|
||||
|
||||
SSEクライアントも用意しています。自動再接続や `Last-Event-ID` による再開にも対応しています。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
httplib::sse::SSEClient sse(cli, "/events");
|
||||
|
||||
sse.on_message([](const httplib::sse::SSEMessage &msg) {
|
||||
std::cout << msg.event << ": " << msg.data << std::endl;
|
||||
});
|
||||
|
||||
sse.start(); // ブロッキング、自動再接続あり
|
||||
```
|
||||
|
||||
イベントタイプごとにハンドラーを分けることもできますよ。
|
||||
|
||||
```cpp
|
||||
sse.on_event("update", [](const httplib::sse::SSEMessage &msg) {
|
||||
// "update" イベントだけ処理
|
||||
});
|
||||
```
|
||||
|
||||
## 認証
|
||||
|
||||
クライアントにはBasic認証、Bearer Token認証、Digest認証のヘルパーを用意しています。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://api.example.com");
|
||||
cli.set_basic_auth("user", "password");
|
||||
cli.set_bearer_token_auth("my-token");
|
||||
```
|
||||
|
||||
## 圧縮
|
||||
|
||||
gzip、Brotli、Zstandardによる圧縮・展開に対応しています。使いたい方式のマクロを定義してコンパイルしましょう。
|
||||
|
||||
| 圧縮方式 | マクロ定義 |
|
||||
| -- | -- |
|
||||
| gzip | `CPPHTTPLIB_ZLIB_SUPPORT` |
|
||||
| Brotli | `CPPHTTPLIB_BROTLI_SUPPORT` |
|
||||
| Zstandard | `CPPHTTPLIB_ZSTD_SUPPORT` |
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://example.com");
|
||||
cli.set_compress(true); // リクエストボディを圧縮
|
||||
cli.set_decompress(true); // レスポンスボディを展開
|
||||
```
|
||||
|
||||
## プロキシ
|
||||
|
||||
HTTPプロキシ経由で接続できます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://example.com");
|
||||
cli.set_proxy("proxy.example.com", 8080);
|
||||
cli.set_proxy_basic_auth("user", "password");
|
||||
```
|
||||
|
||||
## タイムアウト
|
||||
|
||||
接続・読み取り・書き込みのタイムアウトを個別に設定できます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://example.com");
|
||||
cli.set_connection_timeout(5, 0); // 5秒
|
||||
cli.set_read_timeout(10, 0); // 10秒
|
||||
cli.set_write_timeout(10, 0); // 10秒
|
||||
```
|
||||
|
||||
## Keep-Alive
|
||||
|
||||
同じサーバーに何度もリクエストするなら、Keep-Aliveを有効にしましょう。TCP接続を再利用するので効率的です。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://example.com");
|
||||
cli.set_keep_alive(true);
|
||||
```
|
||||
|
||||
## サーバーのミドルウェア
|
||||
|
||||
リクエスト処理の前後にフックを挟めます。
|
||||
|
||||
```cpp
|
||||
svr.set_pre_routing_handler([](const auto &req, auto &res) {
|
||||
// すべてのリクエストの前に実行される
|
||||
return httplib::Server::HandlerResponse::Unhandled; // 通常のルーティングに進む
|
||||
});
|
||||
|
||||
svr.set_post_routing_handler([](const auto &req, auto &res) {
|
||||
// レスポンスが返された後に実行される
|
||||
res.set_header("X-Server", "cpp-httplib");
|
||||
});
|
||||
```
|
||||
|
||||
`res.user_data` を使うと、ミドルウェアからハンドラーにデータを渡せます。認証トークンのデコード結果を共有するときに便利です。
|
||||
|
||||
```cpp
|
||||
svr.set_pre_routing_handler([](const auto &req, auto &res) {
|
||||
res.user_data["auth_user"] = std::string("alice");
|
||||
return httplib::Server::HandlerResponse::Unhandled;
|
||||
});
|
||||
|
||||
svr.Get("/me", [](const auto &req, auto &res) {
|
||||
auto user = std::any_cast<std::string>(res.user_data.at("auth_user"));
|
||||
res.set_content("Hello, " + user, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
エラーや例外のハンドラーもカスタマイズできますよ。
|
||||
|
||||
```cpp
|
||||
svr.set_error_handler([](const auto &req, auto &res) {
|
||||
res.set_content("Custom Error Page", "text/html");
|
||||
});
|
||||
|
||||
svr.set_exception_handler([](const auto &req, auto &res, std::exception_ptr ep) {
|
||||
res.status = 500;
|
||||
res.set_content("Internal Server Error", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
## ロギング
|
||||
|
||||
サーバーでもクライアントでもロガーを設定できます。
|
||||
|
||||
```cpp
|
||||
svr.set_logger([](const auto &req, const auto &res) {
|
||||
std::cout << req.method << " " << req.path << " " << res.status << std::endl;
|
||||
});
|
||||
```
|
||||
|
||||
## Unix Domain Socket
|
||||
|
||||
TCP以外に、Unix Domain Socketでの通信にも対応しています。同じマシン上のプロセス間通信に使えます。
|
||||
|
||||
```cpp
|
||||
// サーバー
|
||||
httplib::Server svr;
|
||||
svr.set_address_family(AF_UNIX);
|
||||
svr.listen("/tmp/httplib.sock", 0);
|
||||
```
|
||||
|
||||
```cpp
|
||||
// クライアント
|
||||
httplib::Client cli("http://localhost");
|
||||
cli.set_address_family(AF_UNIX);
|
||||
cli.set_hostname_addr_map({{"localhost", "/tmp/httplib.sock"}});
|
||||
|
||||
auto res = cli.Get("/");
|
||||
```
|
||||
|
||||
## さらに詳しく
|
||||
|
||||
もっと詳しく知りたいときは、以下を参照してください。
|
||||
|
||||
- Cookbook — よくあるユースケースのレシピ集
|
||||
- [README](https://github.com/yhirose/cpp-httplib/blob/master/README.md) — 全APIのリファレンス
|
||||
- [README-sse](https://github.com/yhirose/cpp-httplib/blob/master/README-sse.md) — Server-Sent Eventsの使い方
|
||||
- [README-stream](https://github.com/yhirose/cpp-httplib/blob/master/README-stream.md) — Streaming APIの使い方
|
||||
- [README-websocket](https://github.com/yhirose/cpp-httplib/blob/master/README-websocket.md) — WebSocketサーバーの使い方
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
title: "A Tour of cpp-httplib"
|
||||
order: 0
|
||||
---
|
||||
|
||||
cpp-httplibの基本を、順番に学んでいくチュートリアルです。各章は前の章の内容を踏まえて進む構成なので、1章から順に読んでください。
|
||||
|
||||
1. [Getting Started](01-getting-started) — httplib.h の入手とHello Worldサーバー
|
||||
2. [Basic Client](02-basic-client) — GET/POST・パスパラメーターのリクエスト送信
|
||||
3. [Basic Server](03-basic-server) — ルーティング、パスパラメーター、レスポンスの組み立て
|
||||
4. [Static File Server](04-static-file-server) — 静的ファイルの配信
|
||||
5. [TLS Setup](05-tls-setup) — OpenSSL / mbedTLS のセットアップ
|
||||
6. [HTTPS Client](06-https-client) — HTTPSサイトへのリクエスト
|
||||
7. [HTTPS Server](07-https-server) — HTTPSサーバーの構築
|
||||
8. [WebSocket](08-websocket) — WebSocket通信の基本
|
||||
9. [What's Next](09-whats-next) — さらなる機能の紹介
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
OUT_DIR="$SCRIPT_DIR/build/desktop-app"
|
||||
DOCS_DIR="$SCRIPT_DIR/../../docs-src/pages/ja/llm-app"
|
||||
|
||||
source "$SCRIPT_DIR/extract_code.sh"
|
||||
|
||||
echo "=== Setting up Desktop App (Chapter 6) ==="
|
||||
|
||||
mkdir -p "$OUT_DIR"/{src,public}
|
||||
cd "$OUT_DIR"
|
||||
|
||||
# --- Extract source files from book ---
|
||||
echo "Extracting source from book..."
|
||||
CH05="$DOCS_DIR/ch05-web-ui.md"
|
||||
CH06="$DOCS_DIR/ch06-desktop-app.md"
|
||||
|
||||
extract_code "$CH06" "CMakeLists.txt" > CMakeLists.txt
|
||||
extract_code "$CH06" "main.cpp" > src/main.cpp
|
||||
extract_code "$CH05" "index.html" > public/index.html
|
||||
extract_code "$CH05" "style.css" > public/style.css
|
||||
extract_code "$CH05" "script.js" > public/script.js
|
||||
|
||||
# --- Build ---
|
||||
echo "Building..."
|
||||
cmake -B build 2>&1 | tail -1
|
||||
cmake --build build -j 2>&1 | tail -1
|
||||
|
||||
echo ""
|
||||
echo "=== Done ==="
|
||||
echo "Run: cd $OUT_DIR && ./build/translate-app"
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
OUT_DIR="$SCRIPT_DIR/build/web-app"
|
||||
DOCS_DIR="$SCRIPT_DIR/../../docs-src/pages/ja/llm-app"
|
||||
|
||||
source "$SCRIPT_DIR/extract_code.sh"
|
||||
|
||||
echo "=== Setting up Web App (Chapter 5) ==="
|
||||
|
||||
mkdir -p "$OUT_DIR"/{src,public}
|
||||
cd "$OUT_DIR"
|
||||
|
||||
# --- Extract source files from book ---
|
||||
echo "Extracting source from book..."
|
||||
CH04="$DOCS_DIR/ch04-model-management.md"
|
||||
CH05="$DOCS_DIR/ch05-web-ui.md"
|
||||
|
||||
extract_code "$CH04" "CMakeLists.txt" > CMakeLists.txt
|
||||
extract_code "$CH05" "main.cpp" > src/main.cpp
|
||||
extract_code "$CH05" "index.html" > public/index.html
|
||||
extract_code "$CH05" "style.css" > public/style.css
|
||||
extract_code "$CH05" "script.js" > public/script.js
|
||||
|
||||
# --- Build ---
|
||||
echo "Building..."
|
||||
cmake -B build 2>&1 | tail -1
|
||||
cmake --build build -j 2>&1 | tail -1
|
||||
|
||||
echo ""
|
||||
echo "=== Done ==="
|
||||
echo "Run: cd $OUT_DIR && ./build/translate-server"
|
||||
@@ -0,0 +1,18 @@
|
||||
# Extract code block from a <details> section identified by data-file attribute.
|
||||
# Usage: extract_code <file> <data-file>
|
||||
# Example: extract_code ch01.md "main.cpp"
|
||||
extract_code() {
|
||||
local file="$1" name="$2"
|
||||
local output
|
||||
output=$(awk -v name="$name" '
|
||||
$0 ~ "data-file=\"" name "\"" { found=1; next }
|
||||
found && /^```/ && !inside { inside=1; next }
|
||||
inside && /^```/ { exit }
|
||||
inside { print }
|
||||
' "$file")
|
||||
if [ -z "$output" ]; then
|
||||
echo "ERROR: extract_code: no match for data-file=\"$name\" in $file" >&2
|
||||
return 1
|
||||
fi
|
||||
printf '%s\n' "$output"
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
#!/bin/bash
|
||||
# Generate the desktop app project by extracting source from the cpp-httplib book.
|
||||
# Usage: generate_desktop_app_project.sh <output-dir>
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
OUT_DIR="${1:?Usage: $0 <output-dir>}"
|
||||
|
||||
BASE_URL="https://raw.githubusercontent.com/yhirose/cpp-httplib/master/docs-src/pages/ja/llm-app"
|
||||
CACHE_DIR="$SCRIPT_DIR/.cache"
|
||||
|
||||
source "$SCRIPT_DIR/extract_code.sh"
|
||||
|
||||
# --- Helper: download markdown files (always fetch latest) ---
|
||||
fetch_md() {
|
||||
local name="$1"
|
||||
local path="$CACHE_DIR/$name"
|
||||
curl -sfL "$BASE_URL/$name" -o "$path" || { echo "ERROR: Failed to download $name" >&2; return 1; }
|
||||
echo "$path"
|
||||
}
|
||||
|
||||
# --- Main ---
|
||||
echo "=== Generating desktop app project ==="
|
||||
|
||||
mkdir -p "$CACHE_DIR" "$OUT_DIR/src" "$OUT_DIR/public"
|
||||
|
||||
CH05=$(fetch_md "ch05-web-ui.md")
|
||||
CH06=$(fetch_md "ch06-desktop-app.md")
|
||||
|
||||
echo "Extracting source files..."
|
||||
extract_code "$CH06" "CMakeLists.txt" > "$OUT_DIR/CMakeLists.txt"
|
||||
extract_code "$CH06" "main.cpp" > "$OUT_DIR/src/main.cpp"
|
||||
extract_code "$CH05" "index.html" > "$OUT_DIR/public/index.html"
|
||||
extract_code "$CH05" "style.css" > "$OUT_DIR/public/style.css"
|
||||
extract_code "$CH05" "script.js" > "$OUT_DIR/public/script.js"
|
||||
|
||||
echo "=== Done ==="
|
||||
echo "Generated files in: $OUT_DIR"
|
||||
@@ -0,0 +1,28 @@
|
||||
# List available targets
|
||||
default:
|
||||
@just --list
|
||||
|
||||
# Remove build artifacts
|
||||
clean:
|
||||
rm -rf build
|
||||
|
||||
# Test the Book
|
||||
test-book:
|
||||
bash test_book.sh
|
||||
|
||||
# Build Web App (Chapter 5)
|
||||
build-web-app:
|
||||
bash build_web_app.sh
|
||||
|
||||
# Stop any running server, then run Web App
|
||||
run-web-app: build-web-app
|
||||
-lsof -ti :8080 | xargs kill 2>/dev/null
|
||||
cd build/web-app && ./build/translate-server
|
||||
|
||||
# Build Desktop App (Chapter 6)
|
||||
build-desktop-app:
|
||||
bash build_desktop_app.sh
|
||||
|
||||
# Run Desktop App
|
||||
run-desktop-app: build-desktop-app
|
||||
cd build/desktop-app && ./build/translate-app
|
||||
Executable
+561
@@ -0,0 +1,561 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# =============================================================================
|
||||
# test_book.sh — LLM App Tutorial (Ch1–Ch5) E2E Test
|
||||
#
|
||||
# Code is extracted from the doc markdown files (<!-- test:full-code --> and
|
||||
# <!-- test:cmake --> markers), so tests always stay in sync with the docs.
|
||||
# =============================================================================
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
||||
DOCS_DIR="$PROJECT_ROOT/docs-src/pages/ja/llm-app"
|
||||
WORKDIR=$(mktemp -d)
|
||||
MODEL_NAME="gemma-2-2b-it-Q4_K_M.gguf"
|
||||
MODEL_URL="https://huggingface.co/bartowski/gemma-2-2b-it-GGUF/resolve/main/${MODEL_NAME}"
|
||||
PORT=18080
|
||||
GECKODRIVER_PORT=4444
|
||||
SERVER_PID=""
|
||||
GECKODRIVER_PID=""
|
||||
PASS_COUNT=0
|
||||
FAIL_COUNT=0
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Cleanup
|
||||
# ---------------------------------------------------------------------------
|
||||
cleanup() {
|
||||
if [[ -n "$SERVER_PID" ]]; then
|
||||
kill "$SERVER_PID" 2>/dev/null || true
|
||||
wait "$SERVER_PID" 2>/dev/null || true
|
||||
fi
|
||||
if [[ -n "$GECKODRIVER_PID" ]]; then
|
||||
kill "$GECKODRIVER_PID" 2>/dev/null || true
|
||||
wait "$GECKODRIVER_PID" 2>/dev/null || true
|
||||
fi
|
||||
rm -rf "$WORKDIR"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
log() { echo "=== $*"; }
|
||||
pass() { echo " PASS: $*"; PASS_COUNT=$((PASS_COUNT + 1)); }
|
||||
fail() { echo " FAIL: $*"; FAIL_COUNT=$((FAIL_COUNT + 1)); }
|
||||
|
||||
source "$SCRIPT_DIR/extract_code.sh"
|
||||
|
||||
wait_for_server() {
|
||||
local max_wait=30
|
||||
local i=0
|
||||
while ! curl -s -o /dev/null "http://127.0.0.1:${PORT}/" 2>/dev/null; do
|
||||
sleep 1
|
||||
i=$((i + 1))
|
||||
if [[ $i -ge $max_wait ]]; then
|
||||
fail "Server did not start within ${max_wait}s"
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
stop_server() {
|
||||
if [[ -n "$SERVER_PID" ]]; then
|
||||
kill "$SERVER_PID" 2>/dev/null || true
|
||||
wait "$SERVER_PID" 2>/dev/null || true
|
||||
SERVER_PID=""
|
||||
fi
|
||||
}
|
||||
|
||||
# Make an HTTP request and capture status + body
|
||||
# Usage: http_request METHOD PATH [DATA]
|
||||
# Sets: HTTP_STATUS, HTTP_BODY
|
||||
http_request() {
|
||||
local method="$1" path="$2" data="${3:-}"
|
||||
local tmp
|
||||
tmp=$(mktemp)
|
||||
if [[ -n "$data" ]]; then
|
||||
HTTP_STATUS=$(curl -s -o "$tmp" -w '%{http_code}' \
|
||||
-X "$method" "http://127.0.0.1:${PORT}${path}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$data")
|
||||
else
|
||||
HTTP_STATUS=$(curl -s -o "$tmp" -w '%{http_code}' \
|
||||
-X "$method" "http://127.0.0.1:${PORT}${path}")
|
||||
fi
|
||||
HTTP_BODY=$(cat "$tmp")
|
||||
rm -f "$tmp"
|
||||
}
|
||||
|
||||
# Make an SSE request and capture the raw stream
|
||||
# Usage: http_sse PATH DATA
|
||||
# Sets: HTTP_STATUS, HTTP_BODY
|
||||
http_sse() {
|
||||
local path="$1" data="$2"
|
||||
HTTP_SSE_FILE=$(mktemp)
|
||||
HTTP_STATUS=$(curl -s -N -o "$HTTP_SSE_FILE" -w '%{http_code}' \
|
||||
-X POST "http://127.0.0.1:${PORT}${path}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$data")
|
||||
HTTP_BODY=$(cat "$HTTP_SSE_FILE")
|
||||
rm -f "$HTTP_SSE_FILE"
|
||||
}
|
||||
|
||||
assert_status() {
|
||||
local expected="$1" label="$2"
|
||||
if [[ "$HTTP_STATUS" == "$expected" ]]; then
|
||||
pass "$label (status=$HTTP_STATUS)"
|
||||
else
|
||||
fail "$label (expected=$expected, got=$HTTP_STATUS)"
|
||||
echo " body: $HTTP_BODY"
|
||||
fi
|
||||
}
|
||||
|
||||
assert_json_field() {
|
||||
local field="$1" label="$2"
|
||||
if echo "$HTTP_BODY" | python3 -c "import sys,json; d=json.load(sys.stdin); assert '$field' in d" 2>/dev/null; then
|
||||
pass "$label (field '$field' exists)"
|
||||
else
|
||||
fail "$label (field '$field' missing in response)"
|
||||
echo " body: $HTTP_BODY"
|
||||
fi
|
||||
}
|
||||
|
||||
assert_json_value() {
|
||||
local field="$1" expected="$2" label="$3"
|
||||
local actual
|
||||
actual=$(echo "$HTTP_BODY" | python3 -c "import sys,json; print(json.load(sys.stdin)['$field'])" 2>/dev/null || echo "")
|
||||
if [[ "$actual" == "$expected" ]]; then
|
||||
pass "$label ($field='$actual')"
|
||||
else
|
||||
fail "$label (expected $field='$expected', got='$actual')"
|
||||
fi
|
||||
}
|
||||
|
||||
assert_json_nonempty() {
|
||||
local field="$1" label="$2"
|
||||
local val
|
||||
val=$(echo "$HTTP_BODY" | python3 -c "import sys,json; v=json.load(sys.stdin)['$field']; assert len(str(v))>0; print(v)" 2>/dev/null || echo "")
|
||||
if [[ -n "$val" ]]; then
|
||||
pass "$label ($field is non-empty)"
|
||||
else
|
||||
fail "$label ($field is empty or missing)"
|
||||
echo " body: $HTTP_BODY"
|
||||
fi
|
||||
}
|
||||
|
||||
# Patch port number in extracted source code (8080 -> test port)
|
||||
patch_port() {
|
||||
sed "s/127\.0\.0\.1\", 8080/127.0.0.1\", ${PORT}/g; s/127\.0\.0\.1:8080/127.0.0.1:${PORT}/g"
|
||||
}
|
||||
|
||||
# Patch model path in extracted source code
|
||||
patch_model() {
|
||||
sed "s|models/gemma-2-2b-it-Q4_K_M.gguf|models/${MODEL_NAME}|g"
|
||||
}
|
||||
|
||||
# =============================================================================
|
||||
# Ch1: Skeleton Server
|
||||
# =============================================================================
|
||||
test_ch1() {
|
||||
log "Ch1: Project Setup & Skeleton Server"
|
||||
|
||||
local APP_DIR="$WORKDIR/translate-app"
|
||||
mkdir -p "$APP_DIR/src" "$APP_DIR/models"
|
||||
cd "$APP_DIR"
|
||||
|
||||
# Copy httplib.h from project root (test current version)
|
||||
cp "$PROJECT_ROOT/httplib.h" .
|
||||
|
||||
# Download json.hpp into nlohmann/ directory to match #include <nlohmann/json.hpp>
|
||||
mkdir -p nlohmann
|
||||
curl -sL -o nlohmann/json.hpp \
|
||||
https://github.com/nlohmann/json/releases/latest/download/json.hpp
|
||||
|
||||
# CMakeLists.txt — ch1 doesn't need llama.cpp, so use a minimal version
|
||||
# (the doc's cmake includes llama.cpp which isn't cloned yet in ch1)
|
||||
cat > CMakeLists.txt << 'CMAKE_EOF'
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(translate-server LANGUAGES CXX)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
add_executable(translate-server src/main.cpp)
|
||||
target_include_directories(translate-server PRIVATE ${CMAKE_SOURCE_DIR})
|
||||
CMAKE_EOF
|
||||
|
||||
# Extract main.cpp from ch1 doc and patch port
|
||||
extract_code "$DOCS_DIR/ch01-setup.md" "main.cpp" | patch_port > src/main.cpp
|
||||
|
||||
# Build
|
||||
log "Ch1: Building..."
|
||||
cmake -B build -DCMAKE_BUILD_TYPE=Release 2>&1 | tail -1
|
||||
cmake --build build -j 2>&1 | tail -3
|
||||
|
||||
# Start server
|
||||
./build/translate-server &
|
||||
SERVER_PID=$!
|
||||
wait_for_server
|
||||
|
||||
# Tests
|
||||
http_request POST /translate '{"text":"hello","target_lang":"ja"}'
|
||||
assert_status 200 "Ch1 POST /translate"
|
||||
assert_json_value translation "TODO" "Ch1 POST /translate returns TODO"
|
||||
|
||||
http_request GET /models
|
||||
assert_status 200 "Ch1 GET /models"
|
||||
assert_json_field models "Ch1 GET /models"
|
||||
|
||||
http_request POST /models/select '{"model":"test"}'
|
||||
assert_status 200 "Ch1 POST /models/select"
|
||||
assert_json_value status "TODO" "Ch1 POST /models/select returns TODO"
|
||||
|
||||
stop_server
|
||||
log "Ch1: Done"
|
||||
}
|
||||
|
||||
# =============================================================================
|
||||
# Ch2: REST API with llama.cpp
|
||||
# =============================================================================
|
||||
test_ch2() {
|
||||
log "Ch2: REST API with llama.cpp"
|
||||
|
||||
local APP_DIR="$WORKDIR/translate-app"
|
||||
cd "$APP_DIR"
|
||||
|
||||
# Clone llama.cpp
|
||||
if [[ ! -d llama.cpp ]]; then
|
||||
log "Ch2: Cloning llama.cpp..."
|
||||
git clone --depth 1 https://github.com/ggml-org/llama.cpp.git 2>&1 | tail -1
|
||||
fi
|
||||
|
||||
# Download cpp-llamalib.h
|
||||
if [[ ! -f cpp-llamalib.h ]]; then
|
||||
curl -sL -o cpp-llamalib.h \
|
||||
https://raw.githubusercontent.com/yhirose/cpp-llamalib/main/cpp-llamalib.h
|
||||
fi
|
||||
|
||||
# Download model
|
||||
if [[ ! -f "models/$MODEL_NAME" ]]; then
|
||||
log "Ch2: Downloading model ${MODEL_NAME} (~1.6GB)..."
|
||||
curl -L -o "models/$MODEL_NAME" "$MODEL_URL"
|
||||
fi
|
||||
|
||||
# CMakeLists.txt from ch1 doc (includes llama.cpp)
|
||||
extract_code "$DOCS_DIR/ch01-setup.md" "CMakeLists.txt" > CMakeLists.txt
|
||||
|
||||
# Extract main.cpp from ch2 doc and patch port + model path
|
||||
extract_code "$DOCS_DIR/ch02-rest-api.md" "main.cpp" | patch_port | patch_model > src/main.cpp
|
||||
|
||||
# Build (clean rebuild needed — cmake config changed)
|
||||
log "Ch2: Building (this may take a while for llama.cpp)..."
|
||||
rm -rf build
|
||||
cmake -B build -DCMAKE_BUILD_TYPE=Release 2>&1 | tail -1
|
||||
cmake --build build -j 2>&1 | tail -3
|
||||
|
||||
# Start server
|
||||
./build/translate-server &
|
||||
SERVER_PID=$!
|
||||
wait_for_server
|
||||
|
||||
# Tests — normal request
|
||||
http_request POST /translate \
|
||||
'{"text":"I had a great time visiting Tokyo last spring. The cherry blossoms were beautiful.","target_lang":"ja"}'
|
||||
assert_status 200 "Ch2 POST /translate normal"
|
||||
assert_json_nonempty translation "Ch2 POST /translate has translation"
|
||||
|
||||
# Tests — invalid JSON
|
||||
http_request POST /translate 'not json'
|
||||
assert_status 400 "Ch2 POST /translate invalid JSON"
|
||||
|
||||
# Tests — missing text
|
||||
http_request POST /translate '{"target_lang":"ja"}'
|
||||
assert_status 400 "Ch2 POST /translate missing text"
|
||||
|
||||
# Tests — empty text
|
||||
http_request POST /translate '{"text":""}'
|
||||
assert_status 400 "Ch2 POST /translate empty text"
|
||||
|
||||
stop_server
|
||||
log "Ch2: Done"
|
||||
}
|
||||
|
||||
# =============================================================================
|
||||
# Ch3: SSE Streaming
|
||||
# =============================================================================
|
||||
test_ch3() {
|
||||
log "Ch3: SSE Streaming"
|
||||
|
||||
local APP_DIR="$WORKDIR/translate-app"
|
||||
cd "$APP_DIR"
|
||||
|
||||
# Extract main.cpp from ch3 doc and patch port + model path
|
||||
extract_code "$DOCS_DIR/ch03-sse-streaming.md" "main.cpp" | patch_port | patch_model > src/main.cpp
|
||||
|
||||
# Build (incremental — only main.cpp changed)
|
||||
log "Ch3: Building..."
|
||||
cmake --build build -j 2>&1 | tail -3
|
||||
|
||||
# Start server
|
||||
./build/translate-server &
|
||||
SERVER_PID=$!
|
||||
wait_for_server
|
||||
|
||||
# Tests — /translate still works
|
||||
http_request POST /translate \
|
||||
'{"text":"Hello world","target_lang":"ja"}'
|
||||
assert_status 200 "Ch3 POST /translate still works"
|
||||
|
||||
# Tests — SSE streaming
|
||||
http_sse /translate/stream \
|
||||
'{"text":"I had a great time visiting Tokyo last spring. The cherry blossoms were beautiful.","target_lang":"ja"}'
|
||||
assert_status 200 "Ch3 POST /translate/stream status"
|
||||
|
||||
# Check SSE format: has data: lines and ends with [DONE]
|
||||
local data_lines
|
||||
data_lines=$(echo "$HTTP_BODY" | grep -c '^data: ' || true)
|
||||
if [[ $data_lines -ge 2 ]]; then
|
||||
pass "Ch3 SSE has multiple data: lines ($data_lines)"
|
||||
else
|
||||
fail "Ch3 SSE expected multiple data: lines, got $data_lines"
|
||||
echo " body: $HTTP_BODY"
|
||||
fi
|
||||
|
||||
if echo "$HTTP_BODY" | grep -q 'data: \[DONE\]'; then
|
||||
pass "Ch3 SSE ends with data: [DONE]"
|
||||
else
|
||||
fail "Ch3 SSE missing data: [DONE]"
|
||||
echo " body: $HTTP_BODY"
|
||||
fi
|
||||
|
||||
# Tests — SSE invalid JSON
|
||||
http_sse /translate/stream 'not json'
|
||||
assert_status 400 "Ch3 POST /translate/stream invalid JSON"
|
||||
|
||||
stop_server
|
||||
log "Ch3: Done"
|
||||
}
|
||||
|
||||
# =============================================================================
|
||||
# Ch4: Model Management
|
||||
# =============================================================================
|
||||
test_ch4() {
|
||||
log "Ch4: Model Management"
|
||||
|
||||
local APP_DIR="$WORKDIR/translate-app"
|
||||
cd "$APP_DIR"
|
||||
|
||||
# Ch4+ uses ~/.translate-app/models/ — symlink model there
|
||||
local MODELS_HOME="$HOME/.translate-app/models"
|
||||
mkdir -p "$MODELS_HOME"
|
||||
ln -sf "$APP_DIR/models/$MODEL_NAME" "$MODELS_HOME/$MODEL_NAME"
|
||||
|
||||
# CMakeLists.txt from ch4 (adds OpenSSL)
|
||||
extract_code "$DOCS_DIR/ch04-model-management.md" "CMakeLists.txt" > CMakeLists.txt
|
||||
|
||||
# Extract main.cpp from ch4 doc
|
||||
extract_code "$DOCS_DIR/ch04-model-management.md" "main.cpp" | patch_port > src/main.cpp
|
||||
|
||||
# Build (reconfigure for OpenSSL, incremental — reuses llama.cpp objects)
|
||||
log "Ch4: Building..."
|
||||
cmake -B build -DCMAKE_BUILD_TYPE=Release 2>&1 | tail -1
|
||||
cmake --build build -j 2>&1 | tail -3
|
||||
|
||||
# Start server
|
||||
./build/translate-server &
|
||||
SERVER_PID=$!
|
||||
wait_for_server
|
||||
|
||||
# Tests — GET /models
|
||||
http_request GET /models
|
||||
assert_status 200 "Ch4 GET /models"
|
||||
assert_json_field models "Ch4 GET /models has models array"
|
||||
|
||||
# デフォルトモデルがdownloaded+selectedであること
|
||||
local selected
|
||||
selected=$(echo "$HTTP_BODY" | python3 -c "
|
||||
import sys, json
|
||||
models = json.load(sys.stdin)['models']
|
||||
sel = [m for m in models if m['selected']]
|
||||
print(sel[0]['downloaded'] if sel else '')
|
||||
" 2>/dev/null || echo "")
|
||||
if [[ "$selected" == "True" ]]; then
|
||||
pass "Ch4 GET /models default model is downloaded and selected"
|
||||
else
|
||||
fail "Ch4 GET /models default model state unexpected"
|
||||
echo " body: $HTTP_BODY"
|
||||
fi
|
||||
|
||||
# Tests — POST /models/select with already-downloaded model (SSE)
|
||||
http_sse /models/select '{"model": "gemma-2-2b-it"}'
|
||||
assert_status 200 "Ch4 POST /models/select already downloaded"
|
||||
|
||||
if echo "$HTTP_BODY" | grep -q '"ready"'; then
|
||||
pass "Ch4 POST /models/select returns ready"
|
||||
else
|
||||
fail "Ch4 POST /models/select missing ready status"
|
||||
echo " body: $HTTP_BODY"
|
||||
fi
|
||||
|
||||
# Tests — POST /models/select unknown model
|
||||
http_request POST /models/select '{"model": "nonexistent"}'
|
||||
assert_status 404 "Ch4 POST /models/select unknown model"
|
||||
|
||||
# Tests — POST /models/select missing model field
|
||||
http_request POST /models/select '{"foo": "bar"}'
|
||||
assert_status 400 "Ch4 POST /models/select missing model field"
|
||||
|
||||
# Tests — /translate still works after model select
|
||||
http_request POST /translate '{"text": "Hello", "target_lang": "ja"}'
|
||||
assert_status 200 "Ch4 POST /translate still works"
|
||||
assert_json_nonempty translation "Ch4 POST /translate has translation"
|
||||
|
||||
# Tests — switch model via symlink (avoids downloading a second model)
|
||||
# Place a symlink so the server sees Llama-3.1-8B-Instruct as "downloaded"
|
||||
ln -sf "$MODELS_HOME/$MODEL_NAME" "$MODELS_HOME/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf"
|
||||
|
||||
http_sse /models/select '{"model": "Llama-3.1-8B-Instruct"}'
|
||||
assert_status 200 "Ch4 POST /models/select switch model"
|
||||
|
||||
if echo "$HTTP_BODY" | grep -q '"ready"'; then
|
||||
pass "Ch4 model switch returns ready"
|
||||
else
|
||||
fail "Ch4 model switch missing ready"
|
||||
echo " body: $HTTP_BODY"
|
||||
fi
|
||||
|
||||
# Translate with the switched model
|
||||
http_request POST /translate \
|
||||
'{"text": "The weather is nice today.", "target_lang": "ja"}'
|
||||
assert_status 200 "Ch4 POST /translate after model switch"
|
||||
assert_json_nonempty translation "Ch4 POST /translate switched model has translation"
|
||||
|
||||
# Verify model list reflects the switch
|
||||
http_request GET /models
|
||||
local new_selected
|
||||
new_selected=$(echo "$HTTP_BODY" | python3 -c "
|
||||
import sys, json
|
||||
models = json.load(sys.stdin)['models']
|
||||
sel = [m for m in models if m['selected']]
|
||||
print(sel[0]['name'] if sel else '')
|
||||
" 2>/dev/null || echo "")
|
||||
if [[ "$new_selected" == "Llama-3.1-8B-Instruct" ]]; then
|
||||
pass "Ch4 GET /models reflects model switch"
|
||||
else
|
||||
fail "Ch4 GET /models expected Llama-3.1-8B-Instruct selected, got '$new_selected'"
|
||||
fi
|
||||
|
||||
stop_server
|
||||
log "Ch4: Done"
|
||||
}
|
||||
|
||||
# =============================================================================
|
||||
# Ch5: Web UI (browser tests via geckodriver + webdriver.h)
|
||||
# =============================================================================
|
||||
|
||||
start_geckodriver() {
|
||||
geckodriver --port "$GECKODRIVER_PORT" &>/dev/null &
|
||||
GECKODRIVER_PID=$!
|
||||
# Wait for geckodriver to be ready
|
||||
local i=0
|
||||
while ! curl -s -o /dev/null "http://127.0.0.1:${GECKODRIVER_PORT}/status" 2>/dev/null; do
|
||||
sleep 0.5
|
||||
i=$((i + 1))
|
||||
if [[ $i -ge 20 ]]; then
|
||||
fail "geckodriver did not start within 10s"
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
stop_geckodriver() {
|
||||
if [[ -n "$GECKODRIVER_PID" ]]; then
|
||||
kill "$GECKODRIVER_PID" 2>/dev/null || true
|
||||
wait "$GECKODRIVER_PID" 2>/dev/null || true
|
||||
GECKODRIVER_PID=""
|
||||
fi
|
||||
}
|
||||
|
||||
test_ch5() {
|
||||
log "Ch5: Web UI (browser tests)"
|
||||
|
||||
# Check for geckodriver
|
||||
if ! command -v geckodriver &>/dev/null; then
|
||||
log "Ch5: Skipping browser tests (geckodriver not found)"
|
||||
log "Ch5: Install with: brew install geckodriver"
|
||||
return 0
|
||||
fi
|
||||
|
||||
local APP_DIR="$WORKDIR/translate-app"
|
||||
cd "$APP_DIR"
|
||||
|
||||
# Extract source files from ch05
|
||||
extract_code "$DOCS_DIR/ch05-web-ui.md" "main.cpp" \
|
||||
| patch_port > src/main.cpp
|
||||
|
||||
mkdir -p public
|
||||
extract_code "$DOCS_DIR/ch05-web-ui.md" "index.html" > public/index.html
|
||||
extract_code "$DOCS_DIR/ch05-web-ui.md" "style.css" > public/style.css
|
||||
extract_code "$DOCS_DIR/ch05-web-ui.md" "script.js" > public/script.js
|
||||
|
||||
# Build (incremental — only main.cpp changed)
|
||||
log "Ch5: Building server..."
|
||||
cmake --build build -j 2>&1 | tail -3
|
||||
|
||||
# Build browser test program
|
||||
log "Ch5: Building browser test..."
|
||||
g++ -std=c++17 \
|
||||
-I"$APP_DIR" \
|
||||
-I"$SCRIPT_DIR" \
|
||||
-o "$APP_DIR/build/test_webui" \
|
||||
"$SCRIPT_DIR/test_webui.cpp" \
|
||||
-pthread
|
||||
|
||||
# Start server
|
||||
./build/translate-server &
|
||||
SERVER_PID=$!
|
||||
wait_for_server
|
||||
|
||||
# Start geckodriver
|
||||
start_geckodriver
|
||||
|
||||
# Run browser tests
|
||||
log "Ch5: Running browser tests..."
|
||||
local test_exit=0
|
||||
"$APP_DIR/build/test_webui" "$PORT" || test_exit=$?
|
||||
|
||||
# Parse pass/fail from test output and add to totals
|
||||
# (test_webui prints its own pass/fail, but we track via exit code)
|
||||
if [[ $test_exit -ne 0 ]]; then
|
||||
fail "Ch5 browser tests had failures"
|
||||
else
|
||||
pass "Ch5 browser tests all passed"
|
||||
fi
|
||||
|
||||
stop_geckodriver
|
||||
stop_server
|
||||
log "Ch5: Done"
|
||||
}
|
||||
|
||||
# =============================================================================
|
||||
# Main
|
||||
# =============================================================================
|
||||
|
||||
log "LLM App Tutorial E2E Test"
|
||||
log "Working directory: $WORKDIR"
|
||||
echo ""
|
||||
|
||||
test_ch1
|
||||
echo ""
|
||||
test_ch2
|
||||
echo ""
|
||||
test_ch3
|
||||
echo ""
|
||||
test_ch4
|
||||
echo ""
|
||||
test_ch5
|
||||
|
||||
log "Results: $PASS_COUNT passed, $FAIL_COUNT failed"
|
||||
|
||||
if [[ $FAIL_COUNT -gt 0 ]]; then
|
||||
exit 1
|
||||
fi
|
||||
@@ -0,0 +1,300 @@
|
||||
// test_webui.cpp — Browser-based E2E tests for Ch5 Web UI.
|
||||
// Uses webdriver.h (cpp-httplib + json.hpp) to control headless Firefox.
|
||||
//
|
||||
// Usage: test_webui <port>
|
||||
// port: the translate-server port (e.g. 18080)
|
||||
|
||||
#include "webdriver.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
// ─── Test framework (minimal) ────────────────────────────────
|
||||
|
||||
static int pass_count = 0;
|
||||
static int fail_count = 0;
|
||||
|
||||
#define PASS(label) \
|
||||
do { \
|
||||
std::cout << " PASS: " << (label) << "\n"; \
|
||||
++pass_count; \
|
||||
} while (0)
|
||||
|
||||
#define FAIL(label, detail) \
|
||||
do { \
|
||||
std::cout << " FAIL: " << (label) << "\n"; \
|
||||
std::cout << " " << (detail) << "\n"; \
|
||||
++fail_count; \
|
||||
} while (0)
|
||||
|
||||
#define ASSERT_TRUE(cond, label) \
|
||||
do { \
|
||||
if (cond) { \
|
||||
PASS(label); \
|
||||
} else { \
|
||||
FAIL(label, "condition was false"); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define ASSERT_CONTAINS(haystack, needle, label) \
|
||||
do { \
|
||||
if (std::string(haystack).find(needle) != std::string::npos) { \
|
||||
PASS(label); \
|
||||
} else { \
|
||||
FAIL(label, "'" + std::string(haystack) + "' does not contain '" + \
|
||||
std::string(needle) + "'"); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define ASSERT_ELEMENT_EXISTS(session, selector) \
|
||||
do { \
|
||||
try { \
|
||||
(session).css(selector); \
|
||||
PASS("Element " selector " exists"); \
|
||||
} catch (...) { FAIL("Element " selector " exists", "not found"); } \
|
||||
} while (0)
|
||||
|
||||
// ─── Helpers ─────────────────────────────────────────────────
|
||||
|
||||
static std::string base_url;
|
||||
|
||||
void navigate_and_wait_for_models(webdriver::Session &session) {
|
||||
session.navigate(base_url);
|
||||
session.wait_until(
|
||||
"return document.querySelectorAll('#model-select option').length > 0",
|
||||
5000);
|
||||
}
|
||||
|
||||
void test_page_loads(webdriver::Session &session) {
|
||||
std::cout << "=== TC1: Page loads with correct structure\n";
|
||||
|
||||
session.navigate(base_url);
|
||||
|
||||
auto title = session.title();
|
||||
ASSERT_CONTAINS(title, "Translate", "Page title contains 'Translate'");
|
||||
|
||||
// Verify main DOM elements exist
|
||||
ASSERT_ELEMENT_EXISTS(session, "#model-select");
|
||||
ASSERT_ELEMENT_EXISTS(session, "#input-text");
|
||||
ASSERT_ELEMENT_EXISTS(session, "#output-text");
|
||||
ASSERT_ELEMENT_EXISTS(session, "#target-lang");
|
||||
}
|
||||
|
||||
void test_model_dropdown(webdriver::Session &session) {
|
||||
std::cout << "=== TC2: Model dropdown is populated\n";
|
||||
|
||||
navigate_and_wait_for_models(session);
|
||||
|
||||
// Note: WebDriver findElements cannot find <option> elements directly
|
||||
// in geckodriver/Firefox, so we use JS to count them.
|
||||
auto option_count = session.execute_script(
|
||||
"return document.querySelectorAll('#model-select option').length");
|
||||
ASSERT_TRUE(option_count != "0" && option_count != "null",
|
||||
"Model dropdown has options (count=" + option_count + ")");
|
||||
|
||||
// Check that at least one option has a selected attribute
|
||||
auto selected_val = session.execute_script(
|
||||
"return document.querySelector('#model-select').value");
|
||||
ASSERT_TRUE(selected_val != "null" && !selected_val.empty(),
|
||||
"A model is selected (value='" + selected_val + "')");
|
||||
}
|
||||
|
||||
void test_translation_sse(webdriver::Session &session) {
|
||||
std::cout << "=== TC3: Translation with SSE streaming\n";
|
||||
|
||||
navigate_and_wait_for_models(session);
|
||||
|
||||
// Clear and type input — debounce auto-translate triggers after 300ms
|
||||
auto input = session.css("#input-text");
|
||||
input.clear();
|
||||
input.send_keys("Hello world");
|
||||
|
||||
// Wait for output to appear (debounce 300ms + LLM inference)
|
||||
bool has_output = session.wait_until(
|
||||
"return document.querySelector('#output-text').textContent.length > 0",
|
||||
120000);
|
||||
ASSERT_TRUE(has_output, "Translation output appeared");
|
||||
|
||||
auto output_text = session.execute_script(
|
||||
"return document.querySelector('#output-text').textContent");
|
||||
ASSERT_TRUE(!output_text.empty() && output_text != "null",
|
||||
"Output text is non-empty ('" + output_text.substr(0, 50) +
|
||||
"...')");
|
||||
|
||||
// Wait for busy state to be cleared after completion
|
||||
bool busy_cleared = session.wait_until(
|
||||
"return !document.body.classList.contains('busy')", 120000);
|
||||
ASSERT_TRUE(busy_cleared, "Busy state cleared after translation");
|
||||
}
|
||||
|
||||
void test_busy_state(webdriver::Session &session) {
|
||||
std::cout << "=== TC4: Busy state during translation\n";
|
||||
|
||||
navigate_and_wait_for_models(session);
|
||||
|
||||
auto input = session.css("#input-text");
|
||||
input.clear();
|
||||
|
||||
// Clear previous output
|
||||
session.execute_script(
|
||||
"document.querySelector('#output-text').textContent = ''");
|
||||
|
||||
input.send_keys(
|
||||
"I had a great time visiting Tokyo last spring. "
|
||||
"The cherry blossoms were beautiful and the food was amazing.");
|
||||
|
||||
// Check busy state (debounce 300ms then translation starts)
|
||||
bool went_busy = session.wait_until(
|
||||
"return document.body.classList.contains('busy')", 5000);
|
||||
ASSERT_TRUE(went_busy, "Body gets 'busy' class during translation");
|
||||
|
||||
// Wait for completion
|
||||
session.wait_until("return !document.body.classList.contains('busy')",
|
||||
120000);
|
||||
PASS("Busy class removed after completion");
|
||||
}
|
||||
|
||||
void test_empty_input(webdriver::Session &session) {
|
||||
std::cout << "=== TC5: Empty input does nothing\n";
|
||||
|
||||
navigate_and_wait_for_models(session);
|
||||
|
||||
// Clear input and output
|
||||
auto input = session.css("#input-text");
|
||||
input.clear();
|
||||
session.execute_script(
|
||||
"document.querySelector('#output-text').textContent = ''");
|
||||
|
||||
// Trigger input event on empty textarea
|
||||
session.execute_script("document.querySelector('#input-text').dispatchEvent("
|
||||
" new Event('input'));");
|
||||
|
||||
// Wait longer than debounce (300ms) — nothing should happen
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
|
||||
auto output_text = session.execute_script(
|
||||
"return document.querySelector('#output-text').textContent");
|
||||
ASSERT_TRUE(output_text.empty() || output_text == "null" || output_text == "",
|
||||
"No output for empty input");
|
||||
}
|
||||
|
||||
void test_target_lang_selector(webdriver::Session &session) {
|
||||
std::cout << "=== TC6: Target language selector\n";
|
||||
|
||||
navigate_and_wait_for_models(session);
|
||||
|
||||
// Check available language options (use JS — WebDriver can't find <option>)
|
||||
auto lang_count = session.execute_script(
|
||||
"return document.querySelectorAll('#target-lang option').length");
|
||||
ASSERT_TRUE(lang_count != "0" && lang_count != "null",
|
||||
"Language selector has multiple options (count=" + lang_count +
|
||||
")");
|
||||
|
||||
// Switch to English and translate
|
||||
session.execute_script("document.querySelector('#target-lang').value = 'en';"
|
||||
"document.querySelector('#target-lang').dispatchEvent("
|
||||
" new Event('change'));");
|
||||
|
||||
// Clear output, then type — debounce auto-translate triggers
|
||||
session.execute_script(
|
||||
"document.querySelector('#output-text').textContent = ''");
|
||||
|
||||
auto input = session.css("#input-text");
|
||||
input.clear();
|
||||
input.send_keys("こんにちは");
|
||||
|
||||
bool has_output = session.wait_until(
|
||||
"return document.querySelector('#output-text').textContent.length > 0",
|
||||
120000);
|
||||
ASSERT_TRUE(has_output, "Translation with target_lang=en produced output");
|
||||
}
|
||||
|
||||
void test_model_switch(webdriver::Session &session) {
|
||||
std::cout << "=== TC7: Model switching\n";
|
||||
|
||||
navigate_and_wait_for_models(session);
|
||||
|
||||
auto options = session.css_all("#model-select option");
|
||||
if (options.size() < 2) {
|
||||
PASS("Model switch skipped (only 1 model available)");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get current model
|
||||
auto current = session.execute_script(
|
||||
"return document.querySelector('#model-select').value");
|
||||
|
||||
// Switch to a different model (pick the second option's value)
|
||||
auto other_value = options[1].attribute("value");
|
||||
if (other_value == current && options.size() > 2) {
|
||||
other_value = options[2].attribute("value");
|
||||
}
|
||||
|
||||
session.execute_script(
|
||||
"document.querySelector('#model-select').value = '" + other_value +
|
||||
"';"
|
||||
"document.querySelector('#model-select').dispatchEvent("
|
||||
" new Event('change'));");
|
||||
|
||||
// Wait for model switch to complete (SSE: downloading → loading → ready)
|
||||
bool ready = session.wait_until(
|
||||
"return !document.body.classList.contains('busy')", 120000);
|
||||
ASSERT_TRUE(ready, "Model switch completed");
|
||||
|
||||
auto new_value = session.execute_script(
|
||||
"return document.querySelector('#model-select').value");
|
||||
ASSERT_TRUE(new_value == other_value,
|
||||
"Model changed to '" + other_value + "'");
|
||||
}
|
||||
|
||||
void test_download_dialog_structure(webdriver::Session &session) {
|
||||
std::cout << "=== TC8: Download dialog DOM structure\n";
|
||||
|
||||
session.navigate(base_url);
|
||||
|
||||
ASSERT_ELEMENT_EXISTS(session, "#download-dialog");
|
||||
ASSERT_ELEMENT_EXISTS(session, "#download-progress");
|
||||
ASSERT_ELEMENT_EXISTS(session, "#download-status");
|
||||
ASSERT_ELEMENT_EXISTS(session, "#download-cancel");
|
||||
}
|
||||
|
||||
// ─── Main ────────────────────────────────────────────────────
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cerr << "Usage: test_webui <server-port>\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
int port = std::atoi(argv[1]);
|
||||
base_url = "http://127.0.0.1:" + std::to_string(port);
|
||||
|
||||
std::cout << "=== Ch5 Web UI Browser Tests\n";
|
||||
std::cout << "=== Server: " << base_url << "\n\n";
|
||||
|
||||
try {
|
||||
webdriver::Session session;
|
||||
|
||||
test_page_loads(session);
|
||||
test_model_dropdown(session);
|
||||
test_translation_sse(session);
|
||||
test_busy_state(session);
|
||||
test_empty_input(session);
|
||||
test_target_lang_selector(session);
|
||||
test_model_switch(session);
|
||||
test_download_dialog_structure(session);
|
||||
|
||||
} catch (const webdriver::Error &e) {
|
||||
std::cerr << "WebDriver error: " << e.what() << "\n";
|
||||
++fail_count;
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Error: " << e.what() << "\n";
|
||||
++fail_count;
|
||||
}
|
||||
|
||||
std::cout << "\n=== Results: " << pass_count << " passed, " << fail_count
|
||||
<< " failed\n";
|
||||
|
||||
return fail_count > 0 ? 1 : 0;
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
// webdriver.h — Thin W3C WebDriver client using cpp-httplib + nlohmann/json.
|
||||
// SPDX-License-Identifier: MIT
|
||||
//
|
||||
// Usage:
|
||||
// webdriver::Session session; // starts headless Firefox via geckodriver
|
||||
// session.navigate("http://localhost:8080");
|
||||
// auto el = session.css("h1");
|
||||
// assert(el.text() == "Hello!");
|
||||
// // session destructor closes the browser
|
||||
#pragma once
|
||||
|
||||
#include "httplib.h"
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace webdriver {
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
// ─── Errors ──────────────────────────────────────────────────
|
||||
|
||||
class Error : public std::runtime_error {
|
||||
public:
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
// ─── Forward declarations ────────────────────────────────────
|
||||
|
||||
class Session;
|
||||
|
||||
// ─── Element ─────────────────────────────────────────────────
|
||||
|
||||
class Element {
|
||||
friend class Session;
|
||||
|
||||
httplib::Client *cli_;
|
||||
std::string session_id_;
|
||||
std::string element_id_;
|
||||
|
||||
public:
|
||||
Element(httplib::Client *cli, const std::string &session_id,
|
||||
const std::string &element_id)
|
||||
: cli_(cli), session_id_(session_id), element_id_(element_id) {}
|
||||
|
||||
std::string url(const std::string &suffix = "") const {
|
||||
return "/session/" + session_id_ + "/element/" + element_id_ + suffix;
|
||||
}
|
||||
|
||||
public:
|
||||
std::string text() const {
|
||||
auto res = cli_->Get(url("/text"));
|
||||
if (!res || res->status != 200) {
|
||||
throw Error("Failed to get element text");
|
||||
}
|
||||
return json::parse(res->body)["value"].get<std::string>();
|
||||
}
|
||||
|
||||
std::string attribute(const std::string &name) const {
|
||||
auto res = cli_->Get(url("/attribute/" + name));
|
||||
if (!res || res->status != 200) { return ""; }
|
||||
auto val = json::parse(res->body)["value"];
|
||||
return val.is_null() ? "" : val.get<std::string>();
|
||||
}
|
||||
|
||||
std::string property(const std::string &name) const {
|
||||
auto res = cli_->Get(url("/property/" + name));
|
||||
if (!res || res->status != 200) { return ""; }
|
||||
auto val = json::parse(res->body)["value"];
|
||||
return val.is_null() ? "" : val.get<std::string>();
|
||||
}
|
||||
|
||||
void click() const {
|
||||
auto res = cli_->Post(url("/click"), "{}", "application/json");
|
||||
if (!res || res->status != 200) { throw Error("Failed to click element"); }
|
||||
}
|
||||
|
||||
void send_keys(const std::string &keys) const {
|
||||
json body = {{"text", keys}};
|
||||
auto res = cli_->Post(url("/value"), body.dump(), "application/json");
|
||||
if (!res || res->status != 200) {
|
||||
throw Error("Failed to send keys to element");
|
||||
}
|
||||
}
|
||||
|
||||
void clear() const {
|
||||
auto res = cli_->Post(url("/clear"), "{}", "application/json");
|
||||
if (!res || res->status != 200) { throw Error("Failed to clear element"); }
|
||||
}
|
||||
|
||||
std::string tag_name() const {
|
||||
auto res = cli_->Get(url("/name"));
|
||||
if (!res || res->status != 200) { throw Error("Failed to get tag name"); }
|
||||
return json::parse(res->body)["value"].get<std::string>();
|
||||
}
|
||||
|
||||
bool is_displayed() const {
|
||||
auto res = cli_->Get(url("/displayed"));
|
||||
if (!res || res->status != 200) { return false; }
|
||||
return json::parse(res->body)["value"].get<bool>();
|
||||
}
|
||||
};
|
||||
|
||||
// ─── Session ─────────────────────────────────────────────────
|
||||
|
||||
class Session {
|
||||
httplib::Client cli_;
|
||||
std::string session_id_;
|
||||
|
||||
// W3C WebDriver uses this key for element references
|
||||
static constexpr const char *ELEMENT_KEY =
|
||||
"element-6066-11e4-a52e-4f735466cecf";
|
||||
|
||||
std::string extract_element_id(const json &value) const {
|
||||
if (value.contains(ELEMENT_KEY)) {
|
||||
return value[ELEMENT_KEY].get<std::string>();
|
||||
}
|
||||
// Fallback: try "ELEMENT" (older protocol)
|
||||
if (value.contains("ELEMENT")) {
|
||||
return value["ELEMENT"].get<std::string>();
|
||||
}
|
||||
throw Error("No element identifier in response: " + value.dump());
|
||||
}
|
||||
|
||||
std::string url(const std::string &suffix) const {
|
||||
return "/session/" + session_id_ + suffix;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit Session(const std::string &host = "127.0.0.1", int port = 4444)
|
||||
: cli_(host, port) {
|
||||
cli_.set_read_timeout(std::chrono::seconds(30));
|
||||
cli_.set_connection_timeout(std::chrono::seconds(5));
|
||||
|
||||
json caps = {
|
||||
{"capabilities",
|
||||
{{"alwaysMatch",
|
||||
{{"moz:firefoxOptions", {{"args", json::array({"-headless"})}}}}}}}};
|
||||
|
||||
auto res = cli_.Post("/session", caps.dump(), "application/json");
|
||||
if (!res) { throw Error("Cannot connect to geckodriver"); }
|
||||
if (res->status != 200) {
|
||||
throw Error("Failed to create session: " + res->body);
|
||||
}
|
||||
|
||||
auto body = json::parse(res->body);
|
||||
session_id_ = body["value"]["sessionId"].get<std::string>();
|
||||
}
|
||||
|
||||
~Session() {
|
||||
try {
|
||||
cli_.Delete(url(""));
|
||||
} catch (...) {}
|
||||
}
|
||||
|
||||
// Non-copyable, non-movable (owns a session)
|
||||
Session(const Session &) = delete;
|
||||
Session &operator=(const Session &) = delete;
|
||||
|
||||
// ─── Navigation ──────────────────────────────────────────
|
||||
|
||||
void navigate(const std::string &nav_url) {
|
||||
json body = {{"url", nav_url}};
|
||||
auto res = cli_.Post(url("/url"), body.dump(), "application/json");
|
||||
if (!res || res->status != 200) {
|
||||
throw Error("Failed to navigate to: " + nav_url);
|
||||
}
|
||||
}
|
||||
|
||||
std::string title() {
|
||||
auto res = cli_.Get(url("/title"));
|
||||
if (!res || res->status != 200) { throw Error("Failed to get title"); }
|
||||
return json::parse(res->body)["value"].get<std::string>();
|
||||
}
|
||||
|
||||
std::string current_url() {
|
||||
auto res = cli_.Get(url("/url"));
|
||||
if (!res || res->status != 200) {
|
||||
throw Error("Failed to get current URL");
|
||||
}
|
||||
return json::parse(res->body)["value"].get<std::string>();
|
||||
}
|
||||
|
||||
// ─── Find elements ──────────────────────────────────────
|
||||
|
||||
Element find(const std::string &using_, const std::string &value) {
|
||||
json body = {{"using", using_}, {"value", value}};
|
||||
auto res = cli_.Post(url("/element"), body.dump(), "application/json");
|
||||
if (!res || res->status != 200) {
|
||||
throw Error("Element not found: " + using_ + "=" + value);
|
||||
}
|
||||
auto eid = extract_element_id(json::parse(res->body)["value"]);
|
||||
return Element(&cli_, session_id_, eid);
|
||||
}
|
||||
|
||||
std::vector<Element> find_all(const std::string &using_,
|
||||
const std::string &value) {
|
||||
json body = {{"using", using_}, {"value", value}};
|
||||
auto res = cli_.Post(url("/elements"), body.dump(), "application/json");
|
||||
if (!res || res->status != 200) { return {}; }
|
||||
|
||||
std::vector<Element> elements;
|
||||
for (auto &v : json::parse(res->body)["value"]) {
|
||||
elements.emplace_back(&cli_, session_id_, extract_element_id(v));
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
|
||||
// Convenience: find by CSS selector
|
||||
Element css(const std::string &selector) {
|
||||
return find("css selector", selector);
|
||||
}
|
||||
|
||||
std::vector<Element> css_all(const std::string &selector) {
|
||||
return find_all("css selector", selector);
|
||||
}
|
||||
|
||||
// ─── Wait ────────────────────────────────────────────────
|
||||
|
||||
// Poll for an element until it appears or timeout
|
||||
Element wait_for(const std::string &selector, int timeout_ms = 5000) {
|
||||
auto deadline = std::chrono::steady_clock::now() +
|
||||
std::chrono::milliseconds(timeout_ms);
|
||||
while (std::chrono::steady_clock::now() < deadline) {
|
||||
try {
|
||||
return css(selector);
|
||||
} catch (...) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
}
|
||||
}
|
||||
throw Error("Timeout waiting for element: " + selector);
|
||||
}
|
||||
|
||||
// Wait until a JS expression returns truthy
|
||||
bool wait_until(const std::string &script, int timeout_ms = 5000) {
|
||||
auto deadline = std::chrono::steady_clock::now() +
|
||||
std::chrono::milliseconds(timeout_ms);
|
||||
while (std::chrono::steady_clock::now() < deadline) {
|
||||
auto result = execute_script(script);
|
||||
if (result != "null" && result != "false" && result != "" &&
|
||||
result != "0" && result != "undefined") {
|
||||
return true;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// ─── Execute script ─────────────────────────────────────
|
||||
|
||||
std::string execute_script(const std::string &script,
|
||||
const json &args = json::array()) {
|
||||
json body = {{"script", script}, {"args", args}};
|
||||
auto res = cli_.Post(url("/execute/sync"), body.dump(), "application/json");
|
||||
if (!res || res->status != 200) {
|
||||
throw Error("Failed to execute script: " + script);
|
||||
}
|
||||
auto val = json::parse(res->body)["value"];
|
||||
if (val.is_null()) { return "null"; }
|
||||
if (val.is_string()) { return val.get<std::string>(); }
|
||||
return val.dump();
|
||||
}
|
||||
|
||||
// ─── Page source ────────────────────────────────────────
|
||||
|
||||
std::string page_source() {
|
||||
auto res = cli_.Get(url("/source"));
|
||||
if (!res || res->status != 200) {
|
||||
throw Error("Failed to get page source");
|
||||
}
|
||||
return json::parse(res->body)["value"].get<std::string>();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace webdriver
|
||||
@@ -0,0 +1,12 @@
|
||||
FROM alpine as builder
|
||||
WORKDIR /src/example
|
||||
RUN apk add g++ make openssl-dev zlib-dev brotli-dev
|
||||
COPY ./httplib.h /src
|
||||
COPY ./example/hello.cc /src/example
|
||||
COPY ./example/Makefile /src/example
|
||||
RUN make hello
|
||||
|
||||
FROM alpine
|
||||
RUN apk --no-cache add brotli libstdc++
|
||||
COPY --from=builder /src/example/hello /bin/hello
|
||||
CMD ["/bin/hello"]
|
||||
+43
-15
@@ -1,42 +1,70 @@
|
||||
|
||||
#CXX = clang++
|
||||
CXXFLAGS = -std=c++14 -I.. -Wall -Wextra -pthread
|
||||
OPENSSL_DIR = /usr/local/opt/openssl
|
||||
CXXFLAGS = -O2 -std=c++11 -I.. -Wall -Wextra -pthread
|
||||
|
||||
PREFIX ?= $(shell brew --prefix)
|
||||
|
||||
OPENSSL_DIR = $(PREFIX)/opt/openssl@3
|
||||
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto
|
||||
|
||||
ifneq ($(OS), Windows_NT)
|
||||
UNAME_S := $(shell uname -s)
|
||||
ifeq ($(UNAME_S), Darwin)
|
||||
OPENSSL_SUPPORT += -framework CoreFoundation -framework Security
|
||||
endif
|
||||
endif
|
||||
|
||||
ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
|
||||
|
||||
all: server client hello simplecli simplesvr upload redirect sse benchmark
|
||||
BROTLI_DIR = $(PREFIX)/opt/brotli
|
||||
BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
|
||||
|
||||
all: server client hello simplecli simplesvr upload redirect ssesvr ssecli wsecho benchmark one_time_request server_and_client accept_header
|
||||
|
||||
server : server.cc ../httplib.h Makefile
|
||||
$(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
|
||||
$(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
client : client.cc ../httplib.h Makefile
|
||||
$(CXX) -o client $(CXXFLAGS) client.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
|
||||
$(CXX) -o client $(CXXFLAGS) client.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
hello : hello.cc ../httplib.h Makefile
|
||||
$(CXX) -o hello $(CXXFLAGS) hello.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
|
||||
$(CXX) -o hello $(CXXFLAGS) hello.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
simplecli : simplecli.cc ../httplib.h Makefile
|
||||
$(CXX) -o simplecli $(CXXFLAGS) simplecli.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
|
||||
$(CXX) -o simplecli $(CXXFLAGS) simplecli.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
simplesvr : simplesvr.cc ../httplib.h Makefile
|
||||
$(CXX) -o simplesvr $(CXXFLAGS) simplesvr.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
|
||||
$(CXX) -o simplesvr $(CXXFLAGS) simplesvr.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
upload : upload.cc ../httplib.h Makefile
|
||||
$(CXX) -o upload $(CXXFLAGS) upload.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
|
||||
$(CXX) -o upload $(CXXFLAGS) upload.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
redirect : redirect.cc ../httplib.h Makefile
|
||||
$(CXX) -o redirect $(CXXFLAGS) redirect.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
|
||||
$(CXX) -o redirect $(CXXFLAGS) redirect.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
sse : sse.cc ../httplib.h Makefile
|
||||
$(CXX) -o sse $(CXXFLAGS) sse.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
|
||||
ssesvr : ssesvr.cc ../httplib.h Makefile
|
||||
$(CXX) -o ssesvr $(CXXFLAGS) ssesvr.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
ssecli : ssecli.cc ../httplib.h Makefile
|
||||
$(CXX) -o ssecli $(CXXFLAGS) ssecli.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
wsecho : wsecho.cc ../httplib.h Makefile
|
||||
$(CXX) -o wsecho $(CXXFLAGS) wsecho.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
benchmark : benchmark.cc ../httplib.h Makefile
|
||||
$(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
|
||||
$(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
one_time_request : one_time_request.cc ../httplib.h Makefile
|
||||
$(CXX) -o one_time_request $(CXXFLAGS) one_time_request.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
server_and_client : server_and_client.cc ../httplib.h Makefile
|
||||
$(CXX) -o server_and_client $(CXXFLAGS) server_and_client.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
accept_header : accept_header.cc ../httplib.h Makefile
|
||||
$(CXX) -o accept_header $(CXXFLAGS) accept_header.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||
|
||||
pem:
|
||||
openssl genrsa 2048 > key.pem
|
||||
openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem
|
||||
|
||||
clean:
|
||||
rm server client hello simplecli simplesvr upload redirect sse benchmark *.pem
|
||||
rm server client hello simplecli simplesvr upload redirect ssesvr ssecli wsecho benchmark one_time_request server_and_client accept_header *.pem
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
using namespace httplib;
|
||||
|
||||
// Example usage of parse_accept_header function
|
||||
std::cout << "=== Accept Header Parser Example ===" << std::endl;
|
||||
|
||||
// Example 1: Simple Accept header
|
||||
std::string accept1 = "text/html,application/json,text/plain";
|
||||
std::vector<std::string> result1;
|
||||
if (detail::parse_accept_header(accept1, result1)) {
|
||||
std::cout << "\nExample 1: " << accept1 << std::endl;
|
||||
std::cout << "Parsed order:" << std::endl;
|
||||
for (size_t i = 0; i < result1.size(); ++i) {
|
||||
std::cout << " " << (i + 1) << ". " << result1[i] << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "\nExample 1: Failed to parse Accept header" << std::endl;
|
||||
}
|
||||
|
||||
// Example 2: Accept header with quality values
|
||||
std::string accept2 = "text/html;q=0.9,application/json;q=1.0,text/plain;q=0.8";
|
||||
std::vector<std::string> result2;
|
||||
if (detail::parse_accept_header(accept2, result2)) {
|
||||
std::cout << "\nExample 2: " << accept2 << std::endl;
|
||||
std::cout << "Parsed order (sorted by priority):" << std::endl;
|
||||
for (size_t i = 0; i < result2.size(); ++i) {
|
||||
std::cout << " " << (i + 1) << ". " << result2[i] << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "\nExample 2: Failed to parse Accept header" << std::endl;
|
||||
}
|
||||
|
||||
// Example 3: Browser-like Accept header
|
||||
std::string accept3 = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
|
||||
std::vector<std::string> result3;
|
||||
if (detail::parse_accept_header(accept3, result3)) {
|
||||
std::cout << "\nExample 3: " << accept3 << std::endl;
|
||||
std::cout << "Parsed order:" << std::endl;
|
||||
for (size_t i = 0; i < result3.size(); ++i) {
|
||||
std::cout << " " << (i + 1) << ". " << result3[i] << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "\nExample 3: Failed to parse Accept header" << std::endl;
|
||||
}
|
||||
|
||||
// Example 4: Invalid Accept header examples
|
||||
std::cout << "\n=== Invalid Accept Header Examples ===" << std::endl;
|
||||
|
||||
std::vector<std::string> invalid_examples = {
|
||||
"text/html;q=1.5,application/json", // q > 1.0
|
||||
"text/html;q=-0.1,application/json", // q < 0.0
|
||||
"text/html;q=invalid,application/json", // invalid q value
|
||||
"invalidtype,application/json", // invalid media type
|
||||
",application/json" // empty entry
|
||||
};
|
||||
|
||||
for (const auto& invalid_accept : invalid_examples) {
|
||||
std::vector<std::string> temp_result;
|
||||
std::cout << "\nTesting invalid: " << invalid_accept << std::endl;
|
||||
if (detail::parse_accept_header(invalid_accept, temp_result)) {
|
||||
std::cout << " Unexpectedly succeeded!" << std::endl;
|
||||
} else {
|
||||
std::cout << " Correctly rejected as invalid" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Example 4: Server usage example
|
||||
std::cout << "\n=== Server Usage Example ===" << std::endl;
|
||||
Server svr;
|
||||
|
||||
svr.Get("/api/data", [](const Request& req, Response& res) {
|
||||
// Get Accept header
|
||||
auto accept_header = req.get_header_value("Accept");
|
||||
if (accept_header.empty()) {
|
||||
accept_header = "*/*"; // Default if no Accept header
|
||||
}
|
||||
|
||||
// Parse accept header to get preferred content types
|
||||
std::vector<std::string> preferred_types;
|
||||
if (!detail::parse_accept_header(accept_header, preferred_types)) {
|
||||
// Invalid Accept header
|
||||
res.status = 400; // Bad Request
|
||||
res.set_content("Invalid Accept header", "text/plain");
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "Client Accept header: " << accept_header << std::endl;
|
||||
std::cout << "Preferred types in order:" << std::endl;
|
||||
for (size_t i = 0; i < preferred_types.size(); ++i) {
|
||||
std::cout << " " << (i + 1) << ". " << preferred_types[i] << std::endl;
|
||||
}
|
||||
|
||||
// Choose response format based on client preference
|
||||
std::string response_content;
|
||||
std::string content_type;
|
||||
|
||||
for (const auto& type : preferred_types) {
|
||||
if (type == "application/json" || type == "application/*" || type == "*/*") {
|
||||
response_content = "{\"message\": \"Hello, World!\", \"data\": [1, 2, 3]}";
|
||||
content_type = "application/json";
|
||||
break;
|
||||
} else if (type == "text/html" || type == "text/*") {
|
||||
response_content = "<html><body><h1>Hello, World!</h1><p>Data: 1, 2, 3</p></body></html>";
|
||||
content_type = "text/html";
|
||||
break;
|
||||
} else if (type == "text/plain") {
|
||||
response_content = "Hello, World!\nData: 1, 2, 3";
|
||||
content_type = "text/plain";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (response_content.empty()) {
|
||||
// No supported content type found
|
||||
res.status = 406; // Not Acceptable
|
||||
res.set_content("No acceptable content type found", "text/plain");
|
||||
return;
|
||||
}
|
||||
|
||||
res.set_content(response_content, content_type);
|
||||
std::cout << "Responding with: " << content_type << std::endl;
|
||||
});
|
||||
|
||||
std::cout << "Server configured. You can test it with:" << std::endl;
|
||||
std::cout << " curl -H \"Accept: application/json\" http://localhost:8080/api/data" << std::endl;
|
||||
std::cout << " curl -H \"Accept: text/html\" http://localhost:8080/api/data" << std::endl;
|
||||
std::cout << " curl -H \"Accept: text/plain\" http://localhost:8080/api/data" << std::endl;
|
||||
std::cout << " curl -H \"Accept: text/html;q=0.9,application/json;q=1.0\" http://localhost:8080/api/data" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -21,12 +21,12 @@ struct StopWatch {
|
||||
int main(void) {
|
||||
string body(1024 * 5, 'a');
|
||||
|
||||
httplib::Client cli("httpbin.org", 80);
|
||||
httplib::Client cli("httpcan.org", 80);
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
StopWatch sw(to_string(i).c_str());
|
||||
auto res = cli.Post("/post", body, "application/octet-stream");
|
||||
assert(res->status == 200);
|
||||
assert(res->status == httplib::StatusCode::OK_200);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
+4
-4
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// client.cc
|
||||
//
|
||||
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
|
||||
// Copyright (c) 2026 Yuji Hirose. All rights reserved.
|
||||
// MIT License
|
||||
//
|
||||
|
||||
@@ -23,14 +23,14 @@ int main(void) {
|
||||
httplib::Client cli("localhost", 8080);
|
||||
#endif
|
||||
|
||||
auto res = cli.Get("/hi");
|
||||
if (res) {
|
||||
if (auto res = cli.Get("/hi")) {
|
||||
cout << res->status << endl;
|
||||
cout << res->get_header_value("Content-Type") << endl;
|
||||
cout << res->body << endl;
|
||||
} else {
|
||||
cout << "error code: " << res.error() << std::endl;
|
||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
auto result = cli.get_openssl_verify_result();
|
||||
auto result = cli.get_verify_result();
|
||||
if (result) {
|
||||
cout << "verify error: " << X509_verify_cert_error_string(result) << endl;
|
||||
}
|
||||
|
||||
@@ -22,34 +22,34 @@
|
||||
<ProjectGuid>{6DB1FC63-B153-4279-92B7-D8A11AF285D6}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>client</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// hello.cc
|
||||
//
|
||||
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
|
||||
// Copyright (c) 2026 Yuji Hirose. All rights reserved.
|
||||
// MIT License
|
||||
//
|
||||
|
||||
@@ -15,5 +15,5 @@ int main(void) {
|
||||
res.set_content("Hello World!", "text/plain");
|
||||
});
|
||||
|
||||
svr.listen("localhost", 8080);
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <httplib.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace httplib;
|
||||
|
||||
const char *HOST = "localhost";
|
||||
const int PORT = 1234;
|
||||
|
||||
void one_time_request_server(const char *label) {
|
||||
std::thread th;
|
||||
Server svr;
|
||||
|
||||
svr.Get("/hi", [&](const Request & /*req*/, Response &res) {
|
||||
res.set_content(std::string("Hello from ") + label, "text/plain");
|
||||
|
||||
// Stop server
|
||||
th = std::thread([&]() { svr.stop(); });
|
||||
});
|
||||
|
||||
svr.listen(HOST, PORT);
|
||||
th.join();
|
||||
|
||||
std::cout << label << " ended..." << std::endl;
|
||||
}
|
||||
|
||||
void send_request(const char *label) {
|
||||
Client cli(HOST, PORT);
|
||||
|
||||
std::cout << "Send " << label << " request" << std::endl;
|
||||
auto res = cli.Get("/hi");
|
||||
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl;
|
||||
} else {
|
||||
std::cout << "Request error: " + to_string(res.error()) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
auto th1 = std::thread([&]() { one_time_request_server("Server #1"); });
|
||||
auto th2 = std::thread([&]() { one_time_request_server("Server #2"); });
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
||||
send_request("1st");
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
||||
send_request("2nd");
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
||||
send_request("3rd");
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
||||
th1.join();
|
||||
th2.join();
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// redirect.cc
|
||||
//
|
||||
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
|
||||
// Copyright (c) 2026 Yuji Hirose. All rights reserved.
|
||||
// MIT License
|
||||
//
|
||||
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// sample.cc
|
||||
//
|
||||
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
|
||||
// Copyright (c) 2026 Yuji Hirose. All rights reserved.
|
||||
// MIT License
|
||||
//
|
||||
|
||||
|
||||
@@ -18,38 +18,41 @@
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="simplesvr.cc" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{864CD288-050A-4C8B-9BEF-3048BD876C5B}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>sample</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
@@ -151,9 +154,6 @@
|
||||
<AdditionalDependencies>Ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="server.cc" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
//
|
||||
// server_and_client.cc
|
||||
//
|
||||
// Copyright (c) 2026 Yuji Hirose. All rights reserved.
|
||||
// MIT License
|
||||
//
|
||||
|
||||
#include <httplib.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace httplib;
|
||||
|
||||
std::string dump_headers(const Headers &headers) {
|
||||
std::string s;
|
||||
char buf[BUFSIZ];
|
||||
|
||||
for (auto it = headers.begin(); it != headers.end(); ++it) {
|
||||
const auto &x = *it;
|
||||
snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
|
||||
s += buf;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void logger(const Request &req, const Response &res) {
|
||||
std::string s;
|
||||
char buf[BUFSIZ];
|
||||
|
||||
s += "================================\n";
|
||||
|
||||
snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
|
||||
req.version.c_str(), req.path.c_str());
|
||||
s += buf;
|
||||
|
||||
std::string query;
|
||||
for (auto it = req.params.begin(); it != req.params.end(); ++it) {
|
||||
const auto &x = *it;
|
||||
snprintf(buf, sizeof(buf), "%c%s=%s",
|
||||
(it == req.params.begin()) ? '?' : '&', x.first.c_str(),
|
||||
x.second.c_str());
|
||||
query += buf;
|
||||
}
|
||||
snprintf(buf, sizeof(buf), "%s\n", query.c_str());
|
||||
s += buf;
|
||||
|
||||
s += dump_headers(req.headers);
|
||||
|
||||
s += "--------------------------------\n";
|
||||
|
||||
snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str());
|
||||
s += buf;
|
||||
s += dump_headers(res.headers);
|
||||
s += "\n";
|
||||
|
||||
if (!res.body.empty()) { s += res.body; }
|
||||
|
||||
s += "\n";
|
||||
|
||||
std::cout << s;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
// Server
|
||||
Server svr;
|
||||
svr.set_logger(logger);
|
||||
|
||||
svr.Post("/post", [&](const Request & /*req*/, Response &res) {
|
||||
res.set_content("POST", "text/plain");
|
||||
});
|
||||
|
||||
auto th = std::thread([&]() { svr.listen("localhost", 8080); });
|
||||
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
th.join();
|
||||
});
|
||||
|
||||
svr.wait_until_ready();
|
||||
|
||||
// Client
|
||||
Client cli{"localhost", 8080};
|
||||
|
||||
std::string body = R"({"hello": "world"})";
|
||||
|
||||
auto res = cli.Post("/post", body, "application/json");
|
||||
std::cout << "--------------------------------" << std::endl;
|
||||
std::cout << to_string(res.error()) << std::endl;
|
||||
}
|
||||
@@ -1,31 +1,28 @@
|
||||
//
|
||||
// simplecli.cc
|
||||
//
|
||||
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
|
||||
// Copyright (c) 2026 Yuji Hirose. All rights reserved.
|
||||
// MIT License
|
||||
//
|
||||
|
||||
#include <httplib.h>
|
||||
#include <iostream>
|
||||
|
||||
#define CA_CERT_FILE "./ca-bundle.crt"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
auto res = httplib::Client2("https://localhost:8080")
|
||||
.set_ca_cert_path(CA_CERT_FILE)
|
||||
// .enable_server_certificate_verification(true)
|
||||
.Get("/hi");
|
||||
auto scheme_host_port = "https://localhost:8080";
|
||||
#else
|
||||
auto res = httplib::Client2("http://localhost:8080").Get("/hi");
|
||||
auto scheme_host_port = "http://localhost:8080";
|
||||
#endif
|
||||
|
||||
if (res) {
|
||||
if (auto res = httplib::Client(scheme_host_port).Get("/hi")) {
|
||||
cout << res->status << endl;
|
||||
cout << res->get_header_value("Content-Type") << endl;
|
||||
cout << res->body << endl;
|
||||
} else {
|
||||
cout << res.error() << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user