mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-21 11:39:48 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3da4a0ac69 | ||
|
|
9d12b3f20e | ||
|
|
852a374748 | ||
|
|
3b5bab3308 | ||
|
|
69e75f4a67 | ||
|
|
b0fd4befb1 | ||
|
|
3e80666a74 | ||
|
|
3e4567bae8 | ||
|
|
db075d8cf9 | ||
|
|
16df0ef37e | ||
|
|
f1a2ac5108 | ||
|
|
e5743b358d | ||
|
|
1184bbe4cb | ||
|
|
9dcffda7ae | ||
|
|
e5903635e2 | ||
|
|
510b4eaaae | ||
|
|
d7e63b4316 | ||
|
|
e5dd410256 | ||
|
|
951e46929e | ||
|
|
c8adac30f4 | ||
|
|
b2b4f7635f | ||
|
|
649b1d2172 | ||
|
|
dc5f9ba164 |
No files matched your search
@@ -66,6 +66,7 @@ if(Git_FOUND)
|
|||||||
# Gets the latest tag as a string like "v0.6.6"
|
# Gets the latest tag as a string like "v0.6.6"
|
||||||
# Can silently fail if git isn't on the system
|
# Can silently fail if git isn't on the system
|
||||||
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0
|
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0
|
||||||
|
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||||
OUTPUT_VARIABLE _raw_version_string
|
OUTPUT_VARIABLE _raw_version_string
|
||||||
ERROR_VARIABLE _git_tag_error
|
ERROR_VARIABLE _git_tag_error
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,8 +7,41 @@ A C++11 single-file header-only cross platform HTTP/HTTPS library.
|
|||||||
|
|
||||||
It's extremely easy to setup. Just include **httplib.h** file in your code!
|
It's extremely easy to setup. Just include **httplib.h** file in your code!
|
||||||
|
|
||||||
Server Example
|
NOTE: This is a 'blocking' HTTP library. If you are looking for a 'non-blocking' library, this is not the one that you want.
|
||||||
--------------
|
|
||||||
|
Simple examples
|
||||||
|
---------------
|
||||||
|
|
||||||
|
#### Server
|
||||||
|
|
||||||
|
```c++
|
||||||
|
httplib::Server svr;
|
||||||
|
|
||||||
|
svr.Get("/hi", [](const httplib::Request &, httplib::Response &res) {
|
||||||
|
res.set_content("Hello World!", "text/plain");
|
||||||
|
});
|
||||||
|
|
||||||
|
svr.listen("0.0.0.0", 8080);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Client
|
||||||
|
|
||||||
|
```c++
|
||||||
|
httplib::Client cli("http://cpp-httplib-server.yhirose.repl.co");
|
||||||
|
|
||||||
|
auto res = cli.Get("/hi");
|
||||||
|
|
||||||
|
res->status; // 200
|
||||||
|
res->body; // "Hello World!"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Try out the examples on Repl.it!
|
||||||
|
|
||||||
|
1. Run server at https://repl.it/@yhirose/cpp-httplib-server
|
||||||
|
2. Run client at https://repl.it/@yhirose/cpp-httplib-client
|
||||||
|
|
||||||
|
Server
|
||||||
|
------
|
||||||
|
|
||||||
```c++
|
```c++
|
||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
@@ -175,6 +208,7 @@ svr.Get("/stream", [&](const Request &req, Response &res) {
|
|||||||
|
|
||||||
res.set_content_provider(
|
res.set_content_provider(
|
||||||
data->size(), // Content length
|
data->size(), // Content length
|
||||||
|
"text/plain", // Content type
|
||||||
[data](size_t offset, size_t length, DataSink &sink) {
|
[data](size_t offset, size_t length, DataSink &sink) {
|
||||||
const auto &d = *data;
|
const auto &d = *data;
|
||||||
sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE));
|
sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE));
|
||||||
@@ -184,6 +218,25 @@ svr.Get("/stream", [&](const Request &req, Response &res) {
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Without content length:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
svr.Get("/stream", [&](const Request &req, Response &res) {
|
||||||
|
res.set_content_provider(
|
||||||
|
"text/plain", // Content type
|
||||||
|
[&](size_t offset, size_t length, DataSink &sink) {
|
||||||
|
if (/* there is still data */) {
|
||||||
|
std::vector<char> data;
|
||||||
|
// prepare data...
|
||||||
|
sink.write(data.data(), data.size());
|
||||||
|
} else {
|
||||||
|
sink.done(); // No more data
|
||||||
|
}
|
||||||
|
return true; // return 'false' if you want to cancel the process.
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
### Chunked transfer encoding
|
### Chunked transfer encoding
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
@@ -193,7 +246,7 @@ svr.Get("/chunked", [&](const Request& req, Response& res) {
|
|||||||
sink.write("123", 3);
|
sink.write("123", 3);
|
||||||
sink.write("345", 3);
|
sink.write("345", 3);
|
||||||
sink.write("789", 3);
|
sink.write("789", 3);
|
||||||
sink.done();
|
sink.done(); // No more data
|
||||||
return true; // return 'false' if you want to cancel the process.
|
return true; // return 'false' if you want to cancel the process.
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -275,8 +328,8 @@ svr.new_task_queue = [] {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Client Example
|
Client
|
||||||
--------------
|
------
|
||||||
|
|
||||||
```c++
|
```c++
|
||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
@@ -286,10 +339,14 @@ int main(void)
|
|||||||
{
|
{
|
||||||
httplib::Client cli("localhost", 1234);
|
httplib::Client cli("localhost", 1234);
|
||||||
|
|
||||||
auto res = cli.Get("/hi");
|
if (auto res = cli.Get("/hi")) {
|
||||||
if (res && res->status == 200) {
|
if (res->status == 200) {
|
||||||
std::cout << res->body << std::endl;
|
std::cout << res->body << std::endl;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
auto err = res.error();
|
||||||
|
...
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -434,8 +491,7 @@ auto res = cli_.Post(
|
|||||||
httplib::Client client(url, port);
|
httplib::Client client(url, port);
|
||||||
|
|
||||||
// prints: 0 / 000 bytes => 50% complete
|
// prints: 0 / 000 bytes => 50% complete
|
||||||
std::shared_ptr<httplib::Response> res =
|
auto res = cli.Get("/", [](uint64_t len, uint64_t total) {
|
||||||
cli.Get("/", [](uint64_t len, uint64_t total) {
|
|
||||||
printf("%lld / %lld bytes => %d%% complete\n",
|
printf("%lld / %lld bytes => %d%% complete\n",
|
||||||
len, total,
|
len, total,
|
||||||
(int)(len*100/total));
|
(int)(len*100/total));
|
||||||
@@ -541,9 +597,9 @@ NOTE: cpp-httplib currently supports only version 1.1.1.
|
|||||||
```c++
|
```c++
|
||||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
|
|
||||||
SSLServer svr("./cert.pem", "./key.pem");
|
httplib::SSLServer svr("./cert.pem", "./key.pem");
|
||||||
|
|
||||||
SSLClient cli("localhost", 8080);
|
httplib::SSLClient cli("localhost", 1234); // or `httplib::Client cli("https://localhost:1234");`
|
||||||
cli.set_ca_cert_path("./ca-bundle.crt");
|
cli.set_ca_cert_path("./ca-bundle.crt");
|
||||||
cli.enable_server_certificate_verification(true);
|
cli.enable_server_certificate_verification(true);
|
||||||
```
|
```
|
||||||
|
|||||||
+2
-2
@@ -23,12 +23,12 @@ int main(void) {
|
|||||||
httplib::Client cli("localhost", 8080);
|
httplib::Client cli("localhost", 8080);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
auto res = cli.Get("/hi");
|
if (auto res = cli.Get("/hi")) {
|
||||||
if (res) {
|
|
||||||
cout << res->status << endl;
|
cout << res->status << endl;
|
||||||
cout << res->get_header_value("Content-Type") << endl;
|
cout << res->get_header_value("Content-Type") << endl;
|
||||||
cout << res->body << endl;
|
cout << res->body << endl;
|
||||||
} else {
|
} else {
|
||||||
|
cout << "error code: " << res.error() << std::endl;
|
||||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
auto result = cli.get_openssl_verify_result();
|
auto result = cli.get_openssl_verify_result();
|
||||||
if (result) {
|
if (result) {
|
||||||
|
|||||||
@@ -17,12 +17,12 @@ int main(void) {
|
|||||||
auto scheme_host_port = "http://localhost:8080";
|
auto scheme_host_port = "http://localhost:8080";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
auto res = httplib::Client(scheme_host_port).Get("/hi");
|
if (auto res = httplib::Client(scheme_host_port).Get("/hi")) {
|
||||||
|
|
||||||
if (res) {
|
|
||||||
cout << res->status << endl;
|
cout << res->status << endl;
|
||||||
cout << res->get_header_value("Content-Type") << endl;
|
cout << res->get_header_value("Content-Type") << endl;
|
||||||
cout << res->body << endl;
|
cout << res->body << endl;
|
||||||
|
} else {
|
||||||
|
cout << res.error() << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+338
-175
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user