summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/interfaces/bmcweb/0004-Remove-QueryString.patch
blob: 238fb83c7daef07d4bec6da8188bbb0bc5112a78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
From f8749b5898403ee04a623a5bc534bd939865e221 Mon Sep 17 00:00:00 2001
From: James Feist <james.feist@linux.intel.com>
Date: Wed, 22 Jul 2020 09:08:38 -0700
Subject: [PATCH 1/1] Remove QueryString

QueryString is an error-prone library that was
leftover from crow. Replace it with boost::url,
a header only library based and written by the
one of the authors of boost beast.

Tested: Verified logging paging still worked
as expected

Change-Id: I47c225089aa7d0f7d2299142f91806294f879381
Signed-off-by: James Feist <james.feist@linux.intel.com>
---
 CMakeLists.txt                     |   2 +
 CMakeLists.txt.in                  |  10 +
 http/http_connection.h             |  21 +-
 http/http_request.h                |   5 +-
 http/query_string.h                | 421 -----------------------------
 redfish-core/lib/event_service.hpp |   7 +-
 redfish-core/lib/log_services.hpp  |  20 +-
 7 files changed, 45 insertions(+), 441 deletions(-)
 delete mode 100644 http/query_string.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2886438..50483ad 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -280,6 +280,8 @@ add_definitions (-DBOOST_ALL_NO_LIB)
 add_definitions (-DBOOST_NO_RTTI)
 add_definitions (-DBOOST_NO_TYPEID)
 add_definitions (-DBOOST_COROUTINES_NO_DEPRECATION_WARNING)
+add_definitions (-DBOOST_URL_STANDALONE)
+add_definitions (-DBOOST_URL_HEADER_ONLY)
 
 # sdbusplus
 if (NOT ${YOCTO_DEPENDENCIES})
diff --git a/CMakeLists.txt.in b/CMakeLists.txt.in
index d14910f..5cd73f6 100644
--- a/CMakeLists.txt.in
+++ b/CMakeLists.txt.in
@@ -53,3 +53,13 @@ externalproject_add (
     cp -r "${CMAKE_BINARY_DIR}/nlohmann-json-src/include/nlohmann"
     "${CMAKE_BINARY_DIR}/prefix/include"
 )
+
+externalproject_add (
+    Boost-URL GIT_REPOSITORY "https://github.com/CPPAlliance/url.git" GIT_TAG
+    a56ae0df6d3078319755fbaa67822b4fa7fd352b SOURCE_DIR
+    "${CMAKE_BINARY_DIR}/boost-url-src" BINARY_DIR
+    "${CMAKE_BINARY_DIR}/boost-url-build" CONFIGURE_COMMAND "" BUILD_COMMAND
+    "" INSTALL_COMMAND mkdir -p "${CMAKE_BINARY_DIR}/prefix/include" &&
+    cp -r "${CMAKE_BINARY_DIR}/boost-url-src/include/boost"
+    "${CMAKE_BINARY_DIR}/prefix/include"
+)
diff --git a/http/http_connection.h b/http/http_connection.h
index 35bf99c..8dba3d6 100644
--- a/http/http_connection.h
+++ b/http/http_connection.h
@@ -728,13 +728,9 @@ class Connection :
                     return;
                 }
 
-                // Compute the url parameters for the request
-                req->url = req->target();
-                std::size_t index = req->url.find("?");
-                if (index != std::string_view::npos)
-                {
-                    req->url = req->url.substr(0, index);
-                }
+                req->urlView = boost::urls::url_view(req->target());
+                req->url = req->urlView.encoded_path();
+
                 crow::authorization::authenticate(*req, res, session);
 
                 bool loggedIn = req && req->session;
@@ -743,7 +739,16 @@ class Connection :
                     startDeadline(loggedInAttempts);
                     BMCWEB_LOG_DEBUG << "Starting slow deadline";
 
-                    req->urlParams = QueryString(std::string(req->target()));
+                    req->urlParams = req->urlView.params();
+
+#ifdef BMCWEB_ENABLE_DEBUG
+                    std::string paramList = "";
+                    for (const auto param : req->urlParams)
+                    {
+                        paramList += param->key() + " " + param->value() + " ";
+                    }
+                    BMCWEB_LOG_DEBUG << "QueryParams: " << paramList;
+#endif
                 }
                 else
                 {
diff --git a/http/http_request.h b/http/http_request.h
index 0691465..95f88c7 100644
--- a/http/http_request.h
+++ b/http/http_request.h
@@ -1,7 +1,6 @@
 #pragma once
 
 #include "common.h"
-#include "query_string.h"
 
 #include "sessions.hpp"
 
@@ -9,6 +8,7 @@
 #include <boost/beast/http.hpp>
 #include <boost/beast/ssl/ssl_stream.hpp>
 #include <boost/beast/websocket.hpp>
+#include <boost/url/url_view.hpp>
 
 namespace crow
 {
@@ -24,7 +24,8 @@ struct Request
     boost::beast::http::request<boost::beast::http::string_body>& req;
     boost::beast::http::fields& fields;
     std::string_view url{};
-    QueryString urlParams{};
+    boost::urls::url_view urlView{};
+    boost::urls::url_view::params_type urlParams{};
     bool isSecure{false};
 
     const std::string& body;
diff --git a/http/query_string.h b/http/query_string.h
deleted file mode 100644
index e980280..0000000
--- a/http/query_string.h
+++ /dev/null
@@ -1,421 +0,0 @@
-#pragma once
-
-#include <cstdio>
-#include <cstring>
-#include <iostream>
-#include <string>
-#include <vector>
-
-namespace crow
-{
-// ----------------------------------------------------------------------------
-// qs_parse (modified)
-// https://github.com/bartgrantham/qs_parse
-// ----------------------------------------------------------------------------
-/*  Similar to strncmp, but handles URL-encoding for either string  */
-int qsStrncmp(const char* s, const char* qs, size_t n);
-
-/*  Finds the beginning of each key/value pair and stores a pointer in qs_kv.
- *  Also decodes the value portion of the k/v pair *in-place*.  In a future
- *  enhancement it will also have a compile-time option of sorting qs_kv
- *  alphabetically by key.  */
-size_t qsParse(char* qs, char* qs_kv[], size_t qs_kv_size);
-
-/*  Used by qs_parse to decode the value portion of a k/v pair  */
-int qsDecode(char* qs);
-
-/*  Looks up the value according to the key on a pre-processed query string
- *  A future enhancement will be a compile-time option to look up the key
- *  in a pre-sorted qs_kv array via a binary search.  */
-// char * qs_k2v(const char * key, char * qs_kv[], int qs_kv_size);
-char* qsK2v(const char* key, char* const* qs_kv, int qs_kv_size, int nth);
-
-/*  Non-destructive lookup of value, based on key.  User provides the
- *  destinaton string and length.  */
-char* qsScanvalue(const char* key, const char* qs, char* val, size_t val_len);
-
-// TODO: implement sorting of the qs_kv array; for now ensure it's not compiled
-#undef _qsSORTING
-
-// isxdigit _is_ available in <ctype.h>, but let's avoid another header instead
-#define BMCWEB_QS_ISHEX(x)                                                     \
-    ((((x) >= '0' && (x) <= '9') || ((x) >= 'A' && (x) <= 'F') ||              \
-      ((x) >= 'a' && (x) <= 'f'))                                              \
-         ? 1                                                                   \
-         : 0)
-#define BMCWEB_QS_HEX2DEC(x)                                                   \
-    (((x) >= '0' && (x) <= '9')                                                \
-         ? (x)-48                                                              \
-         : ((x) >= 'A' && (x) <= 'F')                                          \
-               ? (x)-55                                                        \
-               : ((x) >= 'a' && (x) <= 'f') ? (x)-87 : 0)
-#define BMCWEB_QS_ISQSCHR(x)                                                   \
-    ((((x) == '=') || ((x) == '#') || ((x) == '&') || ((x) == '\0')) ? 0 : 1)
-
-inline int qsStrncmp(const char* s, const char* qs, size_t n)
-{
-    int i = 0;
-    char u1, u2;
-    char unyb, lnyb;
-
-    while (n-- > 0)
-    {
-        u1 = *s++;
-        u2 = *qs++;
-
-        if (!BMCWEB_QS_ISQSCHR(u1))
-        {
-            u1 = '\0';
-        }
-        if (!BMCWEB_QS_ISQSCHR(u2))
-        {
-            u2 = '\0';
-        }
-
-        if (u1 == '+')
-        {
-            u1 = ' ';
-        }
-        if (u1 == '%') // easier/safer than scanf
-        {
-            unyb = static_cast<char>(*s++);
-            lnyb = static_cast<char>(*s++);
-            if (BMCWEB_QS_ISHEX(unyb) && BMCWEB_QS_ISHEX(lnyb))
-            {
-                u1 = static_cast<char>((BMCWEB_QS_HEX2DEC(unyb) * 16) +
-                                       BMCWEB_QS_HEX2DEC(lnyb));
-            }
-            else
-            {
-                u1 = '\0';
-            }
-        }
-
-        if (u2 == '+')
-        {
-            u2 = ' ';
-        }
-        if (u2 == '%') // easier/safer than scanf
-        {
-            unyb = static_cast<char>(*qs++);
-            lnyb = static_cast<char>(*qs++);
-            if (BMCWEB_QS_ISHEX(unyb) && BMCWEB_QS_ISHEX(lnyb))
-            {
-                u2 = static_cast<char>((BMCWEB_QS_HEX2DEC(unyb) * 16) +
-                                       BMCWEB_QS_HEX2DEC(lnyb));
-            }
-            else
-            {
-                u2 = '\0';
-            }
-        }
-
-        if (u1 != u2)
-        {
-            return u1 - u2;
-        }
-        if (u1 == '\0')
-        {
-            return 0;
-        }
-        i++;
-    }
-    if (BMCWEB_QS_ISQSCHR(*qs))
-    {
-        return -1;
-    }
-    else
-    {
-        return 0;
-    }
-}
-
-inline size_t qsParse(char* qs, char* qs_kv[], size_t qs_kv_size)
-{
-    size_t i;
-    size_t j;
-    char* substrPtr;
-
-    for (i = 0; i < qs_kv_size; i++)
-    {
-        qs_kv[i] = nullptr;
-    }
-
-    // find the beginning of the k/v substrings or the fragment
-    substrPtr = qs + strcspn(qs, "?#");
-    if (substrPtr[0] != '\0')
-    {
-        substrPtr++;
-    }
-    else
-    {
-        return 0; // no query or fragment
-    }
-
-    i = 0;
-    while (i < qs_kv_size)
-    {
-        qs_kv[i++] = substrPtr;
-        j = strcspn(substrPtr, "&");
-        if (substrPtr[j] == '\0')
-        {
-            break;
-        }
-        substrPtr += j + 1;
-    }
-
-    // we only decode the values in place, the keys could have '='s in them
-    // which will hose our ability to distinguish keys from values later
-    for (j = 0; j < i; j++)
-    {
-        substrPtr = qs_kv[j] + strcspn(qs_kv[j], "=&#");
-        if (substrPtr[0] == '&' || substrPtr[0] == '\0')
-        { // blank value: skip decoding
-            substrPtr[0] = '\0';
-        }
-        else
-        {
-            qsDecode(++substrPtr);
-        }
-    }
-
-#ifdef _qsSORTING
-// TODO: qsort qs_kv, using qs_strncmp() for the comparison
-#endif
-
-    return i;
-}
-
-inline int qsDecode(char* qs)
-{
-    int i = 0, j = 0;
-
-    while (BMCWEB_QS_ISQSCHR(qs[j]))
-    {
-        if (qs[j] == '+')
-        {
-            qs[i] = ' ';
-        }
-        else if (qs[j] == '%') // easier/safer than scanf
-        {
-            if (!BMCWEB_QS_ISHEX(qs[j + 1]) || !BMCWEB_QS_ISHEX(qs[j + 2]))
-            {
-                qs[i] = '\0';
-                return i;
-            }
-            qs[i] = static_cast<char>((BMCWEB_QS_HEX2DEC(qs[j + 1]) * 16) +
-                                      BMCWEB_QS_HEX2DEC(qs[j + 2]));
-            j += 2;
-        }
-        else
-        {
-            qs[i] = qs[j];
-        }
-        i++;
-        j++;
-    }
-    qs[i] = '\0';
-
-    return i;
-}
-
-inline char* qsK2v(const char* key, char* const* qs_kv, int qs_kv_size,
-                   int nth = 0)
-{
-    int i;
-    size_t keyLen, skip;
-
-    keyLen = strlen(key);
-
-#ifdef _qsSORTING
-// TODO: binary search for key in the sorted qs_kv
-#else  // _qsSORTING
-    for (i = 0; i < qs_kv_size; i++)
-    {
-        // we rely on the unambiguous '=' to find the value in our k/v pair
-        if (qsStrncmp(key, qs_kv[i], keyLen) == 0)
-        {
-            skip = strcspn(qs_kv[i], "=");
-            if (qs_kv[i][skip] == '=')
-            {
-                skip++;
-            }
-            // return (zero-char value) ? ptr to trailing '\0' : ptr to value
-            if (nth == 0)
-            {
-                return qs_kv[i] + skip;
-            }
-            else
-            {
-                --nth;
-            }
-        }
-    }
-#endif // _qsSORTING
-
-    return nullptr;
-}
-
-inline char* qsScanvalue(const char* key, const char* qs, char* val,
-                         size_t val_len)
-{
-    size_t i, keyLen;
-    const char* tmp;
-
-    // find the beginning of the k/v substrings
-    if ((tmp = strchr(qs, '?')) != nullptr)
-    {
-        qs = tmp + 1;
-    }
-
-    keyLen = strlen(key);
-    while (qs[0] != '#' && qs[0] != '\0')
-    {
-        if (qsStrncmp(key, qs, keyLen) == 0)
-        {
-            break;
-        }
-        qs += strcspn(qs, "&") + 1;
-    }
-
-    if (qs[0] == '\0')
-    {
-        return nullptr;
-    }
-
-    qs += strcspn(qs, "=&#");
-    if (qs[0] == '=')
-    {
-        qs++;
-        i = strcspn(qs, "&=#");
-        strncpy(val, qs, (val_len - 1) < (i + 1) ? (val_len - 1) : (i + 1));
-        qsDecode(val);
-    }
-    else
-    {
-        if (val_len > 0)
-        {
-            val[0] = '\0';
-        }
-    }
-
-    return val;
-}
-} // namespace crow
-// ----------------------------------------------------------------------------
-
-namespace crow
-{
-class QueryString
-{
-  public:
-    static const size_t maxKeyValuePairsCount = 256;
-
-    QueryString() = default;
-
-    QueryString(const QueryString& qs) : url(qs.url)
-    {
-        for (auto p : qs.keyValuePairs)
-        {
-            keyValuePairs.push_back(
-                const_cast<char*>(p - qs.url.c_str() + url.c_str()));
-        }
-    }
-
-    QueryString& operator=(const QueryString& qs)
-    {
-        if (this == &qs)
-        {
-            return *this;
-        }
-
-        url = qs.url;
-        keyValuePairs.clear();
-        for (auto p : qs.keyValuePairs)
-        {
-            keyValuePairs.push_back(
-                const_cast<char*>(p - qs.url.c_str() + url.c_str()));
-        }
-        return *this;
-    }
-
-    QueryString& operator=(QueryString&& qs)
-    {
-        keyValuePairs = std::move(qs.keyValuePairs);
-        auto* oldData = const_cast<char*>(qs.url.c_str());
-        url = std::move(qs.url);
-        for (auto& p : keyValuePairs)
-        {
-            p += const_cast<char*>(url.c_str()) - oldData;
-        }
-        return *this;
-    }
-
-    explicit QueryString(std::string newUrl) : url(std::move(newUrl))
-    {
-        if (url.empty())
-        {
-            return;
-        }
-
-        keyValuePairs.resize(maxKeyValuePairsCount);
-
-        size_t count =
-            qsParse(&url[0], &keyValuePairs[0], maxKeyValuePairsCount);
-        keyValuePairs.resize(count);
-    }
-
-    void clear()
-    {
-        keyValuePairs.clear();
-        url.clear();
-    }
-
-    friend std::ostream& operator<<(std::ostream& os, const QueryString& qs)
-    {
-        os << "[ ";
-        for (size_t i = 0; i < qs.keyValuePairs.size(); ++i)
-        {
-            if (i != 0u)
-            {
-                os << ", ";
-            }
-            os << qs.keyValuePairs[i];
-        }
-        os << " ]";
-        return os;
-    }
-
-    char* get(const std::string& name) const
-    {
-        char* ret = qsK2v(name.c_str(), keyValuePairs.data(),
-                          static_cast<int>(keyValuePairs.size()));
-        return ret;
-    }
-
-    std::vector<char*> getList(const std::string& name) const
-    {
-        std::vector<char*> ret;
-        std::string plus = name + "[]";
-        char* element = nullptr;
-
-        int count = 0;
-        while (true)
-        {
-            element = qsK2v(plus.c_str(), keyValuePairs.data(),
-                            static_cast<int>(keyValuePairs.size()), count++);
-            if (element == nullptr)
-            {
-                break;
-            }
-            ret.push_back(element);
-        }
-        return ret;
-    }
-
-  private:
-    std::string url;
-    std::vector<char*> keyValuePairs;
-};
-
-} // namespace crow
diff --git a/redfish-core/lib/event_service.hpp b/redfish-core/lib/event_service.hpp
index b27c6e0..8bd30f5 100644
--- a/redfish-core/lib/event_service.hpp
+++ b/redfish-core/lib/event_service.hpp
@@ -445,13 +445,16 @@ class EventServiceSSE : public Node
         subValue->protocol = "Redfish";
         subValue->retryPolicy = "TerminateAfterRetries";
 
-        char* filters = req.urlParams.get("$filter");
-        if (filters == nullptr)
+        boost::urls::url_view::params_type::iterator it =
+            req.urlParams.find("$filter");
+        if (it == req.urlParams.end())
         {
             subValue->eventFormatType = "Event";
         }
+
         else
         {
+            std::string filters = it->value();
             // Reading from query params.
             bool status = readSSEQueryParams(
                 filters, subValue->eventFormatType, subValue->registryMsgIds,
diff --git a/redfish-core/lib/log_services.hpp b/redfish-core/lib/log_services.hpp
index bee1a92..590243c 100644
--- a/redfish-core/lib/log_services.hpp
+++ b/redfish-core/lib/log_services.hpp
@@ -218,12 +218,14 @@ static bool getEntryTimestamp(sd_journal* journal, std::string& entryTimestamp)
 static bool getSkipParam(crow::Response& res, const crow::Request& req,
                          uint64_t& skip)
 {
-    char* skipParam = req.urlParams.get("$skip");
-    if (skipParam != nullptr)
+    boost::urls::url_view::params_type::iterator it =
+        req.urlParams.find("$skip");
+    if (it != req.urlParams.end())
     {
+        std::string skipParam = it->value();
         char* ptr = nullptr;
-        skip = std::strtoul(skipParam, &ptr, 10);
-        if (*skipParam == '\0' || *ptr != '\0')
+        skip = std::strtoul(skipParam.c_str(), &ptr, 10);
+        if (skipParam.empty() || *ptr != '\0')
         {
 
             messages::queryParameterValueTypeError(res, std::string(skipParam),
@@ -238,12 +240,14 @@ static constexpr const uint64_t maxEntriesPerPage = 1000;
 static bool getTopParam(crow::Response& res, const crow::Request& req,
                         uint64_t& top)
 {
-    char* topParam = req.urlParams.get("$top");
-    if (topParam != nullptr)
+    boost::urls::url_view::params_type::iterator it =
+        req.urlParams.find("$top");
+    if (it != req.urlParams.end())
     {
+        std::string topParam = it->value();
         char* ptr = nullptr;
-        top = std::strtoul(topParam, &ptr, 10);
-        if (*topParam == '\0' || *ptr != '\0')
+        top = std::strtoul(topParam.c_str(), &ptr, 10);
+        if (topParam.empty() || *ptr != '\0')
         {
             messages::queryParameterValueTypeError(res, std::string(topParam),
                                                    "$top");
-- 
2.17.1