/src/uWebSockets/src/QueryParser.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Authored by Alex Hultman, 2018-2020. |
3 | | * Intellectual property of third-party. |
4 | | |
5 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | * you may not use this file except in compliance with the License. |
7 | | * You may obtain a copy of the License at |
8 | | |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | /* This module implements URI query parsing and retrieval of value given key */ |
19 | | |
20 | | #ifndef UWS_QUERYPARSER_H |
21 | | #define UWS_QUERYPARSER_H |
22 | | |
23 | | #include <string_view> |
24 | | |
25 | | namespace uWS { |
26 | | |
27 | | /* Takes raw query including initial '?' sign. Will inplace decode, so input will mutate */ |
28 | 0 | static inline std::string_view getDecodedQueryValue(std::string_view key, std::string_view rawQuery) { |
29 | 0 |
|
30 | 0 | /* Can't have a value without a key */ |
31 | 0 | if (!key.length()) { |
32 | 0 | return {}; |
33 | 0 | } |
34 | 0 |
|
35 | 0 | /* Start with the whole querystring including initial '?' */ |
36 | 0 | std::string_view queryString = rawQuery; |
37 | 0 |
|
38 | 0 | /* List of key, value could be cached for repeated fetches similar to how headers are, todo! */ |
39 | 0 | while (queryString.length()) { |
40 | 0 | /* Find boundaries of this statement */ |
41 | 0 | std::string_view statement = queryString.substr(1, queryString.find('&', 1) - 1); |
42 | 0 |
|
43 | 0 | /* Only bother if first char of key match (early exit) */ |
44 | 0 | if (statement.length() && statement[0] == key[0]) { |
45 | 0 | /* Equal sign must be present and not in the end of statement */ |
46 | 0 | auto equality = statement.find('='); |
47 | 0 | if (equality != std::string_view::npos) { |
48 | 0 |
|
49 | 0 | std::string_view statementKey = statement.substr(0, equality); |
50 | 0 | std::string_view statementValue = statement.substr(equality + 1); |
51 | 0 |
|
52 | 0 | /* String comparison */ |
53 | 0 | if (key == statementKey) { |
54 | 0 |
|
55 | 0 | /* Decode value inplace, put null at end if before length of original */ |
56 | 0 | char *in = (char *) statementValue.data(); |
57 | 0 |
|
58 | 0 | /* Write offset */ |
59 | 0 | unsigned int out = 0; |
60 | 0 |
|
61 | 0 | /* Walk over all chars until end or null char, decoding in place */ |
62 | 0 | for (unsigned int i = 0; i < statementValue.length() && in[i]; i++) { |
63 | 0 | /* Only bother with '%' */ |
64 | 0 | if (in[i] == '%') { |
65 | 0 | /* Do we have enough data for two bytes hex? */ |
66 | 0 | if (i + 2 >= statementValue.length()) { |
67 | 0 | return {}; |
68 | 0 | } |
69 | 0 |
|
70 | 0 | /* Two bytes hex */ |
71 | 0 | int hex1 = in[i + 1] - '0'; |
72 | 0 | if (hex1 > 9) { |
73 | 0 | hex1 &= 223; |
74 | 0 | hex1 -= 7; |
75 | 0 | } |
76 | 0 |
|
77 | 0 | int hex2 = in[i + 2] - '0'; |
78 | 0 | if (hex2 > 9) { |
79 | 0 | hex2 &= 223; |
80 | 0 | hex2 -= 7; |
81 | 0 | } |
82 | 0 |
|
83 | 0 | *((unsigned char *) &in[out]) = (unsigned char) (hex1 * 16 + hex2); |
84 | 0 | i += 2; |
85 | 0 | } else { |
86 | 0 | /* Is this even a rule? */ |
87 | 0 | if (in[i] == '+') { |
88 | 0 | in[out] = ' '; |
89 | 0 | } else { |
90 | 0 | in[out] = in[i]; |
91 | 0 | } |
92 | 0 | } |
93 | 0 |
|
94 | 0 | /* We always only write one char */ |
95 | 0 | out++; |
96 | 0 | } |
97 | 0 |
|
98 | 0 | /* If decoded string is shorter than original, put null char to stop next read */ |
99 | 0 | if (out < statementValue.length()) { |
100 | 0 | in[out] = 0; |
101 | 0 | } |
102 | 0 |
|
103 | 0 | return statementValue.substr(0, out); |
104 | 0 | } |
105 | 0 | } else { |
106 | 0 | /* This querystring is invalid, cannot parse it */ |
107 | 0 | return {nullptr, 0}; |
108 | 0 | } |
109 | 0 | } |
110 | 0 |
|
111 | 0 | queryString.remove_prefix(statement.length() + 1); |
112 | 0 | } |
113 | 0 |
|
114 | 0 | /* Nothing found is given as nullptr, while empty string is given as some pointer to the given buffer */ |
115 | 0 | return {nullptr, 0}; |
116 | 0 | } |
117 | | |
118 | | } |
119 | | |
120 | | #endif |