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 */ 41 struct otUrl 42 { 43 const char *mProtocol; ///< The URL protocol. 44 const char *mPath; ///< The path. 45 const char *mQuery; ///< The start of the URL query string. 46 const char *mEnd; ///< The end of the URL buffer. 47 }; 48 49 namespace ot { 50 namespace Url { 51 52 /** 53 * Implements the URL processing. 54 * 55 */ 56 class Url : public otUrl 57 { 58 public: 59 Url(void); 60 61 /** 62 * Initializes the URL. 63 * 64 * @param[in] aUrl A buffer stores the null-terminated URL string. 65 * 66 * @retval OT_ERROR_NONE Successfully parsed the URL. 67 * @retval OT_ERROR_PARSE Failed to parse the string as a URL. 68 * 69 */ 70 otError Init(char *aUrl); 71 72 /** 73 * Gets the path in URL. 74 * 75 * @returns The path in URL. 76 * 77 */ GetPath(void) const78 const char *GetPath(void) const { return mPath; } 79 80 /** 81 * Gets the value of parameter @p aName. 82 * 83 * @param[in] aName The parameter name. 84 * @param[in] aLastValue The last iterated parameter value, nullptr for the first value. 85 * 86 * @returns The parameter value. 87 * 88 */ 89 const char *GetValue(const char *aName, const char *aLastValue = nullptr) const; 90 91 /** 92 * Returns the URL protocol. 93 * 94 * @returns The URL protocol. 95 * 96 */ GetProtocol(void) const97 const char *GetProtocol(void) const { return mProtocol; } 98 99 /** 100 * Indicates whether or not the url contains the parameter. 101 * 102 * @param[in] aName A pointer to the parameter name. 103 * 104 * @retval TRUE The url contains the parameter. 105 * @retval FALSE The url doesn't support the parameter. 106 * 107 */ HasParam(const char * aName) const108 bool HasParam(const char *aName) const { return (GetValue(aName) != nullptr); } 109 110 /** 111 * Parses a `uint32_t` parameter value. 112 * 113 * The parameter value in string is parsed as decimal or hex format (if contains `0x` or `0X` prefix). 114 * 115 * @param[in] aName A pointer to the parameter name. 116 * @param[out] aValue A reference to an `uint32_t` variable to output the parameter value. 117 * The original value of @p aValue won't change if failed to get the value. 118 * 119 * @retval OT_ERROR_NONE The parameter value was parsed successfully. 120 * @retval OT_ERROR_NOT_FOUND The parameter name was not found. 121 * @retval OT_ERROR_INVALID_ARGS The parameter value was not contain valid number (e.g., value out of range). 122 * 123 */ 124 otError ParseUint32(const char *aName, uint32_t &aValue) const; 125 126 /** 127 * Parses a `uint16_t` parameter value. 128 * 129 * The parameter value in string is parsed as decimal or hex format (if contains `0x` or `0X` prefix). 130 * 131 * @param[in] aName A pointer to the parameter name. 132 * @param[out] aValue A reference to an `uint16_t` variable to output the parameter value. 133 * The original value of @p aValue won't change if failed to get the value. 134 * 135 * @retval OT_ERROR_NONE The parameter value was parsed successfully. 136 * @retval OT_ERROR_NOT_FOUND The parameter name was not found. 137 * @retval OT_ERROR_INVALID_ARGS The parameter value was not contain valid number (e.g., value out of range). 138 * 139 */ 140 otError ParseUint16(const char *aName, uint16_t &aValue) const; 141 142 /** 143 * Parses a `uint8_t` parameter value. 144 * 145 * The parameter value in string is parsed as decimal or hex format (if contains `0x` or `0X` prefix). 146 * 147 * @param[in] aName A pointer to the parameter name. 148 * @param[out] aValue A reference to an `uint16_t` variable to output the parameter value. 149 * The original value of @p aValue won't change if failed to get the value. 150 * 151 * @retval OT_ERROR_NONE The parameter value was parsed successfully. 152 * @retval OT_ERROR_NOT_FOUND The parameter name was not found. 153 * @retval OT_ERROR_INVALID_ARGS The parameter value was not contain valid number (e.g., value out of range). 154 * 155 */ 156 otError ParseUint8(const char *aName, uint8_t &aValue) const; 157 158 /** 159 * Parses a `int32_t` parameter value. 160 * 161 * The parameter value in string is parsed as decimal or hex format (if contains `0x` or `0X` prefix). The string 162 * can start with `+`/`-` sign. 163 * 164 * @param[in] aName A pointer to the parameter name. 165 * @param[out] aValue A reference to an `int32_t` variable to output the parameter value. 166 * The original value of @p aValue won't change if failed to get the value. 167 * 168 * @retval OT_ERROR_NONE The parameter value was parsed successfully. 169 * @retval OT_ERROR_NOT_FOUND The parameter name was not found. 170 * @retval OT_ERROR_INVALID_ARGS The parameter value was not contain valid number (e.g., value out of range). 171 * 172 */ 173 otError ParseInt32(const char *aName, int32_t &aValue) const; 174 175 /** 176 * Parses a `int16_t` parameter value. 177 * 178 * The parameter value in string is parsed as decimal or hex format (if contains `0x` or `0X` prefix). The string 179 * can start with `+`/`-` sign. 180 * 181 * @param[in] aName A pointer to the parameter name. 182 * @param[out] aValue A reference to an `int16_t` variable to output the parameter value. 183 * The original value of @p aValue won't change if failed to get the value. 184 * 185 * @retval OT_ERROR_NONE The parameter value was parsed successfully. 186 * @retval OT_ERROR_NOT_FOUND The parameter name was not found. 187 * @retval OT_ERROR_INVALID_ARGS The parameter value was not contain valid number (e.g., value out of range). 188 * 189 */ 190 otError ParseInt16(const char *aName, int16_t &aValue) const; 191 192 /** 193 * Parses a `int8_t` parameter value. 194 * 195 * The parameter value in string is parsed as decimal or hex format (if contains `0x` or `0X` prefix). The string 196 * can start with `+`/`-` sign. 197 * 198 * @param[in] aName A pointer to the parameter name. 199 * @param[out] aValue A reference to an `int8_t` variable to output the parameter value. 200 * The original value of @p aValue won't change if failed to get the value. 201 * 202 * @retval OT_ERROR_NONE The parameter value was parsed successfully. 203 * @retval OT_ERROR_NOT_FOUND The parameter name was not found. 204 * @retval OT_ERROR_INVALID_ARGS The parameter value was not contain valid number (e.g., value out of range). 205 * 206 */ 207 otError ParseInt8(const char *aName, int8_t &aValue) const; 208 }; 209 210 } // namespace Url 211 } // namespace ot 212 213 #endif // OT_LIB_URL_URL_HPP_ 214