Skip to content

Commit

Permalink
add fastcgi
Browse files Browse the repository at this point in the history
  • Loading branch information
hggq committed Oct 25, 2023
1 parent 0b5e3dd commit 0fac008
Show file tree
Hide file tree
Showing 15 changed files with 3,092 additions and 1,525 deletions.
4 changes: 4 additions & 0 deletions conf/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ http2_enable=1
;controlsopath=/Users/hzq/paozhu/docs/controller
method_pre= ;api/dev/hostcors
method_after=
isuse_php=1
rewrite_php=/Users/hzq/Documents/www/thinkphp/public|index.php
fastcgi_host=127.0.0.1
fastcgi_port=9000
[www.paozhu.org]
wwwpath=/Users/hzq/paozhu/docs
;controlsopath=/Users/hzq/paozhu/docs/controller
7 changes: 5 additions & 2 deletions vendor/httpserver/include/client_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <asio/spawn.hpp>
#include <asio/ssl.hpp>
#include "httpclient.h"

#include "fastcgi.h"
namespace http
{

Expand All @@ -32,9 +32,11 @@ class client_context
void run();
void time_out_loop();
void taskloop();
asio::awaitable<void> fastcgi_client_task(std::shared_ptr<fastcgi>);
asio::awaitable<void> http_client_task(std::shared_ptr<client>);
asio::awaitable<void> websocket_client_task(std::shared_ptr<client>);
void add_http_task(std::shared_ptr<client>);
void add_fastcgi_task(std::shared_ptr<fastcgi>);
~client_context();

public:
Expand All @@ -50,6 +52,7 @@ class client_context
std::mutex queue_mutex;
std::condition_variable condition;
std::queue<std::shared_ptr<client>> clienttasks;
std::queue<std::shared_ptr<fastcgi>> cgitasks;

std::mutex timeout_mutex;
std::condition_variable timeout_condition;
Expand All @@ -58,4 +61,4 @@ class client_context
client_context &get_client_context_obj();

}// namespace http
#endif
#endif
125 changes: 125 additions & 0 deletions vendor/httpserver/include/fastcgi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#ifndef FRAME_FASTCGI_H
#define FRAME_FASTCGI_H

#include <cstdarg>
#include <cstring>
#include <stdexcept>
#include <string>
#include <list>
#include <vector>
#include <iostream>
#include <sstream>
#include <asio.hpp>

#include "client_session.h"
#include "httppeer.h"

namespace http
{

typedef struct
{
unsigned char version;// fastCGI协议号
unsigned char type; // fastCGI 消息类型
unsigned char requestIdB1;
unsigned char requestIdB0;
unsigned char contentLengthB1;
unsigned char contentLengthB0;
unsigned char paddingLength;
unsigned char reserved;
} FASTCGI_Record;

struct FASTCGI_Header
{
unsigned char version;
unsigned char type;
unsigned char requestIdB1;
unsigned char requestIdB0;
unsigned char contentLengthB1;
unsigned char contentLengthB0;
unsigned char paddingLength;
unsigned char reserved;
};

// 允许传送的最大数据 65536
#define FASTCGI_MAX_LENGTH 0xffff
// fast_cgi header长度
#define FASTCGI_HEADER_LENGTH 8
// fast_cgi 版本
#define FASTCGI_VERSION 1

#define FASTCGI_BEGIN_REQUEST 1
#define FASTCGI_ABORT_REQUEST 2
#define FASTCGI_END_REQUEST 3
#define FASTCGI_PARAMS 4
#define FASTCGI_STDIN 5
#define FASTCGI_STDOUT 6
#define FASTCGI_STDERR 7
#define FASTCGI_DATA 8
#define FASTCGI_GET_VALUES 9
#define FASTCGI_GET_VALUES_RESULT 10
#define FASTCGI_UNKNOWN_TYPE 11
#define FASTCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)

#define FASTCGI_KEEP_CONN 1
#define FASTCGI_RESPONDER 1
#define FASTCGI_AUTHORIZER 2
#define FASTCGI_FILTER 3

struct FASTCGI_BeginRequestBody
{
unsigned char roleB1;
unsigned char roleB0;
unsigned char flags;
unsigned char reserved[5];
};

struct FASTCGI_BeginRequestRecord
{
FASTCGI_Header header;
FASTCGI_BeginRequestBody body;
};

struct FASTCGI_EndRequestBody
{
unsigned char appStatusB3;
unsigned char appStatusB2;
unsigned char appStatusB1;
unsigned char appStatusB0;
unsigned char protocolStatus;
unsigned char reserved[3];
};

class fastcgi
{
public:
void make_Header(FASTCGI_Header &, int type, int requestId, int contentLength, int paddingLength);
void make_BeginRequestBody(FASTCGI_BeginRequestBody &, int role, int keepConnection);
asio::awaitable<bool> co_init_http_sock();
asio::awaitable<void> co_send();
asio::awaitable<void> send_postbody(std::shared_ptr<httppeer> peer);
asio::awaitable<void> send_endheader();
asio::awaitable<void> send_startheader();
asio::awaitable<void> send_exit(std::shared_ptr<httppeer> peer);
std::string pack_length(unsigned int, unsigned int);

public:
std::string host;
std::string port;
std::string request_url;
//std::string request_query;
std::string document_root;
// FASTCGI_Header header_data;
// FASTCGI_BeginRequestBody request_body{};
int request_id;
//unsigned char method_type = 0;

std::function<void(const std::string &)> add_error_msg;

std::shared_ptr<asio::ip::tcp::socket> sock = {nullptr};
std::weak_ptr<httppeer> peer_ptr;
std::string error_msg;
asio::error_code ec;
};
}// namespace http
#endif
10 changes: 6 additions & 4 deletions vendor/httpserver/include/httppeer.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class httppeer : public std::enable_shared_from_this<httppeer>
std::string get_hosturl();
std::string get_sitepath();
unsigned char has_urlfileext();
bool isuse_fastcgi(unsigned char type_temp);
void goto_url(const std::string &url, unsigned char second = 0, const std::string &msg = "");
bool isset_type();

Expand Down Expand Up @@ -163,8 +164,8 @@ class httppeer : public std::enable_shared_from_this<httppeer>
bool keepalive = true;
bool isso = false;

unsigned char posttype;
unsigned char compress;
unsigned char posttype = 0;
unsigned char compress = 0;
unsigned int host_index = 0;
unsigned int stream_id = 0;
unsigned int status_code = 0;
Expand All @@ -180,7 +181,8 @@ class httppeer : public std::enable_shared_from_this<httppeer>

http::OBJ_VALUE session;
unsigned long long sessionfile_time = 0;
Cookie send_cookie;
//Cookie send_cookie;
std::list<std::string> send_cookie_lists;
std::map<std::string, std::string> send_header;
std::map<unsigned char, std::string> http2_send_header;

Expand Down Expand Up @@ -217,4 +219,4 @@ extern std::map<std::string, regmethold_t> _http_regmethod_table;
extern std::map<std::string, std::vector<std::string>> _http_regurlpath_table;
void make_404_content(std::shared_ptr<httppeer> peer);
}// namespace http
#endif
#endif
4 changes: 2 additions & 2 deletions vendor/httpserver/include/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ class httpserver
bool http2_send_file_range(std::shared_ptr<httppeer> peer);

void http2send_filedata(struct http2sendblock_t &http2_ff_send);

void add_error_lists(const std::string &);
asio::awaitable<void> http1loop(unsigned int sig, std::shared_ptr<httppeer>, std::shared_ptr<client_session>);
void websocket_loop(int myid);
void listeners();
void listener();

asio::awaitable<size_t> co_user_task(std::shared_ptr<httppeer> peer, asio::use_awaitable_t<> h = {});

asio::awaitable<size_t> co_user_fastcgi_task(std::shared_ptr<httppeer> peer, asio::use_awaitable_t<> h = {});
void add_runsocketthread();
int checkhttp2(std::shared_ptr<client_session> peer_session);
void http1_send_bad_request(unsigned int, std::shared_ptr<client_session>);
Expand Down
124 changes: 66 additions & 58 deletions vendor/httpserver/include/serverconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define FRAME_SERVERCONFIG_H
#include <string>
#include <map>
#include <tuple>
#include <vector>
#include "request.h"
#include "client_session.h"
Expand All @@ -11,65 +12,72 @@
namespace http
{

class serverconfig;
extern std::string server_config_path;
extern std::map<std::string, std::map<std::string, std::string>> loadserversconfig(std::string filename);
class serverconfig;
extern std::string server_config_path;
extern std::map<std::string, std::map<std::string, std::string>> loadserversconfig(std::string filename);

serverconfig &getserversysconfig();
struct site_host_info_t{
std::string wwwpath;
std::string aliaspath;
std::string mainhost;
std::string rewrite_404_action;
std::string certificate_file;
std::string privateKey_file;
std::vector<std::string> action_404_lists;
std::vector<std::string> action_pre_lists;
std::vector<std::string> action_after_lists;
unsigned int rewrite404=0;
bool isrewrite=false;
bool http2_enable=false;
bool is_method_pre=false;
bool is_method_after=false;
};
class serverconfig
{
serverconfig &getserversysconfig();
struct site_host_info_t
{
std::string wwwpath;
std::string aliaspath;
std::string mainhost;
std::string fastcgi_host;
std::string fastcgi_port;
std::string php_root_document;
std::string rewrite_404_action;
std::string certificate_file;
std::string privateKey_file;
std::vector<std::string> action_404_lists;
std::vector<std::string> action_pre_lists;
std::vector<std::string> action_after_lists;
std::vector<std::pair<std::string, std::string>> rewrite_php_lists;
unsigned int rewrite404 = 0;
bool isuse_php = false;
bool isrewrite = false;
bool http2_enable = false;
bool is_method_pre = false;
bool is_method_after = false;
};
class serverconfig
{

public:
std::string getsitepath(const std::string &);
std::string getsitewwwpath(unsigned int);
std::tuple<unsigned int, std::string> gethost404(const std::string &host);
bool loadserverglobalconfig();
bool checkmaindomain(const char *);
void init_path();
unsigned char get_co_thread_num();
unsigned int get_ssl_port();
unsigned int get_port();
std::string ssl_chain_file();
std::string ssl_key_file();
std::string ssl_dh_file();
SSL_CTX *getctx(std::string filename);
SSL_CTX *getdefaultctx();

public:
std::string getsitepath(const std::string &);
std::string getsitewwwpath(unsigned int);
std::tuple<unsigned int,std::string> gethost404(const std::string &host);
bool loadserverglobalconfig();
bool checkmaindomain(const char *);
void init_path();
unsigned char get_co_thread_num();
unsigned int get_ssl_port();
unsigned int get_port();
std::string ssl_chain_file();
std::string ssl_key_file();
std::string ssl_dh_file();
SSL_CTX *getctx(std::string filename);
SSL_CTX *getdefaultctx();
public:
std::string serverpath;
std::string wwwpath;
std::string mainhost;
std::string secondhost;
// std::map<std::string,std::function<std::shared_ptr<websockets_api>(std::weak_ptr<client_session>)>> websocketmethodcallback;
std::map<std::string, std::function<std::string(http::client_session &)>> methodcallback;
std::string configfile;
std::string configpath;
std::map<std::string, std::map<std::string, std::string>> map_value;
bool reloadmysql = true;
bool reloadserverconfig = true;
unsigned int siteusehtmlchachetime = 0;
bool siteusehtmlchache = false;
bool isallnothttp2=true;
std::map<std::string,SSL_CTX*> g_ctxMap;
std::map<unsigned long long,bool> domain_http2;
std::vector<struct site_host_info_t> sitehostinfos;
std::map<std::string,unsigned int> host_toint;
};
public:
std::string serverpath;
std::string wwwpath;
std::string mainhost;
std::string secondhost;
// std::map<std::string,std::function<std::shared_ptr<websockets_api>(std::weak_ptr<client_session>)>> websocketmethodcallback;
std::map<std::string, std::function<std::string(http::client_session &)>> methodcallback;
std::string configfile;
std::string configpath;
std::map<std::string, std::map<std::string, std::string>> map_value;
bool reloadmysql = true;
bool reloadserverconfig = true;
unsigned int siteusehtmlchachetime = 0;
bool siteusehtmlchache = false;
bool isallnothttp2 = true;
std::map<std::string, SSL_CTX *> g_ctxMap;
std::map<unsigned long long, bool> domain_http2;
std::vector<struct site_host_info_t> sitehostinfos;
std::map<std::string, unsigned int> host_toint;
};

}
}// namespace http
#endif
Loading

0 comments on commit 0fac008

Please sign in to comment.