Giter Club home page Giter Club logo

simplecpphttp's Introduction

Simple CPPHTTP

Simple HTTP server implementation using linux epoll, sockets, STL library.

Quick start

HTTP Server:

  • Create logger and HTTP Handler
    #include "Logger/SimpleLogger.h"
    #include "implementations/http_handler/HTTPHandler.h"
    
    ...
    logs::SimpleLogger logger;
    // Create HTTP handler
    cpphttp::HTTPHandler httpHandler(16 // read size of raw response
                                     , logger); // logger
    ...
    
  • Add endpoint to handler
    // endpoint '/' - return { "hello" : "world" }
    httpHandler.addResource("/", cpphttp::GET, [&](std::shared_ptr<cpphttp::Request> &req, std::shared_ptr<cpphttp::Response> &resp) {
        resp->headers.emplace("Content-Type", "application/json");
        Document d(rapidjson::kObjectType);
        Value v(rapidjson::kStringType);
        v.SetString("world", std::strlen("world"), d.GetAllocator());
        d.AddMember("hello", v, d.GetAllocator());
        buffer.Clear();
        Writer<StringBuffer> writer(buffer);
        d.Accept(writer);
        resp->body = std::string(buffer.GetString());
    });
    
  • Create TCPServer
    ...
    // Create server instance
    cpphttp::TCPServer server(8080, 16386, 16386, 1, httpHandler, logger);
    ...
  • Run TCPServer method listenAndServe()
    ...
    // Start server
    server.listenAndServe();
    ...

HTTP Client

  • Create TCPClient
    #include "HTTPLib/TCPClient.h"
    
    ...
    TCPClient client;
    
  • Send HTTP request and get cpphttp::Response
    ...
    auto res = client.send(HTTP_METHOD::GET, "wirelesstag.net", 80, "");
    // Output response
    std::cout << res->toString() << std::endl;
    ...
    

System requirements

  • rapidjson library
  • fmt library
  • libpq library (optional - using ${ROOT}/PG headers)

simplecpphttp's People

Contributors

an-nguen 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.