fastdo  0.6.8
http_url.hpp
浏览该文件的文档.
1 #ifndef __HTTP_URL_HPP__
2 #define __HTTP_URL_HPP__
3 
4 namespace http
5 {
6 
9 {
10 public:
13  {
14  urlSimple = 0,
15  urlQueryVars = 1,
16  urlPathComp = 2,
17  urlPathMatrixParams = 4 | urlPathComp,
18  urlCanRelative = 8,
19  urlDefault = urlQueryVars | urlCanRelative,
20  urlAll = urlPathComp | urlPathMatrixParams | urlQueryVars | urlCanRelative,
21  };
22 
23 public:
24  explicit Url( int flags = urlDefault ) : _flags(flags), _isRelative(false)
25  {
26  }
27 
28  explicit Url( winux::String const & url, int flags = urlDefault ) : _flags(flags), _isRelative(false)
29  {
30  this->parse(url);
31  }
32 
33  virtual ~Url()
34  {
35  }
36 
37  // attributes -------------------------------------------------------------------------
38 
40  winux::uint getFlags() const { return _flags; }
41 
45  bool isRelative() const { return _isRelative; }
46 
50  winux::String const & getScheme() const { return _scheme; }
51 
53  winux::String const & getUser() const { return _user; }
54 
56  winux::String const & getPwd() const { return _pwd; }
57 
59  winux::String const & getHost() const { return _host; }
60 
62  winux::String const & getPort() const { return _port; }
63 
67  winux::String const & getRawPathStr() const { return _rawPathStr; }
68 
73  size_t getPathCompsCount() const { return _pathComps.getCount(); }
74 
78  winux::String getPath() const;
79 
80  //winux::Mixed const & getPathMatrixParams( winux::String const & pathComp ) const;
81  //winux::Mixed const & getPathMatrixParam( winux::String const & pathComp, winux::String const & name ) const;
82 
84  winux::Mixed const & getPathMatrixParams( int iPathComp ) const;
85 
87  winux::Mixed const & getPathMatrixParam( int iPathComp, winux::String const & name ) const;
88 
90  winux::String const & getRawQueryStr() const { return _rawQueryStr; }
91 
93  winux::Mixed const & getQueryParams() const { return _query; }
94 
96  winux::Mixed const & getQueryParam( winux::String const & name ) const { return _query[name]; }
97 
99  winux::String const & getFragment() const { return _fragment; }
100 
101  // methods ----------------------------------------------------------------------------
102 
104  Url & clear()
105  {
106  // scheme segment
107  _scheme.clear();
108  // host segment
109  _user.clear();
110  _pwd.clear();
111  _host.clear();
112  _port.clear();
113  // path segment
114  _rawPathStr.clear(); // 首不含'/'
115  //[ { pathname:{ a:1, b:2 } }, { pathname1:{ a:1, b:2 } }, ... ]
116  _pathComps.free();
117  // query segment
118  _rawQueryStr.clear(); // 首不含'?'
119  _query.free();
120  // fragment segment
121  _fragment.clear(); // 首不含'#'
122  return *this;
123  }
124 
126  winux::String toString() const;
127 
129  bool assembleSchemeSegment( winux::String * urlSegment ) const;
130 
132  bool assembleHostSegment( winux::String * urlSegment, bool hasSchemeSegment ) const;
133 
135  bool assemblePathSegment( winux::String * urlSegment, bool hasHostSegment, bool hasQuerySegment, bool hasFragmentSegment ) const;
136 
138  bool assembleQuerySegment( winux::String * urlSegment, bool incStartChar ) const;
139 
141  bool assembleFragmentSegment( winux::String * urlSegment, bool incStartChar ) const;
142 
144  winux::Mixed dump() const;
145 
147  Url & parse( winux::String const & url, bool hasSchemeSegment = true, bool hasHostSegment = true, bool hasPathSegment = true, bool hasQuerySegment = true, bool hasFragmentSegment = true );
148 
150  Url & setFlags( winux::uint flags ) { _flags = flags; return *this; }
151 
155  Url & setRelative( bool relative ) { _isRelative = relative; return *this; }
156 
158  Url & setScheme( winux::String const & scheme ) { _scheme = scheme; return *this; }
159 
161  Url & setUser( winux::String const & user ) { _user = user; return *this; }
162 
164  Url & setPwd( winux::String const & pwd ) { _pwd = pwd; return *this; }
165 
167  Url & setHost( winux::String const & host ) { _host = host; return *this; }
168 
170  Url & setPort( winux::String const & port ) { _port = port; return *this; }
171 
173  Url & setRawPathStr( winux::String const & rawPathStr );
174 
178  Url & setPathComps( winux::Mixed const & pathComps );
179 
181  Url & setRawQueryStr( winux::String const & rawQueryStr );
182 
184  Url & setQuery( winux::Mixed const & query );
185 
187  Url & setFragment( winux::String const & fragment ) { _fragment = fragment; return *this; }
188 
189 
190 private:
191  winux::uint _flags;
192  bool _isRelative;
193  // scheme segment
194  winux::String _scheme;
195  // host segment
196  winux::String _user;
197  winux::String _pwd;
198  winux::String _host;
199  winux::String _port;
200  // path segment
201  winux::String _rawPathStr; // 首不含'/'
202  //[ { pathname:{ a:1, b:2 } }, { pathname1:{ a:1, b:2 } }, ... ]
203  winux::Mixed _pathComps;
204  // query segment
205  winux::String _rawQueryStr; // 首不含'?'
206  winux::Mixed _query;
207  // fragment segment
208  winux::String _fragment; // 首不含'#'
209 
210  friend class UrlParser;
211 };
212 
215 {
216 public:
217  UrlParser( Url * pUrl ) : _pUrl(pUrl) { }
218 
220  void parse( winux::String const & url, bool hasSchemeSegment = true, bool hasHostSegment = true, bool hasPathSegment = true, bool hasQuerySegment = true, bool hasFragmentSegment = true );
222  void parseHostSegment( winux::String const & hostSegment );
224  void parsePathSegment( winux::String const & pathSegment );
226  void parseQuerySegment( winux::String const & querySegment );
227 
228 private:
230  void _parseMatrixUriParams( winux::String const & matrixSegment, winux::Mixed * pMatrixParams );
231 
232  Url * _pUrl;
233 };
234 
235 } // namespace http
236 
237 
238 #endif // __HTTP_URL_HPP__
#define HTTPWRAPPER_DLL
Definition: http.hpp:21
UrlParser(Url *pUrl)
Definition: http_url.hpp:217
Url & setUser(winux::String const &user)
设置URL主机段的用户名(可选).
Definition: http_url.hpp:161
Url & setRelative(bool relative)
设置URL是否为相对.
Definition: http_url.hpp:155
Url & setScheme(winux::String const &scheme)
设置URL方案.
Definition: http_url.hpp:158
bool isRelative() const
是否为相对URL.
Definition: http_url.hpp:45
Url & setPort(winux::String const &port)
设置URL主机段的端口号(可选).
Definition: http_url.hpp:170
XString< tchar > String
Definition: utilities.hpp:216
Url(int flags=urlDefault)
Definition: http_url.hpp:24
winux::uint getFlags() const
获取URL解析和使用标志.
Definition: http_url.hpp:40
Url & setFragment(winux::String const &fragment)
设置URL Fragment段.
Definition: http_url.hpp:187
http协议的相关简单类封装
Definition: http.hpp:32
winux::String const & getRawQueryStr() const
获取未解析的查询段字符串.不以&#39;?&#39;开头.
Definition: http_url.hpp:90
URL类
Definition: http_url.hpp:8
Url & clear()
清空
Definition: http_url.hpp:104
winux::Mixed const & getQueryParam(winux::String const &name) const
获取指定的查询参数.
Definition: http_url.hpp:96
Url & setPwd(winux::String const &pwd)
设置URL主机段的密码(可选).
Definition: http_url.hpp:164
winux::String const & getScheme() const
获取URL方案.
Definition: http_url.hpp:50
size_t getPathCompsCount() const
获取路径部件数.
Definition: http_url.hpp:73
winux::String const & getRawPathStr() const
获取未解析的路径段字符串.不以&#39;/&#39;开头.
Definition: http_url.hpp:67
unsigned int uint
Definition: utilities.hpp:170
winux::String const & getPort() const
获取端口号,如果不指定则为scheme默认决定的端口.
Definition: http_url.hpp:62
winux::String const & getHost() const
获取主机名(域名).
Definition: http_url.hpp:59
Url(winux::String const &url, int flags=urlDefault)
Definition: http_url.hpp:28
URL解析器
Definition: http_url.hpp:214
winux::String const & getPwd() const
获取密码.
Definition: http_url.hpp:56
winux::String const & getUser() const
获取用户名.
Definition: http_url.hpp:53
Url & setHost(winux::String const &host)
设置URL主机段的主机名.
Definition: http_url.hpp:167
混合体,能表示多种类型的值
Definition: utilities.hpp:750
UrlSegmentFlags
URL段解析/使用标记
Definition: http_url.hpp:12
Url & setFlags(winux::uint flags)
设置URL解析和使用标志.
Definition: http_url.hpp:150
winux::String const & getFragment() const
获取文章分段名.
Definition: http_url.hpp:99
virtual ~Url()
Definition: http_url.hpp:33
winux::Mixed const & getQueryParams() const
获取查询参数集合的常引用.
Definition: http_url.hpp:93