fastdo  0.6.8
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 protected:
62 public:
63  SessionServer() : _cleanTimeInterval(1800), _sessionLifeTime(3600) { }
64  virtual ~SessionServer() { }
65 
66  winux::uint getCleanTimeInterval() const { return _cleanTimeInterval; }
67  winux::uint getSessionLifeTime() const { return _sessionLifeTime; }
68 
70  virtual void clean() = 0;
72  virtual bool startup( winux::String const & sessid, SessionData * sessData ) = 0;
74  virtual bool save( winux::String const & sessid, SessionData const * sessData, winux::uint thisLifeTime = 0 ) = 0;
75 };
76 
82 {
83 private:
85  typedef std::map<winux::String, SessionStruct> SessionStructMap;
86  SessionStructMap _sessions;
87 public:
88  MemorySessionServer( winux::uint cleanTimeInterval = 1800, winux::uint sessionLifeTime = 1800 );
89  virtual void clean();
90  virtual bool startup( winux::String const & sessid, SessionData * sessData );
91  virtual bool save( winux::String const & sessid, SessionData const * sessData, winux::uint thisLifeTime = 0 );
92 
94 };
95 
98 {
99 public:
100  FileSessionServer( winux::String const & savePath, winux::uint cleanTimeInterval = 1800, winux::uint sessionLifeTime = 1800 );
101  virtual void clean();
102  virtual bool startup( winux::String const & sessid, SessionData * sessData );
103  virtual bool save( winux::String const & sessid, SessionData const * sessData, winux::uint thisLifeTime = 0 );
104 private:
105  winux::String _savePath; // session文件保存路径
107 };
108 
116 {
117  Request & _req;
118  SessionData _data;
119  winux::String _sessid; // session唯一标识
120  winux::uint _cookieSessIdLifeTime; // sessid cookie生命期
121  winux::uint _sessionLifeTime; // session生命期
122  bool _isModified; // 是否修改过
123 public:
124  Session( Request & req, winux::String const & sessid = "", winux::uint cookieSessIdLifeTime = 0, winux::uint sessionLifeTime = 0, winux::String const & cookiesPath = "" );
125  ~Session();
126  winux::Mixed & operator [] ( winux::String const & name );
127  winux::Mixed const & operator [] ( winux::String const & name ) const;
128  bool has( winux::String const & name ) const;
129  winux::Mixed const & get( winux::String const & name ) const;
130  void set( winux::String const & name, winux::Mixed const & v );
131  bool del( winux::String const & name );
132 
134 };
135 
138 {
139 private:
141 public:
142  Request();
143  ~Request();
144 
146  void init( FcgiApp * app, int sock, int flags );
147 
149  void free( bool close = false );
150 
154  void finish();
155 
159  bool accept();
160 
162  bool processData();
163 
165  winux::String const & operator [] ( winux::String const & name ) const;
166 
170  void dumpEnvTo( winux::String * str, char * * envp = NULL ) const;
171 
182 
183  friend class Response;
184  friend class FcgiApp;
185 
187 };
188 
194 {
195 private:
197 
198  void _backupStd();
199  void _resumeStd();
200  void _redirectToFcgi();
201 
202  // 尝试编码转换。返回值 0:不需要进行编码转换,1:需要进行编码转换并且执行了,2:需要进行编码转换,由于之前已经转换成功,所以直接返回。
203  int _tryEncodingConvert();
204  // 是否需要进行编码转换
205  // 不需要转换的情况:a.没有设置本地编码或者目标编码,b.本地编码和目标编码相同,c.MIME不是text类型
206  bool _isNeedEncodingConvert() const;
207 
208 public:
209  // FCGX_Stream * fcgiOut, FCGX_Stream * fcgiErr
210  FcgiOutputMgr( struct FCGX_Stream * fcgiOut, struct FCGX_Stream * fcgiErr );
211  ~FcgiOutputMgr();
212 
217  void setLocalEncoding( winux::String const & local );
218 
222  void setTargetEncoding( winux::String const & target );
223 
224  winux::String getLocalEncoding() const;
225  winux::String getTargetEncoding() const;
226 
228  winux::String convFrom( winux::String const & str ) const;
230  winux::String convTo( winux::String const & str ) const;
231 
232  void echo( winux::String const & str );
233  void write( void const * data, int size );
234  void printf( char const * format, va_list va );
236  winux::uint getCacheOutStreamSize() const;
238  winux::uint getConvertedContentSize();
239 
240  friend class Response;
241 
243 };
244 
247 {
248 private:
249  FcgiOutputMgr _outputMgr;
250  winux::String _mimeType;
251  bool _autoContentType;
252 public:
255 
256  explicit Response( Request & request );
257  ~Response();
258 
259  void echo( winux::String const & str );
260  void write( void const * data, int size );
261  void write( winux::Buffer const & buf );
262  void printf( char const * format, ... );
263 
264  winux::String getLocalEncoding() const { return _outputMgr.getLocalEncoding(); }
265  winux::String getTargetEncoding() const { return _outputMgr.getTargetEncoding(); }
266 
267  winux::String convFrom( winux::String const & str ) const { return _outputMgr.convFrom(str); }
268  winux::String convTo( winux::String const & str ) const { return _outputMgr.convTo(str); }
270  void setMimeType( winux::String const & mime ) { _mimeType = mime; }
271  winux::String getMimeType() const { return _mimeType; }
272 
277  void setAutoContentType( bool autoContentType = true ) { _autoContentType = autoContentType; }
278 
283  void setCharset( winux::String const & charset ) { _outputMgr.setTargetEncoding(charset); }
284  winux::String getCharset() const { return _outputMgr.getTargetEncoding(); }
285 
291  void import( winux::String const & doFile, void * runParam = NULL, int * retCode = NULL );
292 
294 };
295 
298 {
299 public:
300 
305  FcgiApp( winux::Configure & conf, struct FcgiServerData * servData = NULL );
306  ~FcgiApp();
307 
309  int run( void * runParam );
310 
312  void * getParam() const { return _param; };
313  template < typename _Ty >
314  _Ty getParam() const { return reinterpret_cast<_Ty>(_param); };
315 
317  void setParam( void * param ) { this->_param = param; }
318 
320  void * getRunParam() const { return _runParam; };
321  template < typename _Ty >
322  _Ty getRunParam() const { return reinterpret_cast<_Ty>(_runParam); };
323 
325  struct FcgiServerData * getServerData() { return _servData; }
326  void setServerData( struct FcgiServerData * data ) { _servData = data; }
327 
329  SessionServer * getSessServ() { return _sessServ; }
330  void setSessServ( SessionServer * sessServ ) { this->_sessServ = sessServ; }
331 
334  {
339  };
340 
345  virtual ErrorCode execWebMain( Response * rsp, void * runParam, int * retCode );
346 
354  ErrorCode importDoFile( winux::String const & doFile, Response * rsp, void * runParam, int * retCode, winux::String * errDetail );
355 
357  int loadedDoFiles( winux::Mixed * arrDoFiles ) const;
358 
360  bool unloadDoFile( winux::String const & doFullPath );
361 
363  winux::DllLoader & loadModule( winux::String const & modFile );
364 
366  int loadedModules( winux::Mixed * arrModules ) const;
367 
369  bool unloadModule( winux::String const & modFile );
370 
373 
376 
377  // 获取一些已经加载的配置
380  {
393 
394  ConfigParams() : isProduction(false), maxUrlencodedPostSize(0), maxMultiFormPostSize(0)
395  {
396 
397  }
398 
399  } configParams;
400 
401 
402 private:
405  SessionServer * _sessServ; // session server
406  void * _param; // setParam()传递进来的参数
407  void * _runParam; // run()传递进来的参数
408  struct FcgiServerData * _servData; // server data
410  ErrorCode _importDoFileDevelopment( winux::String const & doFullPath, Response * rsp, void * runParam, int * retCode, winux::String * errDetail );
411  ErrorCode _importDoFileProduction( winux::String const & doFullPath, Response * rsp, void * runParam, int * retCode, winux::String * errDetail );
412 
413  friend class Request;
414  friend class Response;
416 };
417 
418 } // namespace fcgi
419 
420 
421 #endif // __FCGIPLUS_HPP__
__int64 int64
Definition: utilities.hpp:187
winux::StringStringMap environVars
环境变量
Definition: fcgiplus.hpp:173
代表HTTP头部
Definition: http_misc.hpp:10
virtual ~SessionServer()
Definition: fcgiplus.hpp:64
http::Vars post
POST变量 readonly.
Definition: fcgiplus.hpp:179
void setParam(void *param)
设置do传递参数
Definition: fcgiplus.hpp:317
winux::uint getCleanTimeInterval() const
Definition: fcgiplus.hpp:66
_Ty getParam() const
Definition: fcgiplus.hpp:314
struct FcgiServerData * getServerData()
取得服务器相关数据
Definition: fcgiplus.hpp:325
void setAutoContentType(bool autoContentType=true)
是否自动设置Content-Type
Definition: fcgiplus.hpp:277
每个用户的会话结构
Definition: fcgiplus.hpp:49
std::map< String, Mixed > StringMixedMap
Definition: utilities.hpp:233
Request & req
Request对象的引用
Definition: fcgiplus.hpp:253
FcgiApp * fcgiApp
FcgiApp对象指针
Definition: fcgiplus.hpp:181
SessionServer * getSessServ()
取得/设置SessionServer
Definition: fcgiplus.hpp:329
配置文件类
Definition: archives.hpp:11
FCGI程序类,代表整个FCGI程序
Definition: fcgiplus.hpp:297
XString< tchar > String
Definition: utilities.hpp:216
代表客户请求
Definition: fcgiplus.hpp:137
winux::String convFrom(winux::String const &str) const
Definition: fcgiplus.hpp:267
winux::uint _sessionLifeTime
默认session生命期
Definition: fcgiplus.hpp:61
std::map< String, String > StringStringMap
Definition: utilities.hpp:229
winux::StringMixedMap SessionData
会话数据
Definition: fcgiplus.hpp:43
bool isProduction
isProduction指示是否以产品级模式运行.
Definition: fcgiplus.hpp:384
http::Header & header
表示头部
Definition: fcgiplus.hpp:254
void setCharset(winux::String const &charset)
设置charset,即设置输出目标编码
Definition: fcgiplus.hpp:283
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:81
DLL动态载入器
Definition: system.hpp:252
winux::String getMimeType() const
Definition: fcgiplus.hpp:271
会话服务基类
Definition: fcgiplus.hpp:57
Request request
请求
Definition: fcgiplus.hpp:372
http::Cookies cookies
Cookie变量 read/write.
Definition: fcgiplus.hpp:175
解析和设置get/post变量
Definition: http_misc.hpp:286
winux::String outputTplPath
获取已经从配置文件中加载的【输出模板的路径】
Definition: fcgiplus.hpp:392
网站会话
Definition: fcgiplus.hpp:115
winux::String uploadTmpPath
上传文件的临时目录
Definition: fcgiplus.hpp:390
winux::Configure & config
配置
Definition: fcgiplus.hpp:375
代表一个客户响应
Definition: fcgiplus.hpp:246
void setSessServ(SessionServer *sessServ)
Definition: fcgiplus.hpp:330
缓冲区,表示内存中一块二进制数据(利用malloc/realloc进行内存分配)
Definition: utilities.hpp:528
void setMimeType(winux::String const &mime)
设置mime
Definition: fcgiplus.hpp:270
winux::String getCharset() const
Definition: fcgiplus.hpp:284
#define FCGIPLUS_DLL
Definition: fcgiplus.hpp:32
void * getParam() const
取得do传递的参数
Definition: fcgiplus.hpp:312
ErrorCode
执行do文件的错误码
Definition: fcgiplus.hpp:333
winux::int64 maxMultiFormPostSize
当请求的Content-Type是multipart/form-data时,多部分表单POST发送数据的最大大小
Definition: fcgiplus.hpp:388
void setTargetEncoding(winux::String const &target)
指定目标编码(Response不会自动设置)
winux::String convFrom(winux::String const &str) const
目标编码转到本地编码
unsigned int uint
Definition: utilities.hpp:170
winux::String getLocalEncoding() const
Definition: fcgiplus.hpp:264
winux::String getLocalEncoding() const
winux::uint getSessionLifeTime() const
Definition: fcgiplus.hpp:67
winux::String convTo(winux::String const &str) const
Definition: fcgiplus.hpp:268
winux::String getTargetEncoding() const
winux::int64 maxUrlencodedPostSize
当请求的Content-Type是application/x-www-form-urlencoded时,表单POST发送数据的最大大小 ...
Definition: fcgiplus.hpp:386
winux::String convTo(winux::String const &str) const
本地编码转到目标编码
winux::uint _cleanTimeInterval
执行"清理过期session"的时间间隔
Definition: fcgiplus.hpp:60
void setServerData(struct FcgiServerData *data)
Definition: fcgiplus.hpp:326
混合体,能表示多种类型的值
Definition: utilities.hpp:750
文件式会话服务
Definition: fcgiplus.hpp:97
内存式会话服务
Definition: fcgiplus.hpp:81
_Ty getRunParam() const
Definition: fcgiplus.hpp:322
Fcgi输出管理器
Definition: fcgiplus.hpp:193
提供操作Cookies相关的功能
Definition: http_misc.hpp:233
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:320
winux::String getTargetEncoding() const
Definition: fcgiplus.hpp:265