fastdo  0.6.8
http_misc.hpp
浏览该文件的文档.
1 #ifndef __HTTP_MISC_HPP__
2 #define __HTTP_MISC_HPP__
3 
4 namespace http
5 {
6 
11 {
12 public:
15  {
16  public:
18  HeaderField( winux::String const & fieldVal ) { parse(fieldVal); }
19  HeaderField( winux::String const & firstVal, winux::Mixed const & options );
20 
21  virtual winux::String toString() const;
22  HeaderField & parse( winux::String const & fieldVal );
23  void clear() { _firstVal.clear(); _options.clear(); }
24 
25  winux::String getOption( winux::String const & name ) const { return winux::isset( _options, name ) ? _options.at(name) : ""; }
26  void setOption( winux::String const & name, winux::String const & value ) { _options[name] = value; }
27  bool hasOption( winux::String const & name ) const { return winux::isset( _options, name ); }
28 
29  winux::StringStringMap & getOptions() { return _options; }
30  winux::StringStringMap const & getOptions() const { return _options; }
31  winux::String getFirstVal() const { return _firstVal; }
32  void setFirstVal( winux::String const & firstVal ) { _firstVal = winux::StrLower(firstVal); }
33  protected:
34  static void _ParseOptions( winux::String const & strOptions, winux::StringStringMap * options );
35 
38  };
41  {
42  public:
44  ContentType( winux::String const & fieldVal ) : HeaderField(fieldVal) { }
45  ContentType( winux::String const & mimeType, winux::Mixed const & options ) : HeaderField( mimeType, options ) { }
46 
47  virtual winux::String toString() const;
48  winux::String getMimeType() const { return _firstVal; }
49  void setMimeType( winux::String const & mimeType ) { _firstVal = winux::StrLower(mimeType); }
50  winux::String getCharset() const { return getOption("charset"); }
51  void setCharset( winux::String const & charset ) { setOption( "charset", winux::StrLower(charset) ); }
52  winux::String getBoundary() const { return getOption("boundary"); }
53  void setBoundary( winux::String const & boundary ) { setOption( "boundary", boundary ); }
54  };
57  {
58  public:
60  ContentDisposition( winux::String const & fieldVal ) : HeaderField(fieldVal) { }
61  ContentDisposition( winux::String const & firstVal, winux::Mixed const & options ) : HeaderField( firstVal, options ) { }
62  };
63 
64 public:
66  Header();
68  Header( std::initializer_list< std::pair< winux::String, winux::Mixed > > list );
70  void clear();
72  winux::String toString( bool isAddCrlfAtEnd = true ) const;
73 
75  Header & parse( winux::String const & headerStr );
76 
77  winux::String const & operator [] ( winux::String const & name ) const;
78  winux::String & operator [] ( winux::String const & name );
79 
81  winux::StringArray getHeaders( winux::String const & name ) const;
82  winux::String getHeader( winux::String const & name, winux::String const & defval = winux::String() ) const { return _headers.has(name) ? _headers[name].refAnsi() : defval; }
83  template < typename _Ty > _Ty getHeader( winux::String const & name, _Ty const & defval = _Ty() ) const { return _headers.has(name) ? (_Ty)_headers[name] : defval; }
89  void addHeader( winux::String const & name, winux::String const & value );
90  void setHeader( winux::String const & name, winux::String const & value ) { _headers[name] = value; }
91  bool hasHeader( winux::String const & name ) const { return _headers.has(name); }
92  void delHeader( winux::String const & name ) { _headers.del(name); }
93 
94  template < typename _HeaderField >
95  bool get( winux::String const & name, _HeaderField * field ) const
96  {
97  if ( _headers.has(name) )
98  {
99  field->parse( *_headers[name]._pStr );
100  return true;
101  }
102  return false;
103  }
104  template < typename _HeaderField >
105  _HeaderField get( winux::String const & name ) const
106  {
107  _HeaderField f;
108  f.parse( (*this)[name] );
109  return f;
110  }
111 
113  winux::StringWriter operator () ( winux::String const & name );
114 
118  winux::StringWriter operator () ( winux::String const & name, int i );
119 
120  winux::String getMultiHeader( winux::String const & name, int i, winux::String const & defval = winux::String() ) const;
121  void addMultiHeader( winux::String const & name, winux::String const & value );
122  bool hasMultiHeader( winux::String const & name ) const;
123  void delMultiHeader( int index );
124 
128  void setResponseLine( winux::String const & responseLine, bool setStatus = true );
129 
131  void setRequestLine( winux::String const & requestLine );
132 
134  winux::String getStatusLine( bool isAddCrlf = false ) const;
135 
137  winux::String const & getMethod() const { return _method; }
139  winux::String const & getUrl() const { return _url; }
141  winux::String const & getStatusCode() const { return _statusCode; }
143  winux::String const & getStatusStr() const { return _statusStr; }
148  winux::String const & getVersion() const { return _version; }
149 
151  Header & setMethod( winux::String const & method ) { _method = method; return *this; }
153  Header & setUrl( winux::String const & url ) { _url = url; return *this; }
155  Header & setStatusCode( winux::String const & statusCode ) { _statusCode = statusCode; return *this; }
157  Header & setStatusStr( winux::String const & statusStr ) { _statusStr = statusStr; return *this; }
162  Header & setVersion( winux::String const & version ) { _version = version; return *this; }
163 
165  bool enumHeader( winux::String * name, winux::String * value ) const;
166 
167 private:
168  winux::String _method, _url; // for a request
169  winux::String _statusCode, _statusStr; // for a response
170  winux::String _version; // for a request/response
171 
172  winux::Mixed _headers; // 存放只能一次的头
173  std::vector< std::pair< winux::String, winux::String > > _multiHeaders; // 存放可多次的头
174 
175  // 枚举相关
176  mutable int _curHeaderIndex; // 当前枚举到的头部索引
177  mutable int _curMultiHeaderIndex; // 当前枚举到的多头部索引
178  mutable bool _isCompleteEnum; // 是否完成枚举,从而进行下一次枚举,默认是true
179 };
180 
183 {
184  class HTTPWRAPPER_DLL ExpiresVal
185  {
186  winux::String _vStr;
187  public:
188  bool empty() const { return _vStr.empty(); }
189  operator winux::String & () { return _vStr; }
190  operator winux::String const & () const { return _vStr; }
191 
192  void operator = ( winux::int64 v );
193  winux::String & operator = ( winux::String const & v ) { return _vStr = v; }
194  };
195 public:
198  ExpiresVal expires;
201  bool secure;
202  bool httpOnly;
203 
204  bool wantSet; // 该Cookie是否要设置
205 
206  Cookie() : secure(false), httpOnly(false), wantSet(false) { }
207  Cookie(
208  winux::String name,
209  winux::String value = "",
210  winux::int64 expires = -1,
211  winux::String domain = "",
212  winux::String path = "",
213  bool secure = false,
214  bool httpOnly = false
215  );
216 
217  operator winux::String const & () const { return this->value; }
218 
219  // 设置值时将wantSet标志设真
220  winux::String & operator = ( winux::String const & v );
221  // 获得一个用于Set-Cookie的字符串,不包含"Set-Cookie:"串
222  winux::String toSetString() const;
223 
224 };
225 
226 inline std::ostream & operator << ( std::ostream & o, Cookie const & cookie )
227 {
228  o << (winux::String)cookie;
229  return o;
230 }
231 
234 {
235  std::map< winux::String, http::Cookie > _cookies;
236 public:
237  Cookies() { }
238 
242  void set(
243  winux::String name,
244  winux::String value = "",
245  winux::int64 expires = -1,
246  winux::String domain = "",
247  winux::String path = "",
248  bool secure = false,
249  bool httpOnly = false
250  );
251 
252  http::Cookie const & get( winux::String const & name ) const { return _cookies.at(name); }
253 
255  void clear() { _cookies.clear(); }
256 
258  void loadCookies( winux::AnsiString const & cookiesStr );
259 
261  void commitTo( http::Header * header );
262 
266  winux::Mixed dump() const;
267 
271  bool has( winux::String const & name ) const { return _cookies.find(name) != _cookies.end(); }
272 
278  http::Cookie & operator [] ( winux::String const & name );
279 
281  http::Cookie const & operator [] ( winux::String const & name ) const { return _cookies.at(name); }
282 
283 };
284 
287 {
288  winux::Mixed _vars;
289 public:
290  Vars( winux::AnsiString const & varsData = "" );
291  Vars( winux::Mixed const & vars );
292  void clear();
293  winux::String toString() const;
294  winux::Mixed const & operator [] ( winux::String const & name ) const;
295  winux::Mixed & operator [] ( winux::String const & name );
296  bool has( winux::String const & name ) const;
298  Vars & parse( winux::AnsiString const & varsData );
299 
303  template < typename _Ty >
304  _Ty get( winux::String const & name, _Ty defval = _Ty() ) const
305  {
306  return this->has(name) ? (_Ty)_vars[name] : defval;
307  }
308 
309  winux::Mixed & getVars() { return _vars; }
310 };
311 
312 } // namespace http
313 
314 
315 #endif // __HTTP_MISC_HPP__
XString< char > AnsiString
Definition: utilities.hpp:212
__int64 int64
Definition: utilities.hpp:187
winux::String getMimeType() const
Definition: http_misc.hpp:48
代表HTTP头部
Definition: http_misc.hpp:10
#define HTTPWRAPPER_DLL
Definition: http.hpp:21
Header & setVersion(winux::String const &version)
HTTP版本.
Definition: http_misc.hpp:162
HeaderField(winux::String const &fieldVal)
Definition: http_misc.hpp:18
winux::String getHeader(winux::String const &name, winux::String const &defval=winux::String()) const
Definition: http_misc.hpp:82
Header & setUrl(winux::String const &url)
URL. For a request: required.
Definition: http_misc.hpp:153
winux::StringStringMap _options
Definition: http_misc.hpp:37
ContentType(winux::String const &mimeType, winux::Mixed const &options)
Definition: http_misc.hpp:45
用来使得String能够用operator<<来赋值
Definition: strings.hpp:475
void setMimeType(winux::String const &mimeType)
Definition: http_misc.hpp:49
XString< tchar > String
Definition: utilities.hpp:216
winux::String const & getVersion() const
HTTP版本.
Definition: http_misc.hpp:148
std::ostream & operator<<(std::ostream &o, Cookie const &cookie)
Definition: http_misc.hpp:226
Header & setMethod(winux::String const &method)
方法. For a request: required.
Definition: http_misc.hpp:151
Header & setStatusCode(winux::String const &statusCode)
状态码. For a response: required.
Definition: http_misc.hpp:155
winux::String getFirstVal() const
Definition: http_misc.hpp:31
http协议的相关简单类封装
Definition: http.hpp:32
std::map< String, String > StringStringMap
Definition: utilities.hpp:229
void setBoundary(winux::String const &boundary)
Definition: http_misc.hpp:53
winux::String getBoundary() const
Definition: http_misc.hpp:52
void setCharset(winux::String const &charset)
Definition: http_misc.hpp:51
解析和设置get/post变量
Definition: http_misc.hpp:286
解析/描述头域字段的值
Definition: http_misc.hpp:14
winux::String getOption(winux::String const &name) const
Definition: http_misc.hpp:25
void setOption(winux::String const &name, winux::String const &value)
Definition: http_misc.hpp:26
winux::String const & getStatusCode() const
状态码. For a response: required.
Definition: http_misc.hpp:141
winux::String const & getStatusStr() const
状态文本. For a response: Not required
Definition: http_misc.hpp:143
void setFirstVal(winux::String const &firstVal)
Definition: http_misc.hpp:32
bool hasHeader(winux::String const &name) const
Definition: http_misc.hpp:91
bool isset(_MAP const &m, _KEY const &k)
检测map中是否有该键的值
Definition: utilities.hpp:268
winux::String const & getMethod() const
方法. For a request: required.
Definition: http_misc.hpp:137
bool hasOption(winux::String const &name) const
Definition: http_misc.hpp:27
ContentType(winux::String const &fieldVal)
Definition: http_misc.hpp:44
ContentDisposition(winux::String const &firstVal, winux::Mixed const &options)
Definition: http_misc.hpp:61
_Ty getHeader(winux::String const &name, _Ty const &defval=_Ty()) const
Definition: http_misc.hpp:83
winux::Mixed & getVars()
Definition: http_misc.hpp:309
winux::StringStringMap const & getOptions() const
Definition: http_misc.hpp:30
winux::String const & getUrl() const
URL. For a request: required.
Definition: http_misc.hpp:139
bool has(winux::String const &name) const
检测是否有该cookie
Definition: http_misc.hpp:271
winux::String getCharset() const
Definition: http_misc.hpp:50
winux::String _firstVal
Definition: http_misc.hpp:36
AnsiString StrLower(AnsiString str)
使字符串小写
Definition: strings.hpp:238
混合体,能表示多种类型的值
Definition: utilities.hpp:750
void setHeader(winux::String const &name, winux::String const &value)
Definition: http_misc.hpp:90
Content-Disposition域
Definition: http_misc.hpp:56
Header & setStatusStr(winux::String const &statusStr)
状态文本. For a response: Not required
Definition: http_misc.hpp:157
提供操作Cookies相关的功能
Definition: http_misc.hpp:233
XStringArray< tchar > StringArray
Definition: utilities.hpp:227
winux::StringStringMap & getOptions()
Definition: http_misc.hpp:29
void clear()
清空内部的映射表
Definition: http_misc.hpp:255
void delHeader(winux::String const &name)
Definition: http_misc.hpp:92
ContentDisposition(winux::String const &fieldVal)
Definition: http_misc.hpp:60
Content-Type域
Definition: http_misc.hpp:40