fastdo  0.6.16
http_Header.hpp
浏览该文件的文档.
1 #ifndef __HTTP_HEADER_HPP__
2 #define __HTTP_HEADER_HPP__
3 
4 namespace http
5 {
6 
11 {
12 public:
15  {
16  public:
17  HeaderField();
18  HeaderField( winux::String const & fieldVal );
19  HeaderField( winux::String const & firstVal, winux::Mixed const & options );
20 
21  void clear();
22 
23  virtual winux::String toString( bool semicolonSpace = true, bool commaSpace = false, bool keyNoValForceQuote = false, bool valueForceQuote = false ) const;
24  HeaderField & parse( winux::String const & fieldVal );
25 
26  winux::String getOption( winux::String const & name, size_t i = 0 ) const { return i < _optionss.size() && _optionss[i].has(name) ? _optionss[i][name] : ""; }
27  void setOption( winux::String const & name, winux::String const & value, size_t i = 0 );
28  bool hasOption( winux::String const & name, size_t i = 0 ) const { return i < _optionss.size() && _optionss[i].has(name); }
29 
30  size_t getOptionsCount() const { return _optionss.size(); }
31  winux::Mixed & getOptions( size_t i = 0 ) { return _optionss[i]; }
32  winux::Mixed const & getOptions( size_t i = 0 ) const { return _optionss[i]; }
33 
34  winux::String getFirstVal( size_t i = 0 ) const;
35  void setFirstVal( winux::String const & firstVal, size_t i = 0 );
36 
37  protected:
38  std::vector<winux::Mixed> _optionss;
39  };
40 
43  {
44  public:
46  ContentType( winux::String const & fieldVal ) : HeaderField(fieldVal) { }
47  ContentType( winux::String const & mimeType, winux::Mixed const & options ) : HeaderField( mimeType, options ) { }
48 
49  winux::String getMimeType() const;
50  void setMimeType( winux::String const & mimeType );
51 
52  winux::String getCharset() const;
53  void setCharset( winux::String const & charset );
54 
55  winux::String getBoundary() const;
56  void setBoundary( winux::String const & boundary );
57  };
58 
61  {
62  public:
64  ContentDisposition( winux::String const & fieldVal ) : HeaderField(fieldVal) { }
65  ContentDisposition( winux::String const & firstVal, winux::Mixed const & options ) : HeaderField( firstVal, options ) { }
66  };
67 
68 public:
70  Header();
72  Header( std::initializer_list< std::pair< winux::String, winux::Mixed > > list );
74  void clear();
76  winux::String toString( bool isAddCrlfAtEnd = true ) const;
77 
79  Header & parse( winux::String const & headerStr );
80 
81  winux::String const & operator [] ( winux::String const & name ) const;
82  winux::String & operator [] ( winux::String const & name );
83 
85  winux::StringArray getHeaders( winux::String const & name ) const;
86  winux::String getHeader( winux::String const & name, winux::String const & defval = winux::String() ) const { return _headers.has(name) ? _headers[name].refAnsi() : defval; }
87  template < typename _Ty > _Ty getHeader( winux::String const & name, _Ty const & defval = _Ty() ) const { return _headers.has(name) ? (_Ty)_headers[name] : defval; }
93  void addHeader( winux::String const & name, winux::String const & value );
94  void setHeader( winux::String const & name, winux::String const & value ) { _headers[name] = value; }
95  bool hasHeader( winux::String const & name ) const { return _headers.has(name); }
96  void delHeader( winux::String const & name ) { _headers.del(name); }
97 
98  template < typename _HeaderField >
99  bool get( winux::String const & name, _HeaderField * field ) const
100  {
101  if ( _headers.has(name) )
102  {
103  field->parse( *_headers[name]._pStr );
104  return true;
105  }
106  return false;
107  }
108  template < typename _HeaderField >
109  _HeaderField get( winux::String const & name ) const
110  {
111  _HeaderField f;
112  f.parse( (*this)[name] );
113  return f;
114  }
115 
117  winux::StringWriter operator () ( winux::String const & name );
118 
122  winux::StringWriter operator () ( winux::String const & name, int i );
123 
124  winux::String getMultiHeader( winux::String const & name, int i, winux::String const & defval = winux::String() ) const;
125  void addMultiHeader( winux::String const & name, winux::String const & value );
126  bool hasMultiHeader( winux::String const & name ) const;
127  void delMultiHeader( int index );
128 
132  void setResponseLine( winux::String const & responseLine, bool setStatus = true );
133 
135  void setRequestLine( winux::String const & requestLine );
136 
138  winux::String getStatusLine( bool isAddCrlf = false ) const;
139 
141  winux::String const & getMethod() const { return _method; }
143  winux::String const & getUrl() const { return _url; }
145  winux::String const & getStatusCode() const { return _statusCode; }
147  winux::String const & getStatusStr() const { return _statusStr; }
152  winux::String const & getVersion() const { return _version; }
153 
155  Header & setMethod( winux::String const & method ) { _method = method; return *this; }
157  Header & setUrl( winux::String const & url ) { _url = url; return *this; }
159  Header & setStatusCode( winux::String const & statusCode ) { _statusCode = statusCode; return *this; }
161  Header & setStatusStr( winux::String const & statusStr ) { _statusStr = statusStr; return *this; }
166  Header & setVersion( winux::String const & version ) { _version = version; return *this; }
167 
169  bool enumHeader( winux::String * name, winux::String * value ) const;
170 
171 private:
172  winux::String _method, _url; // for a request
173  winux::String _statusCode, _statusStr; // for a response
174  winux::String _version; // for a request/response
175 
176  winux::Mixed _headers; // 存放只能一次的头
177  std::vector< std::pair< winux::String, winux::String > > _multiHeaders; // 存放可多次的头
178 
179  // 枚举相关
180  mutable int _curHeaderIndex; // 当前枚举到的头部索引
181  mutable int _curMultiHeaderIndex; // 当前枚举到的多头部索引
182  mutable bool _isCompleteEnum; // 是否完成枚举,从而进行下一次枚举,默认是true
183 };
184 
185 } // namespace http
186 
187 
188 #endif // __HTTP_HEADER_HPP__
代表HTTP头部
Definition: http_Header.hpp:10
std::vector< winux::Mixed > _optionss
Definition: http_Header.hpp:38
#define HTTPWRAPPER_DLL
Definition: http_base.hpp:24
Header & setVersion(winux::String const &version)
HTTP版本.
winux::String getHeader(winux::String const &name, winux::String const &defval=winux::String()) const
Definition: http_Header.hpp:86
Header & setUrl(winux::String const &url)
URL. For a request: required.
ContentType(winux::String const &mimeType, winux::Mixed const &options)
Definition: http_Header.hpp:47
用来使得String能够用operator<<来赋值
Definition: strings.hpp:724
XString< tchar > String
Definition: utilities.hpp:261
winux::String const & getVersion() const
HTTP版本.
Header & setMethod(winux::String const &method)
方法. For a request: required.
Header & setStatusCode(winux::String const &statusCode)
状态码. For a response: required.
winux::Mixed & getOptions(size_t i=0)
Definition: http_Header.hpp:31
HTTP协议的相关简单类封装
Definition: http_base.hpp:32
解析/描述头域字段的值
Definition: http_Header.hpp:14
winux::String const & getStatusCode() const
状态码. For a response: required.
size_t getOptionsCount() const
Definition: http_Header.hpp:30
winux::Mixed const & getOptions(size_t i=0) const
Definition: http_Header.hpp:32
winux::String const & getStatusStr() const
状态文本. For a response: Not required
bool hasHeader(winux::String const &name) const
Definition: http_Header.hpp:95
winux::String const & getMethod() const
方法. For a request: required.
ContentType(winux::String const &fieldVal)
Definition: http_Header.hpp:46
ContentDisposition(winux::String const &firstVal, winux::Mixed const &options)
Definition: http_Header.hpp:65
_Ty getHeader(winux::String const &name, _Ty const &defval=_Ty()) const
Definition: http_Header.hpp:87
winux::String getOption(winux::String const &name, size_t i=0) const
Definition: http_Header.hpp:26
winux::String const & getUrl() const
URL. For a request: required.
混合体,能表示多种类型的值
Definition: utilities.hpp:1440
void setHeader(winux::String const &name, winux::String const &value)
Definition: http_Header.hpp:94
Content-Disposition域
Definition: http_Header.hpp:60
bool hasOption(winux::String const &name, size_t i=0) const
Definition: http_Header.hpp:28
Header & setStatusStr(winux::String const &statusStr)
状态文本. For a response: Not required
XStringArray< tchar > StringArray
Definition: utilities.hpp:272
void delHeader(winux::String const &name)
Definition: http_Header.hpp:96
ContentDisposition(winux::String const &fieldVal)
Definition: http_Header.hpp:64