• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2020, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef OT_LIB_URL_URL_HPP_
30 #define OT_LIB_URL_URL_HPP_
31 
32 #include <stdint.h>
33 #include <openthread/error.h>
34 
35 /**
36  * @struct otUrl
37  *
38  * Represents a URL.
39  */
40 struct otUrl
41 {
42     const char *mProtocol; ///< The URL protocol.
43     const char *mPath;     ///< The path.
44     const char *mQuery;    ///< The start of the URL query string.
45     const char *mEnd;      ///< The end of the URL buffer.
46 };
47 
48 namespace ot {
49 namespace Url {
50 
51 /**
52  * Implements the URL processing.
53  */
54 class Url : public otUrl
55 {
56 public:
57     Url(void);
58 
59     /**
60      * Initializes the URL.
61      *
62      * @param[in]   aUrl    A buffer stores the null-terminated URL string.
63      *
64      * @retval  OT_ERROR_NONE   Successfully parsed the URL.
65      * @retval  OT_ERROR_PARSE  Failed to parse the string as a URL.
66      */
67     otError Init(char *aUrl);
68 
69     /**
70      * Gets the path in URL.
71      *
72      * @returns The path in URL.
73      */
GetPath(void) const74     const char *GetPath(void) const { return mPath; }
75 
76     /**
77      * Gets the value of parameter @p aName.
78      *
79      * @param[in] aName       The parameter name.
80      * @param[in] aLastValue  The last iterated parameter value, nullptr for the first value.
81      *
82      * @returns The parameter value.
83      */
84     const char *GetValue(const char *aName, const char *aLastValue = nullptr) const;
85 
86     /**
87      * Returns the URL protocol.
88      *
89      * @returns The URL protocol.
90      */
GetProtocol(void) const91     const char *GetProtocol(void) const { return mProtocol; }
92 
93     /**
94      * Indicates whether or not the url contains the parameter.
95      *
96      * @param[in]  aName  A pointer to the parameter name.
97      *
98      * @retval TRUE   The url contains the parameter.
99      * @retval FALSE  The url doesn't support the parameter.
100      */
HasParam(const char * aName) const101     bool HasParam(const char *aName) const { return (GetValue(aName) != nullptr); }
102 
103     /**
104      * Parses a `uint32_t` parameter value.
105      *
106      * The parameter value in string is parsed as decimal or hex format (if contains `0x` or `0X` prefix).
107      *
108      * @param[in]  aName    A pointer to the parameter name.
109      * @param[out] aValue   A reference to an `uint32_t` variable to output the parameter value.
110      *                      The original value of @p aValue won't change if failed to get the value.
111      *
112      * @retval OT_ERROR_NONE          The parameter value was parsed successfully.
113      * @retval OT_ERROR_NOT_FOUND     The parameter name was not found.
114      * @retval OT_ERROR_INVALID_ARGS  The parameter value was not contain valid number (e.g., value out of range).
115      */
116     otError ParseUint32(const char *aName, uint32_t &aValue) const;
117 
118     /**
119      * Parses a `uint16_t` parameter value.
120      *
121      * The parameter value in string is parsed as decimal or hex format (if contains `0x` or `0X` prefix).
122      *
123      * @param[in]  aName    A pointer to the parameter name.
124      * @param[out] aValue   A reference to an `uint16_t` variable to output the parameter value.
125      *                      The original value of @p aValue won't change if failed to get the value.
126      *
127      * @retval OT_ERROR_NONE          The parameter value was parsed successfully.
128      * @retval OT_ERROR_NOT_FOUND     The parameter name was not found.
129      * @retval OT_ERROR_INVALID_ARGS  The parameter value was not contain valid number (e.g., value out of range).
130      */
131     otError ParseUint16(const char *aName, uint16_t &aValue) const;
132 
133     /**
134      * Parses a `uint8_t` parameter value.
135      *
136      * The parameter value in string is parsed as decimal or hex format (if contains `0x` or `0X` prefix).
137      *
138      * @param[in]  aName    A pointer to the parameter name.
139      * @param[out] aValue   A reference to an `uint16_t` variable to output the parameter value.
140      *                      The original value of @p aValue won't change if failed to get the value.
141      *
142      * @retval OT_ERROR_NONE          The parameter value was parsed successfully.
143      * @retval OT_ERROR_NOT_FOUND     The parameter name was not found.
144      * @retval OT_ERROR_INVALID_ARGS  The parameter value was not contain valid number (e.g., value out of range).
145      */
146     otError ParseUint8(const char *aName, uint8_t &aValue) const;
147 
148     /**
149      * Parses a `int32_t` parameter value.
150      *
151      * The parameter value in string is parsed as decimal or hex format (if contains `0x` or `0X` prefix). The string
152      * can start with `+`/`-` sign.
153      *
154      * @param[in]  aName    A pointer to the parameter name.
155      * @param[out] aValue   A reference to an `int32_t` variable to output the parameter value.
156      *                      The original value of @p aValue won't change if failed to get the value.
157      *
158      * @retval OT_ERROR_NONE          The parameter value was parsed successfully.
159      * @retval OT_ERROR_NOT_FOUND     The parameter name was not found.
160      * @retval OT_ERROR_INVALID_ARGS  The parameter value was not contain valid number (e.g., value out of range).
161      */
162     otError ParseInt32(const char *aName, int32_t &aValue) const;
163 
164     /**
165      * Parses a `int16_t` parameter value.
166      *
167      * The parameter value in string is parsed as decimal or hex format (if contains `0x` or `0X` prefix). The string
168      * can start with `+`/`-` sign.
169      *
170      * @param[in]  aName    A pointer to the parameter name.
171      * @param[out] aValue   A reference to an `int16_t` variable to output the parameter value.
172      *                      The original value of @p aValue won't change if failed to get the value.
173      *
174      * @retval OT_ERROR_NONE          The parameter value was parsed successfully.
175      * @retval OT_ERROR_NOT_FOUND     The parameter name was not found.
176      * @retval OT_ERROR_INVALID_ARGS  The parameter value was not contain valid number (e.g., value out of range).
177      */
178     otError ParseInt16(const char *aName, int16_t &aValue) const;
179 
180     /**
181      * Parses a `int8_t` parameter value.
182      *
183      * The parameter value in string is parsed as decimal or hex format (if contains `0x` or `0X` prefix). The string
184      * can start with `+`/`-` sign.
185      *
186      * @param[in]  aName    A pointer to the parameter name.
187      * @param[out] aValue   A reference to an `int8_t` variable to output the parameter value.
188      *                      The original value of @p aValue won't change if failed to get the value.
189      *
190      * @retval OT_ERROR_NONE          The parameter value was parsed successfully.
191      * @retval OT_ERROR_NOT_FOUND     The parameter name was not found.
192      * @retval OT_ERROR_INVALID_ARGS  The parameter value was not contain valid number (e.g., value out of range).
193      */
194     otError ParseInt8(const char *aName, int8_t &aValue) const;
195 };
196 
197 } // namespace Url
198 } // namespace ot
199 
200 #endif // OT_LIB_URL_URL_HPP_
201