Skip to content

OlexiyKhokhlov/h2pp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Support Ukraine Ubuntu-x64 Windows-x64

h2pp - modern C++ HTTP/2 client library

This is an implementation of HTTP/2 in C++20 that works over boost asio

The main goal of the library is provide a simple and usefull HTTP/2 client.

Example

This is a simple command line application that sends a GET request by the given url. A full buildable sample is here

#include <iostream>
#include <thread>

#include <boost/asio/io_context.hpp>
#include <boost/asio/use_future.hpp>
#include <boost/url.hpp>

#include <client_session.h>

boost::asio::io_context io_context;

int main(int argc, char **argv) {
  if (argc != 2) {
    std::cerr << "Usage: https_client <url>" << std::endl;
    return EXIT_FAILURE;
  }

  http2::client_session<http2::connection<>> client(io_context);

  std::jthread iothr([]() {
    boost::asio::io_context::work work(io_context);
    io_context.run();
  });

  try {
    auto url = boost::urls::parse_uri(argv[1]);
    if (url.has_error()) {
      return EXIT_FAILURE;
    }

    std::string service = url->port();
    if (service.empty()) {
      service = "443";
    }

    client
        .async_connect(
            url->host_name(), service,
            [](boost::system::error_code ec) {
              if (ec) {
                std::cerr << "HTTP2 Client unexpectedly stopped: " << ec << std::endl;
              }
            },
            boost::asio::use_future)
        .get();

    http2::request request;
    request.method(http2::method::GET).url(argv[1]).headers({{"accept", "*/*"}, {"user-agent", "h2pp/0.0.1"}});
    auto response = client.async_send(std::move(request), boost::asio::use_future).get();
    for (const auto &h : response.headers()) {
      std::cout << h.name_view() << ":  " << h.value_view() << std::endl;
    }
    std::cout << response.body_as<std::string>() << std::endl;

    client.async_disconnect(boost::asio::use_future).get();
  } catch (std::exception &e) {
    std::cerr << "Exception: " << e.what() << std::endl;
  }

  io_context.stop();
  iothr.join();

  return EXIT_SUCCESS;
}

Dependencies

Development status

I've fixed al known bugs so it works... But it isn't so well tested as required to say - release. Also is not implemented:

  • pushes;
  • stream priorities;
  • dynamic settings changing;

License

Distributed under the GNU Aferro General Public License v3.0

If need more just ask me. Everything is negotiable.

Usefull links

Releases

No releases published

Packages

No packages published