Giter VIP home page Giter VIP logo

belle's Introduction

Belle

An HTTP / Websocket library in C++17 using Boost.Beast and Boost.ASIO.

Belle enables C++ programs to communicate asynchronously over HTTP and Websockets. It aims to have an intuitive API, reasonable defaults, and great performance.

A Brief Tour

An HTTP server listening on 127.0.0.1:8080 that responds to a POST request to the path '/'.

#include "belle.hh"
namespace Belle = OB::Belle;

#include <string>

int main()
{
  // init the server with local address and port
  Belle::Server app {"127.0.0.1", 8080};

  // handle route POST '/'
  app.on_http("/", Belle::Method::post, [](auto& ctx)
  {
    // echo back the request body
    ctx.res.body() = ctx.req.body();
  });

  // start the server
  app.listen();

  return 0;
}

An HTTP client connecting to 127.0.0.1:8080 that sends a POST request to the path '/'.

#include "belle.hh"
namespace Belle = OB::Belle;

#include <string>
#include <iostream>

int main()
{
  // init the client with remote address and port
  Belle::Client app {"127.0.0.1", 8080};

  // request parameters and response handler
  app.on_http(Belle::Method::post, "/", "Hello, Belle!", [](auto& ctx)
  {
    // print the response body
    std::cout << ctx.res.body() << "\n";
  });

  // start the client
  app.connect();

  return 0;
}

About

Belle is a single header C++17 library for working with HTTP and Websockets. It utilizes the HTTP and Websocket functionalities of Boost.Beast, along with the asynchronous networking capabilities of Boost.ASIO.

The design goals for Belle are the following:

  • Intuitive API: Interactions with the library should be as straightforward and concise as that of a modern scripting language.
  • Reasonable Defaults: The default state of data and behaviour should be logical and expected, allowing customization when needed.
  • Great Performance: Speed and memory usage is important and sought after, but not at the cost of diminishing the goals listed above.

There are many HTTP and Websocket libraries currently available, each with their own goals and advantages. As the networking layer is often a crucial component in your program, make sure to browse around and select a library that is going to be the most beneficial for you.

This library is undergoing active development and is working its way towards a 1.0.0 release. Be aware that while the major version is zero, 0.y.z, the public API should not be considered stable, and is subject to change at any time.

Belle welcomes and encourages your thoughts and feedback!

The following lists describe an overview of the current implemented features, along with the planned future features:

Server Features

  • HTTP 1.0 / 1.1 server (async, SSL / TLS, multithreaded)
  • Websocket server (async, SSL / TLS)
  • Serve static content
  • Serve dynamic content
  • Use lambdas as handlers
  • Routes can match a single, multiple, or all HTTP methods
  • Use regular expressions with capture groups to match and tokenize routes
  • Parse query parameters
  • URL percent-decode query parameters
  • Handle HTTP and Websocket on the same port
  • Group Websocket connections into channels for broadcasting

Client Features

  • HTTP 1.0 / 1.1 client (async, SSL / TLS)
  • Use lambdas as handlers
  • Serialize query parameters
  • URL percent-encode query parameters

Future Features

  • Add an async, SSL / TLS Websocket client
  • add Websocket ping, pong, and timeout
  • Add an interface for a custom logger
  • Add connection manager for clean shutdown
  • Chunked encoding support
  • Request and response streaming support
  • General optimizations to improve performance

Install

Belle is a single header file. It can either be installed into your systems include directory, or copied directly into your projects directory.

The default install location is /usr/local/include/ob/belle.hh.

To install on your system:

$ ./install.sh

To copy to your project:

$ cp ./include/belle.hh ./<project>

Build

Environment

  • Linux (supported)
  • BSD (untested)
  • macOS (untested)
  • Windows (untested)

Requirements

  • C++17 compiler
  • Boost >= 1.67
  • OpenSSL >= 1.1.0 (if SSL is enabled)
  • CMake >= 3.8 (to build examples)

Dependencies

  • ssl (libssl)
  • crypto (libcrypto)
  • pthread (libpthread)
  • boost (libboost_system)

Flags

Use the following define flags at compile time to alter the library:

  • -D OB_BELLE_CONFIG_SSL_OFF
    • Disable SSL support
  • -D OB_BELLE_CONFIG_CLIENT_OFF
    • Disable client support
  • -D OB_BELLE_CONFIG_SERVER_OFF
    • Disable server support

Usage

If Belle is installed on your system:

#include <ob/belle.hh>
namespace Belle = OB::Belle;

If Belle is copied to your project directory:

#include "belle.hh"
namespace Belle = OB::Belle;

Make sure to link the Dependencies, along with meeting the Requirements listed above.

Documentation

The source code contains helpful comments and explanations. A detailed set of documentation and guides are planned for the future.

Basic documentation can be generated using Doxygen:

$ doxygen ./.doxyfile

Examples

There are several examples located in the ./example directory. Each example has its own CMake build file, and can be built in either Debug or Release mode by passing the appropriate CMake flag, -DCMAKE_BUILD_TYPE=Debug or -DCMAKE_BUILD_TYPE=Release. Several examples rely on relative paths, therefore, they expect to be executed from a specific path. The example programs should be executed in their respective build directory.

Within an examples directory, the following will build and run it in Debug mode.

$ mkdir -p ./build
$ cd ./build
$ cmake ../ -DCMAKE_BUILD_TYPE=Debug
$ make
$ ./app

The examples depend on the Belle source file in the ./include directory. To use one of the examples as a starting point, copy the selected example to a new directory, and then follow the Install and Build instructions listed above.

Server

  • hello: an HTTP server with a get endpoint, post endpoint, and static file handling
  • http: a multithreaded HTTP server with multiple endpoints, static file handling, signal handling, and error handling
  • https: a multithreaded HTTPS server with multiple endpoints, static file handling, signal handling, and error handling
  • chat: a single threaded HTTP / Websocket chat room server with multiple endpoints, static file handling, signal handling, and error handling, with a basic html/css/js client interface
  • chat-ssl: a single threaded HTTPS / Websocket Secure chat room server with multiple endpoints, static file handling, signal handling, and error handling, with a basic html/css/js client interface

Client

  • http: an HTTP client that makes multiple requests to a remote endpoint
  • https: an HTTPS client that makes multiple requests to a remote endpoint

Tests

There are currently no tests at this time, but there are plans to add them in the future.

Benchmarks

There are currently no benchmarks at this time, but there are plans to add them in the future.

Versioning

This project uses Semantic Versioning.

License

This project is licensed under the MIT License.

Copyright (c) 2018 Brett Robinson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

belle's People

Contributors

octobanana avatar wolfgangdvelop avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

belle's Issues

Compiler Error C2888 on Visual Studio (15.8.9)

Good morning,
I've tried to compile your library (the first, very very simple example for a http server that just echos back the request body) using microsoft visual studio (v15.8.9) and there a a lot of C2888 errors:

1> c:\dev\packages.conan\data\belle\0.9.0\auto\stable\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\belle\include\belle.hh(1201): error C2888: 'void <lambda_59fc3b57f109080765fee6635e6fcd48>::operator ()(_T1,_T2 &&) const': symbol cannot be defined within namespace 'Belle'
1> c:\dev\packages.conan\data\belle\0.9.0\auto\stable\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\belle\include\belle.hh(1216): error C2888: 'void <lambda_59fc3b57f109080765fee6635e6fcd48>::<lambda_invoker_cdecl>(_T1,_T2 &&)': symbol cannot be defined within namespace 'Belle'

=> Compiler Error C2888
'identifier' : symbol cannot be defined within namespace 'namespace'
https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-2/compiler-error-c2888?view=vs-2017

simple example not compiling

This is the first example in the README (called http.cpp here).

$ g++ -std=c++17 http.cpp -Wfatal-errors                                                                                                                 
In file included from http.cpp:1:
belle/include/belle.hh:618:24: error: ‘OB::Belle::websocket::role_type’ has not been declared
  618 |   friend void teardown(websocket::role_type,
      |                        ^~~~~~~~~

$ g++ -std=c++17 http.cpp -Wfatal-errors                                                                                                                    In file included from http.cpp:1:
belle/include/belle.hh:618:24: error: ‘OB::Belle::websocket::role_type’ has not been declared
  618 |   friend void teardown(websocket::role_type,
      |                        ^~~~~~~~~

Using Boost 1.71

Mvc pattern

Does this application implements model view controller approach .. one extra question is this project alive..

Suggest use of clang-format

It's hard to comply to the coding style, a clang-format file would help at some points:

The "nearest" style actually seems something like

Language: Cpp
BasedOnStyle: Mozilla
AlignAfterOpenBracket: DontAlign
AlignOperands: false
AlignTrailingComments: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
  AfterClass: true
  AfterControlStatement: true
  AfterFunction: true
  AfterNamespace: true
  AfterStruct: true
  BeforeCatch: true
  BeforeElse: true
BreakBeforeBraces: Custom
BreakConstructorInitializers: AfterColon
ColumnLimit: 0
Cpp11BracedListStyle: true
MaxEmptyLinesToKeep: 1
SortIncludes: false

(Used whatstyle to analyze).

What do you think about adding some reasonable style format?

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.