mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 15:09:59 +01:00
Add GetQueryParameter helper function
Easily get the query parameter from the URI, with optional default value.
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
@@ -30,11 +31,12 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <event2/thread.h>
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/bufferevent.h>
|
||||
#include <event2/util.h>
|
||||
#include <event2/http.h>
|
||||
#include <event2/keyvalq_struct.h>
|
||||
#include <event2/thread.h>
|
||||
#include <event2/util.h>
|
||||
|
||||
#include <support/events.h>
|
||||
|
||||
@@ -639,6 +641,37 @@ HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod() const
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::string> HTTPRequest::GetQueryParameter(const std::string& key) const
|
||||
{
|
||||
const char* uri{evhttp_request_get_uri(req)};
|
||||
|
||||
return GetQueryParameterFromUri(uri, key);
|
||||
}
|
||||
|
||||
std::optional<std::string> GetQueryParameterFromUri(const char* uri, const std::string& key)
|
||||
{
|
||||
evhttp_uri* uri_parsed{evhttp_uri_parse(uri)};
|
||||
const char* query{evhttp_uri_get_query(uri_parsed)};
|
||||
std::optional<std::string> result;
|
||||
|
||||
if (query) {
|
||||
// Parse the query string into a key-value queue and iterate over it
|
||||
struct evkeyvalq params_q;
|
||||
evhttp_parse_query_str(query, ¶ms_q);
|
||||
|
||||
for (struct evkeyval* param{params_q.tqh_first}; param != nullptr; param = param->next.tqe_next) {
|
||||
if (param->key == key) {
|
||||
result = param->value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
evhttp_clear_headers(¶ms_q);
|
||||
}
|
||||
evhttp_uri_free(uri_parsed);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
|
||||
{
|
||||
LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
|
||||
|
||||
Reference in New Issue
Block a user