5

I'm trying to write milt-threaded HTTP proxy for learning C++/socket/HTTP

I'm looking for a HTTP client library like HttpURLConnection available in Java.

I looked at some libraries eg, libcurl for C/C++. These libraries can make http request but they will return with the full content. I need a library that can read content partially in a buffer so that I'm able to ship it immediately to requesting client, without storing the entire content in memory.

Any links/suggestions is highly appreciated :)

thank you!

user406481
  • 145
  • 2
  • 6
  • Have you tried this : [Ninja_Search](http://www.google.com/search?q=http+library+C%2B%2B&sitesearch=stackoverflow.com/questions&qscrl=1) – gideon Mar 18 '11 at 05:12
  • 1
    "[libcurl] can make http request but they will return with the full content." With libcurl you set the function you want to use when it receives data and it calls that function with each chunk of data -- you don't likely get the whole response at once (unless it's very small). See http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTWRITEFUNCTION – user470379 Mar 18 '11 at 05:17

4 Answers4

5

libcurl docs have an example page on how to get incremental download callbacks (into a memory buffer) as data streams in from a request:

http://curl.haxx.se/libcurl/c/getinmemory.html

In your case, you would just forward the data buffer on to the client that originally made the request.

selbie
  • 100,020
  • 15
  • 103
  • 173
4

Beast is an open source C++ library which supports the functionality you desire. It has a "Writer" concept which supports suspending and resuming, incremental rendering of the body, and coroutines: http://vinniefalco.github.io/beast/beast/types/Writer.html

The library is here: http://vinniefalco.github.io/

Here's a complete example 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;
}
Vinnie Falco
  • 5,173
  • 28
  • 43
2

You can try cpp-netlib

Sergei Nikulov
  • 5,029
  • 23
  • 36
1

Microsoft released "cpprestsdk" a modern, cross-platform, asynchronous rest client / server with json serialization / deserialization support. https://github.com/microsoft/cpprestsdk

mancini0
  • 4,285
  • 1
  • 29
  • 31