From c5bfd5be8c2121722978dec771ff317233657b5e Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Mon, 9 Jun 2025 20:30:06 -0700 Subject: [PATCH] Add http target to ReactCxxPlatform (#51899) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51899 changelog: [internal] Reviewed By: andrewdacenko Differential Revision: D76240550 fbshipit-source-id: 5817b80d8683d951ac686390ad74b0927a2d6d2a --- .../react/http/IHttpClient.cpp | 13 +++ .../ReactCxxPlatform/react/http/IHttpClient.h | 89 +++++++++++++++++++ .../react/http/IWebSocketClient.cpp | 13 +++ .../react/http/IWebSocketClient.h | 47 ++++++++++ 4 files changed, 162 insertions(+) create mode 100644 packages/react-native/ReactCxxPlatform/react/http/IHttpClient.cpp create mode 100644 packages/react-native/ReactCxxPlatform/react/http/IHttpClient.h create mode 100644 packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.cpp create mode 100644 packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.h diff --git a/packages/react-native/ReactCxxPlatform/react/http/IHttpClient.cpp b/packages/react-native/ReactCxxPlatform/react/http/IHttpClient.cpp new file mode 100644 index 00000000000..c19f481ee22 --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/http/IHttpClient.cpp @@ -0,0 +1,13 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +namespace facebook::react { + +// NOLINTNEXTLINE(modernize-avoid-c-arrays) +extern const char HttpClientFactoryKey[] = "HttpClientFactory"; + +} // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/http/IHttpClient.h b/packages/react-native/ReactCxxPlatform/react/http/IHttpClient.h new file mode 100644 index 00000000000..9b3d78b5c9d --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/http/IHttpClient.h @@ -0,0 +1,89 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace facebook::react { + +namespace http { + +using Headers = std::vector>; + +struct FormDataField { + std::string fieldName; + Headers headers; + std::optional string; + std::optional uri; +}; + +using FormData = std::vector; + +struct Body { + std::optional string; + std::optional blob; + std::optional formData; + std::optional base64; +}; + +using OnUploadProgress = std::function; +using OnResponse = std::function; +using OnBody = std::function body)>; +using OnBodyIncremental = std::function body)>; +using OnBodyProgress = std::function; +using OnResponseComplete = + std::function; + +struct NetworkCallbacks { + OnUploadProgress onUploadProgress{nullptr}; + OnResponse onResponse{nullptr}; + OnBody onBody{nullptr}; + OnBodyIncremental onBodyIncremental{nullptr}; + OnBodyProgress onBodyProgress{nullptr}; + OnResponseComplete onResponseComplete{nullptr}; + bool sendIncrementalUpdates{false}; + bool sendProgressUpdates{false}; +}; + +struct IRequestToken { + virtual ~IRequestToken() = default; + + virtual void cancel() noexcept = 0; +}; + +} // namespace http + +struct IHttpClient { + virtual ~IHttpClient() = default; + + virtual std::unique_ptr sendRequest( + http::NetworkCallbacks&& callback, + const std::string& method, + const std::string& url, + const http::Headers& headers = {}, + const http::Body& body = {}, + uint32_t timeout = 0, + std::optional loggingId = std::nullopt) = 0; +}; + +extern const char HttpClientFactoryKey[]; + +using HttpClientFactory = std::function()>; + +HttpClientFactory getHttpClientFactory(); + +} // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.cpp b/packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.cpp new file mode 100644 index 00000000000..93800a35a7b --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.cpp @@ -0,0 +1,13 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +namespace facebook::react { + +// NOLINTNEXTLINE(modernize-avoid-c-arrays) +extern const char WebSocketClientFactoryKey[] = "WebSocketClientFactory"; + +} // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.h b/packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.h new file mode 100644 index 00000000000..a2ec1b0859b --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include +#include +#include +#include + +namespace facebook::react { + +class IWebSocketClient { + public: + using OnConnectCallback = std::function; + using OnClosedCallback = std::function; + using OnMessageCallback = std::function; + + virtual ~IWebSocketClient() = default; + + virtual void setOnClosedCallback(OnClosedCallback&& callback) noexcept = 0; + + virtual void setOnMessageCallback(OnMessageCallback&& callback) noexcept = 0; + + virtual void connect( + const std::string& url, + OnConnectCallback&& = nullptr) = 0; + + virtual void close(const std::string& reason) = 0; + + virtual void send(const std::string& message) = 0; + + virtual void ping() = 0; +}; + +extern const char WebSocketClientFactoryKey[]; + +using WebSocketClientFactory = + std::function()>; + +WebSocketClientFactory getWebSocketClientFactory(); + +} // namespace facebook::react