xbmc
NptUri.h
1 /*****************************************************************
2 |
3 | Neptune - URI
4 |
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
6 | All rights reserved.
7 |
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
18 |
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 |
30  ****************************************************************/
31 
32 #ifndef _NPT_URI_H_
33 #define _NPT_URI_H_
34 
35 /*----------------------------------------------------------------------
36 | includes
37 +---------------------------------------------------------------------*/
38 #include "NptStrings.h"
39 #include "NptList.h"
40 
41 /*----------------------------------------------------------------------
42 | constants
43 +---------------------------------------------------------------------*/
44 const NPT_UInt16 NPT_URL_INVALID_PORT = 0;
45 const NPT_UInt16 NPT_URL_DEFAULT_HTTP_PORT = 80;
46 const NPT_UInt16 NPT_URL_DEFAULT_HTTPS_PORT = 443;
47 
48 /*----------------------------------------------------------------------
49 | NPT_Uri
50 +---------------------------------------------------------------------*/
51 class NPT_Uri {
52 public:
53  // types
54  typedef enum {
55  SCHEME_ID_UNKNOWN,
56  SCHEME_ID_HTTP,
57  SCHEME_ID_HTTPS
58  } SchemeId;
59 
60  // constants. use as a parameter to Encode()
61  static const char* const PathCharsToEncode;
62  static const char* const QueryCharsToEncode;
63  static const char* const FragmentCharsToEncode;
64  static const char* const UnsafeCharsToEncode;
65 
66  // class methods
67  static NPT_String PercentEncode(const char* str, const char* chars, bool encode_percents=true);
68  static NPT_String PercentDecode(const char* str);
69  static SchemeId ParseScheme(const NPT_String& scheme);
70 
71  // methods
72  NPT_Uri() : m_SchemeId(SCHEME_ID_UNKNOWN) {}
73  virtual ~NPT_Uri() {}
74  const NPT_String& GetScheme() const {
75  return m_Scheme;
76  }
77  void SetScheme(const char* scheme);
78  NPT_Result SetSchemeFromUri(const char* uri);
79  SchemeId GetSchemeId() const {
80  return m_SchemeId;
81  }
82 
83 protected:
84  // members
85  NPT_String m_Scheme;
86  SchemeId m_SchemeId;
87 };
88 
89 /*----------------------------------------------------------------------
90 | NPT_UrlQuery
91 +---------------------------------------------------------------------*/
93 {
94 public:
95  // class methods
96  static NPT_String UrlEncode(const char* str, bool encode_percents=true);
97  static NPT_String UrlDecode(const char* str);
98 
99  // types
100  struct Field {
101  Field(const char* name, const char* value, bool encoded);
102  NPT_String m_Name;
103  NPT_String m_Value;
104  };
105 
106  // constructor
107  NPT_UrlQuery() {}
108  NPT_UrlQuery(const char* query);
109 
110  // accessors
111  NPT_List<Field>& GetFields() { return m_Fields; }
112 
113  // methods
114  NPT_Result Parse(const char* query);
115  NPT_Result SetField(const char* name, const char* value, bool encoded=false);
116  NPT_Result AddField(const char* name, const char* value, bool encoded=false);
117  const char* GetField(const char* name);
118  NPT_String ToString();
119 
120 private:
121  // members
122  NPT_List<Field> m_Fields;
123 };
124 
125 /*----------------------------------------------------------------------
126 | NPT_Url
127 +---------------------------------------------------------------------*/
128 class NPT_Url : public NPT_Uri {
129 public:
135  NPT_Url();
136 
145  NPT_Url(const char* url, NPT_UInt16 default_port = 0);
146 
159  NPT_Url(const char* scheme,
160  const char* host,
161  NPT_UInt16 port,
162  const char* path,
163  const char* query = NULL,
164  const char* fragment = NULL);
165 
172  NPT_Result Parse(const char* url, NPT_UInt16 default_port = 0);
173 
179  NPT_Result ParsePathPlus(const char* path_plus);
180 
184  const NPT_String& GetHost() const { return m_Host; }
185 
189  NPT_UInt16 GetPort() const { return m_Port; }
190 
194  const NPT_String& GetPath() const { return m_Path; }
195 
199  NPT_String GetPath(bool decoded) const { return decoded?NPT_Uri::PercentDecode(m_Path):m_Path;}
200 
204  const NPT_String& GetQuery() const { return m_Query; }
205 
209  const NPT_String& GetFragment() const { return m_Fragment; }
210 
217  virtual bool IsValid() const;
218 
222  void Reset();
223 
229  bool HasQuery() const { return m_HasQuery; }
230 
236  bool HasFragment() const { return m_HasFragment; }
237 
243  NPT_Result SetHost(const char* host);
244 
250  NPT_Result SetPort(NPT_UInt16 port);
251 
260  NPT_Result SetPath(const char* path, bool encoded=false);
261 
270  NPT_Result SetQuery(const char* query, bool encoded=false);
271 
280  NPT_Result SetFragment(const char* fragment, bool encoded=false);
281 
289  virtual NPT_String ToRequestString(bool with_fragment = false) const;
290 
300  virtual NPT_String ToStringWithDefaultPort(NPT_UInt16 default_port, bool with_fragment = true) const;
301 
308  virtual NPT_String ToString(bool with_fragment = true) const;
309 
310 protected:
311  // members
312  NPT_String m_Host;
313  bool m_HostIsIpv6Address;
314  NPT_UInt16 m_Port;
315  NPT_String m_Path;
316  bool m_HasQuery;
317  NPT_String m_Query;
318  bool m_HasFragment;
319  NPT_String m_Fragment;
320 };
321 
322 #endif // _NPT_URI_H_
const NPT_String & GetHost() const
Returns the host part of the URL, in its encoded form.
Definition: NptUri.h:184
bool HasQuery() const
Returns whether the URL has a query part or not.
Definition: NptUri.h:229
NPT_UInt16 GetPort() const
Returns the port number of the URL.
Definition: NptUri.h:189
Definition: NptUri.h:128
Definition: NptUri.h:100
bool HasFragment() const
Returns whether the URL has a fragment part or not.
Definition: NptUri.h:236
NPT_String GetPath(bool decoded) const
Returns the path part of the URL, in its encoded or decoded form.
Definition: NptUri.h:199
Definition: NptUri.h:51
Definition: NptUri.h:92
const NPT_String & GetQuery() const
Returns the query part of the URL, in its encoded form.
Definition: NptUri.h:204
Definition: NptStrings.h:57
Definition: NptList.h:54
const NPT_String & GetPath() const
Returns the path part of the URL, in its encoded form.
Definition: NptUri.h:194
const NPT_String & GetFragment() const
Returns the fragment part of the URL, in its encoded form.
Definition: NptUri.h:209