uri.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef COMMON_HDFS_URI_H_
  19. #define COMMON_HDFS_URI_H_
  20. #include <iostream>
  21. #include <string>
  22. #include <vector>
  23. #include <cstdint>
  24. #include <stdexcept>
  25. namespace hdfs
  26. {
  27. class uri_parse_error : public std::invalid_argument {
  28. public:
  29. uri_parse_error(const char *what_str) : std::invalid_argument(what_str) {}
  30. uri_parse_error(const std::string& what_str) : std::invalid_argument(what_str) {}
  31. };
  32. class URI {
  33. public:
  34. // Parse a string into a URI. Throw a hdfs::uri_parse_error if URI is malformed.
  35. static URI parse_from_string(const std::string &str);
  36. // URI encode/decode strings
  37. static std::string encode (const std::string &input);
  38. static std::string decode (const std::string &input);
  39. URI();
  40. std::string get_scheme(bool encoded_output=false) const;
  41. void set_scheme(const std::string &s, bool encoded_input=false);
  42. // empty if none.
  43. std::string get_host(bool encoded_output=false) const;
  44. void set_host(const std::string& h, bool encoded_input=false);
  45. // true if port has been set
  46. bool has_port() const;
  47. // undefined if port hasn't been set
  48. uint16_t get_port() const;
  49. // use default if port hasn't been set
  50. uint16_t get_port_or_default(uint16_t default_val) const;
  51. void set_port(uint16_t p);
  52. void clear_port();
  53. std::string get_path(bool encoded_output=false) const;
  54. void set_path(const std::string &p, bool encoded_input=false);
  55. void add_path(const std::string &p, bool encoded_input=false);
  56. std::vector<std::string> get_path_elements(bool encoded_output=false) const;
  57. struct Query {
  58. Query(const std::string& key, const std::string& val);
  59. std::string key;
  60. std::string value;
  61. };
  62. std::string get_query(bool encoded_output=false) const;
  63. std::vector<Query> get_query_elements(bool encoded_output=false) const;
  64. // Not that set_query must always pass in encoded strings
  65. void set_query(const std::string &q);
  66. // Adds a parameter onto the query; does not check if it already exists
  67. // e.g. parseFromString("foo?bar=baz").addQuery("bing","bang")
  68. // would leave "bar=baz&bing=bang" as the query
  69. void add_query(const std::string &name, const std::string & value, bool encoded_input=false);
  70. // Removes the query part if exists
  71. // e.g. parseFromString("foo?bar=baz&bing=bang&bar=bong").removeQueries("bar")
  72. // would leave bing=bang as the query
  73. void remove_query(const std::string &q_name, bool encoded_input=false);
  74. std::string get_fragment(bool encoded_output=false) const;
  75. void set_fragment(const std::string &f, bool encoded_input=false);
  76. std::string str(bool encoded_output=true) const;
  77. // Get a string with each URI field printed on a separate line
  78. std::string GetDebugString() const;
  79. private:
  80. // These are stored in encoded form
  81. std::string scheme;
  82. std::string user;
  83. std::string pass;
  84. std::string host;
  85. std::vector<std::string> path;
  86. std::vector<Query> queries;
  87. std::string fragment;
  88. // implicitly narrowed to uint16_t if positive
  89. // -1 to indicate uninitialized
  90. int32_t _port;
  91. // URI encoding helpers
  92. static std::string from_encoded(bool encoded_output, const std::string & input);
  93. static std::string to_encoded(bool encoded_input, const std::string & input);
  94. bool has_authority() const;
  95. std::string build_authority(bool encoded_output) const;
  96. std::string build_path(bool encoded_output) const;
  97. void parse_path(bool input_encoded, const std::string &input_path);
  98. };
  99. inline std::ostream& operator<<(std::ostream &out, const URI &uri) {
  100. return out << uri.str();
  101. }
  102. }
  103. #endif