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
This commit is contained in:
Christoph Purrer
2025-06-09 20:30:06 -07:00
committed by Facebook GitHub Bot
parent 43ba9df55f
commit c5bfd5be8c
4 changed files with 162 additions and 0 deletions
@@ -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
@@ -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 <folly/io/IOBuf.h>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
namespace facebook::react {
namespace http {
using Headers = std::vector<std::pair<std::string, std::string>>;
struct FormDataField {
std::string fieldName;
Headers headers;
std::optional<std::string> string;
std::optional<std::string> uri;
};
using FormData = std::vector<FormDataField>;
struct Body {
std::optional<std::string> string;
std::optional<std::string> blob;
std::optional<FormData> formData;
std::optional<std::string> base64;
};
using OnUploadProgress = std::function<void(int64_t progress, int64_t size)>;
using OnResponse = std::function<void(uint16_t responseCode, Headers headers)>;
using OnBody = std::function<void(std::unique_ptr<folly::IOBuf> body)>;
using OnBodyIncremental = std::function<int64_t(
int64_t progress,
int64_t total,
std::unique_ptr<folly::IOBuf> body)>;
using OnBodyProgress = std::function<void(int64_t loaded, int64_t total)>;
using OnResponseComplete =
std::function<void(std::string error, bool timeoutError)>;
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<http::IRequestToken> 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<std::string> loggingId = std::nullopt) = 0;
};
extern const char HttpClientFactoryKey[];
using HttpClientFactory = std::function<std::unique_ptr<IHttpClient>()>;
HttpClientFactory getHttpClientFactory();
} // namespace facebook::react
@@ -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
@@ -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 <functional>
#include <memory>
#include <string>
#include <utility>
namespace facebook::react {
class IWebSocketClient {
public:
using OnConnectCallback = std::function<void(bool, const std::string&)>;
using OnClosedCallback = std::function<void(const std::string&)>;
using OnMessageCallback = std::function<void(const std::string&)>;
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<std::unique_ptr<IWebSocketClient>()>;
WebSocketClientFactory getWebSocketClientFactory();
} // namespace facebook::react