restful.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. 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,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #ifndef BRPC_RESTFUL_H
  18. #define BRPC_RESTFUL_H
  19. #include <string>
  20. #include "butil/strings/string_piece.h"
  21. #include "brpc/server.h"
  22. namespace brpc {
  23. struct RestfulMethodPath {
  24. std::string service_name;
  25. std::string prefix;
  26. std::string postfix;
  27. bool has_wildcard;
  28. std::string to_string() const;
  29. };
  30. struct RestfulMapping {
  31. RestfulMethodPath path;
  32. std::string method_name;
  33. };
  34. // Split components of `path_in' into `path_out'.
  35. // * path_out->service_name does not have /.
  36. // * path_out->prefix is normalized as
  37. // prefix := "/COMPONENT" prefix | "" (no dot in COMPONENT)
  38. // Returns true on success.
  39. bool ParseRestfulPath(butil::StringPiece path_in, RestfulMethodPath* path_out);
  40. // Parse "PATH1 => NAME1, PATH2 => NAME2 ..." where:
  41. // * PATHs are acceptible by ParseRestfulPath.
  42. // * NAMEs are valid as method names in protobuf.
  43. // Returns true on success.
  44. bool ParseRestfulMappings(const butil::StringPiece& mappings,
  45. std::vector<RestfulMapping>* list);
  46. struct RestfulMethodProperty : public Server::MethodProperty {
  47. RestfulMethodPath path;
  48. ServiceOwnership ownership;
  49. };
  50. // Store paths under a same toplevel name.
  51. class RestfulMap {
  52. public:
  53. typedef std::map<std::string, RestfulMethodProperty> DedupMap;
  54. typedef std::vector<RestfulMethodProperty*> PathList;
  55. explicit RestfulMap(const std::string& service_name)
  56. : _service_name(service_name) {}
  57. virtual ~RestfulMap();
  58. // Map `path' to the method denoted by `method_name' in `service'.
  59. // Returns MethodStatus of the method on success, NULL otherwise.
  60. bool AddMethod(const RestfulMethodPath& path,
  61. google::protobuf::Service* service,
  62. const Server::MethodProperty::OpaqueParams& params,
  63. const std::string& method_name,
  64. MethodStatus* status);
  65. // Remove by RestfulMethodPath::to_string() of the path to AddMethod()
  66. // Returns number of methods removed (should be 1 or 0 currently)
  67. size_t RemoveByPathString(const std::string& path);
  68. // Remove all methods.
  69. void ClearMethods();
  70. // Called after by Server at starting moment, to refresh _sorted_paths
  71. void PrepareForFinding();
  72. // Find the method by path.
  73. // Time complexity in worst-case is #slashes-in-input * log(#paths-stored)
  74. const Server::MethodProperty*
  75. FindMethodProperty(const butil::StringPiece& method_path,
  76. std::string* unresolved_path) const;
  77. const std::string& service_name() const { return _service_name; }
  78. // Number of methods in this map. Only for UT right now.
  79. size_t size() const { return _dedup_map.size(); }
  80. private:
  81. DISALLOW_COPY_AND_ASSIGN(RestfulMap);
  82. std::string _service_name;
  83. // refreshed each time
  84. PathList _sorted_paths;
  85. DedupMap _dedup_map;
  86. };
  87. std::ostream& operator<<(std::ostream& os, const RestfulMethodPath&);
  88. } // namespace brpc
  89. #endif // BRPC_RESTFUL_H