fastdo  0.5.12
eiennet_httpserv.hpp
浏览该文件的文档.
1 #ifndef __HTTPSERV_HPP__
2 #define __HTTPSERV_HPP__
3 
4 #pragma once
5 
6 namespace eiennet
7 {
8 
11 {
19  double durationSec;
21 
22  std::map< winux::String, winux::String > mime; // MIME
23 
25  HttpServerConfig( winux::Configure const & confObj );
27  HttpServerConfig( winux::String const & serverIp, winux::ushort port, int threadCount, int listenBacklog, double durationSec );
28 
29  winux::String getMime( winux::String const & extName ) const;
30  void initMimeList();
31 };
32 
34 class HttpClientCtx : public ClientCtx
35 {
36 public:
38  : ClientCtx( clientId, clientEpStr, clientSockPtr ), url(http::Url::urlSimple), config(nullptr)
39  {
40  }
41  virtual ~HttpClientCtx()
42  {
43  if ( !this->config || this->config->outputVerbose )
44  ColorOutput( winux::fgPurple, "Client[", clientId, "]客户场景`", clientEpStr, "`析构" );
45  }
46  DataRecvSendCtx forClient; // 数据收发场景
47  winux::AnsiString requestHeaderStr; // 请求头字符串
49  winux::GrowBuffer requestBody; // 请求体数据
50  http::Url url; // URL
51  HttpServerConfig const * config; // 引用配置对象
52 };
53 
55 class EIENNET_DLL HttpServer : public Server<HttpClientCtx>
56 {
57 public:
59 
60  #define RESPONSE_HANDLER_PARAMS eiennet::HttpServer::ClientCtxSharedPointer & clientCtxPtr, http::Header const & reqHdr, http::Url const & url, http::Header & rspHdr, std::ostream & rspOut
61  using ResponseHandlerFunction = std::function< void ( RESPONSE_HANDLER_PARAMS ) >;
62 
64  HttpServer( HttpServerConfig const & confObj );
66  HttpServer( winux::String const & serverIp, winux::ushort port, int threadCount = 4, int listenBacklog = 10, double durationSec = 0.1 );
67 
68  void setHandler( winux::String const & urlPath, ResponseHandlerFunction handler ) { _handlers[urlPath] = handler; }
69 
70 private:
71  // 业务启动函数
72  virtual void onStartup( ClientCtxSharedPointer clientCtxPtr ) override;
73 
74  // 尝试接收一个请求头的任务
75  void _doRecvRequestHeaderTask( ClientCtxSharedPointer clientCtxPtr );
76 
77  // 尝试接收一个请求体的任务
78  void _doRecvRequestBodyTask( ClientCtxSharedPointer clientCtxPtr );
79 
80  // 处理一个请求的任务
81  void _doRequestTask( ClientCtxSharedPointer clientCtxPtr );
82 
83  // Web逻辑处理
84  void _webProcess( ClientCtxSharedPointer & clientCtxPtr, http::Header const & reqHdr, http::Url const & url, http::Header & rspHdr, std::ostream & rspOut );
85 
86  std::map< winux::String, ResponseHandlerFunction > _handlers; // 响应处理函数
87 
88  friend class HttpClientCtx;
89 };
90 
91 
92 
93 } // namespace eiennet
94 
95 #endif // __HTTPSERV_HPP__
代表HTTP头部
Definition: http_misc.hpp:10
客户场景类基础
HttpServerConfig const config
配置参数
std::basic_string< char > AnsiString
Definition: utilities.hpp:165
HttpClientCtx(winux::uint64 clientId, winux::String clientEpStr, winux::SharedPointer< ip::tcp::Socket > clientSockPtr)
static void ColorOutput(winux::ConsoleAttr const &ca, _ArgType &&...arg)
Definition: console.hpp:178
配置文件类
Definition: archives.hpp:11
std::map< winux::String, winux::String > mime
http协议的相关简单类封装
Definition: http.hpp:32
URL类
Definition: http_url.hpp:8
HTTP服务器配置
winux::GrowBuffer requestBody
#define EIENNET_DLL
数据收发场景,存放数据收发过程中的一些变量
winux::AnsiString requestHeaderStr
服务器类基础
void setHandler(winux::String const &urlPath, ResponseHandlerFunction handler)
网络通信库
高效的可增长缓冲区,1.33倍冗余量
Definition: utilities.hpp:531
unsigned short ushort
Definition: utilities.hpp:131
std::function< void(eiennet::HttpServer::ClientCtxSharedPointer &clientCtxPtr, http::Header const &reqHdr, http::Url const &url, http::Header &rspHdr, std::ostream &rspOut) > ResponseHandlerFunction
unsigned long long uint64
Definition: utilities.hpp:146
HttpServerConfig const * config
std::basic_string< tchar > String
Definition: utilities.hpp:162