From 057054f665f02bcda1155f0e637c265cf01b3b3e Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Thu, 31 Mar 2016 09:20:42 -0700 Subject: [PATCH] Added grpc_uri_get_query_arg per comments. --- src/core/lib/client_config/uri_parser.c | 12 ++++++++++++ src/core/lib/client_config/uri_parser.h | 4 ++++ test/core/client_config/uri_parser_test.c | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/src/core/lib/client_config/uri_parser.c b/src/core/lib/client_config/uri_parser.c index d818d4c4d1a..dfcd623e849 100644 --- a/src/core/lib/client_config/uri_parser.c +++ b/src/core/lib/client_config/uri_parser.c @@ -281,6 +281,18 @@ grpc_uri *grpc_uri_parse(const char *uri_text, int suppress_errors) { return uri; } +const char *grpc_uri_get_query_arg(const grpc_uri *uri, const char *key) { + GPR_ASSERT(key != NULL); + if (key[0] == '\0') return NULL; + + for (size_t i = 0; i < uri->num_query_parts; ++i) { + if (0 == strcmp(key, uri->query_parts[i])) { + return uri->query_parts_values[i]; + } + } + return NULL; +} + void grpc_uri_destroy(grpc_uri *uri) { if (!uri) return; gpr_free(uri->scheme); diff --git a/src/core/lib/client_config/uri_parser.h b/src/core/lib/client_config/uri_parser.h index 115dd5a2f6c..b44188b8cdf 100644 --- a/src/core/lib/client_config/uri_parser.h +++ b/src/core/lib/client_config/uri_parser.h @@ -53,6 +53,10 @@ typedef struct { /** parse a uri, return NULL on failure */ grpc_uri *grpc_uri_parse(const char *uri_text, int suppress_errors); +/** return the part of a query string after the '=' in "?key=xxx&...", or NULL + * if key is not present */ +const char *grpc_uri_get_query_arg(const grpc_uri *uri, const char *key); + /** destroy a uri */ void grpc_uri_destroy(grpc_uri *uri); diff --git a/test/core/client_config/uri_parser_test.c b/test/core/client_config/uri_parser_test.c index bc3e01c90ab..6cb8c1d535b 100644 --- a/test/core/client_config/uri_parser_test.c +++ b/test/core/client_config/uri_parser_test.c @@ -80,6 +80,11 @@ static void test_query_parts() { GPR_ASSERT(0 == strcmp("", uri->query_parts[3])); GPR_ASSERT(NULL == uri->query_parts_values[3]); + GPR_ASSERT(NULL == grpc_uri_get_query_arg(uri, "a")); + GPR_ASSERT(0 == strcmp("B", grpc_uri_get_query_arg(uri, "b"))); + GPR_ASSERT(0 == strcmp("", grpc_uri_get_query_arg(uri, "c"))); + GPR_ASSERT(NULL == grpc_uri_get_query_arg(uri, "")); + GPR_ASSERT(0 == strcmp("frag", uri->fragment)); grpc_uri_destroy(uri); }