Git Product home page Git Product logo

beast_http_server's Introduction

SYNOPSIS Build Status

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. The implementations websocket is here...

FEATURES

  • Header-only
  • HTTP 1.0 / 1.1
  • TLS/SSL
  • Pipeline
  • Asynchronous/Synchronous request, response handling
  • Thread pool support
  • Timer manage (default timeout: 10 seconds, default action: Closing connection)
  • Platform independent
  • Simple way to add REST resources using regex for path, and anonymous functions

DEPENDENCIES

USAGE

More examples, an also JSON-RPC 2.0 server is here...

Add new route for GET request with "/" resource:

    http::server my_http_server;

    my_http_server.get("/", [](auto & req, auto & /*session*/){
        cout << req << endl;
    });

    // or

    my_http_server.get("/a/b", [](auto & /*req*/, auto & /*session*/, auto & next){
        // process '/a'
        next();
    }, [](auto & /*req*/, auto & /*session*/){
        // process '/b'
    });

Add a route using a literal operator (as above)

    "/"_get.bind(my_http_server, [](auto & req, auto & /*session*/){
        cout << req << endl;
    });

Getting a parameter from a URI /user/1992 :

    my_http_server.param<int>().get("/user/(\\d+)",
       [](auto & /*req*/, auto & /*session*/, auto & args){
        assert(args._1 == 1992);
    });

    // or

    my_http_server.param<int>().get("/user/(\\d+)",
       [](auto & /*req*/, auto & /*session*/, auto & next, auto & /*args*/){
        // process '/user'
        next();
    }, [](auto & /*req*/, auto & /*session*/, auto & args){
        // process '/id'
        assert(args._1 == 1992);
    });

Create modular, mounted route handlers:

    auto animals_router = my_http_server.BasicRouter();

    animals_router.get("/cat", [](auto & req, auto & session){ // '/animals/cat'
        session.do_write(make_response(req, "me-ow\n"));
    });

    animals_router.get("/dog", [](auto & req, auto & session){ // '/animals/dog'
        session.do_write(make_response(req, "aw! aw! Rrrrr\n"));
    });

    animals_router.get("/mouse", [](auto & req, auto & session){ // '/animals/mouse'
        session.do_write(make_response(req, "...\n"));
    });

    animals_router.get("[/]??", [](auto & req, auto & session){ // '/animals' or '/animals/'
        session.do_write(make_response(req, "animals home page\n"));
    });

    my_http_server.use("/animals", animals_router);

Create handlers routes, forming a chain, for the route path:

    auto books_router = my_http_server.ChainRouter();

    books_router.route("/book") // 'books/book'
      .get([](auto & req, auto & session){
        session.do_write(make_response(req, "get a random book\n"));
    }).post([](auto & req, auto & session){
        session.do_write(make_response(req, "add a book\n"));
    }).put([](auto & req, auto & session){
        session.do_write(make_response(req, "update the book\n"));
    });

    my_http_server.use("/books", books_router);

Start listening on localhost:80

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

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

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

Request content from the server (Async):

    http::client my_http_client;

    my_http_client.on_connect = [](auto & session){
        http::base::out("Successful connected!");
        session.do_write(make_request());
    };

    my_http_client.on_message = [](auto & res, auto & session){
        cout << res << endl;
        
        // The answer is received, stop!
        session.do_close();
        http::base::processor::get().stop();
    };

    if(!my_http_client.invoke("www.example.com", 80, [](auto & error){
        cout << "Connection failed with code " << error << endl;
        http::base::processor::get().stop();
    })){
        cout << "Failed to resolve address!" << endl;
        return -1;
    }

Request content from the server (Sync):

    boost::system::error_code ec;
    auto connection_p = http::base::processor::get().create_connection
                                         <http::base::connection>("www.example.com", 80, ec);

    if(!connection_p){
        cout << "Failed to resolve address!" << endl;
        return -1;
    }

    if(ec){
        cout << "Connection invalid!" << endl;
        return -1;
    }
    
    // Send to host
    if(http::send_request(connection_p, make_request())){
        cout << "http::send_request fail!" << endl;
        return -1;
    }

    auto res = make_response();
    // Receive from host
    if(http::recv_responce(connection_p, res)){
        cout << "http::recv_responce fail!" << endl;
        return -1;
    }

    cout << res << endl;

LICENSE

Copyright © 2018 0xdead4ead

BSD 2-Clause "Simplified" License

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.