https://github.com/uctakeoff/uc-curl
uc::curl is a libcurl wrapper library created C++11 single-header.
https://github.com/uctakeoff/uc-curl
http http-client libcurl wrapper
Last synced: 3 days ago
JSON representation
uc::curl is a libcurl wrapper library created C++11 single-header.
- Host: GitHub
- URL: https://github.com/uctakeoff/uc-curl
- Owner: uctakeoff
- License: mit
- Created: 2017-01-22T05:49:57.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-01-28T13:19:36.000Z (over 4 years ago)
- Last Synced: 2023-09-04T06:31:17.278Z (over 2 years ago)
- Topics: http, http-client, libcurl, wrapper
- Language: C++
- Size: 54.7 KB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# uc::curl
uc::curl is a libcurl wrapper library created C++11 single-header.
It depends only on libcurl and STL.
```cpp:sample.cpp
#include
#include
#include "uccurl.h"
int main()
{
try {
uc::curl::global libcurlInit;
std::string data;
uc::curl::easy("http://www.example.com/") >> data;
} catch (std::exception& ex) {
std::cerr << "exception : " << ex.what() << std::endl;
return 1;
}
return 0;
}
```
build
```bash
$ g++ sample.cpp -std=c++11 -lcurl
```
## License
MIT-Lisence
## EASY interface
### Simple to use.
[sample.c](https://curl.haxx.se/libcurl/c/simple.html) above is roughly rewritten as follows.
```cpp
// See https://curl.haxx.se/libcurl/c/simple.html
#include
#include "uccurl.h"
int main()
{
try {
uc::curl::easy("http://example.com").perform();
} catch (std::exception& ex) {
std::cerr << "exception : " << ex.what() << std::endl;
return 1;
}
return 0;
}
```
`uc::curl::easy("http://example.com")` has the same effect as:
```c
CURL* curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 20L);
```
The value of [`CURLOPT_MAXREDIRS`](https://curl.haxx.se/libcurl/c/CURLOPT_MAXREDIRS.html) can be specified as the second argument. The default value is 20.
* `uc::curl::easy("http://example.com", 0)` will make libcurl refuse any redirect.
* `uc::curl::easy("http://example.com", -1)` for an infinite number of redirects.
### setopt
There are several ways.
```cpp
uc::curl::easy curl;
curl.setopt(CURLOPT_VERBOSE, 1L); // OK: Compatible with conventional
curl.setopt(1L); // OK: argument type check
curl.setopt(); // OK: If no argument is specified, 1L is specified.
// curl.setopt("text"); // Complie error
std::string url("http://example.com");
curl.uri(url); // Dedicated function
curl.setopt(CURLOPT_URL, url.c_str());
curl.setopt(url);
```
### getinfo
Automatically resolve type.
```cpp
const char* url = curl.uri(); // getinfo();
const char* type = curl.getinfo();
long code = curl.getinfo();
double time = curl.getinfo();
curl_certinfo* info = curl.getinfo();
uc::curl::slist list = curl.getinfo();
```
### GET method
You can use `std::string`, `std::ostream`, `size_t(const char*, size_t)` for the `operator>>()`.
```cpp
uc::curl::easy curl("http://example.com");
// output to cerr
curl >> std::cerr;
// output file
curl >> std::ofstream("page.out", std::ios::out | std::ios::binary);
// get in memory
std::string response;
curl >> response;
// callback function
curl >> [](const char* ptr, size_t size) {
std::cout << "## receive : " << size << "bytes\n"
<< std::string(ptr, size) << "\n\n";
return size;
};
```
`operator>>()` performs the following processing in order.
1. Set [`CURLOPT_WRITEDATA`](https://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html) and [`CURLOPT_WRITEFUNCTION`](https://curl.haxx.se/libcurl/c/CURLOPT_WRITEDATA.html).
1. Call `uc::curl::easy::perform()`.
1. Clear [`CURLOPT_WRITEDATA`](https://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html) and [`CURLOPT_WRITEFUNCTION`](https://curl.haxx.se/libcurl/c/CURLOPT_WRITEDATA.html).
### POST method
[simplepost.c](https://curl.haxx.se/libcurl/c/simplepost.html) above is roughly rewritten as follows.
```cpp
// POST string
uc::curl::easy(url).postfields("moo mooo moo moo").perform();
```
`postfields()` can take `std::string`, `std::istream`, `uc::curl::form` as arguments.
```cpp
// POST file
std::ifstream is("formdata.txt", std::ios::in | std::ios::binary);
uc::curl::easy(url).postfields(is).perform();
```
```cpp
// POST form data
uc::curl::form formpost;
formpost
.file("sendfile", "postit2.c")
.contents("filename", "postit2.c")
.contents("submit", "send");
uc::curl::easy(url).postfields(formpost).perform();
```
`uc::curl::form` is a `struct curl_httppost` wrapper using `std::unique_ptr`.
### PUT method
```cpp
std::ifstream is(filename, std::ios::in | std::ios::binary);
uc::curl::easy(uri).setopt().body(is).perform();
```
### HEAD method
```cpp
// You can use `std::string`, `std::ostream`, `size_t(const char*, size_t)` for the `response_header()`.
auto resheader = [](const char* ptr, size_t size) {
std::cout << "###" << std::string(ptr, size);
return size;
};
uc::curl::easy(url).setopt().response_header(resheader).perform();
```
### DELETE method
```cpp
uc::curl::easy(url).setopt("DELETE").perform();
```
### uc::curl::slist
`uc::curl::slist` is a `struct curl_slist` wrapper using `std::unique_ptr`.
set sample
```cpp
auto chunk = uc::curl::create_slist(
"Accept:",
"Another: yes",
"Host: example.com",
"X-silly-header;");
uc::curl::easy(url).header(chunk).perform();
```
get sample
```cpp
uc::curl::slist list = curl.getinfo();
// e : const char*
for (auto&& e : list) {
std::cout << e << "\n";
}
```
## MULTI interface
### Simple to use.
[multi-single.c](https://curl.haxx.se/libcurl/c/multi-single.html) above is roughly rewritten as follows.
```cpp
#include
#include
#include
#include "uccurl.h"
int main()
{
try {
uc::curl::global libcurlInit;
uc::curl::easy http_handle("http://www.example.com/");
uc::curl::multi multi_handle;
multi_handle.add(http_handle);
while (multi_handle.perform() > 0) {
multi_handle.poll(std::chrono::seconds{1});
}
multi_handle.remove(http_handle);
} catch (std::exception& ex) {
std::cerr << "exception : " << ex.what() << std::endl;
return 1;
}
return 0;
}
```
### `curl_multi_info_read()` API
Instead of `curl_multi_info_read()`, there are `for_each_done_info()`.
before
```cpp
while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
if (msg->msg == CURLMSG_DONE) {
char *url;
curl_easy_getinfo(msg->easy_handle, CURLINFO_EFFECTIVE_URL, &url);
printf("%d : %s\n", msg->data.result, url);
}
}
```
after
```
multi_handle.for_each_done_info([](uc::curl::easy_ref&& h, CURLcode result) {
std::cout << result << " : " << h.uri() << "\n";
});
```