Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 2788 - urls with http support in preview #3281

Merged
merged 11 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ private void PropagateToScene()
component.RealmName = data.realmData.RealmName;
component.NetworkId = data.realmData.NetworkId;
component.CommsAdapter = data.realmData.CommsAdapter;
data.commsRoomInfo.WriteToComponent(component);
component.IsPreview = data.realmData.IsLocalSceneDevelopment;

// component.IsPreview // TODO: when E@ supports running in preview mode
data.commsRoomInfo.WriteToComponent(component);
}, SpecialEntitiesID.SCENE_ROOT_ENTITY, (realmData, commsRoomInfo));
}

Expand All @@ -73,16 +73,16 @@ public class CommsRoomInfo
private readonly ObjectProxy<IRoomHub> roomHubProxy;
private readonly ISceneData sceneData;

public string IslandSid { get; private set; }

public bool IsConnectedSceneRoom { get; private set; }

public CommsRoomInfo(ObjectProxy<IRoomHub> roomHubProxy, ISceneData sceneData)
{
this.roomHubProxy = roomHubProxy;
this.sceneData = sceneData;
}

public string IslandSid { get; private set; }

public bool IsConnectedSceneRoom { get; private set; }

/// <summary>
/// Returns true if rooms info has changed
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,24 @@ public void Dispose() { }
string redirect,
int timeout,
IWebRequestController webController,
CancellationToken ct
CancellationToken ct,
bool isLocalSceneDevelopment
)
{
RequestMethod parsedRequestMethod = ParseRequestMethod(requestMethod);
try
{
// if we're in LocalSceneDevelopment mode to allow connecting to unsafe websocket server to the client
if (!isLocalSceneDevelopment && !url.ToLower().StartsWith("https://"))
throw new Exception("Can't make an unsafe http request, please upgrade to https. url=" + url);

if (parsedRequestMethod == RequestMethod.INVALID)
throw new ArgumentException("Invalid request method.");
RequestMethod parsedRequestMethod = ParseRequestMethod(requestMethod);

var commonArguments = new CommonArguments(URLAddress.FromString(url), timeout: timeout);
WebRequestHeadersInfo webRequestHeaders = HeadersFromJsObject(headers);
if (parsedRequestMethod == RequestMethod.INVALID)
throw new ArgumentException("Invalid request method.");

var commonArguments = new CommonArguments(URLAddress.FromString(url), timeout: timeout);
WebRequestHeadersInfo webRequestHeaders = HeadersFromJsObject(headers);

try
{
await UniTask.SwitchToMainThread();

switch (parsedRequestMethod)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class CommunicationsControllerAPIWrapper : JsApiWrapperBase<ICommunicatio

private readonly ISceneExceptionsHandler sceneExceptionsHandler;

public CommunicationsControllerAPIWrapper(ICommunicationsControllerAPI api, IInstancePoolsProvider instancePoolsProvider, ISceneExceptionsHandler sceneExceptionsHandler) : base(api)
public CommunicationsControllerAPIWrapper(ICommunicationsControllerAPI api, IInstancePoolsProvider instancePoolsProvider, ISceneExceptionsHandler sceneExceptionsHandler, bool isLocalSceneDevelopment) : base(api, isLocalSceneDevelopment)
{
this.instancePoolsProvider = instancePoolsProvider;
this.sceneExceptionsHandler = sceneExceptionsHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SceneRuntime.Apis.Modules.FetchApi
public interface ISimpleFetchApi : IDisposable
{
public UniTask<Response> FetchAsync(string requestMethod, string url, object headers, bool hasBody, string body,
string redirect, int timeout, IWebRequestController webController, CancellationToken ct);
string redirect, int timeout, IWebRequestController webController, CancellationToken ct, bool isLocalSceneDevelopment);

public struct Response
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public LogSimpleFetchApi(ISimpleFetchApi origin)
}

public async UniTask<ISimpleFetchApi.Response> FetchAsync(string requestMethod, string url, object headers, bool hasBody, string body,
string redirect, int timeout, IWebRequestController webController, CancellationToken ct)
string redirect, int timeout, IWebRequestController webController, CancellationToken ct, bool isLocalSceneDevelopment)
{
string args = $"request method: {requestMethod} "
+ $"url: {url} "
Expand All @@ -30,7 +30,7 @@ public LogSimpleFetchApi(ISimpleFetchApi origin)

try
{
ISimpleFetchApi.Response result = await origin.FetchAsync(requestMethod, url, headers, hasBody, body, redirect, timeout, webController, ct);
ISimpleFetchApi.Response result = await origin.FetchAsync(requestMethod, url, headers, hasBody, body, redirect, timeout, webController, ct, isLocalSceneDevelopment);
ReportHub.Log(ReportCategory.GENERIC_WEB_REQUEST, $"SimpleFetchApi, Fetch request successes with: {args}");
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using JetBrains.Annotations;
using Microsoft.ClearScript;
using System;
using System.Collections.Generic;
using System.Threading;
using Utility;

Expand All @@ -13,7 +14,7 @@ public class SimpleFetchApiWrapper : JsApiWrapperBase<ISimpleFetchApi>
private readonly CancellationTokenSource cancellationTokenSource;
private readonly IWebRequestController webController;

public SimpleFetchApiWrapper(ISimpleFetchApi api, IWebRequestController webController) : base(api)
public SimpleFetchApiWrapper(ISimpleFetchApi api, IWebRequestController webController, bool isLocalSceneDevelopment) : base(api, isLocalSceneDevelopment)
{
cancellationTokenSource = new CancellationTokenSource();
this.webController = webController;
Expand All @@ -32,12 +33,12 @@ public object Fetch(string requestMethod, string url, object headers, bool hasBo

async UniTask<ResponseToJs> FetchAsync(CancellationToken ct)
{
ISimpleFetchApi.Response response = await api.FetchAsync(requestMethod, url, headers, hasBody, body, redirect, timeout, webController, ct);
ISimpleFetchApi.Response response = await api.FetchAsync(requestMethod, url, headers, hasBody, body, redirect, timeout, webController, ct, isLocalSceneDevelopment);

var headersToJs = new PropertyBag();

if (response.Headers != null)
foreach (var header in response.Headers)
foreach (KeyValuePair<string, string> header in response.Headers)
headersToJs.Add(header.Key, header.Value);

return new ResponseToJs
Expand All @@ -57,14 +58,14 @@ async UniTask<ResponseToJs> FetchAsync(CancellationToken ct)
[Serializable]
public struct ResponseToJs
{
public PropertyBag headers;
public bool ok;
public bool redirected;
public int status;
public string statusText;
public string url;
public string data;
public string type;
public PropertyBag headers;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class RealmInfo
public string realmName;
public int networkId;
public string commsAdapter;
public bool preview;
public bool isPreview;
public string protocol;

public RealmInfo(IRealmData realmData) : this(
Expand All @@ -77,7 +77,7 @@ public RealmInfo(string baseUrl, string realmName, int networkId, string commsAd
this.realmName = realmName;
this.networkId = networkId;
this.commsAdapter = commsAdapter;
preview = isPreview;
this.isPreview = isPreview;
this.protocol = protocol;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class RuntimeWrapper : JsApiWrapperBase<IRuntime>

private readonly CancellationTokenSource cancellationTokenSource;

public RuntimeWrapper(IRuntime api, ISceneExceptionsHandler exceptionsHandler) : base(api)
public RuntimeWrapper(IRuntime api, ISceneExceptionsHandler exceptionsHandler, bool isLocalSceneDevelopment) : base(api, isLocalSceneDevelopment)
{
this.exceptionsHandler = exceptionsHandler;
cancellationTokenSource = new CancellationTokenSource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class WebSocketApiWrapper : JsApiWrapperBase<IWebSocketApi>
private readonly CancellationTokenSource cancellationTokenSource;
private readonly IJavaScriptApiExceptionsHandler exceptionsHandler;

public WebSocketApiWrapper(IWebSocketApi api, IJavaScriptApiExceptionsHandler exceptionsHandler) : base(api)
public WebSocketApiWrapper(IWebSocketApi api, IJavaScriptApiExceptionsHandler exceptionsHandler, bool isLocalSceneDevelopment) : base(api, isLocalSceneDevelopment)
{
this.exceptionsHandler = exceptionsHandler;
cancellationTokenSource = new CancellationTokenSource();
Expand All @@ -35,8 +35,18 @@ public int GetState(int webSocketId) =>
[PublicAPI("Used by StreamingAssets/Js/Modules/webSocketApi.js")]
public object ConnectAsync(int websocketId, string url)
{
try { return api.ConnectAsync(websocketId, url, cancellationTokenSource.Token).ReportAndRethrowException(exceptionsHandler).ToDisconnectedPromise(); }
catch (Exception e) { return Task.FromException(e).ToPromise(); }
try
{
// if we're in isLocalSceneDevelopment mode to allow connecting to unsafe websocket server to the client
if (!isLocalSceneDevelopment && !url.ToLower().StartsWith("wss://"))
throw new Exception("Can't make an unsafe http request, please upgrade to https. url=" + url);

return api.ConnectAsync(websocketId, url, cancellationTokenSource.Token).ReportAndRethrowException(exceptionsHandler).ToDisconnectedPromise();
}
catch (Exception e)
{
return Task.FromException(e).ToPromise();
}
}

[PublicAPI("Used by StreamingAssets/Js/Modules/webSocketApi.js")]
Expand All @@ -48,7 +58,7 @@ public object SendAsync(int webSocketId, string str)
.ReportAndRethrowException(exceptionsHandler)
.ToDisconnectedPromise();
}
catch (Exception e) { return Task.FromException(e).ToPromise();}
catch (Exception e) { return Task.FromException(e).ToPromise(); }
}

[PublicAPI("Used by StreamingAssets/Js/Modules/webSocketApi.js")]
Expand All @@ -60,7 +70,7 @@ public object SendAsync(int webSocketId, IArrayBuffer arrayBuffer)
.ReportAndRethrowException(exceptionsHandler)
.ToDisconnectedPromise();
}
catch (Exception e) { return Task.FromException(e).ToPromise();}
catch (Exception e) { return Task.FromException(e).ToPromise(); }
}

[PublicAPI("Used by StreamingAssets/Js/Modules/webSocketApi.js")]
Expand All @@ -72,7 +82,7 @@ public object SendAsync(int webSocketId, ITypedArray<byte> typedArray)
.ReportAndRethrowException(exceptionsHandler)
.ToDisconnectedPromise();
}
catch (Exception e) { return Task.FromException(e).ToPromise();}
catch (Exception e) { return Task.FromException(e).ToPromise(); }
}

[PublicAPI("Used by StreamingAssets/Js/Modules/webSocketApi.js")]
Expand Down
32 changes: 16 additions & 16 deletions Explorer/Assets/Scripts/SceneRuntime/ISceneRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ IRemoteMetadata remoteMetadata
sceneRuntime.RegisterSignedFetch(webRequestController, decentralandUrlsSource, sceneData, realmData);
sceneRuntime.RegisterRestrictedActionsApi(restrictedActionsAPI);
sceneRuntime.RegisterUserActions(restrictedActionsAPI);
sceneRuntime.RegisterRuntime(runtime, exceptionsHandler);
sceneRuntime.RegisterRuntime(runtime, exceptionsHandler, realmData.IsLocalSceneDevelopment);
sceneRuntime.RegisterEthereumApi(ethereumApi, web3IdentityCache, exceptionsHandler);
sceneRuntime.RegisterUserIdentityApi(profileRepository, web3IdentityCache, exceptionsHandler);
sceneRuntime.RegisterWebSocketApi(webSocketApi, exceptionsHandler);
sceneRuntime.RegisterSimpleFetchApi(simpleFetchApi, webRequestController);
sceneRuntime.RegisterCommunicationsControllerApi(communicationsControllerAPI, instancePoolsProvider, exceptionsHandler);
sceneRuntime.RegisterWebSocketApi(webSocketApi, exceptionsHandler, realmData.IsLocalSceneDevelopment);
sceneRuntime.RegisterSimpleFetchApi(simpleFetchApi, webRequestController, realmData.IsLocalSceneDevelopment);
sceneRuntime.RegisterCommunicationsControllerApi(communicationsControllerAPI, instancePoolsProvider, exceptionsHandler, realmData.IsLocalSceneDevelopment);
sceneRuntime.RegisterPortableExperiencesApi(portableExperiencesController, exceptionsHandler);
}

Expand Down Expand Up @@ -115,12 +115,12 @@ IRemoteMetadata remoteMetadata
sceneRuntime.RegisterSignedFetch(webRequestController, decentralandUrlsSource, sceneData, realmData);
sceneRuntime.RegisterRestrictedActionsApi(restrictedActionsAPI);
sceneRuntime.RegisterUserActions(restrictedActionsAPI);
sceneRuntime.RegisterRuntime(runtime, exceptionsHandler);
sceneRuntime.RegisterRuntime(runtime, exceptionsHandler, realmData.IsLocalSceneDevelopment);
sceneRuntime.RegisterEthereumApi(ethereumApi, web3IdentityCache, exceptionsHandler);
sceneRuntime.RegisterUserIdentityApi(profileRepository, web3IdentityCache, exceptionsHandler);
sceneRuntime.RegisterWebSocketApi(webSocketApi, exceptionsHandler);
sceneRuntime.RegisterSimpleFetchApi(simpleFetchApi, webRequestController);
sceneRuntime.RegisterCommunicationsControllerApi(communicationsControllerAPI, instancePoolsProvider, exceptionsHandler);
sceneRuntime.RegisterWebSocketApi(webSocketApi, exceptionsHandler, realmData.IsLocalSceneDevelopment);
sceneRuntime.RegisterSimpleFetchApi(simpleFetchApi, webRequestController, realmData.IsLocalSceneDevelopment);
sceneRuntime.RegisterCommunicationsControllerApi(communicationsControllerAPI, instancePoolsProvider, exceptionsHandler, realmData.IsLocalSceneDevelopment);
sceneRuntime.RegisterPortableExperiencesApi(portableExperiencesController, exceptionsHandler);
}

Expand Down Expand Up @@ -169,9 +169,9 @@ private static void RegisterUserActions(this ISceneRuntime sceneRuntime, IRestri
sceneRuntime.Register("UnityUserActions", new UserActionsWrapper(api));
}

private static void RegisterRuntime(this ISceneRuntime sceneRuntime, IRuntime api, ISceneExceptionsHandler sceneExceptionsHandler)
private static void RegisterRuntime(this ISceneRuntime sceneRuntime, IRuntime api, ISceneExceptionsHandler sceneExceptionsHandler, bool isLocalSceneDevelopment)
{
sceneRuntime.Register("UnityRuntime", new RuntimeWrapper(api, sceneExceptionsHandler));
sceneRuntime.Register("UnityRuntime", new RuntimeWrapper(api, sceneExceptionsHandler, isLocalSceneDevelopment));
}

private static void RegisterEthereumApi(this ISceneRuntime sceneRuntime, IEthereumApi ethereumApi, IWeb3IdentityCache web3IdentityCache, ISceneExceptionsHandler sceneExceptionsHandler)
Expand All @@ -184,19 +184,19 @@ private static void RegisterUserIdentityApi(this ISceneRuntime sceneRuntime, IPr
sceneRuntime.Register("UnityUserIdentityApi", new UserIdentityApiWrapper(profileRepository, identityCache, sceneExceptionsHandler));
}

private static void RegisterWebSocketApi(this ISceneRuntime sceneRuntime, IWebSocketApi webSocketApi, ISceneExceptionsHandler sceneExceptionsHandler)
private static void RegisterWebSocketApi(this ISceneRuntime sceneRuntime, IWebSocketApi webSocketApi, ISceneExceptionsHandler sceneExceptionsHandler, bool isLocalSceneDevelopment)
{
sceneRuntime.Register("UnityWebSocketApi", new WebSocketApiWrapper(webSocketApi, sceneExceptionsHandler));
sceneRuntime.Register("UnityWebSocketApi", new WebSocketApiWrapper(webSocketApi, sceneExceptionsHandler, isLocalSceneDevelopment));
}

private static void RegisterCommunicationsControllerApi(this ISceneRuntime sceneRuntime, ICommunicationsControllerAPI api, IInstancePoolsProvider instancePoolsProvider, ISceneExceptionsHandler sceneExceptionsHandler)
private static void RegisterCommunicationsControllerApi(this ISceneRuntime sceneRuntime, ICommunicationsControllerAPI api, IInstancePoolsProvider instancePoolsProvider, ISceneExceptionsHandler sceneExceptionsHandler, bool isLocalSceneDevelopment)
{
sceneRuntime.Register("UnityCommunicationsControllerApi", new CommunicationsControllerAPIWrapper(api, instancePoolsProvider, sceneExceptionsHandler));
sceneRuntime.Register("UnityCommunicationsControllerApi", new CommunicationsControllerAPIWrapper(api, instancePoolsProvider, sceneExceptionsHandler, isLocalSceneDevelopment));
}

private static void RegisterSimpleFetchApi(this ISceneRuntime sceneRuntime, ISimpleFetchApi simpleFetchApi, IWebRequestController webRequestController)
private static void RegisterSimpleFetchApi(this ISceneRuntime sceneRuntime, ISimpleFetchApi simpleFetchApi, IWebRequestController webRequestController, bool isLocalSceneDevelopment)
{
sceneRuntime.Register("UnitySimpleFetchApi", new SimpleFetchApiWrapper(simpleFetchApi, webRequestController));
sceneRuntime.Register("UnitySimpleFetchApi", new SimpleFetchApiWrapper(simpleFetchApi, webRequestController, isLocalSceneDevelopment));
}

public static void RegisterSDKMessageBusCommsApi(this ISceneRuntime sceneRuntime, ISDKMessageBusCommsControllerAPI api)
Expand Down
4 changes: 3 additions & 1 deletion Explorer/Assets/Scripts/SceneRuntime/JsApiWrapperBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ namespace SceneRuntime
public class JsApiWrapperBase<TApi> : IJsApiWrapper where TApi: IDisposable
{
protected readonly TApi api;
protected readonly bool isLocalSceneDevelopment;

protected JsApiWrapperBase(TApi api)
protected JsApiWrapperBase(TApi api, bool isLocalSceneDevelopment)
{
this.api = api;
this.isLocalSceneDevelopment = isLocalSceneDevelopment;
}

public void Dispose()
Expand Down
8 changes: 5 additions & 3 deletions Explorer/Assets/StreamingAssets/Js/Modules/FetchApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ declare function fetch(url: string, init?: RequestInit): Promise<Response>
async function restrictedFetch(message) {
const canUseFetch = true // TODO: this should come from somewhere

if (message.url.toLowerCase().substr(0, 8) !== "https://") {
return Promise.reject(new Error("Can't make an unsafe http request, please upgrade to https. url=" + message.url))
}
//TODO: delete commented out code. This was commented to move the responsability to check
// if we're in preview mode to allow connecting to unsafe websocket server to the client
// if (message.url.toLowerCase().substr(0, 8) !== "https://") {
// return Promise.reject(new Error("Can't make an unsafe http request, please upgrade to https. url=" + message.url))
// }

if (!canUseFetch) {
return Promise.reject(new Error("This scene is not allowed to use fetch."))
Expand Down
Loading
Loading