summaryrefslogtreecommitdiffhomepage
path: root/plugins/fcgi/fcgi.h
blob: 4f77719306afb650f04b752ae00c0cbe653fd01f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once

#include "../../plugin_interface.h"

#include <boost/asio.hpp>

#include <cstdint>
#include <mutex>
#include <set>

// TODO: multithreading
class FCGI_ID
{
 std::set<uint16_t >m_unused;
 uint16_t m_current_max{};

public:
 FCGI_ID(){}

 // starting at 1
 uint16_t getID(){
  if (m_unused.empty()) {
   m_current_max++;
   return m_current_max;
  } else {
   uint16_t result{*m_unused.begin()};
   m_unused.erase(m_unused.begin());
   return result;
  }
 }

 void putID(uint16_t id){
  m_unused.insert(id);
 }
};

// automatically reserves ID, and releases it via RAII
class FCGI_ID_Guard
{
 FCGI_ID& m_fcgi_id;
 uint16_t m_id;

public:
 FCGI_ID_Guard(FCGI_ID& fcgi_id): m_fcgi_id(fcgi_id), m_id(fcgi_id.getID())
 {
 }

 ~FCGI_ID_Guard()
 {
  m_fcgi_id.putID(m_id);
 }

 uint16_t getID() const { return m_id; }
};

struct FCGIContext;

class fcgi_plugin: public webserver_plugin_interface 
{
 FCGI_ID m_fcgi_id;
 boost::asio::io_context m_io_context;
 boost::asio::ip::tcp::resolver m_resolver;

 std::mutex m_socket_mutex; // guard m_socket use in different threads
 std::unordered_map<std::string, boost::asio::ip::tcp::socket> m_sockets;

public:
 fcgi_plugin();
 ~fcgi_plugin();
 
 std::string name();
 std::string generate_page(
  std::function<std::string(const std::string& key)>& GetServerParam,
  std::function<std::string(const std::string& key)>& GetRequestParam, // request including body (POST...)
  std::function<void(const std::string& key, const std::string& value)>& SetResponseHeader // to be added to result string
 );

 std::string fcgiQuery(FCGIContext& context);
};

extern "C" BOOST_SYMBOL_EXPORT fcgi_plugin webserver_plugin;
fcgi_plugin webserver_plugin;