|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef SRC_CLIENTS_HTTP_FETCHER_ASYNC_CURL_REQUEST_DATA_H_ |
| 16 | +#define SRC_CLIENTS_HTTP_FETCHER_ASYNC_CURL_REQUEST_DATA_H_ |
| 17 | + |
| 18 | +#include <memory> |
| 19 | +#include <string> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +#include <curl/curl.h> |
| 23 | +#include <event2/event.h> |
| 24 | +#include <event2/event_struct.h> |
| 25 | + |
| 26 | +#include "absl/container/flat_hash_map.h" |
| 27 | +#include "absl/functional/any_invocable.h" |
| 28 | +#include "absl/status/statusor.h" |
| 29 | +#include "absl/time/time.h" |
| 30 | + |
| 31 | +namespace privacy_sandbox::server_common::clients { |
| 32 | + |
| 33 | +using OnDoneFetchUrl = absl::AnyInvocable<void(absl::StatusOr<std::string>) &&>; |
| 34 | +using OnDoneFetchUrls = |
| 35 | + absl::AnyInvocable<void(std::vector<absl::StatusOr<std::string>>) &&>; |
| 36 | + |
| 37 | +struct HTTPRequest { |
| 38 | + std::string url; |
| 39 | + // Optional |
| 40 | + std::vector<std::string> headers = {}; |
| 41 | + // Optional |
| 42 | + std::string body = ""; |
| 43 | + |
| 44 | + // Will include response headers in the response with the specified |
| 45 | + // case insensitive name. |
| 46 | + // Defaults to empty. |
| 47 | + std::vector<std::string> include_headers = {}; |
| 48 | + |
| 49 | + struct RedirectConfig { |
| 50 | + // Limits redirects to HTTP/HTTPS only. |
| 51 | + // Defaults to false. |
| 52 | + bool strict_http = false; |
| 53 | + |
| 54 | + // Get redirect URL in response metadata. |
| 55 | + // Defaults to false. |
| 56 | + bool get_redirect_url = false; |
| 57 | + } redirect_config; |
| 58 | +}; |
| 59 | + |
| 60 | +struct HTTPResponse { |
| 61 | + // The pointer to this is used to write the request output. |
| 62 | + std::string body; |
| 63 | + |
| 64 | + // Optional. Included if include_headers has values. |
| 65 | + absl::flat_hash_map<std::string, absl::StatusOr<std::string>> headers; |
| 66 | + |
| 67 | + // Optional. Included if get_redirect_url is true. |
| 68 | + std::string final_url; |
| 69 | +}; |
| 70 | + |
| 71 | +// Maintains state about the data to upload vi CURL. |
| 72 | +struct DataToUpload { |
| 73 | + // JSON string to upload. |
| 74 | + std::string data; |
| 75 | + // First offset in the data that has not yet been uploaded. |
| 76 | + int offset = 0; |
| 77 | +}; |
| 78 | + |
| 79 | +using OnDoneFetchUrlWithMetadata = |
| 80 | + absl::AnyInvocable<void(absl::StatusOr<HTTPResponse>) &&>; |
| 81 | +using OnDoneFetchUrlsWithMetadata = |
| 82 | + absl::AnyInvocable<void(std::vector<absl::StatusOr<HTTPResponse>>) &&>; |
| 83 | + |
| 84 | +// Maintains the information about the socket associated with each curl request. |
| 85 | +struct SocketInfo { |
| 86 | + // Socket file descriptor. |
| 87 | + curl_socket_t sock_fd; |
| 88 | + // Last activity recorded on the descriptor. |
| 89 | + int activity; |
| 90 | + // Event type monitored by libevent's event loop. |
| 91 | + struct event tracked_event; |
| 92 | +}; |
| 93 | + |
| 94 | +// Maximum default number of pending curl requests that are awaiting processing. |
| 95 | +constexpr inline int kDefaultMaxCurlPendingRequests = 5000; |
| 96 | +// Maximum default wait time that a request can sit in the processing queue, |
| 97 | +// after which the request will be removed from the queue. |
| 98 | +constexpr inline absl::Duration kDefaultMaxRequestWaitTime = |
| 99 | + absl::Milliseconds(500); |
| 100 | +// Default number of curl workers to use to process the curl requests. |
| 101 | +constexpr inline int kDefaultNumCurlWorkers = 1; |
| 102 | + |
| 103 | +struct MultiCurlHttpFetcherAsyncOptions { |
| 104 | + // TODO(b/412330778): Maybe these should be absl::Duration |
| 105 | + const int64_t keepalive_interval_sec = 2; |
| 106 | + const int64_t keepalive_idle_sec = 2; |
| 107 | + std::string ca_cert = "/etc/ssl/certs/ca-certificates.crt"; |
| 108 | + const int64_t curlmopt_maxconnects = 0L; |
| 109 | + const int64_t curlmopt_max_total_connections = 0L; |
| 110 | + const int64_t curlmopt_max_host_connections = 0L; |
| 111 | + int num_curl_workers = kDefaultNumCurlWorkers; |
| 112 | + // TODO(b/412330778): Rename to `curl_max_wait_time_ms` |
| 113 | + absl::Duration curl_max_wait_time = kDefaultMaxRequestWaitTime; |
| 114 | + int curl_queue_length = kDefaultMaxCurlPendingRequests; |
| 115 | +}; |
| 116 | + |
| 117 | +// This struct maintains the data related to a Curl request, some of which |
| 118 | +// has to stay valid throughout the life of the request. The code maintains a |
| 119 | +// reference in curl_data_map_ till the request is completed. The destructor |
| 120 | +// is then to free the resources in this class after the request completes. |
| 121 | +struct CurlRequestData { |
| 122 | + explicit CurlRequestData(const std::vector<std::string>& headers, |
| 123 | + OnDoneFetchUrlWithMetadata on_done, |
| 124 | + std::vector<std::string> response_header_keys, |
| 125 | + bool include_redirect_url); |
| 126 | + ~CurlRequestData(); |
| 127 | + |
| 128 | + // The easy handle provided by libcurl, registered to |
| 129 | + // multi_curl_request_manager_. |
| 130 | + CURL* req_handle; |
| 131 | + |
| 132 | + // The pointer to the linked list of the request HTTP headers. |
| 133 | + struct curl_slist* headers_list_ptr = nullptr; |
| 134 | + |
| 135 | + // The callback function for this request from FetchUrl. |
| 136 | + OnDoneFetchUrlWithMetadata done_callback; |
| 137 | + |
| 138 | + // Pointer to the body of request. Relevant only for HTTP methods that |
| 139 | + // upload data to server. |
| 140 | + std::unique_ptr<DataToUpload> body; |
| 141 | + |
| 142 | + // Set response headers in the output. |
| 143 | + std::vector<std::string> response_headers; |
| 144 | + |
| 145 | + // Set the final redirect URL in the output. |
| 146 | + bool include_redirect_url; |
| 147 | + |
| 148 | + HTTPResponse response_with_metadata; |
| 149 | + |
| 150 | + // Records the time when the request was started. |
| 151 | + absl::Time start_time; |
| 152 | +}; |
| 153 | + |
| 154 | +} // namespace privacy_sandbox::server_common::clients |
| 155 | + |
| 156 | +#endif // SRC_CLIENTS_HTTP_FETCHER_ASYNC_CURL_REQUEST_DATA_H_ |
0 commit comments