Giter Site home page Giter Site logo

alexandreofbh / beast_http_server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 0xdead4ead/beasthttp

0.0 2.0 0.0 32 KB

Easy HTTP library implemented using C++14 and Boost.Beast. Allows you to get or provide REST resources available from an application in C ++.

C++ 100.00%

beast_http_server's Introduction

SYNOPSIS

Easy HTTP library implemented using C++14 and Boost.Beast. Allows you to get or provide REST resources available from an application in C ++. Use all the features of the Boost.Beast when constructing queries and answers.

base.hpp: Add/remove listener, create connections, manage signals, io, ...

session.hpp: Handles an HTTP server/client connection

client.hpp: Class for communication with a remote host

server.hpp: HTTP server class, registration of new routes

list_cb.hpp: Container for storing callback functions

FEATURES

  • Header-only
  • HTTP 1.0 / 1.1 / 2.0
  • Asynchronous request, response handling
  • Thread pool support
  • Platform independent
  • Simple way to add REST resources using regex for path, and anonymous functions

DEPENDENCIES

  • Boost.Regex, Boost.Asio & Boost.Beast library
  • Minimal C++ 14 compatible compilator (for generic lambda)

USAGE

Server and client examples -> ./examples

Run the I/O service on the requested number of threads

    uint32_t pool_size = boost::thread::hardware_concurrency();
    http::base::processor::get().start(pool_size == 0 ? 4 : pool_size << 1);
    http::base::processor::get().wait();

Add new route for GET request with "/" resource

    http::server my_http_server;

    my_http_server.route("^/$", http::method_t::get, [](auto & req, auto & session){
        cout << req << endl;
    });

    // and chain handling

    my_http_server.route("^/a/b$", http::method_t::get, [](auto & req, auto & session, auto & next){
        // process a
        next();
    }, [](auto & req, auto & session){
        // process b
    });

Start listening on localhost:80

    my_http_server.listen("127.0.0.1", 80, [](auto & session){
        http::base::out("New client!!!");
        session.do_read();
    });

Request content from the server

    http::client my_http_client;

    const auto & host = "www.google.com";
    const auto & port = 80;

    const auto & on_connect = [&host](auto & session){
        http::base::out("Successful connected!");

        // Create request and fill fields...
        
        // Send 
        // session.do_write(boost::move(req));
    };

    // Handler on received object
    const auto & on_receive = [](auto & res, auto & session){
        cout << res << endl;

        session.do_close();
        // The answer is received, stop!
        http::base::processor::get().stop();
    };

    // Connect to host:port
    my_http_client.invoke(host, port, on_connect, on_receive);

beast_http_server's People

Contributors

0xdead4ead avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.