fastdo  0.6.8
eiennet_httpserv.hpp
浏览该文件的文档.
1 #ifndef __HTTPSERV_HPP__
2 #define __HTTPSERV_HPP__
3 
4 #pragma once
5 
6 namespace eiennet
7 {
8 
11 {
12 public:
13  struct CacheItem
14  {
15  time_t timeStamp;
18  };
19 
20  // map< name, cache_item >
21  typedef std::map< winux::String, CacheItem > CacheItemsMap;
22 
23  // 构造函数
24  StaticFileMemoryCache( time_t lifeTime = 0 ) : _cacheLifeTime(lifeTime), _mtxCache(true)
25  {
26  }
27 
28  bool hasCache( winux::String const & cacheName ) const
29  {
30  if ( this->_cacheLifeTime < 1 ) //如果未开启缓存功能,直接返回false
31  {
32  return false;
33  }
34  if ( true )
35  {
36  winux::ScopeGuard guard( const_cast<winux::RecursiveMutex &>(_mtxCache) );
37  if ( winux::isset( this->_cacheItems, cacheName ) ) // 存在缓存
38  {
39  time_t cacheFileMTime = this->_cacheItems.at(cacheName).timeStamp; //取得最后修改时间
40  if ( cacheFileMTime + this->_cacheLifeTime >= winux::GetUtcTime() ) // 尚未过期
41  {
42  return true;
43  }
44  }
45  }
46  return false;
47  }
48 
49  CacheItem const & get( winux::String const & cacheName ) const
50  {
51  winux::ScopeGuard guard( const_cast<winux::RecursiveMutex &>(_mtxCache) );
52  return this->_cacheItems.at(cacheName);
53  }
54 
56  {
57  CacheItem * item = nullptr;
58  if ( true )
59  {
60  winux::ScopeGuard guard( const_cast<winux::RecursiveMutex &>(_mtxCache) );
61  item = &this->_cacheItems[cacheName];
62  item->timeStamp = winux::GetUtcTime();
63  item->mime = std::move(mime);
64  item->content = std::move(content);
65  }
66  return *item;
67  }
68 
69  void setLifeTime( winux::uint64 lifeTime = 0 )
70  {
71  this->_cacheLifeTime = lifeTime;
72  }
73 
75  {
76  return this->_cacheLifeTime;
77  }
78 private:
79  time_t _cacheLifeTime; // 缓存生命期 0表示不缓存
80  CacheItemsMap _cacheItems;
81  winux::RecursiveMutex _mtxCache;
82 };
83 
86 {
88 
100  double serverWait;
104  bool verbose;
109 
118 
120  std::map< winux::String, winux::String > mime; // MIME
121 
124 
126  HttpServerConfig( winux::ConfigureSettings const & settings );
127 
130  winux::ConfigureSettings const * settings,
131  eiennet::ip::EndPoint const & ep,
132  int threadCount = 4,
133  int backlog = 0,
134  double serverWait = 0.002,
135  double verboseInterval = 0.01,
136  bool verbose = true,
137  int cacheLifeTime = 86400
138  );
139 
141  bool init( winux::ConfigureSettings const & settings );
142 
144  bool init(
145  winux::ConfigureSettings const * settings,
146  eiennet::ip::EndPoint const & ep,
147  int threadCount = 4,
148  int backlog = 0,
149  double serverWait = 0.002,
150  double verboseInterval = 0.01,
151  bool verbose = true,
152  int cacheLifeTime = 86400
153  );
154 
156  winux::String getMime( winux::String const & extName ) const;
157 
158  void initMime();
159 };
160 
161 
162 namespace old_v1
163 {
166 {
167  winux::Configure const * constConfObj; // 可能为nullptr
175  double durationSec;
178 
179  std::map< winux::String, winux::String > mime; // MIME
180 
182  HttpServerConfig( winux::Configure const & confObj );
184  HttpServerConfig( winux::String const & serverIp, winux::ushort port, int threadCount, int listenBacklog, double durationSec );
185 
186  winux::String getMime( winux::String const & extName ) const;
187  void initMimeList();
188 };
189 
191 class HttpClientCtx : public ClientCtx
192 {
193 public:
195  : ClientCtx( clientId, clientEpStr, clientSockPtr ), url(http::Url::urlSimple), config(nullptr)
196  {
197  }
198  virtual ~HttpClientCtx()
199  {
200  if ( !this->config || this->config->outputVerbose )
201  ColorOutput( winux::fgPurple, "Client[", clientId, "]客户场景`", clientEpStr, "`析构" );
202  }
203  DataRecvSendCtx forClient; // 数据收发场景
204  winux::AnsiString requestHeaderStr; // 请求头字符串
206  winux::GrowBuffer requestBody; // 请求体数据
207  http::Url url; // URL
208  HttpServerConfig * config; // 引用配置对象
209 };
210 
212 class EIENNET_DLL HttpServer : public Server<HttpClientCtx>
213 {
214 public:
216 
217  #define RESPONSE_HANDLER_PARAMS eiennet::old_v1::HttpServer::ClientCtxSharedPointer & clientCtxPtr, http::Header const & reqHdr, http::Url const & url, http::Header & rspHdr, std::ostream & rspOut
218  using ResponseHandlerFunction = std::function< void ( RESPONSE_HANDLER_PARAMS ) >;
219 
221  HttpServer( HttpServerConfig const & confObj );
223  HttpServer( winux::String const & serverIp, winux::ushort port, int threadCount = 4, int listenBacklog = 10, double durationSec = 0.1 );
225  void setHandler( winux::String const & urlPath, ResponseHandlerFunction handler ) { _handlers[urlPath] = handler; }
226 
227 protected:
228  // 业务启动函数
229  virtual void onStartup( ClientCtxSharedPointer clientCtxPtr ) override;
230 
231  // 尝试接收一个请求头的任务
232  void _doRecvRequestHeaderTask( ClientCtxSharedPointer clientCtxPtr );
233 
234  // 尝试接收一个请求体的任务
235  void _doRecvRequestBodyTask( ClientCtxSharedPointer clientCtxPtr );
236 
237  // 处理一个请求的任务
238  void _doRequestTask( ClientCtxSharedPointer clientCtxPtr );
239 
240  // Web逻辑处理
241  void _webProcess( ClientCtxSharedPointer & clientCtxPtr, http::Header const & reqHdr, http::Url const & url, http::Header & rspHdr, std::ostream & rspOut );
242 
243 private:
244  std::map< winux::String, ResponseHandlerFunction > _handlers; // 响应处理函数
245  StaticFileMemoryCache _cache; // 静态文件内存缓存
246 
247  friend class HttpClientCtx;
248 };
249 
250 } // namespace old_v1
251 
252 } // namespace eiennet
253 
254 #endif // __HTTPSERV_HPP__
XString< char > AnsiString
Definition: utilities.hpp:212
CacheItem & writeCache(winux::String const &cacheName, winux::String mime, winux::Buffer content)
代表HTTP头部
Definition: http_misc.hpp:10
int sockTimeout
套接字超时时间(整数秒)
winux::uint64 getLifeTime() const
winux::Configure const * constConfObj
double verboseInterval
verbose信息刷新间隔(小数秒)
static void ColorOutput(winux::ConsoleAttr const &ca, _ArgType &&...arg)
Definition: console.hpp:191
配置文件类
Definition: archives.hpp:11
XString< tchar > String
Definition: utilities.hpp:216
std::map< winux::String, winux::String > mime
一些静态文件的MIME
int retryCount
连接重试次数
void setLifeTime(winux::uint64 lifeTime=0)
std::map< winux::String, winux::String > mime
更强大的配置文件类
Definition: archives.hpp:72
http协议的相关简单类封装
Definition: http.hpp:32
winux::ConfigureSettings const * confSettings
std::map< String, String > StringStringMap
Definition: utilities.hpp:229
winux::StringStringMap errorPages
错误页
HttpServerConfig config
配置参数
double serverWait
服务器IO等待间隔时间(小数秒)
std::function< void(eiennet::old_v1::HttpServer::ClientCtxSharedPointer &clientCtxPtr, http::Header const &reqHdr, http::Url const &url, http::Header &rspHdr, std::ostream &rspOut) > ResponseHandlerFunction
URL类
Definition: http_url.hpp:8
HttpServer配置参数
bool verbose
显示冗长信息
StaticFileMemoryCache(time_t lifeTime=0)
静态文件内存缓存(带互斥锁)
作用域范围保护
Definition: system.hpp:207
缓冲区,表示内存中一块二进制数据(利用malloc/realloc进行内存分配)
Definition: utilities.hpp:528
#define EIENNET_DLL
数据收发场景,存放数据收发过程中的一些变量
time_t GetUtcTime(void)
获取UTC时间秒数,或者调用CRT的time(NULL)
winux::StringArray documentIndex
文档首页
winux::String serverIp
服务器IP,可留空
winux::ushort serverPort
服务器监听端口
winux::String documentRoot
文档根目录
bool isset(_MAP const &m, _KEY const &k)
检测map中是否有该键的值
Definition: utilities.hpp:268
客户场景类基础
winux::String serverName
服务器名,可留空
void setHandler(winux::String const &urlPath, ResponseHandlerFunction handler)
设置动态页面处理
网络通信库
bool hasCache(winux::String const &cacheName) const
高效的可增长缓冲区,1.33倍冗余量
Definition: utilities.hpp:659
unsigned short ushort
Definition: utilities.hpp:173
std::map< winux::String, CacheItem > CacheItemsMap
HttpClientCtx(winux::uint64 clientId, winux::String clientEpStr, winux::SharedPointer< ip::tcp::Socket > clientSockPtr)
服务器类基础
unsigned __int64 uint64
Definition: utilities.hpp:185
XStringArray< tchar > StringArray
Definition: utilities.hpp:227
int listenBacklog
监听积压数
int cacheLifeTime
静态文件缓存生命期