• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_URI_URI_PARSER_H
20 #define GRPC_CORE_LIB_URI_URI_PARSER_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <stddef.h>
25 
26 #include <map>
27 #include <string>
28 #include <vector>
29 
30 #include "absl/status/statusor.h"
31 #include "absl/strings/string_view.h"
32 
33 namespace grpc_core {
34 
35 class URI {
36  public:
37   struct QueryParam {
38     std::string key;
39     std::string value;
40     bool operator==(const QueryParam& other) const {
41       return key == other.key && value == other.value;
42     }
43   };
44 
45   // Creates an instance of GrpcURI by parsing an rfc3986 URI string. Returns
46   // an IllegalArgumentError on failure.
47   static absl::StatusOr<URI> Parse(absl::string_view uri_text);
48   // Explicit construction by individual URI components
49   URI(std::string scheme, std::string authority, std::string path,
50       std::vector<QueryParam> query_parameter_pairs, std::string fragment_);
51   URI() = default;
52   // Copy construction and assignment
53   URI(const URI& other);
54   URI& operator=(const URI& other);
55   // Move construction and assignment
56   URI(URI&&) = default;
57   URI& operator=(URI&&) = default;
58 
scheme()59   const std::string& scheme() const { return scheme_; }
authority()60   const std::string& authority() const { return authority_; }
path()61   const std::string& path() const { return path_; }
62   // Stores the *last* value appearing for each repeated key in the query
63   // string. If you need to capture repeated query parameters, use
64   // `query_parameter_pairs`.
query_parameter_map()65   const std::map<absl::string_view, absl::string_view>& query_parameter_map()
66       const {
67     return query_parameter_map_;
68   }
69   // A vector of key:value query parameter pairs, kept in order of appearance
70   // within the URI search string. Repeated keys are represented as separate
71   // key:value elements.
query_parameter_pairs()72   const std::vector<QueryParam>& query_parameter_pairs() const {
73     return query_parameter_pairs_;
74   }
fragment()75   const std::string& fragment() const { return fragment_; }
76 
77  private:
78   std::string scheme_;
79   std::string authority_;
80   std::string path_;
81   std::map<absl::string_view, absl::string_view> query_parameter_map_;
82   std::vector<QueryParam> query_parameter_pairs_;
83   std::string fragment_;
84 };
85 }  // namespace grpc_core
86 
87 #endif /* GRPC_CORE_LIB_URI_URI_PARSER_H */
88