fastdo  0.6.16
fcgiplus.hpp
浏览该文件的文档.
1 #ifndef __FCGIPLUS_HPP__
2 #define __FCGIPLUS_HPP__
3 
4 #if _MSC_VER > 0
5 #pragma warning( disable: 4244 )
6 #endif
7 
8 #include "http.hpp"
9 
10 struct FCGX_Stream;
11 struct FcgiServerData;
12 
14 namespace fcgi
15 {
16 
17 #ifdef FCGIPLUS_DLL_USE
18  #if defined(_MSC_VER) || defined(WIN32)
19  #pragma warning( disable: 4251 )
20  #ifdef FCGIPLUS_DLL_EXPORTS
21  #define FCGIPLUS_DLL __declspec(dllexport)
22  #else
23  #define FCGIPLUS_DLL __declspec(dllimport)
24  #endif
25 
26  #define FCGIPLUS_API __stdcall
27  #else
28  #define FCGIPLUS_DLL
29  #define FCGIPLUS_API
30  #endif
31 #else
32  #define FCGIPLUS_DLL
33  #define FCGIPLUS_API
34 #endif
35 
36 #define FCGIPLUS_FUNC_DECL(ret) FCGIPLUS_DLL ret FCGIPLUS_API
37 #define FCGIPLUS_FUNC_IMPL(ret) ret FCGIPLUS_API
38 
39 
40 class FcgiApp;
41 class Request;
42 class Response;
43 class Cookies;
44 
47 
50 {
51  SessionData data;
53  SessionStruct() : expires(0) { }
54 };
55 
58 {
59 public:
60  SessionServer() : _cleanTimeInterval(1800), _sessionLifeTime(3600) { }
61  virtual ~SessionServer() { }
62 
63  winux::uint getCleanTimeInterval() const { return _cleanTimeInterval; }
64  winux::uint getSessionLifeTime() const { return _sessionLifeTime; }
65 
67  virtual void clean() = 0;
69  virtual bool startup( winux::String const & sessid, SessionData * sessData ) = 0;
71  virtual bool save( winux::String const & sessid, SessionData const * sessData, winux::uint thisLifeTime = 0 ) = 0;
72 
73 protected:
76 };
77 
83 {
84 public:
85  MemorySessionServer( winux::uint cleanTimeInterval = 1800, winux::uint sessionLifeTime = 1800 );
86  virtual void clean();
87  virtual bool startup( winux::String const & sessid, SessionData * sessData );
88  virtual bool save( winux::String const & sessid, SessionData const * sessData, winux::uint thisLifeTime = 0 );
89 
90 private:
92  typedef std::map<winux::String, SessionStruct> SessionStructMap;
93  SessionStructMap _sessions;
95 };
96 
99 {
100 public:
101  FileSessionServer( winux::String const & savePath, winux::uint cleanTimeInterval = 1800, winux::uint sessionLifeTime = 1800 );
102  virtual void clean();
103  virtual bool startup( winux::String const & sessid, SessionData * sessData );
104  virtual bool save( winux::String const & sessid, SessionData const * sessData, winux::uint thisLifeTime = 0 );
105 
106 private:
107  winux::String _savePath; // session文件保存路径
109 };
110 
118 {
119 public:
120  Session( Request & req, winux::String const & sessid = "", winux::uint cookieSessIdLifeTime = 0, winux::uint sessionLifeTime = 0, winux::String const & cookiesPath = "" );
121  ~Session();
122  winux::Mixed & operator [] ( winux::String const & name );
123  winux::Mixed const & operator [] ( winux::String const & name ) const;
124  bool has( winux::String const & name ) const;
125  winux::Mixed const & get( winux::String const & name ) const;
126  void set( winux::String const & name, winux::Mixed const & v );
127  bool del( winux::String const & name );
128 
129 private:
130  Request & _req;
131  SessionData _data;
132  winux::String _sessid; // session唯一标识
133  winux::uint _cookieSessIdLifeTime; // sessid cookie生命期
134  winux::uint _sessionLifeTime; // session生命期
135  bool _isModified; // 是否修改过
137 };
138 
141 {
142 public:
143  Request();
144  ~Request();
145 
147  void init( FcgiApp * app, int sock, int flags );
148 
150  void free( bool close = false );
151 
155  void finish();
156 
160  bool accept();
161 
163  bool processData();
164 
166  winux::String const & operator [] ( winux::String const & name ) const;
167 
171  void dumpEnvTo( winux::String * str, char * * envp = NULL ) const;
172 
183 
184 private:
186 
187  friend class Response;
188  friend class FcgiApp;
189 
191 };
192 
198 {
199 public:
200  // FCGX_Stream * fcgiOut, FCGX_Stream * fcgiErr
201  FcgiOutputMgr( struct FCGX_Stream * fcgiOut, struct FCGX_Stream * fcgiErr );
202  ~FcgiOutputMgr();
203 
208  void setLocalEncoding( winux::String const & local );
209 
213  void setTargetEncoding( winux::String const & target );
214 
215  winux::String getLocalEncoding() const;
216  winux::String getTargetEncoding() const;
217 
219  winux::String convFrom( winux::String const & str ) const;
221  winux::String convTo( winux::String const & str ) const;
222 
223  void echo( winux::String const & str );
224  void write( void const * data, int size );
225  void printf( char const * format, va_list va );
227  winux::uint getCacheOutStreamSize() const;
229  winux::uint getConvertedContentSize();
230 
231 private:
233 
234  void _backupStd();
235  void _resumeStd();
236  void _redirectToFcgi();
237 
238  // 尝试编码转换。返回值 0:不需要进行编码转换,1:需要进行编码转换并且执行了,2:需要进行编码转换,由于之前已经转换成功,所以直接返回。
239  int _tryEncodingConvert();
240  // 是否需要进行编码转换
241  // 不需要转换的情况:a.没有设置本地编码或者目标编码,b.本地编码和目标编码相同,c.MIME不是text类型
242  bool _isNeedEncodingConvert() const;
243 
244  friend class Response;
245 
247 };
248 
251 {
252 public:
253  explicit Response( Request & request );
254  ~Response();
255 
256  void echo( winux::String const & str );
257  void write( void const * data, int size );
258  void write( winux::Buffer const & buf );
259  void printf( char const * format, ... );
260 
261  winux::String getLocalEncoding() const { return _outputMgr.getLocalEncoding(); }
262  winux::String getTargetEncoding() const { return _outputMgr.getTargetEncoding(); }
263 
264  winux::String convFrom( winux::String const & str ) const { return _outputMgr.convFrom(str); }
265  winux::String convTo( winux::String const & str ) const { return _outputMgr.convTo(str); }
267  void setMimeType( winux::String const & mime ) { _mimeType = mime; }
268  winux::String getMimeType() const { return _mimeType; }
269 
274  void setAutoContentType( bool autoContentType = true ) { _autoContentType = autoContentType; }
275 
280  void setCharset( winux::String const & charset ) { _outputMgr.setTargetEncoding(charset); }
281  winux::String getCharset() const { return _outputMgr.getTargetEncoding(); }
282 
288  void import( winux::String const & doFile, void * runParam = NULL, int * retCode = NULL );
289 
292 
293 private:
294  FcgiOutputMgr _outputMgr;
295  winux::String _mimeType;
296  bool _autoContentType;
298 };
299 
302 {
303 public:
304 
309  FcgiApp( winux::Configure & conf, struct FcgiServerData * servData = NULL );
310  ~FcgiApp();
311 
313  int run( void * runParam );
314 
316  void * getParam() const { return _param; };
317  template < typename _Ty >
318  _Ty getParam() const { return reinterpret_cast<_Ty>(_param); };
319 
321  void setParam( void * param ) { this->_param = param; }
322 
324  void * getRunParam() const { return _runParam; };
325  template < typename _Ty >
326  _Ty getRunParam() const { return reinterpret_cast<_Ty>(_runParam); };
327 
329  struct FcgiServerData * getServerData() { return _servData; }
330  void setServerData( struct FcgiServerData * data ) { _servData = data; }
331 
333  SessionServer * getSessServ() { return _sessServ; }
334  void setSessServ( SessionServer * sessServ ) { this->_sessServ = sessServ; }
335 
338  {
343  };
344 
349  virtual ErrorCode execWebMain( Response * rsp, void * runParam, int * retCode );
350 
358  ErrorCode importDoFile( winux::String const & doFile, Response * rsp, void * runParam, int * retCode, winux::String * errDetail );
359 
361  int loadedDoFiles( winux::Mixed * arrDoFiles ) const;
362 
364  bool unloadDoFile( winux::String const & doFullPath );
365 
367  winux::DllLoader & loadModule( winux::String const & modFile );
368 
370  int loadedModules( winux::Mixed * arrModules ) const;
371 
373  bool unloadModule( winux::String const & modFile );
374 
377 
380 
381  // 获取一些已经加载的配置
384  {
397 
398  ConfigParams() : isProduction(false), maxUrlencodedPostSize(0), maxMultiFormPostSize(0)
399  {
400 
401  }
402 
403  } configParams;
404 
405 
406 private:
409  SessionServer * _sessServ; // session server
410  void * _param; // setParam()传递进来的参数
411  void * _runParam; // run()传递进来的参数
412  struct FcgiServerData * _servData; // server data
414  ErrorCode _importDoFileDevelopment( winux::String const & doFullPath, Response * rsp, void * runParam, int * retCode, winux::String * errDetail );
415  ErrorCode _importDoFileProduction( winux::String const & doFullPath, Response * rsp, void * runParam, int * retCode, winux::String * errDetail );
416 
417  friend class Request;
418  friend class Response;
420 };
421 
422 } // namespace fcgi
423 
424 
425 #endif // __FCGIPLUS_HPP__
__int64 int64
Definition: utilities.hpp:232
winux::StringStringMap environVars
环境变量
Definition: fcgiplus.hpp:174
代表HTTP头部
Definition: http_Header.hpp:10
virtual ~SessionServer()
Definition: fcgiplus.hpp:61
http::Vars post
POST变量 readonly.
Definition: fcgiplus.hpp:180
void setParam(void *param)
设置do传递参数
Definition: fcgiplus.hpp:321
winux::uint getCleanTimeInterval() const
Definition: fcgiplus.hpp:63
_Ty getParam() const
Definition: fcgiplus.hpp:318
struct FcgiServerData * getServerData()
取得服务器相关数据
Definition: fcgiplus.hpp:329
void setAutoContentType(bool autoContentType=true)
是否自动设置Content-Type
Definition: fcgiplus.hpp:274
每个用户的会话结构
Definition: fcgiplus.hpp:49
std::map< String, Mixed > StringMixedMap
Definition: utilities.hpp:278
Request & req
Request对象的引用
Definition: fcgiplus.hpp:290
FcgiApp * fcgiApp
FcgiApp对象指针
Definition: fcgiplus.hpp:182
SessionServer * getSessServ()
取得/设置SessionServer
Definition: fcgiplus.hpp:333
配置文件类
Definition: archives.hpp:10
FCGI程序类,代表整个FCGI程序
Definition: fcgiplus.hpp:301
XString< tchar > String
Definition: utilities.hpp:261
代表客户请求
Definition: fcgiplus.hpp:140
winux::String convFrom(winux::String const &str) const
Definition: fcgiplus.hpp:264
winux::uint _sessionLifeTime
默认session生命期
Definition: fcgiplus.hpp:75
std::map< String, String > StringStringMap
Definition: utilities.hpp:274
winux::StringMixedMap SessionData
会话数据
Definition: fcgiplus.hpp:43
bool isProduction
isProduction指示是否以产品级模式运行.
Definition: fcgiplus.hpp:388
http::Header & header
表示头部
Definition: fcgiplus.hpp:291
void setCharset(winux::String const &charset)
设置charset,即设置输出目标编码
Definition: fcgiplus.hpp:280
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:85
DLL动态载入器
Definition: system.hpp:265
winux::String getMimeType() const
Definition: fcgiplus.hpp:268
会话服务基类
Definition: fcgiplus.hpp:57
Request request
请求
Definition: fcgiplus.hpp:376
http::Cookies cookies
Cookie变量 read/write.
Definition: fcgiplus.hpp:176
变量类。可转为application/x-www-form-urlencoded类型字符串。可用于解析和设置get/post变量 ...
Definition: http_Vars.hpp:16
winux::String outputTplPath
获取已经从配置文件中加载的【输出模板的路径】
Definition: fcgiplus.hpp:396
网站会话
Definition: fcgiplus.hpp:117
winux::String uploadTmpPath
上传文件的临时目录
Definition: fcgiplus.hpp:394
winux::Configure & config
配置
Definition: fcgiplus.hpp:379
代表一个客户响应
Definition: fcgiplus.hpp:250
void setSessServ(SessionServer *sessServ)
Definition: fcgiplus.hpp:334
缓冲区,表示内存中一块二进制数据(利用malloc/realloc进行内存分配)
Definition: utilities.hpp:906
void setMimeType(winux::String const &mime)
设置mime
Definition: fcgiplus.hpp:267
winux::String getCharset() const
Definition: fcgiplus.hpp:281
#define FCGIPLUS_DLL
Definition: fcgiplus.hpp:32
void * getParam() const
取得do传递的参数
Definition: fcgiplus.hpp:316
ErrorCode
执行do文件的错误码
Definition: fcgiplus.hpp:337
winux::int64 maxMultiFormPostSize
当请求的Content-Type是multipart/form-data时,多部分表单POST发送数据的最大大小
Definition: fcgiplus.hpp:392
unsigned int uint
Definition: utilities.hpp:215
winux::String getLocalEncoding() const
Definition: fcgiplus.hpp:261
winux::uint getSessionLifeTime() const
Definition: fcgiplus.hpp:64
winux::String convTo(winux::String const &str) const
Definition: fcgiplus.hpp:265
winux::int64 maxUrlencodedPostSize
当请求的Content-Type是application/x-www-form-urlencoded时,表单POST发送数据的最大大小 ...
Definition: fcgiplus.hpp:390
winux::uint _cleanTimeInterval
执行"清理过期session"的时间间隔
Definition: fcgiplus.hpp:74
void setServerData(struct FcgiServerData *data)
Definition: fcgiplus.hpp:330
混合体,能表示多种类型的值
Definition: utilities.hpp:1440
文件式会话服务
Definition: fcgiplus.hpp:98
内存式会话服务
Definition: fcgiplus.hpp:82
_Ty getRunParam() const
Definition: fcgiplus.hpp:326
Fcgi输出管理器
Definition: fcgiplus.hpp:197
提供操作Cookies相关的功能
Definition: http_Cookies.hpp:8
FastCGI相关封装
Definition: fcgiplus.hpp:14
winux::uint expires
有效期
Definition: fcgiplus.hpp:52
SessionData data
会话数据
Definition: fcgiplus.hpp:51
void * getRunParam() const
取得run()传递的参数
Definition: fcgiplus.hpp:324
winux::String getTargetEncoding() const
Definition: fcgiplus.hpp:262