HTTP client library for C ++
I am trying to write an HTTP proxy with firmware for learning C ++ / socket / HTTP
I'm looking for an HTTP client library like HttpURLConnection available in Java.
I've looked at some libraries like libcurl for C / C ++. These libraries can make an HTTP request, but they return full content. I need a library that can partially read the content in the buffer so that I can send it immediately to the requesting client without keeping all the content in memory.
Any links / suggestions are much appreciated :)
Thank you!
source to share
The libcurl docs has a sample page on how to get incremental load callbacks (into a memory buffer) as data streams from a request:
http://curl.haxx.se/libcurl/c/getinmemory.html
In your case, you are simply forwarding the data buffer to the client who originally made the request.
source to share
Beast is an open source C ++ library that supports the desired functionality. It has a "Writer" concept that supports suspend and resume, incremental body and coroutine rendering: http://vinniefalco.github.io/beast/beast/types/Writer.html
The library is located here: http://vinniefalco.github.io/
Here's a complete sample program:
#include <beast/http.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <string>
int main()
{
// Normal boost::asio setup
std::string const host = "boost.org";
boost::asio::io_service ios;
boost::asio::ip::tcp::resolver r(ios);
boost::asio::ip::tcp::socket sock(ios);
boost::asio::connect(sock,
r.resolve(boost::asio::ip::tcp::resolver::query{host, "http"}));
// Send HTTP request using beast
beast::http::request_v1<beast::http::empty_body> req;
req.method = "GET";
req.url = "/";
req.version = 11;
req.headers.replace("Host", host + ":" + std::to_string(sock.remote_endpoint().port()));
req.headers.replace("User-Agent", "Beast");
beast::http::prepare(req);
beast::http::write(sock, req);
// Receive and print HTTP response using beast
beast::streambuf sb;
beast::http::response_v1<beast::http::streambuf_body> resp;
beast::http::read(sock, sb, resp);
std::cout << resp;
}
source to share