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