2012-09-27 18:05:36 -07:00
|
|
|
cpp-httplib
|
|
|
|
===========
|
2012-09-21 19:38:33 -07:00
|
|
|
|
2012-10-05 10:58:56 -07:00
|
|
|
A C++11 header-only HTTP library.
|
2012-09-22 21:41:21 -07:00
|
|
|
|
2012-10-05 10:58:56 -07:00
|
|
|
It's extremely easy to setup. Just include **httplib.h** file in your code!
|
|
|
|
|
2017-12-05 17:30:13 -07:00
|
|
|
Inspired by [Sinatra](http://www.sinatrarb.com/) and [express](https://github.com/visionmedia/express).
|
|
|
|
|
2012-09-27 18:05:36 -07:00
|
|
|
Server Example
|
|
|
|
--------------
|
2012-09-25 19:21:42 -07:00
|
|
|
|
2015-02-06 20:43:06 -07:00
|
|
|
```c++
|
|
|
|
#include <httplib.h>
|
2012-09-25 19:21:42 -07:00
|
|
|
|
2015-02-06 20:43:06 -07:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
using namespace httplib;
|
2012-10-05 10:58:56 -07:00
|
|
|
|
2015-02-06 20:43:06 -07:00
|
|
|
Server svr;
|
2012-09-27 18:05:36 -07:00
|
|
|
|
2018-05-06 18:16:35 -07:00
|
|
|
svr.Get("/hi", [](const Request& req, Response& res) {
|
2015-02-06 20:43:06 -07:00
|
|
|
res.set_content("Hello World!", "text/plain");
|
|
|
|
});
|
2012-09-27 18:05:36 -07:00
|
|
|
|
2018-05-06 18:16:35 -07:00
|
|
|
svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
|
2016-06-26 11:51:34 -07:00
|
|
|
auto numbers = req.matches[1];
|
2015-02-06 20:43:06 -07:00
|
|
|
res.set_content(numbers, "text/plain");
|
|
|
|
});
|
|
|
|
|
|
|
|
svr.listen("localhost", 1234);
|
|
|
|
}
|
|
|
|
```
|
2012-09-25 19:21:42 -07:00
|
|
|
|
2018-05-06 18:16:35 -07:00
|
|
|
`Post`, `Put`, `Delete` and `Options` methods are also supported.
|
|
|
|
|
2017-11-25 09:59:28 -07:00
|
|
|
### Method Chain
|
|
|
|
|
|
|
|
```cpp
|
2018-05-06 18:16:35 -07:00
|
|
|
svr.Get("/get", [](const auto& req, auto& res) {
|
2017-11-25 09:59:28 -07:00
|
|
|
res.set_content("get", "text/plain");
|
|
|
|
})
|
2018-05-06 18:16:35 -07:00
|
|
|
.Post("/post", [](const auto& req, auto& res) {
|
2017-11-25 09:59:28 -07:00
|
|
|
res.set_content(req.body(), "text/plain");
|
|
|
|
})
|
|
|
|
.listen("localhost", 1234);
|
|
|
|
```
|
|
|
|
|
|
|
|
### Static File Server
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
svr.set_base_dir("./www");
|
|
|
|
```
|
|
|
|
|
|
|
|
### Logging
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
svr.set_logger([](const auto& req, const auto& res) {
|
|
|
|
your_logger(req, res);
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
### Error Handler
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
svr.set_error_handler([](const auto& req, auto& res) {
|
|
|
|
const char* fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
snprintf(buf, sizeof(buf), fmt, res.status);
|
|
|
|
res.set_content(buf, "text/html");
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2017-12-12 20:22:10 -07:00
|
|
|
### 'multipart/form-data' POST data
|
2017-12-05 17:30:13 -07:00
|
|
|
|
|
|
|
```cpp
|
2018-05-06 18:16:35 -07:00
|
|
|
svr.Post("/multipart", [&](const auto& req, auto& res) {
|
2017-12-05 17:30:13 -07:00
|
|
|
auto size = req.files.size();
|
|
|
|
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));
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2012-10-03 22:18:18 -07:00
|
|
|
Client Example
|
|
|
|
--------------
|
|
|
|
|
2017-12-05 17:30:13 -07:00
|
|
|
### GET
|
|
|
|
|
2015-02-06 20:43:06 -07:00
|
|
|
```c++
|
|
|
|
#include <httplib.h>
|
|
|
|
#include <iostream>
|
2012-10-03 22:18:18 -07:00
|
|
|
|
2015-02-06 20:43:06 -07:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
httplib::Client cli("localhost", 1234);
|
2012-10-03 22:18:18 -07:00
|
|
|
|
2018-05-06 18:16:35 -07:00
|
|
|
auto res = cli.Get("/hi");
|
2015-02-06 20:43:06 -07:00
|
|
|
if (res && res->status == 200) {
|
|
|
|
std::cout << res->body << std::endl;
|
2012-10-03 22:18:18 -07:00
|
|
|
}
|
2015-02-06 20:43:06 -07:00
|
|
|
}
|
|
|
|
```
|
2012-10-03 22:18:18 -07:00
|
|
|
|
2017-12-05 17:30:13 -07:00
|
|
|
### POST
|
|
|
|
|
|
|
|
```c++
|
2018-05-06 18:16:35 -07:00
|
|
|
res = cli.Post("/post", "text", "text/plain");
|
|
|
|
res = cli.Post("/person", "name=john1¬e=coder", "application/x-www-form-urlencoded");
|
2017-12-05 17:30:13 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
### POST with parameters
|
|
|
|
|
|
|
|
```c++
|
|
|
|
httplib::Map params;
|
|
|
|
params["name"] = "john";
|
|
|
|
params["note"] = "coder";
|
2018-05-06 18:16:35 -07:00
|
|
|
auto res = cli.Post("/post", params);
|
|
|
|
```
|
|
|
|
|
|
|
|
### PUT
|
|
|
|
|
|
|
|
```c++
|
|
|
|
res = cli.Post("/resource/foo", "text", "text/plain");
|
|
|
|
```
|
|
|
|
|
|
|
|
### DELETE
|
|
|
|
|
|
|
|
```c++
|
|
|
|
res = cli.Delete("/resource/foo");
|
|
|
|
```
|
|
|
|
|
|
|
|
### OPTIONS
|
|
|
|
|
|
|
|
```c++
|
|
|
|
res = cli.Options("*");
|
|
|
|
res = cli.Options("/resource/foo");
|
2017-12-05 17:30:13 -07:00
|
|
|
```
|
|
|
|
|
2017-12-30 12:47:55 -07:00
|
|
|
### Connection Timeout
|
|
|
|
|
|
|
|
```c++
|
|
|
|
httplib::Client cli("localhost", 8080, 5); // timeouts in 5 seconds
|
|
|
|
```
|
2017-11-24 19:39:17 -07:00
|
|
|
### With Progress Callback
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
httplib::Client client(url, port);
|
|
|
|
|
|
|
|
// prints: 0 / 000 bytes => 50% complete
|
2017-12-12 20:20:40 -07:00
|
|
|
std::shared_ptr<httplib::Response> res =
|
2018-05-06 18:16:35 -07:00
|
|
|
cli.Get("/", [](uint64_t len, uint64_t total) {
|
2017-12-12 20:20:40 -07:00
|
|
|
printf("%lld / %lld bytes => %d%% complete\n",
|
2017-11-24 19:39:17 -07:00
|
|
|
len, total,
|
|
|
|
(int)((len/total)*100));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2017-12-12 20:22:10 -07:00
|
|
|
![progress](https://user-images.githubusercontent.com/236374/33138910-495c4ecc-cf86-11e7-8693-2fc6d09615c4.gif)
|
|
|
|
|
|
|
|
This feature was contributed by [underscorediscovery](https://github.com/yhirose/cpp-httplib/pull/23).
|
|
|
|
|
2017-12-20 15:27:36 -07:00
|
|
|
### Range
|
2017-12-12 20:20:40 -07:00
|
|
|
|
|
|
|
```cpp
|
2017-12-20 15:27:36 -07:00
|
|
|
httplib::Client cli("httpbin.org", 80);
|
2017-12-12 20:20:40 -07:00
|
|
|
|
|
|
|
// 'Range: bytes=1-10'
|
|
|
|
httplib::Headers headers = { httplib::make_range_header(1, 10) };
|
|
|
|
|
2018-05-06 18:16:35 -07:00
|
|
|
auto res = cli.Get("/range/32", headers);
|
2017-12-12 20:20:40 -07:00
|
|
|
// res->status should be 206.
|
|
|
|
// res->body should be "bcdefghijk".
|
|
|
|
```
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
OpenSSL Support
|
|
|
|
---------------
|
|
|
|
|
|
|
|
SSL support is available with `CPPHTTPLIB_OPENSSL_SUPPORT`. `libssl` and `libcrypto` should be linked.
|
|
|
|
|
|
|
|
```c++
|
|
|
|
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
|
|
|
|
2017-08-28 06:33:27 -07:00
|
|
|
SSLServer svr("./cert.pem", "./key.pem");
|
2017-04-21 20:00:00 -07:00
|
|
|
|
|
|
|
SSLClient cli("localhost", 8080);
|
|
|
|
```
|
|
|
|
|
2017-12-28 18:47:52 -07:00
|
|
|
Zlib Support
|
|
|
|
------------
|
|
|
|
|
|
|
|
'gzip' compression is available with `CPPHTTPLIB_ZLIB_SUPPORT`.
|
|
|
|
|
|
|
|
The server applies gzip compression to the following MIME type contents:
|
|
|
|
|
|
|
|
* all text types
|
|
|
|
* image/svg+xml
|
|
|
|
* application/javascript
|
|
|
|
* application/json
|
|
|
|
* application/xml
|
|
|
|
* application/xhtml+xml
|
|
|
|
|
2017-12-20 15:27:36 -07:00
|
|
|
License
|
|
|
|
-------
|
|
|
|
|
2018-05-06 18:16:35 -07:00
|
|
|
MIT license (© 2018 Yuji Hirose)
|