Code Monkey home page Code Monkey logo

fcgi's Introduction

FastCGI implementation in C++ {#mainpage}

dmitigr::fcgi - is a FastCGI protocol implementation written in C++.

Hello, World

#include <dmitigr/fcgi/fcgi.hpp>
#include <iostream>

int main()
{
  namespace fcgi = dmitigr::fcgi;
  try {
    const auto port = 9000;
    const auto backlog = 64;
    fcgi::Listener server{fcgi::Listener_options{"0.0.0.0", port, backlog}};
    server.listen();
    while (true) {
      if (const auto conn = server.accept()) {
        conn->out() << "Content-Type: text/plain" << fcgi::crlfcrlf;
        conn->out() << "Hello from dmitigr::fcgi!";
      }
    }
  } catch (const std::exception& e) {
    std::cerr << "Oops: " << e.what() << std::endl;
    return 1;
  }
}

Hello, Multithreaded World

#include <dmitigr/fcgi/fcgi.hpp>
#include <iostream>
#include <thread>
#include <vector>

namespace {

constexpr std::size_t pool_size{64};

} // namespace

int main()
{
  namespace fcgi = dmitigr::fcgi;
  try {
    const auto serve = [](auto* const server)
    {
      while (true) {
        const auto conn = server->accept();
        conn->out() << "Content-Type: text/plain" << fcgi::crlfcrlf;
        conn->out() << "Hello from dmitigr::fcgi!";
        conn->close(); // Optional.
      }
    };

    const auto port = 9000;
    const auto backlog = 64;
    std::clog << "Multi-threaded FastCGI server started:\n"
              << "  port = " << port << "\n"
              << "  backlog = " << backlog << "\n"
              << "  thread pool size = " << pool_size << std::endl;

    fcgi::Listener server{fcgi::Listener_options{"0.0.0.0", port, backlog}};
    server.listen();
    std::vector<std::thread> threads(pool_size);
    for (auto& t : threads)
      t = std::thread{serve, &server};

    for (auto& t : threads)
      t.join();

    server.close(); // Optional.
  } catch (const std::exception& e) {
    std::cerr << "error: " << e.what() << std::endl;
    return 1;
  }
}

Usage

Quick usage as header-only library

Copy the contents of the src directory to a project directory which is under an include path of a compiler, for example, src/3rdparty/dmitigr.

Create hello.cpp:

#include "dmitigr/fcgi/fcgi.hpp"

int main()
{
  // Application code here...
}

Compile hello.cpp:

g++ -std=c++17 -ohello hello.cpp

Quick usage with CMake

Create build directory, configure, build and install:

cd fcgi
mkdir build && cd build
cmake ..
cmake --build .
sudo cmake --install .

Create hello/hello.cpp:

#include "dmitigr/fcgi/fcgi.hpp"

int main()
{
  // Application code here...
}

Create hello/CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)
project(foo)
find_package(dmitigr_cpplipa REQUIRED COMPONENTS fcgi)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(hello hello.cpp)
target_link_libraries(hello dmitigr_fcgi)

Compile hello/hello.cpp:

mkdir hello/build && cd hello/build
cmake ..
cmake --build .

Advanced usage

For more details please, see usage section for hints how to link dmitigr::fcgi.

fcgi's People

Contributors

dmitigr 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

Watchers

 avatar  avatar

fcgi's Issues

conn->out() can parse multiple lines?

hello
why conn->out() only allows single line and when i make two of them server breaks with:
fcgi-hellomt.cpp:25:24: error: missing terminating " character
25 | conn->out() << "<!doctype html>

I'm only trying this:
conn->out() << "<!doctype html>

";

how to join my own daemon?

hello again
I need to do some tasks on the background, so how exactly i should join my own daemon in your threads?
Also i want to send my commands from console to whole thing. For ex.: delete something from DB, or find something, or execute some task, etc.

padding_length <= epptr() - pptr()

After sending over 16kB of data I am getting the following error:

Debug output from /Users/dehart/testing/fcgi/lib/dmitigr/fcgi/streambuf.cpp:293: assertion (padding_length <= epptr() - pptr()) failed
Debug output from /Users/dehart/testing/fcgi/lib/dmitigr/fcgi/streambuf.cpp:293: assertion (padding_length <= epptr() - pptr()) failed
Debug output from /Users/dehart/testing/fcgi/lib/dmitigr/fcgi/streambuf.cpp:293: assertion (padding_length <= epptr() - pptr()) failed
Debug output from /Users/dehart/testing/fcgi/lib/dmitigr/fcgi/streambuf.cpp:41: dmitigr::fcgi::server_Streambuf::~server_Streambuf(): failure

UNIX socket support

Hey @dmitigr,

Does your implementation support UNIX sockets instead of TCP binding? Most modern implementation like php-fcm do rely on UNIX sockets instead of TCP to reduce network congestion and increase responses ratio.

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.