3
* Copyright 2015, Google Inc.
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions are
10
* * Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* * Redistributions in binary form must reproduce the above
13
* copyright notice, this list of conditions and the following disclaimer
14
* in the documentation and/or other materials provided with the
16
* * Neither the name of Google Inc. nor the names of its
17
* contributors may be used to endorse or promote products derived from
18
* this software without specific prior written permission.
20
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
#include "src/core/client_config/uri_parser.h"
38
#include <grpc/support/alloc.h>
39
#include <grpc/support/log.h>
40
#include <grpc/support/string_util.h>
42
static grpc_uri *bad_uri(const char *uri_text, int pos, const char *section,
43
int suppress_errors) {
47
if (!suppress_errors) {
48
gpr_asprintf(&line_prefix, "bad uri.%s: '", section);
49
pfx_len = strlen(line_prefix) + pos;
50
gpr_log(GPR_ERROR, "%s%s'", line_prefix, uri_text);
51
gpr_free(line_prefix);
53
line_prefix = gpr_malloc(pfx_len + 1);
54
memset(line_prefix, ' ', pfx_len);
55
line_prefix[pfx_len] = 0;
56
gpr_log(GPR_ERROR, "%s^ here", line_prefix);
57
gpr_free(line_prefix);
63
static char *copy_fragment(const char *src, int begin, int end) {
64
char *out = gpr_malloc(end - begin + 1);
65
memcpy(out, src + begin, end - begin);
70
grpc_uri *grpc_uri_parse(const char *uri_text, int suppress_errors) {
74
int authority_begin = -1;
75
int authority_end = -1;
80
for (i = scheme_begin; uri_text[i] != 0; i++) {
81
if (uri_text[i] == ':') {
85
if (uri_text[i] >= 'a' && uri_text[i] <= 'z') continue;
86
if (uri_text[i] >= 'A' && uri_text[i] <= 'Z') continue;
87
if (i != scheme_begin) {
88
if (uri_text[i] >= '0' && uri_text[i] <= '9') continue;
89
if (uri_text[i] == '+') continue;
90
if (uri_text[i] == '-') continue;
91
if (uri_text[i] == '.') continue;
95
if (scheme_end == -1) {
96
return bad_uri(uri_text, i, "scheme", suppress_errors);
99
if (uri_text[scheme_end + 1] == '/' && uri_text[scheme_end + 2] == '/') {
100
authority_begin = scheme_end + 3;
101
for (i = authority_begin; uri_text[i] != 0; i++) {
102
if (uri_text[i] == '/') {
105
if (uri_text[i] == '?') {
106
return bad_uri(uri_text, i, "query_not_supported", suppress_errors);
108
if (uri_text[i] == '#') {
109
return bad_uri(uri_text, i, "fragment_not_supported", suppress_errors);
112
if (authority_end == -1 && uri_text[i] == 0) {
115
if (authority_end == -1) {
116
return bad_uri(uri_text, i, "authority", suppress_errors);
118
/* TODO(ctiller): parse the authority correctly */
119
path_begin = authority_end;
121
path_begin = scheme_end + 1;
124
for (i = path_begin; uri_text[i] != 0; i++) {
125
if (uri_text[i] == '?') {
126
return bad_uri(uri_text, i, "query_not_supported", suppress_errors);
128
if (uri_text[i] == '#') {
129
return bad_uri(uri_text, i, "fragment_not_supported", suppress_errors);
134
uri = gpr_malloc(sizeof(*uri));
135
memset(uri, 0, sizeof(*uri));
136
uri->scheme = copy_fragment(uri_text, scheme_begin, scheme_end);
137
uri->authority = copy_fragment(uri_text, authority_begin, authority_end);
138
uri->path = copy_fragment(uri_text, path_begin, path_end);
143
void grpc_uri_destroy(grpc_uri *uri) {
145
gpr_free(uri->scheme);
146
gpr_free(uri->authority);