Compare commits

..

183 Commits

Author SHA1 Message Date
yhirose 39c7bba7b9 Code cleanup 2019-12-17 13:05:08 -05:00
yhirose f2476f21fc Fixed URL encoding problem when sending a request to proxy 2019-12-17 12:58:25 -05:00
yhirose c776454c84 Updated README 2019-12-15 20:31:36 -05:00
yhirose 82a5ac735f Merge pull request #290 from yhirose/interface
Fix #285. Added set_interface method on client
2019-12-15 18:02:51 -05:00
yhirose 08bf806e92 Updated README 2019-12-15 17:55:08 -05:00
yhirose 9a41b16cbb Fix #285. Added set_interface method on client 2019-12-15 17:44:00 -05:00
yhirose 10759f0a38 Updated README 2019-12-15 00:21:32 -05:00
yhirose 58b2814fda Format code 2019-12-14 23:50:53 -05:00
yhirose 260422b7d7 Format code 2019-12-14 23:46:11 -05:00
yhirose d2c7b447d5 Fix #289: Fixed build problem with Visual C++ 2019-12-13 09:12:50 -05:00
yhirose 72b20c08da Better API names 2019-12-13 06:56:00 -05:00
yhirose afd6d5f9dc Removed compress parameter and added compress method on client 2019-12-12 23:09:59 -05:00
yhirose e5827ad16f Fixed build error 2019-12-12 23:09:34 -05:00
yhirose 5324b3d661 Improved multipart form data interface 2019-12-12 22:48:09 -05:00
yhirose 151ccba57e Code cleanup 2019-12-12 21:50:12 -05:00
yhirose 69a28d50f6 Fix #287 2019-12-12 12:50:45 -05:00
Yuji Hirose 048f31109f Updated README 2019-12-10 13:14:23 -05:00
Yuji Hirose d064fb7ff2 Fixed warning 2019-12-10 13:08:07 -05:00
Yuji Hirose 3c2736bb2a Fixed regex syntax error 2019-12-10 13:07:49 -05:00
Yuji Hirose fd4e1b4112 Fix #266 2019-12-10 12:10:14 -05:00
yhirose f6a2365ca5 Fix #282 2019-12-06 12:21:15 -05:00
yhirose df1ff7510b Made code more readable 2019-12-06 12:02:08 -05:00
yhirose 379905bd34 Merge branch 'whitespace-and-libcxx-compat' of https://github.com/matvore/cpp-httplib 2019-12-06 09:51:21 -05:00
yhirose 66719ae3d4 Merge pull request #283 from barryam3/noexcept
Remove use of exceptions.
2019-12-05 21:32:06 -05:00
Matthew DeVore bc9251ea49 Work around incompatibility in <regex> in libc++
libc++ (the implementation of the C++ standard library usually used by
Clang) throws an exception for the regex used by parse_headers before
this patch for certain strings. Work around this by simplifying the
regex and parsing the header lines "by hand" partially. I have repro'd
this problem with Xcode 11.1 which I believe uses libc++ version 8.

This may be a bug in libc++ as I can't see why the regex would result in
asymptotic run-time complexity for any strings. However, it may take a
while for libc++ to be fixed and for everyone to migrate to it, so it
makes sense to work around it in this codebase for now.
2019-12-05 17:14:16 -08:00
Matthew DeVore a9e942d755 Properly trim whitespace from headers
HTTP Whitespace and regex whitespace are not the same, so we can't use
\s in regexes when parsing HTTP headers. Instead, explicitly specify
what is considered whitespace in the regex.
2019-12-05 17:14:16 -08:00
Barry McNamara e1785d6723 Remove use of exceptions. 2019-12-05 15:56:55 -08:00
yhirose b9539b8921 Fixed build errors 2019-12-03 10:30:07 -05:00
yhirose 4c93b973ff Fixed typo in README 2019-12-02 09:50:52 -05:00
yhirose 033bc35723 Improve multipart content reader interface 2019-12-02 07:11:12 -05:00
yhirose d910bfc303 Merge pull request #279 from yhirose/multipart
Content receiver support for multipart content (Fix #241)
2019-12-01 22:12:29 -05:00
yhirose b69c0a1dcb Content receiver support for multipart content (Fix #241) 2019-12-01 22:04:26 -05:00
yhirose 5e37e38398 Updated README 2019-11-29 23:33:19 -05:00
yhirose 295e4d58aa Fix #276 2019-11-29 17:07:51 -05:00
yhirose 448de6a9c6 Added upload example 2019-11-28 18:51:05 -05:00
yhirose 6f58dc728f Fixed problem with requests with no content 2019-11-28 08:28:01 -05:00
yhirose 905f2d84f4 Updated README 2019-11-27 22:53:06 -05:00
yhirose 880f7fa62b Fix #273 2019-11-27 12:54:01 -05:00
yhirose 8f3dbf7f21 Code cleanup 2019-11-27 08:01:25 -05:00
yhirose 924a557fa3 Changed to use 'using' instead of 'typedef' 2019-11-27 07:03:17 -05:00
yhirose d8da740597 Fix #270 2019-11-26 08:48:17 -05:00
yhirose d45676b064 Added NoThread task queue 2019-11-25 13:00:37 -05:00
Hirose Family 94d13e88a5 Fixed regex problem with Apple LLVM version 8.0.0 2019-11-03 19:27:12 -05:00
yhirose 4f9d04cb8e Merge pull request #257 from danielzehe/master
Update README.md
2019-11-01 07:07:30 -04:00
Daniel Zehe 9fb11986a5 Update README.md
added return true to the content provider get example, doesn't compile without it
2019-11-01 14:35:17 +08:00
yhirose 55d04ee354 Merge pull request #256 from Zefz/modernize-code-2
Modernize code 2
2019-10-31 21:17:35 -04:00
Johan Jansen a62a48a7b5 Modernize some additional code 2019-10-31 21:49:04 +01:00
Johan Jansen c652919954 Do not use shared_ptr where not required 2019-10-31 21:48:48 +01:00
Johan Jansen 58753ba33c Fix some virtual override warnings 2019-10-31 21:38:37 +01:00
yhirose 5706828d2c Replace C-style arrays and fix static-code analysis warnings 2019-10-31 21:32:07 +01:00
yhirose e743b8cd57 Fix #254 2019-10-30 08:21:59 -04:00
yhirose 9d57899352 Simplified ContentReceiver interface 2019-10-27 23:38:56 -04:00
yhirose d03937e144 Content receiver support on server 2019-10-27 23:20:56 -04:00
yhirose 8fb37a449d Fix #251 2019-10-27 17:27:57 -04:00
yhirose f0b1b5dbfd Added set_read_timeout. Fix #248. 2019-10-27 14:57:22 -04:00
yhirose 5f32c424c2 Content provider support on client 2019-10-25 18:39:04 -04:00
yhirose f0683f2301 Fixed build errors 2019-10-25 13:11:49 -04:00
yhirose 0d527e2b83 Code formatting 2019-10-25 12:09:26 -04:00
yhirose bea3ebd7af Added 'compress' option to POST, PUT and PATCH. 2019-10-25 11:46:12 -04:00
yhirose 380f725713 Code format 2019-10-24 22:20:42 -04:00
yhirose a106bd314c Merge branch 'master' of https://github.com/yhirose/cpp-httplib 2019-10-23 08:30:49 -04:00
yhirose e4fd9f19ca Updated Makefile 2019-10-23 08:28:15 -04:00
yhirose dfc01338eb Merge branch 'master' of https://github.com/yhirose/cpp-httplib 2019-10-23 08:15:59 -04:00
yhirose c4ebc31345 Merge pull request #246 from BastienDurel/htm
html files may be .htm
2019-10-23 07:06:28 -04:00
Bastien Durel d1abf96581 html files may be .htm 2019-10-23 09:52:21 +02:00
yhirose 001b8a5529 Added unit tests 2019-10-22 23:32:14 -04:00
yhirose 7a3abd2768 Merge pull request #243 from Sil3ntStorm/patch1
Allow use of OpenSSL 1.1.1, fix compile errors
2019-10-20 10:12:14 -04:00
yhirose 4a52524f47 Merge pull request #244 from aaronalbers/aa_bind_to_port_
Added bind_to_port()
2019-10-19 22:19:25 -04:00
Aaron Albers 89e1e9b8fe Added bind_to_port()
- This compliments the existing `bind_to_any_port()`
  where you can determine if the bind succeeded prior
  to calling `listen_after_bind()` but allows you to
  specify the port.
2019-10-19 10:41:19 -06:00
Sil3ntStorm 98d16eb836 Allow use of OpenSSL 1.1.1, fix compile errors 2019-10-19 16:40:06 +02:00
yhirose bcf0c32245 Updated README.md. (FIx #239 and #240) 2019-10-15 10:24:47 -04:00
yhirose dcdb0d047b Fixed PRI request problem 2019-10-03 13:44:18 -04:00
yhirose 1f86e41d97 Changed back to select as default 2019-10-03 13:44:18 -04:00
yhirose 6d8302313c Fixed warning 2019-10-03 13:44:18 -04:00
yhirose 89440ec322 Merge pull request #232 from sux2mfgj/fix_the_invalidhost_test
Fix a test, ConnectionErrorTest::InvalidPort.
2019-10-03 13:43:48 -04:00
yhirose 5edf455d72 Merge pull request #231 from sux2mfgj/fix_the_sample
Fix a sample code of multipart/form-data POST data in the README.md.
2019-10-03 13:42:31 -04:00
Shunsuke Mie 5f49c13f95 Fix a test, ConnectionErrorTest::InvalidPort. currently, the abcde.com is valid, so I change it. The first byte doesn't permit a hyphen. 2019-10-03 21:49:11 +09:00
Shunsuke Mie 760bccc3ad fix a sample code of multipart/form-data POST data in README.md. 2019-10-03 21:23:10 +09:00
yhirose a99e02aeb3 Add HTTP/2 Connection Preface check test 2019-10-01 06:28:45 -04:00
yhirose 4aae1dcc42 Merge pull request #224 from Zefz/configuration
Allow configuration to be overriden without source editing
2019-09-30 17:07:21 -04:00
Johan Jansen f23f9a06a9 Allow configuration to be overriden without source editing 2019-09-30 22:00:17 +02:00
yhirose 46466b1e28 Merge pull request #227 from ha11owed/master
Don't exit if accept fails due to no more file descriptors
2019-09-30 07:47:27 -04:00
Alin Gherman 224119a60a Retry in case of too many sockets opened instead of stopping the server. 2019-09-30 11:48:02 +02:00
yhirose c02849e269 Removed CPPHTTPLIB_USE_POLL, added CPPHTTPLIB_USE_SELECT 2019-09-29 19:43:22 -04:00
yhirose 71979b1e88 Merge pull request #226 from Zefz/mingw-compile-fix
Fix compilation on Mingw-64
2019-09-27 17:29:33 -04:00
yhirose a62d1f79f4 Merge pull request #225 from TangHuaiZhe/master
Fix compile error in android ndk
2019-09-27 17:29:07 -04:00
zefz b14b7b0f8f Fix compilation on Mingw-64 2019-09-27 20:23:16 +02:00
Tang Huaizhe 9dbe0d855c Fix compile error in android ndk 2019-09-27 13:32:23 +08:00
yhirose 2cef8df6ae Merge pull request #223 from Zefz/cpp-style-cast
Fix several -Wold-style-cast warnings in Clang-9
2019-09-26 19:00:45 -04:00
Johan Jansen 94fc229c44 Add missing explicit const_cast 2019-09-26 22:20:33 +02:00
Johan Jansen a7052cba22 Fix several -Wold-style-cast warnings in Clang-9 2019-09-26 22:03:18 +02:00
yhirose c47c6b3910 Updated test.vcxproj 2019-09-26 13:20:53 -04:00
yhirose c946eb7699 Fixed warnings on Windows 2019-09-26 08:13:20 -04:00
yhirose 1f99ad5d6e Updated vcxproj for test 2019-09-25 08:16:15 -04:00
yhirose 96e372bad2 Changed test.yaml to use actions/checkout@v1 2019-09-20 01:07:09 -04:00
yhirose fc56f39d17 Fixed Github Actions badge problem 2019-09-20 00:43:02 -04:00
yhirose 5844432a7b Add badge for Github Actions 2019-09-20 00:31:26 -04:00
yhirose b97420f363 Add test.yaml 2019-09-20 00:01:05 -04:00
yhirose 81610ee080 Merge pull request #218 from p0lloloco/master
Add ssl_context member function to SSLClient
2019-09-18 23:08:14 -04:00
PolloLoco c7f8561472 Added ssl_context member function to SSLClient in
order to allow access to the SSL_CTX struct, for
example to load the windows cert store
2019-09-18 15:10:15 +02:00
yhirose 47bc7456d2 Merge pull request #217 from yhirose/poll
Use 'poll' instead of 'select'
2019-09-18 08:46:20 -04:00
yhirose 4ab9270660 Use 'poll' as default instead of select (Fix #215) 2019-09-18 08:42:18 -04:00
yhirose d599a36c2a Format code 2019-09-16 17:48:17 -04:00
yhirose 6f8f51496d Merge branch 'gulrak-feature-response-handler-with-content-receiver' 2019-09-15 09:17:26 -04:00
yhirose 0c293887d0 Fixed problem with redirect 2019-09-15 09:15:21 -04:00
Steffen Schuemann 7e92ffec48 Added new Client::Get variant that combines a ContentReceiver with a new ResponseHandler
While trying to implement streaming of internet radio, where a ContentReceiver is needed to handle the audio data, I had the problem, that important information about the stream data is part of the HTTP header (e.g. size of audio chunks between meta data), so I added a ResponseHandler and a new Get variant, to gain access to the header before handling the first chunk of data.

The ResponseHandler can abort the request by returning false, in the same way as the ContentReceiver.

A test case was also added.
2019-09-14 14:55:12 +02:00
yhirose 531708816a Update README 2019-09-06 18:29:22 -04:00
yhirose bfec81998b Code cleanup 2019-09-06 18:16:42 -04:00
yhirose c9238434e1 Added redirect support (Fix #211) 2019-09-06 18:07:35 -04:00
yhirose e2babf315c Fixed build error on Windows 2019-09-05 13:22:44 -04:00
yhirose c434d555bb Fixed problem caused by former changes 2019-09-03 17:19:58 -04:00
yhirose 359b06681b Fixed warnings 2019-09-03 17:16:16 -04:00
yhirose a0fe91e84d Fixed unit test problem 2019-09-03 17:16:07 -04:00
yhirose 80f040cf69 Fix #209 2019-09-03 17:10:04 -04:00
yhirose 037b4fc789 Fixed problem with keep-alive on client 2019-09-03 17:05:10 -04:00
yhirose a782d1b609 Added OpenSSL version check. (Currently commented out) 2019-08-31 20:15:50 -04:00
yhirose 6a03ddf912 Update README 2019-08-31 18:01:45 -04:00
yhirose 1e82359329 Keep-alive connection support on client (Fix #36) 2019-08-31 17:52:24 -04:00
yhirose a4160e6ac1 Fix #212 2019-08-25 17:19:20 -04:00
yhirose bdbfc6cfe7 Fix #210 2019-08-25 16:49:10 -04:00
yhirose 22615f9682 Fix #203 2019-08-07 06:34:24 -04:00
yhirose 64991a560a Fixed problems with Win32 build 2019-08-07 15:21:22 +09:00
yhirose d7bb402ca7 Format code 2019-08-06 18:12:05 +09:00
yhirose d58deddbcc Fixed #198 2019-08-06 18:10:41 +09:00
yhirose 3629f87627 Fixed thread pool problem. 2019-08-06 08:31:43 +09:00
yhirose 1b95bf8cc3 Updated README 2019-08-05 18:24:30 +09:00
yhirose e383b277a4 Updated README 2019-08-05 18:17:40 +09:00
yhirose bf541442ea Updated README 2019-08-05 10:51:27 +09:00
yhirose 2823a94fc1 Added 'resource_releaser' for content provider 2019-08-05 09:40:23 +09:00
yhirose 5a13539e57 Revert "Fixed unit test error on Windows"
This reverts commit 531f6ab84d.
2019-08-04 09:50:47 +09:00
yhirose 531f6ab84d Fixed unit test error on Windows 2019-08-03 20:43:27 -04:00
yhirose f4aaba6362 Fixed warnings on Windows 2019-08-03 20:43:15 -04:00
yhirose c899462e75 Added 'Content-Range' header for single range request 2019-08-03 22:12:24 +09:00
yhirose 9785cd47f2 Thread pool support 2019-08-03 16:39:39 +09:00
yhirose 579ff1a0a6 new_task_queue support 2019-08-03 13:15:05 +09:00
yhirose 47312e6df9 Added TaskQueue interface 2019-08-03 12:11:22 +09:00
yhirose 66b81a1497 Code cleanup 2019-08-03 10:47:48 +09:00
yhirose 9d7b717504 Range header support and redesign of content provider interface 2019-08-03 03:28:39 +09:00
yhirose 3291bdad91 Fixed a build error with example/redirect.cc 2019-08-03 03:28:38 +09:00
yhirose e8799383f3 Made unit tests stable 2019-07-30 01:51:53 +00:00
yhirose 58f72bc8b6 Code cleanup 2019-07-29 14:24:40 -04:00
yhirose 4c58b91e40 Added additonal status messages 2019-07-29 09:47:50 -04:00
yhirose 06b3dee5aa Code cleanup 2019-07-28 19:12:44 -04:00
yhirose ba7f9fe4c3 Added more unit tests 2019-07-28 19:11:58 -04:00
yhirose 8480850a3f Fixed build error 2019-07-28 19:09:24 -04:00
yhirose d15df874b3 Code cleanup 2019-07-28 07:52:57 -04:00
yhirose f40de531ab Fix #196 2019-07-26 14:39:09 -04:00
yhirose 7231f6fe00 Merge pull request #193 from omaralvarez/master
Fix small CMake error when looking for the library
2019-07-24 07:02:53 -04:00
yhirose cb11d9412e Fixed connection end problem with HTTP/1.0 client 2019-07-24 06:53:48 -04:00
Omar Alvarez 057a8a0fb9 Fix small CMake error when looking for the header 2019-07-24 10:50:52 +02:00
yhirose 2d30c59340 Update README 2019-07-24 01:22:06 -04:00
yhirose 34651ef89b Fix #121 2019-07-23 09:53:44 -04:00
yhirose 4f237af813 Update README 2019-07-23 08:20:01 -04:00
yhirose eaffe68c1a Fixed README 2019-07-23 08:11:41 -04:00
yhirose e0d327558d Updated README 2019-07-23 08:05:51 -04:00
yhirose dd20e4d418 Fixed build errors 2019-07-22 06:58:05 -04:00
yhirose 7267b3f3e2 Write error handling 2019-07-21 21:34:49 -04:00
yhirose 4c18ac2b18 Added locking_callback for OpenSSL versions prior to 1.1.0 2019-07-19 17:03:47 -04:00
yhirose b5a1d52f36 Test code cleanup 2019-07-19 11:47:08 -04:00
yhirose eaafa5d55c Fix #127 2019-07-19 11:38:06 -04:00
yhirose 3d1ae3a3af Code cleanup 2019-07-17 23:50:47 -04:00
yhirose e4d3766ef2 Updated documentation 2019-07-17 23:46:02 -04:00
yhirose 6f663028e9 Fix #139. Content receiver support 2019-07-17 22:00:18 -04:00
yhirose 31cdadc4b1 Added check code to compress and decompress 2019-07-17 19:00:35 -04:00
yhirose 849add5887 Code cleanup 2019-07-17 15:09:41 -04:00
yhirose fccb84f5e8 Refactoring for reading content 2019-07-17 08:56:49 -04:00
yhirose 3541fe8330 Use -pthread 2019-07-09 22:31:55 -04:00
yhirose 63d0056826 Merge pull request #185 from gaoyadianta/master
fix the example Makefile
2019-07-09 22:31:16 -04:00
libingjun d1080087d3 fix the example Makefile 2019-07-10 10:00:19 +08:00
yhirose cb43980a70 Merge pull request #184 from jaspervandeven/master
Made svr_sock_ and is_running_ variables atomic
2019-07-05 18:53:25 -04:00
Jasper van de Ven 2fdc41c323 Made svr_sock_ and is_running_ variables atomic 2019-07-05 11:03:23 +02:00
yhirose 74e9a0d17e Fix #182 2019-07-03 10:52:24 -04:00
yhirose 30d73051ee Update README.md 2019-07-02 08:52:50 -04:00
yhirose 6f5a3c97ec Update .gitignore 2019-06-30 08:02:22 -04:00
yhirose bb537d93fe Added CPPHTTPLIB_RECV_BUFSIZ 2019-06-29 20:39:18 -04:00
yhirose bde3fd9f78 Improved performance of read_content_without_length 2019-06-29 20:25:16 -04:00
yhirose 2aa35d5f53 Fix #179 2019-06-28 12:08:47 -04:00
yhirose 0b2c506ca4 Added redirect exampe 2019-06-27 21:49:09 -04:00
yhirose 8098f88c90 Corrected licence 2019-06-27 21:48:57 -04:00
yhirose 49f21f74cb Removed linux and gcc from .travis.yml 2019-06-26 15:24:03 -04:00
yhirose cdc45c4601 Changed to use CXX and CXXFLAGS in Makefile 2019-06-26 15:09:34 -04:00
yhirose c1ca091b44 Use gcc-4.9 in travis ci 2019-06-26 14:57:25 -04:00
yhirose 7f7d101440 Updated .travis.yml to support linux and gcc 2019-06-26 14:29:07 -04:00
23 changed files with 4069 additions and 1148 deletions
+5
View File
@@ -0,0 +1,5 @@
BasedOnStyle: LLVM
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
Cpp11BracedListStyle: true
+17
View File
@@ -0,0 +1,17 @@
name: test
on: [push, pull_request]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macOS-latest, ubuntu-latest]
steps:
- name: checkout
uses: actions/checkout@v1
- name: make
run: cd test && make
+2
View File
@@ -5,6 +5,8 @@ example/client
example/hello
example/simplesvr
example/benchmark
example/redirect
example/upload
example/*.pem
test/test
test/test.xcodeproj/xcuser*
+4 -3
View File
@@ -1,12 +1,13 @@
# Environment
language: cpp
os: osx
os:
- osx
# Compiler selection
compiler:
- clang
# Build/test steps
script:
script:
- cd ${TRAVIS_BUILD_DIR}/test
- make all
- make all
+1 -1
View File
@@ -19,7 +19,7 @@ install(TARGETS ${PROJECT_NAME} EXPORT httplibConfig
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
install(FILES httplib.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
install(EXPORT httplibConfig DESTINATION share/httplib/cmake)
+244 -24
View File
@@ -1,15 +1,14 @@
cpp-httplib
===========
[![](https://github.com/yhirose/cpp-httplib/workflows/test/badge.svg)](https://github.com/yhirose/cpp-httplib/actions)
[![Build Status](https://travis-ci.org/yhirose/cpp-httplib.svg?branch=master)](https://travis-ci.org/yhirose/cpp-httplib)
[![Bulid Status](https://ci.appveyor.com/api/projects/status/github/yhirose/cpp-httplib?branch=master&svg=true)](https://ci.appveyor.com/project/yhirose/cpp-httplib)
A C++11 header-only HTTP library.
A C++ single-file header-only cross platform HTTP/HTTPS library.
It's extremely easy to setup. Just include **httplib.h** file in your code!
Inspired by [Sinatra](http://www.sinatrarb.com/) and [express](https://github.com/visionmedia/express).
Server Example
--------------
@@ -31,6 +30,10 @@ int main(void)
res.set_content(numbers, "text/plain");
});
svr.Get("/stop", [&](const Request& req, Response& res) {
svr.stop();
});
svr.listen("localhost", 1234);
}
```
@@ -44,22 +47,19 @@ int port = svr.bind_to_any_port("0.0.0.0");
svr.listen_after_bind();
```
### Method Chain
```cpp
svr.Get("/get", [](const auto& req, auto& res) {
res.set_content("get", "text/plain");
})
.Post("/post", [](const auto& req, auto& res) {
res.set_content(req.body(), "text/plain");
})
.listen("localhost", 1234);
```
### Static File Server
```cpp
svr.set_base_dir("./www");
svr.set_base_dir("./www"); // This is same as `svr.set_base_dir("./www", "/")`;
```
```cpp
svr.set_base_dir("./www", "/public");
```
```cpp
svr.set_base_dir("./www1", "/public"); // 1st order
svr.set_base_dir("./www2", "/public"); // 2nd order
```
### Logging
@@ -86,12 +86,113 @@ svr.set_error_handler([](const auto& req, auto& res) {
```cpp
svr.Post("/multipart", [&](const auto& req, auto& res) {
auto size = req.files.size();
auto ret = req.has_file("name1"));
auto ret = req.has_file("name1");
const auto& file = req.get_file_value("name1");
// file.filename;
// file.content_type;
auto body = req.body.substr(file.offset, file.length));
})
// file.content;
});
```
### Send content with Content provider
```cpp
const uint64_t DATA_CHUNK_SIZE = 4;
svr.Get("/stream", [&](const Request &req, Response &res) {
auto data = new std::string("abcdefg");
res.set_content_provider(
data->size(), // Content length
[data](uint64_t offset, uint64_t length, DataSink sink) {
const auto &d = *data;
sink(&d[offset], std::min(length, DATA_CHUNK_SIZE));
},
[data] { delete data; });
});
```
### Receive content with Content receiver
```cpp
svr.Post("/content_receiver",
[&](const Request &req, Response &res, const ContentReader &content_reader) {
if (req.is_multipart_form_data()) {
MultipartFormDataItems files;
content_reader(
[&](const MultipartFormData &file) {
files.push_back(file);
return true;
},
[&](const char *data, size_t data_length) {
files.back().content.append(data, data_length);
return true;
});
} else {
std::string body;
content_reader([&](const char *data, size_t data_length) {
body.append(data, data_length);
return true;
});
res.set_content(body, "text/plain");
}
});
```
### Chunked transfer encoding
```cpp
svr.Get("/chunked", [&](const Request& req, Response& res) {
res.set_chunked_content_provider(
[](uint64_t offset, DataSink sink, Done done) {
sink("123", 3);
sink("345", 3);
sink("789", 3);
done();
}
);
});
```
### Default thread pool support
Set thread count to 8:
```cpp
#define CPPHTTPLIB_THREAD_POOL_COUNT 8
```
Disable the default thread pool:
```cpp
#define CPPHTTPLIB_THREAD_POOL_COUNT 0
```
### Override the default thread pool with yours
```cpp
class YourThreadPoolTaskQueue : public TaskQueue {
public:
YourThreadPoolTaskQueue(size_t n) {
pool_.start_with_thread_count(n);
}
virtual void enqueue(std::function<void()> fn) override {
pool_.enqueue(fn);
}
virtual void shutdown() override {
pool_.shutdown_gracefully();
}
private:
YourThreadPool pool_;
};
svr.new_task_queue = [] {
return new YourThreadPoolTaskQueue(12);
};
```
Client Example
@@ -114,6 +215,29 @@ int main(void)
}
```
### GET with HTTP headers
```c++
httplib::Headers headers = {
{ "Accept-Encoding", "gzip, deflate" }
};
auto res = cli.Get("/hi", headers);
```
### GET with Content Receiver
```c++
std::string body;
auto res = cli.Get("/large-data",
[&](const char *data, uint64_t data_length) {
body.append(data, data_length);
return true;
});
assert(res->body.empty());
```
### POST
```c++
@@ -141,6 +265,20 @@ httplib::Params params{
auto res = cli.Post("/post", params);
```
### POST with Multipart Form Data
```c++
httplib::MultipartFormDataItems items = {
{ "text1", "text default", "", "" },
{ "text2", "aωb", "", "" },
{ "file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain" },
{ "file2", "{\n \"world\", true\n}\n", "world.json", "application/json" },
{ "file3", "", "", "application/octet-stream" },
};
auto res = cli.Post("/multipart", items);
```
### PUT
```c++
@@ -185,24 +323,90 @@ std::shared_ptr<httplib::Response> res =
This feature was contributed by [underscorediscovery](https://github.com/yhirose/cpp-httplib/pull/23).
### Authentication
NOTE: OpenSSL is required for Digest Authentication, since cpp-httplib uses message digest functions in OpenSSL.
```cpp
httplib::Client cli("httplib.org");
cli.set_auth("user", "pass");
// Basic
auto res = cli.Get("/basic-auth/user/pass");
// res->status should be 200
// res->body should be "{\n \"authenticated\": true, \n \"user\": \"user\"\n}\n".
// Digest
res = cli.Get("/digest-auth/auth/user/pass/SHA-256");
// res->status should be 200
// res->body should be "{\n \"authenticated\": true, \n \"user\": \"user\"\n}\n".
```
### Range
```cpp
httplib::Client cli("httpbin.org", 80);
httplib::Client cli("httpbin.org");
// 'Range: bytes=1-10'
httplib::Headers headers = { httplib::make_range_header(1, 10) };
auto res = cli.Get("/range/32", headers);
auto res = cli.Get("/range/32", {
httplib::make_range_header({{1, 10}}) // 'Range: bytes=1-10'
});
// res->status should be 206.
// res->body should be "bcdefghijk".
```
```cpp
httplib::make_range_header({{1, 10}, {20, -1}}) // 'Range: bytes=1-10, 20-'
httplib::make_range_header({{100, 199}, {500, 599}}) // 'Range: bytes=100-199, 500-599'
httplib::make_range_header({{0, 0}, {-1, 1}}) // 'Range: bytes=0-0, -1'
```
### Keep-Alive connection
```cpp
cli.set_keep_alive_max_count(2); // Default is 5
std::vector<Request> requests;
Get(requests, "/get-request1");
Get(requests, "/get-request2");
Post(requests, "/post-request1", "text", "text/plain");
Post(requests, "/post-request2", "text", "text/plain");
std::vector<Response> responses;
if (cli.send(requests, responses)) {
for (const auto& res: responses) {
...
}
}
```
### Redirect
```cpp
httplib::Client cli("yahoo.com");
auto res = cli.Get("/");
res->status; // 301
cli.set_follow_location(true);
res = cli.Get("/");
res->status; // 200
```
### Use a specitic network interface
NOTE: This feature is not available on Windows, yet.
```cpp
cli.set_interface("eth0"); // Interface name, IP address or host name
```
OpenSSL Support
---------------
SSL support is available with `CPPHTTPLIB_OPENSSL_SUPPORT`. `libssl` and `libcrypto` should be linked.
NOTE: cpp-httplib supports 1.1.1 (until 2023-09-11) and 1.0.2 (2019-12-31).
```c++
#define CPPHTTPLIB_OPENSSL_SUPPORT
@@ -227,6 +431,22 @@ The server applies gzip compression to the following MIME type contents:
* application/xml
* application/xhtml+xml
### Compress content on client
```c++
cli.set_compress(true);
res = cli.Post("/resource/foo", "...", "text/plain");
```
Split httplib.h into .h and .cc
-------------------------------
```bash
> python3 split.py
> ls out
httplib.h httplib.cc
```
NOTE
----
+17 -10
View File
@@ -1,29 +1,36 @@
CC = clang++
CFLAGS = -std=c++14 -I.. -Wall -Wextra -lpthread
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto
#CXX = clang++
CXXFLAGS = -std=c++14 -I.. -Wall -Wextra -pthread
OPENSSL_DIR = /usr/local/opt/openssl
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto
ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
all: server client hello simplesvr benchmark
all: server client hello simplesvr upload redirect benchmark
server : server.cc ../httplib.h Makefile
$(CC) -o server $(CFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
$(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
client : client.cc ../httplib.h Makefile
$(CC) -o client $(CFLAGS) client.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
$(CXX) -o client $(CXXFLAGS) client.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
hello : hello.cc ../httplib.h Makefile
$(CC) -o hello $(CFLAGS) hello.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
$(CXX) -o hello $(CXXFLAGS) hello.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
simplesvr : simplesvr.cc ../httplib.h Makefile
$(CC) -o simplesvr $(CFLAGS) simplesvr.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
$(CXX) -o simplesvr $(CXXFLAGS) simplesvr.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
upload : upload.cc ../httplib.h Makefile
$(CXX) -o upload $(CXXFLAGS) upload.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
redirect : redirect.cc ../httplib.h Makefile
$(CXX) -o redirect $(CXXFLAGS) redirect.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
benchmark : benchmark.cc ../httplib.h Makefile
$(CC) -o benchmark $(CFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
$(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_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 simplesvr *.pem
rm server client hello simplesvr upload redirect benchmark *.pem
+2 -2
View File
@@ -1,8 +1,8 @@
//
// client.cc
//
// Copyright (c) 2012 Yuji Hirose. All rights reserved.
// The Boost Software License 1.0
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
// MIT License
//
#include <httplib.h>
+2 -2
View File
@@ -1,8 +1,8 @@
//
// hello.cc
//
// Copyright (c) 2012 Yuji Hirose. All rights reserved.
// The Boost Software License 1.0
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
// MIT License
//
#include <httplib.h>
+60
View File
@@ -0,0 +1,60 @@
//
// redirect.cc
//
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
// MIT License
//
#include <httplib.h>
#define SERVER_CERT_FILE "./cert.pem"
#define SERVER_PRIVATE_KEY_FILE "./key.pem"
using namespace httplib;
int main(void) {
// HTTP server
Server http;
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
SSLServer https(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
#endif
http.Get("/test", [](const Request & /*req*/, Response &res) {
res.set_content("Test\n", "text/plain");
});
http.set_error_handler([](const Request & /*req*/, Response &res) {
res.set_redirect("https://localhost:8081/");
});
// HTTPS server
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
https.Get("/", [=](const Request & /*req*/, Response &res) {
res.set_redirect("/hi");
});
https.Get("/hi", [](const Request & /*req*/, Response &res) {
res.set_content("Hello World!\n", "text/plain");
});
https.Get("/stop", [&](const Request & /*req*/, Response & /*res*/) {
https.stop();
http.stop();
});
#endif
// Run servers
auto httpThread = std::thread([&]() { http.listen("localhost", 8080); });
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto httpsThread = std::thread([&]() { https.listen("localhost", 8081); });
#endif
httpThread.join();
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
httpsThread.join();
#endif
return 0;
}
+2 -2
View File
@@ -1,8 +1,8 @@
//
// sample.cc
//
// Copyright (c) 2012 Yuji Hirose. All rights reserved.
// The Boost Software License 1.0
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
// MIT License
//
#include <chrono>
+4 -7
View File
@@ -1,8 +1,8 @@
//
// simplesvr.cc
//
// Copyright (c) 2013 Yuji Hirose. All rights reserved.
// The Boost Software License 1.0
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
// MIT License
//
#include <cstdio>
@@ -27,7 +27,7 @@ string dump_headers(const Headers &headers) {
return s;
}
string dump_multipart_files(const MultipartFiles &files) {
string dump_multipart_files(const MultipartFormDataMap &files) {
string s;
char buf[BUFSIZ];
@@ -46,10 +46,7 @@ string dump_multipart_files(const MultipartFiles &files) {
snprintf(buf, sizeof(buf), "content type: %s\n", file.content_type.c_str());
s += buf;
snprintf(buf, sizeof(buf), "text offset: %lu\n", file.offset);
s += buf;
snprintf(buf, sizeof(buf), "text length: %lu\n", file.length);
snprintf(buf, sizeof(buf), "text length: %lu\n", file.content.size());
s += buf;
s += "----------------\n";
+50
View File
@@ -0,0 +1,50 @@
//
// upload.cc
//
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
// MIT License
//
#include <fstream>
#include <httplib.h>
#include <iostream>
using namespace httplib;
using namespace std;
const char *html = R"(
<form id="formElem">
<input type="file" name="file" accept="image/*">
<input type="submit">
</form>
<script>
formElem.onsubmit = async (e) => {
e.preventDefault();
let res = await fetch('/post', {
method: 'POST',
body: new FormData(formElem)
});
console.log(await res.text());
};
</script>
)";
int main(void) {
Server svr;
svr.Get("/", [](const Request & /*req*/, Response &res) {
res.set_content(html, "text/html");
});
svr.Post("/post", [](const Request &req, Response &res) {
auto file = req.get_file_value("file");
cout << "file length: " << file.content.length() << ":" << file.filename
<< endl;
ofstream ofs(file.filename, ios::binary);
ofs << file.content;
res.set_content("done", "text/plain");
});
svr.listen("localhost", 1234);
}
+2699 -885
View File
File diff suppressed because it is too large Load Diff
+24
View File
@@ -0,0 +1,24 @@
import os
border = '// ----------------------------------------------------------------------------'
with open('httplib.h') as f:
lines = f.readlines()
inImplementation = False
os.makedirs('out', exist_ok=True)
with open('out/httplib.h', 'w') as fh:
with open('out/httplib.cc', 'w') as fc:
fc.write('#include "httplib.h"\n')
fc.write('namespace httplib {\n')
for line in lines:
isBorderLine = border in line
if isBorderLine:
inImplementation = not inImplementation
else:
if inImplementation:
fc.write(line.replace('inline ', ''))
pass
else:
fh.write(line)
pass
fc.write('} // namespace httplib\n')
+5 -6
View File
@@ -1,16 +1,15 @@
CC = clang++
#CC = g++
CFLAGS = -ggdb -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -Wtype-limits
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto
#CXX = clang++
CXXFLAGS = -ggdb -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -Wtype-limits
OPENSSL_DIR = /usr/local/opt/openssl
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto
ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
all : test
./test
test : test.cc ../httplib.h Makefile cert.pem
$(CC) -o test $(CFLAGS) test.cc gtest/gtest-all.cc gtest/gtest_main.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) -lpthread
$(CXX) -o test $(CXXFLAGS) test.cc gtest/gtest-all.cc gtest/gtest_main.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) -pthread
cert.pem:
openssl genrsa 2048 > key.pem
+9
View File
@@ -54,6 +54,11 @@
#include <limits>
#include <vector>
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
// Copyright 2005, Google Inc.
// All rights reserved.
//
@@ -19544,4 +19549,8 @@ bool StaticAssertTypeEq() {
} // namespace testing
#if defined(_MSC_VER)
#pragma warning( pop )
#endif
#endif // GTEST_INCLUDE_GTEST_GTEST_H_
+884 -202
View File
File diff suppressed because it is too large Load Diff
+20 -4
View File
@@ -34,7 +34,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
@@ -47,7 +47,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
@@ -69,15 +69,23 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>C:\Program Files\OpenSSL-Win64\lib\VC;C:\Program Files\OpenSSL-Win64\include;$(IncludePath)</IncludePath>
<LibraryPath>$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>C:\Program Files\OpenSSL-Win64\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files\OpenSSL-Win64\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(IncludePath)</IncludePath>
<LibraryPath>$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>C:\Program Files\OpenSSL-Win64\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files\OpenSSL-Win64\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
@@ -87,6 +95,8 @@
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>./;../</AdditionalIncludeDirectories>
<AdditionalUsingDirectories>
</AdditionalUsingDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -102,11 +112,13 @@
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>./;../</AdditionalIncludeDirectories>
<AdditionalUsingDirectories>
</AdditionalUsingDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Ws2_32.lib;libssl.lib;libcrypto.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -119,6 +131,8 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>./;../</AdditionalIncludeDirectories>
<AdditionalUsingDirectories>
</AdditionalUsingDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -138,13 +152,15 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>./;../</AdditionalIncludeDirectories>
<AdditionalUsingDirectories>
</AdditionalUsingDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>Ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Ws2_32.lib;libssl.lib;libcrypto.lib;libssl.lib;libcrypto.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
+8
View File
@@ -0,0 +1,8 @@
<html>
<head>
</head>
<body>
<a href="/dir/test.html">Test</a>
<a href="/hi">hi</a>
</body>
</html>
+1
View File
@@ -0,0 +1 @@
test.html
+8
View File
@@ -0,0 +1,8 @@
<html>
<head>
</head>
<body>
<a href="/dir/test.html">Test</a>
<a href="/hi">hi</a>
</body>
</html>
+1
View File
@@ -0,0 +1 @@
test.html