request.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // MinIO C++ Library for Amazon S3 Compatible Cloud Storage
  2. // Copyright 2022 MinIO, Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #ifndef _MINIO_REQUEST_H
  16. #define _MINIO_REQUEST_H
  17. #include "credentials.h"
  18. #include "providers.h"
  19. #include "signer.h"
  20. namespace minio {
  21. namespace s3 {
  22. const std::string AWS_S3_PREFIX =
  23. "^(((bucket\\.|accesspoint\\.)"
  24. "vpce(-(?!_)[a-z_\\d]+)+\\.s3\\.)|"
  25. "((?!s3)(?!-)(?!_)[a-z_\\d-]{1,63}\\.)"
  26. "s3-control(-(?!_)[a-z_\\d]+)*\\.|"
  27. "(s3(-(?!_)[a-z_\\d]+)*\\.))";
  28. const std::regex HOSTNAME_REGEX(
  29. "^((?!-)(?!_)[a-z_\\d-]{1,63}\\.)*"
  30. "((?!_)(?!-)[a-z_\\d-]{1,63})$",
  31. std::regex_constants::icase);
  32. const std::regex AWS_ENDPOINT_REGEX(".*\\.amazonaws\\.com(|\\.cn)$",
  33. std::regex_constants::icase);
  34. const std::regex AWS_S3_ENDPOINT_REGEX(
  35. AWS_S3_PREFIX + "((?!s3)(?!-)(?!_)[a-z_\\d-]{1,63}\\.)*" +
  36. "amazonaws\\.com(|\\.cn)$",
  37. std::regex_constants::icase);
  38. const std::regex AWS_ELB_ENDPOINT_REGEX(
  39. "^(?!-)(?!_)[a-z_\\d-]{1,63}\\."
  40. "(?!-)(?!_)[a-z_\\d-]{1,63}\\."
  41. "elb\\.amazonaws\\.com$",
  42. std::regex_constants::icase);
  43. const std::regex AWS_S3_PREFIX_REGEX(AWS_S3_PREFIX,
  44. std::regex_constants::icase);
  45. const std::regex REGION_REGEX("^((?!_)(?!-)[a-z_\\d-]{1,63})$",
  46. std::regex_constants::icase);
  47. bool awsRegexMatch(std::string_view value, std::regex regex);
  48. error::Error getAwsInfo(std::string host, bool https, std::string& region,
  49. std::string& aws_s3_prefix,
  50. std::string& aws_domain_suffix, bool& dualstack);
  51. static std::string extractRegion(std::string& host) {
  52. std::stringstream str_stream(host);
  53. std::string token;
  54. std::vector<std::string> tokens;
  55. while (std::getline(str_stream, token, '.')) tokens.push_back(token);
  56. token = tokens[1];
  57. // If token is "dualstack", then region might be in next token.
  58. if (token == "dualstack") token = tokens[2];
  59. // If token is equal to "amazonaws", region is not passed in the host.
  60. if (token == "amazonaws") return "";
  61. // Return token as region.
  62. return token;
  63. }
  64. struct BaseUrl {
  65. bool https = true;
  66. std::string host;
  67. unsigned int port = 0;
  68. std::string region;
  69. std::string aws_s3_prefix;
  70. std::string aws_domain_suffix;
  71. bool dualstack = false;
  72. bool virtual_style = false;
  73. BaseUrl() {}
  74. BaseUrl(std::string host, bool https = true, std::string region = "");
  75. error::Error BuildUrl(http::Url& url, http::Method method, std::string region,
  76. utils::Multimap query_params,
  77. std::string bucket_name = "",
  78. std::string object_name = "");
  79. operator bool() const { return !err_ && !host.empty(); }
  80. error::Error Error() {
  81. if (host.empty() && !err_) return error::Error("empty host");
  82. return err_;
  83. }
  84. private:
  85. error::Error err_;
  86. error::Error BuildAwsUrl(http::Url& url, std::string bucket_name,
  87. bool enforce_path_style, std::string region);
  88. void BuildListBucketsUrl(http::Url& url, std::string region);
  89. }; // struct Url
  90. struct Request {
  91. http::Method method;
  92. std::string region;
  93. BaseUrl& base_url;
  94. std::string user_agent;
  95. utils::Multimap headers;
  96. utils::Multimap query_params;
  97. std::string bucket_name;
  98. std::string object_name;
  99. std::string_view body = "";
  100. http::DataFunction datafunc = NULL;
  101. void* userdata = NULL;
  102. http::ProgressFunction progressfunc = NULL;
  103. void* progress_userdata = NULL;
  104. std::string sha256;
  105. utils::Time date;
  106. bool debug = false;
  107. bool ignore_cert_check = false;
  108. std::string ssl_cert_file;
  109. Request(http::Method method, std::string region, BaseUrl& baseurl,
  110. utils::Multimap extra_headers, utils::Multimap extra_query_params);
  111. http::Request ToHttpRequest(creds::Provider* provider = NULL);
  112. private:
  113. void BuildHeaders(http::Url& url, creds::Provider* provider);
  114. }; // struct Request
  115. } // namespace s3
  116. } // namespace minio
  117. #endif // #ifndef __MINIO_REQUEST_H