Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 3 | #include <cstdio> |
| 4 | #include <cstring> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 5 | #include <iostream> |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 9 | namespace crow |
| 10 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 11 | // ---------------------------------------------------------------------------- |
| 12 | // qs_parse (modified) |
| 13 | // https://github.com/bartgrantham/qs_parse |
| 14 | // ---------------------------------------------------------------------------- |
| 15 | /* Similar to strncmp, but handles URL-encoding for either string */ |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 16 | int qsStrncmp(const char* s, const char* qs, size_t n); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 17 | |
| 18 | /* Finds the beginning of each key/value pair and stores a pointer in qs_kv. |
| 19 | * Also decodes the value portion of the k/v pair *in-place*. In a future |
| 20 | * enhancement it will also have a compile-time option of sorting qs_kv |
| 21 | * alphabetically by key. */ |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 22 | size_t qsParse(char* qs, char* qs_kv[], size_t qs_kv_size); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 23 | |
| 24 | /* Used by qs_parse to decode the value portion of a k/v pair */ |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 25 | int qsDecode(char* qs); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 26 | |
| 27 | /* Looks up the value according to the key on a pre-processed query string |
| 28 | * A future enhancement will be a compile-time option to look up the key |
| 29 | * in a pre-sorted qs_kv array via a binary search. */ |
| 30 | // char * qs_k2v(const char * key, char * qs_kv[], int qs_kv_size); |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 31 | char* qsK2v(const char* key, char* const* qs_kv, int qs_kv_size, int nth); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 32 | |
| 33 | /* Non-destructive lookup of value, based on key. User provides the |
| 34 | * destinaton string and length. */ |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 35 | char* qsScanvalue(const char* key, const char* qs, char* val, size_t val_len); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 36 | |
| 37 | // TODO: implement sorting of the qs_kv array; for now ensure it's not compiled |
| 38 | #undef _qsSORTING |
| 39 | |
| 40 | // isxdigit _is_ available in <ctype.h>, but let's avoid another header instead |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 41 | #define BMCWEB_QS_ISHEX(x) \ |
| 42 | ((((x) >= '0' && (x) <= '9') || ((x) >= 'A' && (x) <= 'F') || \ |
| 43 | ((x) >= 'a' && (x) <= 'f')) \ |
| 44 | ? 1 \ |
| 45 | : 0) |
| 46 | #define BMCWEB_QS_HEX2DEC(x) \ |
| 47 | (((x) >= '0' && (x) <= '9') \ |
| 48 | ? (x)-48 \ |
| 49 | : ((x) >= 'A' && (x) <= 'F') \ |
| 50 | ? (x)-55 \ |
| 51 | : ((x) >= 'a' && (x) <= 'f') ? (x)-87 : 0) |
| 52 | #define BMCWEB_QS_ISQSCHR(x) \ |
| 53 | ((((x) == '=') || ((x) == '#') || ((x) == '&') || ((x) == '\0')) ? 0 : 1) |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 54 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | inline int qsStrncmp(const char* s, const char* qs, size_t n) |
| 56 | { |
| 57 | int i = 0; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 58 | char u1, u2; |
| 59 | char unyb, lnyb; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 60 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 61 | while (n-- > 0) |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 62 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 63 | u1 = *s++; |
| 64 | u2 = *qs++; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 65 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | if (!BMCWEB_QS_ISQSCHR(u1)) |
| 67 | { |
| 68 | u1 = '\0'; |
| 69 | } |
| 70 | if (!BMCWEB_QS_ISQSCHR(u2)) |
| 71 | { |
| 72 | u2 = '\0'; |
| 73 | } |
| 74 | |
| 75 | if (u1 == '+') |
| 76 | { |
| 77 | u1 = ' '; |
| 78 | } |
| 79 | if (u1 == '%') // easier/safer than scanf |
| 80 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 81 | unyb = static_cast<char>(*s++); |
| 82 | lnyb = static_cast<char>(*s++); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | if (BMCWEB_QS_ISHEX(unyb) && BMCWEB_QS_ISHEX(lnyb)) |
| 84 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 85 | u1 = static_cast<char>((BMCWEB_QS_HEX2DEC(unyb) * 16) + |
| 86 | BMCWEB_QS_HEX2DEC(lnyb)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 87 | } |
| 88 | else |
| 89 | { |
| 90 | u1 = '\0'; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (u2 == '+') |
| 95 | { |
| 96 | u2 = ' '; |
| 97 | } |
| 98 | if (u2 == '%') // easier/safer than scanf |
| 99 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 100 | unyb = static_cast<char>(*qs++); |
| 101 | lnyb = static_cast<char>(*qs++); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 102 | if (BMCWEB_QS_ISHEX(unyb) && BMCWEB_QS_ISHEX(lnyb)) |
| 103 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 104 | u2 = static_cast<char>((BMCWEB_QS_HEX2DEC(unyb) * 16) + |
| 105 | BMCWEB_QS_HEX2DEC(lnyb)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 106 | } |
| 107 | else |
| 108 | { |
| 109 | u2 = '\0'; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (u1 != u2) |
| 114 | { |
| 115 | return u1 - u2; |
| 116 | } |
| 117 | if (u1 == '\0') |
| 118 | { |
| 119 | return 0; |
| 120 | } |
| 121 | i++; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 122 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 123 | if (BMCWEB_QS_ISQSCHR(*qs)) |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 124 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 125 | return -1; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 126 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 127 | else |
| 128 | { |
| 129 | return 0; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 130 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 133 | inline size_t qsParse(char* qs, char* qs_kv[], size_t qs_kv_size) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 134 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 135 | size_t i; |
| 136 | size_t j; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 137 | char* substrPtr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 138 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 139 | for (i = 0; i < qs_kv_size; i++) |
| 140 | { |
| 141 | qs_kv[i] = NULL; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 142 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 143 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 144 | // find the beginning of the k/v substrings or the fragment |
| 145 | substrPtr = qs + strcspn(qs, "?#"); |
| 146 | if (substrPtr[0] != '\0') |
| 147 | { |
| 148 | substrPtr++; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 149 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 150 | else |
| 151 | { |
| 152 | return 0; // no query or fragment |
| 153 | } |
| 154 | |
| 155 | i = 0; |
| 156 | while (i < qs_kv_size) |
| 157 | { |
| 158 | qs_kv[i] = substrPtr; |
| 159 | j = strcspn(substrPtr, "&"); |
| 160 | if (substrPtr[j] == '\0') |
| 161 | { |
| 162 | break; |
| 163 | } |
| 164 | substrPtr += j + 1; |
| 165 | i++; |
| 166 | } |
| 167 | i++; // x &'s -> means x iterations of this loop -> means *x+1* k/v pairs |
| 168 | |
| 169 | // we only decode the values in place, the keys could have '='s in them |
| 170 | // which will hose our ability to distinguish keys from values later |
| 171 | for (j = 0; j < i; j++) |
| 172 | { |
| 173 | substrPtr = qs_kv[j] + strcspn(qs_kv[j], "=&#"); |
| 174 | if (substrPtr[0] == '&' || substrPtr[0] == '\0') |
| 175 | { // blank value: skip decoding |
| 176 | substrPtr[0] = '\0'; |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | qsDecode(++substrPtr); |
| 181 | } |
| 182 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 183 | |
| 184 | #ifdef _qsSORTING |
| 185 | // TODO: qsort qs_kv, using qs_strncmp() for the comparison |
| 186 | #endif |
| 187 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 188 | return i; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 191 | inline int qsDecode(char* qs) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 192 | { |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 193 | int i = 0, j = 0; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 194 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 195 | while (BMCWEB_QS_ISQSCHR(qs[j])) |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 196 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 197 | if (qs[j] == '+') |
| 198 | { |
| 199 | qs[i] = ' '; |
| 200 | } |
| 201 | else if (qs[j] == '%') // easier/safer than scanf |
| 202 | { |
| 203 | if (!BMCWEB_QS_ISHEX(qs[j + 1]) || !BMCWEB_QS_ISHEX(qs[j + 2])) |
| 204 | { |
| 205 | qs[i] = '\0'; |
| 206 | return i; |
| 207 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 208 | qs[i] = static_cast<char>(BMCWEB_QS_HEX2DEC(qs[j + 1] * 16) + |
| 209 | BMCWEB_QS_HEX2DEC(qs[j + 2])); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 210 | j += 2; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | qs[i] = qs[j]; |
| 215 | } |
| 216 | i++; |
| 217 | j++; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 218 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 219 | qs[i] = '\0'; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 220 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 221 | return i; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 224 | inline char* qsK2v(const char* key, char* const* qs_kv, int qs_kv_size, |
| 225 | int nth = 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 226 | { |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 227 | int i; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 228 | size_t keyLen, skip; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 229 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 230 | keyLen = strlen(key); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 231 | |
| 232 | #ifdef _qsSORTING |
| 233 | // TODO: binary search for key in the sorted qs_kv |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 234 | #else // _qsSORTING |
| 235 | for (i = 0; i < qs_kv_size; i++) |
| 236 | { |
| 237 | // we rely on the unambiguous '=' to find the value in our k/v pair |
| 238 | if (qsStrncmp(key, qs_kv[i], keyLen) == 0) |
| 239 | { |
| 240 | skip = strcspn(qs_kv[i], "="); |
| 241 | if (qs_kv[i][skip] == '=') |
| 242 | { |
| 243 | skip++; |
| 244 | } |
| 245 | // return (zero-char value) ? ptr to trailing '\0' : ptr to value |
| 246 | if (nth == 0) |
| 247 | { |
| 248 | return qs_kv[i] + skip; |
| 249 | } |
| 250 | else |
| 251 | { |
| 252 | --nth; |
| 253 | } |
| 254 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 255 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 256 | #endif // _qsSORTING |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 257 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 258 | return NULL; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 261 | inline char* qsScanvalue(const char* key, const char* qs, char* val, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 262 | size_t val_len) |
| 263 | { |
| 264 | size_t i, keyLen; |
| 265 | const char* tmp; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 266 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 267 | // find the beginning of the k/v substrings |
| 268 | if ((tmp = strchr(qs, '?')) != NULL) |
| 269 | { |
| 270 | qs = tmp + 1; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 271 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 272 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 273 | keyLen = strlen(key); |
| 274 | while (qs[0] != '#' && qs[0] != '\0') |
| 275 | { |
| 276 | if (qsStrncmp(key, qs, keyLen) == 0) |
| 277 | { |
| 278 | break; |
| 279 | } |
| 280 | qs += strcspn(qs, "&") + 1; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 281 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 282 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 283 | if (qs[0] == '\0') |
| 284 | { |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | qs += strcspn(qs, "=&#"); |
| 289 | if (qs[0] == '=') |
| 290 | { |
| 291 | qs++; |
| 292 | i = strcspn(qs, "&=#"); |
| 293 | strncpy(val, qs, (val_len - 1) < (i + 1) ? (val_len - 1) : (i + 1)); |
| 294 | qsDecode(val); |
| 295 | } |
| 296 | else |
| 297 | { |
| 298 | if (val_len > 0) |
| 299 | { |
| 300 | val[0] = '\0'; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return val; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 305 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 306 | } // namespace crow |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 307 | // ---------------------------------------------------------------------------- |
| 308 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 309 | namespace crow |
| 310 | { |
| 311 | class QueryString |
| 312 | { |
| 313 | public: |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 314 | static const size_t maxKeyValuePairsCount = 256; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 315 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 316 | QueryString() = default; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 317 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 318 | QueryString(const QueryString& qs) : url(qs.url) |
| 319 | { |
| 320 | for (auto p : qs.keyValuePairs) |
| 321 | { |
| 322 | keyValuePairs.push_back( |
| 323 | const_cast<char*>(p - qs.url.c_str() + url.c_str())); |
| 324 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 325 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 326 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 327 | QueryString& operator=(const QueryString& qs) |
| 328 | { |
| 329 | url = qs.url; |
| 330 | keyValuePairs.clear(); |
| 331 | for (auto p : qs.keyValuePairs) |
| 332 | { |
| 333 | keyValuePairs.push_back( |
| 334 | const_cast<char*>(p - qs.url.c_str() + url.c_str())); |
| 335 | } |
| 336 | return *this; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 337 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 338 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 339 | QueryString& operator=(QueryString&& qs) |
| 340 | { |
| 341 | keyValuePairs = std::move(qs.keyValuePairs); |
| 342 | auto* oldData = const_cast<char*>(qs.url.c_str()); |
| 343 | url = std::move(qs.url); |
| 344 | for (auto& p : keyValuePairs) |
| 345 | { |
| 346 | p += const_cast<char*>(url.c_str()) - oldData; |
| 347 | } |
| 348 | return *this; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 349 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 350 | |
Jason M. Bills | 43fcbe5 | 2018-10-16 15:19:20 -0700 | [diff] [blame] | 351 | explicit QueryString(std::string newUrl) : url(std::move(newUrl)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 352 | { |
| 353 | if (url.empty()) |
| 354 | { |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | keyValuePairs.resize(maxKeyValuePairsCount); |
| 359 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 360 | size_t count = |
| 361 | qsParse(&url[0], &keyValuePairs[0], maxKeyValuePairsCount); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 362 | keyValuePairs.resize(count); |
| 363 | } |
| 364 | |
| 365 | void clear() |
| 366 | { |
| 367 | keyValuePairs.clear(); |
| 368 | url.clear(); |
| 369 | } |
| 370 | |
| 371 | friend std::ostream& operator<<(std::ostream& os, const QueryString& qs) |
| 372 | { |
| 373 | os << "[ "; |
| 374 | for (size_t i = 0; i < qs.keyValuePairs.size(); ++i) |
| 375 | { |
| 376 | if (i != 0u) |
| 377 | { |
| 378 | os << ", "; |
| 379 | } |
| 380 | os << qs.keyValuePairs[i]; |
| 381 | } |
| 382 | os << " ]"; |
| 383 | return os; |
| 384 | } |
| 385 | |
| 386 | char* get(const std::string& name) const |
| 387 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 388 | char* ret = qsK2v(name.c_str(), keyValuePairs.data(), |
| 389 | static_cast<int>(keyValuePairs.size())); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | std::vector<char*> getList(const std::string& name) const |
| 394 | { |
| 395 | std::vector<char*> ret; |
| 396 | std::string plus = name + "[]"; |
| 397 | char* element = nullptr; |
| 398 | |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 399 | int count = 0; |
Ed Tanous | f12a13b | 2019-10-24 11:29:41 -0700 | [diff] [blame^] | 400 | while (true) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 401 | { |
| 402 | element = qsK2v(plus.c_str(), keyValuePairs.data(), |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 403 | static_cast<int>(keyValuePairs.size()), count++); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 404 | if (element == nullptr) |
| 405 | { |
| 406 | break; |
| 407 | } |
| 408 | ret.push_back(element); |
| 409 | } |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | private: |
| 414 | std::string url; |
| 415 | std::vector<char*> keyValuePairs; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 416 | }; |
| 417 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 418 | } // namespace crow |