fastdo  0.5.12
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:
65  Header();
67  void clear();
68  winux::String toString( bool isAddCrlfAtEnd = true ) const;
69 
71  Header & parse( winux::String const & headerStr );
72 
73  winux::String const & operator [] ( winux::String const & name ) const;
74  winux::String & operator [] ( winux::String const & name );
75 
76  winux::String getHeader( winux::String const & name, winux::String const & defval = winux::String() ) const { return _headers.has(name) ? *_headers[name]._pStr : defval; }
77  template < typename _Ty > _Ty getHeader( winux::String const & name, _Ty const & defval = _Ty() ) const { return _headers.has(name) ? (_Ty)_headers[name] : defval; }
78  void setHeader( winux::String const & name, winux::String const & value ) { _headers[name] = value; }
79  bool hasHeader( winux::String const & name ) const { return _headers.has(name); }
80  void delHeader( winux::String const & name ) { /*_headers.erase(name);*/ _headers.del(name); }
81 
82  template < typename _HeaderField >
83  bool get( winux::String const & name, _HeaderField * field ) const
84  {
85  if ( _headers.has(name) )
86  {
87  field->parse( *_headers[name]._pStr );
88  return true;
89  }
90  return false;
91  }
92  template < typename _HeaderField >
93  _HeaderField get( winux::String const & name ) const
94  {
95  _HeaderField f;
96  f.parse( (*this)[name] );
97  return f;
98  }
99 
101  winux::OutStringStreamWrapper operator () ( winux::String const & name );
102 
106  winux::OutStringStreamWrapper operator () ( winux::String const & name, int i );
107 
108  winux::String getMultiHeader( winux::String const & name, int i, winux::String const & defval = winux::String() ) const;
109  void addMultiHeader( winux::String const & name, winux::String const & value );
110  void delMultiHeader( int index );
111 
115  void setResponseLine( winux::String const & responseLine, bool setStatus = true );
116 
118  void setRequestLine( winux::String const & requestLine );
119 
121  winux::String getStatusLine( bool isAddCrlf = false ) const;
122 
124  winux::String getMethod() const { return _method; }
126  winux::String getUrl() const { return _url; }
128  winux::String getStatusCode() const { return _statusCode; }
130  winux::String getStatusStr() const { return _statusStr; }
132  winux::String getVersion() const { return _version; }
133 
134  Header & setMethod( winux::String const & method ) { _method = method; return *this; }
135  Header & setUrl( winux::String const & url ) { _url = url; return *this; }
136  Header & setStatusCode( winux::String const & statusCode ) { _statusCode = statusCode; return *this; }
137  Header & setStatusStr( winux::String const & statusStr ) { _statusStr = statusStr; return *this; }
138  Header & setVersion( winux::String const & version ) { _version = version; return *this; }
139 
141  bool enumHeader( winux::String * name, winux::String * value ) const;
142 
143 private:
144  winux::String _method, _url; // for a request */
145  winux::String _statusCode, _statusStr; // for a response
146  winux::String _version;
147 
148  winux::Mixed _headers;
149  std::vector< std::pair< winux::String, winux::String > > _multiHeaders;
150  bool _hasMultiHeader( winux::String const & name ) const;
151 
152  // 枚举相关
153  mutable int _curHeaderIndex; // 当前枚举到的头部索引
154  mutable int _curMultiHeaderIndex; // 当前枚举到的多头部索引
155  mutable bool _isCompleteEnum; // 是否完成枚举,从而进行下一次枚举,默认是true
156 };
157 
160 {
161  class HTTPWRAPPER_DLL ExpiresVal
162  {
163  winux::String _v;
164  public:
165  bool empty() const { return _v.empty(); }
166  operator winux::String & () { return _v; }
167  operator winux::String const & () const { return _v; }
168 
169  void operator = ( int v );
170  winux::String & operator = ( winux::String const & v ) { return _v = v; }
171  };
172 public:
175  ExpiresVal expires;
178  bool secure;
179  bool httpOnly;
180 
181  bool wantSet; // 该Cookie是否要设置
182 
183  Cookie() : secure(false), httpOnly(false), wantSet(false) { }
184  Cookie(
185  winux::String name,
186  winux::String value = "",
187  int expires = -1,
188  winux::String domain = "",
189  winux::String path = "",
190  bool secure = false,
191  bool httpOnly = false
192  );
193 
194  operator winux::String const & () const { return this->value; }
195 
196  // 设置值时将wantSet标志设真
197  winux::String & operator = ( winux::String const & v );
198  // 获得一个用于Set-Cookie的字符串,不包含"Set-Cookie:"串
199  winux::String toSetString() const;
200 
201 };
202 
203 inline std::ostream & operator << ( std::ostream & o, Cookie const & cookie )
204 {
205  o << (winux::String)cookie;
206  return o;
207 }
208 
211 {
212  std::map< winux::String, http::Cookie > _cookies;
213 public:
214  Cookies() { }
215 
219  void set(
220  winux::String name,
221  winux::String value = "",
222  int expires = -1,
223  winux::String domain = "",
224  winux::String path = "",
225  bool secure = false,
226  bool httpOnly = false
227  );
228 
229  http::Cookie const & get( winux::String const & name ) const { return _cookies.at(name); }
230 
232  void clear() { _cookies.clear(); }
233 
235  void loadCookies( char const * cookiesStr );
236 
238  void commitTo( http::Header * header );
239 
243  winux::String dump() const;
244 
248  bool has( winux::String const & name ) const { return _cookies.find(name) != _cookies.end(); }
249 
255  http::Cookie & operator [] ( winux::String const & name );
256 
258  http::Cookie const & operator [] ( winux::String const & name ) const { return _cookies.at(name); }
259 
260 };
261 
264 {
265  winux::Mixed _vars;
266 public:
267  Vars( char const * varsData = NULL );
268  Vars( winux::Mixed const & vars );
269  void clear();
270  winux::String toString() const;
271  winux::Mixed const & operator [] ( winux::String const & name ) const;
272  winux::Mixed & operator [] ( winux::String const & name );
273  bool has( winux::String const & name ) const;
275  Vars & parse( char const * varsData );
276 
280  template < typename _Ty >
281  _Ty get( winux::String const & name, _Ty defval = _Ty() ) const
282  {
283  return this->has(name) ? (_Ty)_vars[name] : defval;
284  }
285 
286  winux::Mixed & getVars() { return _vars; }
287 };
288 
289 } // namespace http
290 
291 
292 #endif // __HTTP_MISC_HPP__
winux::String getMimeType() const
Definition: http_misc.hpp:48
代表HTTP头部
Definition: http_misc.hpp:10
#define HTTPWRAPPER_DLL
Definition: http.hpp:21
String StrLower(String const &str)
Header & setVersion(winux::String const &version)
Definition: http_misc.hpp:138
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:76
Header & setUrl(winux::String const &url)
Definition: http_misc.hpp:135
winux::StringStringMap _options
Definition: http_misc.hpp:37
ContentType(winux::String const &mimeType, winux::Mixed const &options)
Definition: http_misc.hpp:45
void setMimeType(winux::String const &mimeType)
Definition: http_misc.hpp:49
std::ostream & operator<<(std::ostream &o, Cookie const &cookie)
Definition: http_misc.hpp:203
Header & setMethod(winux::String const &method)
Definition: http_misc.hpp:134
Header & setStatusCode(winux::String const &statusCode)
Definition: http_misc.hpp:136
winux::String getFirstVal() const
Definition: http_misc.hpp:31
http协议的相关简单类封装
Definition: http.hpp:32
std::map< String, String > StringStringMap
Definition: utilities.hpp:172
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
winux::String getUrl() const
URL for a request.
Definition: http_misc.hpp:126
winux::String getMethod() const
方法 for a request
Definition: http_misc.hpp:124
解析和设置get/post变量
Definition: http_misc.hpp:263
解析/描述头域字段的值
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 getVersion() const
HTTP版本 for a request/response.
Definition: http_misc.hpp:132
winux::String getStatusCode() const
状态码 for a response
Definition: http_misc.hpp:128
void setFirstVal(winux::String const &firstVal)
Definition: http_misc.hpp:32
bool hasHeader(winux::String const &name) const
Definition: http_misc.hpp:79
bool isset(_MAP const &m, _KEY const &k)
检测map中是否有该键的值
Definition: utilities.hpp:185
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:77
winux::Mixed & getVars()
Definition: http_misc.hpp:286
winux::StringStringMap const & getOptions() const
Definition: http_misc.hpp:30
bool has(winux::String const &name) const
检测是否有该cookie
Definition: http_misc.hpp:248
winux::String getCharset() const
Definition: http_misc.hpp:50
winux::String _firstVal
Definition: http_misc.hpp:36
混合体,能表示多种类型的值
Definition: utilities.hpp:586
void setHeader(winux::String const &name, winux::String const &value)
Definition: http_misc.hpp:78
winux::String getStatusStr() const
状态文本 for a response
Definition: http_misc.hpp:130
Content-Disposition域
Definition: http_misc.hpp:56
Header & setStatusStr(winux::String const &statusStr)
Definition: http_misc.hpp:137
提供操作Cookies相关的功能
Definition: http_misc.hpp:210
winux::StringStringMap & getOptions()
Definition: http_misc.hpp:29
用来使得String能够用operator<<来赋值
Definition: strings.hpp:145
std::basic_string< tchar > String
Definition: utilities.hpp:162
void clear()
清空内部的映射表
Definition: http_misc.hpp:232
void delHeader(winux::String const &name)
Definition: http_misc.hpp:80
ContentDisposition(winux::String const &fieldVal)
Definition: http_misc.hpp:60
Content-Type域
Definition: http_misc.hpp:40