uri_impl.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CHROMEOS_PRINTING_URI_IMPL_H_
  5. #define CHROMEOS_PRINTING_URI_IMPL_H_
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "chromeos/printing/uri.h"
  10. // This file contains a declaration of struct used in the implementation of
  11. // class Uri declared in uri.h. This file is not supposed to be included
  12. // anywhere outside of the class Uri.
  13. namespace chromeos {
  14. using Iter = std::string::const_iterator;
  15. class Uri::Pim {
  16. public:
  17. Pim();
  18. Pim(const Pim&);
  19. ~Pim();
  20. // These methods parse and normalize the corresponding component(s) from the
  21. // input string |begin|-|end|. Each component is saved only if successfully
  22. // parsed and verified. In case of an error, the field |parser_error| is set
  23. // and false is returned. All methods assume that |begin| <= |end|.
  24. // Additional notes for particular components:
  25. // * Scheme: if the current Port is unspecified and the new Scheme has
  26. // default port number, the Port is set to this default value
  27. // * Authority: this is Userinfo + Host + Port, see description in uri.h for
  28. // the grammar
  29. // * Port: an empty string means -1, see the method SavePort(int) below
  30. // * Path: the input string must be empty or starts from '/'
  31. bool ParseScheme(const Iter& begin, const Iter& end);
  32. bool ParseAuthority(const Iter& begin, const Iter& end);
  33. bool ParsePort(const Iter& begin, const Iter& end);
  34. bool ParsePath(const Iter& begin, const Iter& end);
  35. bool ParseQuery(const Iter& begin, const Iter& end);
  36. bool ParseFragment(const Iter& begin, const Iter& end);
  37. // This method parse the whole URI. It calls internally the methods
  38. // Parse*(...) declared above. In case of an error, the method set the field
  39. // |parser_error| and returns false. Parsing stops on the first error,
  40. // components that have been successfully parsed are saved.
  41. bool ParseUri(const Iter& begin, const Iter end);
  42. // This method fails (and return false) <=> |port| is smaller than -1 or
  43. // larger than 65535. If |port| == -1 and the current Scheme has a default
  44. // port, the default port is set as a new Port number. The field
  45. // |parser_error| is set accordingly.
  46. bool SavePort(int port);
  47. // These methods save values of corresponding components. The template
  48. // parameter |encoded| trigger resolution of %-escaped characters. If set to
  49. // true, every % sign in the input value is treated as the beginning of
  50. // %-escaped character; if set to false, % signs are treated as regular ASCII
  51. // characters. All input values are validated and normalized, but without
  52. // %-escaping fragile characters (components are stored in "native" form). In
  53. // case of a failure, false is returned, the value of target component is not
  54. // modified, and the field |parser_error| is set accordingly.
  55. template <bool encoded>
  56. bool SaveUserinfo(const std::string& val);
  57. template <bool encoded>
  58. bool SaveHost(const std::string& val);
  59. template <bool encoded>
  60. bool SavePath(const std::vector<std::string>& val);
  61. template <bool encoded>
  62. bool SaveQuery(const std::vector<std::pair<std::string, std::string>>& val);
  63. template <bool encoded>
  64. bool SaveFragment(const std::string& val);
  65. // Getters for all fields.
  66. const std::string& scheme() const { return scheme_; }
  67. const std::string& userinfo() const { return userinfo_; }
  68. const std::string& host() const { return host_; }
  69. int port() const { return port_; }
  70. const std::vector<std::string>& path() const { return path_; }
  71. const std::vector<std::pair<std::string, std::string>>& query() const {
  72. return query_;
  73. }
  74. const std::string& fragment() const { return fragment_; }
  75. // Access to the |parser_error_|
  76. ParserError& parser_error() { return parser_error_; }
  77. private:
  78. // Reads the string |begin|-|end| and perform the following operations:
  79. // 1. if |plus_to_space| is true, all '+' signs are converted to ' ' (space)
  80. // 2. if |encoded| is true, all % signs are treated as initiators of
  81. // %-escaped characters and decoded to corresponding ASCII
  82. // 3. if |case_insensitive| is true, all capital ASCII letters are converted
  83. // to lowercase
  84. // 4. all UTF-8 characters are validated
  85. // 5. all ASCII characters are validated (see the section Encoding in uri.h)
  86. // The output is saved to |out|. In case of an error, the method set the field
  87. // |parser_error| and returns false.
  88. // The following initial requirements must be met:
  89. // * |begin| <= |end|
  90. // * |out| must point to an empty string
  91. // When the method returns false, |out| may contain invalid value.
  92. template <bool encoded, bool case_insensitive = false>
  93. bool ParseString(const Iter& begin,
  94. const Iter& end,
  95. std::string* out,
  96. bool plus_to_space = false);
  97. // Components values. They are valid and normalized, but before %-escaping.
  98. std::string scheme_;
  99. std::string userinfo_;
  100. std::string host_;
  101. int port_ = -1; // -1 means "unspecified"
  102. // A list of path's segments, without separators ('/').
  103. std::vector<std::string> path_;
  104. // A list of parameters name=value; value may be empty.
  105. std::vector<std::pair<std::string, std::string>> query_;
  106. std::string fragment_;
  107. // The last parser status.
  108. ParserError parser_error_;
  109. };
  110. } // namespace chromeos
  111. #endif // CHROMEOS_PRINTING_URI_IMPL_H_