~ubuntu-branches/ubuntu/wily/grpc/wily

« back to all changes in this revision

Viewing changes to src/core/client_config/uri_parser.c

  • Committer: Package Import Robot
  • Author(s): Andrew Pollock
  • Date: 2015-05-07 13:28:11 UTC
  • Revision ID: package-import@ubuntu.com-20150507132811-ybm4hfq73tnvvd2e
Tags: upstream-0.10.0
ImportĀ upstreamĀ versionĀ 0.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 * Copyright 2015, Google Inc.
 
4
 * All rights reserved.
 
5
 *
 
6
 * Redistribution and use in source and binary forms, with or without
 
7
 * modification, are permitted provided that the following conditions are
 
8
 * met:
 
9
 *
 
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
 
15
 * distribution.
 
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.
 
19
 *
 
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.
 
31
 *
 
32
 */
 
33
 
 
34
#include "src/core/client_config/uri_parser.h"
 
35
 
 
36
#include <string.h>
 
37
 
 
38
#include <grpc/support/alloc.h>
 
39
#include <grpc/support/log.h>
 
40
#include <grpc/support/string_util.h>
 
41
 
 
42
static grpc_uri *bad_uri(const char *uri_text, int pos, const char *section,
 
43
                         int suppress_errors) {
 
44
  char *line_prefix;
 
45
  int pfx_len;
 
46
 
 
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);
 
52
 
 
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);
 
58
  }
 
59
 
 
60
  return NULL;
 
61
}
 
62
 
 
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);
 
66
  out[end - begin] = 0;
 
67
  return out;
 
68
}
 
69
 
 
70
grpc_uri *grpc_uri_parse(const char *uri_text, int suppress_errors) {
 
71
  grpc_uri *uri;
 
72
  int scheme_begin = 0;
 
73
  int scheme_end = -1;
 
74
  int authority_begin = -1;
 
75
  int authority_end = -1;
 
76
  int path_begin = -1;
 
77
  int path_end = -1;
 
78
  int i;
 
79
 
 
80
  for (i = scheme_begin; uri_text[i] != 0; i++) {
 
81
    if (uri_text[i] == ':') {
 
82
      scheme_end = i;
 
83
      break;
 
84
    }
 
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;
 
92
    }
 
93
    break;
 
94
  }
 
95
  if (scheme_end == -1) {
 
96
    return bad_uri(uri_text, i, "scheme", suppress_errors);
 
97
  }
 
98
 
 
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] == '/') {
 
103
        authority_end = i;
 
104
      }
 
105
      if (uri_text[i] == '?') {
 
106
        return bad_uri(uri_text, i, "query_not_supported", suppress_errors);
 
107
      }
 
108
      if (uri_text[i] == '#') {
 
109
        return bad_uri(uri_text, i, "fragment_not_supported", suppress_errors);
 
110
      }
 
111
    }
 
112
    if (authority_end == -1 && uri_text[i] == 0) {
 
113
      authority_end = i;
 
114
    }
 
115
    if (authority_end == -1) {
 
116
      return bad_uri(uri_text, i, "authority", suppress_errors);
 
117
    }
 
118
    /* TODO(ctiller): parse the authority correctly */
 
119
    path_begin = authority_end;
 
120
  } else {
 
121
    path_begin = scheme_end + 1;
 
122
  }
 
123
 
 
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);
 
127
    }
 
128
    if (uri_text[i] == '#') {
 
129
      return bad_uri(uri_text, i, "fragment_not_supported", suppress_errors);
 
130
    }
 
131
  }
 
132
  path_end = i;
 
133
 
 
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);
 
139
 
 
140
  return uri;
 
141
}
 
142
 
 
143
void grpc_uri_destroy(grpc_uri *uri) {
 
144
  if (!uri) return;
 
145
  gpr_free(uri->scheme);
 
146
  gpr_free(uri->authority);
 
147
  gpr_free(uri->path);
 
148
  gpr_free(uri);
 
149
}