Repository navigation
fix: update executor and driver server URL state on directConnect #2500
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
base: master
Are you sure you want to change the base?
Changes from all commits
6d010be
e4eed84
8ee115c
176274b
d23b566
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * See the NOTICE file distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.appium.java_client.internal; | ||
|
|
||
| import io.appium.java_client.remote.AppiumCommandExecutor; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import java.lang.invoke.MethodHandles; | ||
| import java.net.URL; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.function.Function; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
| * Internal bridge exposing private command executor state without widening public API. | ||
| */ | ||
| @NullMarked | ||
| public final class CommandExecutorInternals { | ||
| private static final AtomicReference<@Nullable Function<AppiumCommandExecutor, @Nullable URL>> | ||
| INITIAL_SERVER_URL_RESOLVER = new AtomicReference<>(); | ||
|
|
||
| private CommandExecutorInternals() { | ||
| } | ||
|
|
||
| /** | ||
| * Registers the internal resolver that reads an executor's initial server URL before any URL overrides. | ||
| * | ||
| * @param lookup a private lookup belonging to {@link AppiumCommandExecutor} | ||
| * @param resolver the function returning the initial server URL for an executor | ||
| * @throws SecurityException if {@code lookup} does not have private access to {@link AppiumCommandExecutor} | ||
| */ | ||
| public static void registerInitialServerUrlResolver( | ||
| MethodHandles.Lookup lookup, | ||
| Function<AppiumCommandExecutor, @Nullable URL> resolver | ||
| ) { | ||
| requireNonNull(lookup, "Lookup"); | ||
| requireNonNull(resolver, "Resolver"); | ||
| if (lookup.lookupClass() != AppiumCommandExecutor.class | ||
| || (lookup.lookupModes() & MethodHandles.Lookup.PRIVATE) == 0) { | ||
| throw new SecurityException( | ||
| "Only AppiumCommandExecutor may register the initial server URL resolver" | ||
| ); | ||
| } | ||
| INITIAL_SERVER_URL_RESOLVER.compareAndSet(null, resolver); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the server URL configured on {@code executor} before any {@code directConnect} or | ||
| * {@code overrideServerUrl} override took effect. | ||
| * | ||
| * @param executor the command executor to inspect | ||
| * @return the initial server URL, or {@code null} if none was configured | ||
| */ | ||
| @Nullable | ||
| public static URL getInitialServerUrl(AppiumCommandExecutor executor) { | ||
| requireNonNull(executor, "Command executor"); | ||
| Function<AppiumCommandExecutor, @Nullable URL> resolver = INITIAL_SERVER_URL_RESOLVER.get(); | ||
| return resolver != null ? resolver.apply(executor) : executor.getAddressOfRemoteServer(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * See the NOTICE file distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.appium.java_client.internal; | ||
|
|
||
| import io.appium.java_client.CommandExecutionHelper; | ||
| import io.appium.java_client.ExecutesMethod; | ||
| import io.appium.java_client.remote.AppiumCommandExecutor; | ||
| import io.appium.java_client.remote.AppiumRemoteWebDriver; | ||
| import io.appium.java_client.remote.SessionId; | ||
| import io.appium.java_client.ws.StringWebSocketClient; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import java.net.URI; | ||
| import java.net.URL; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Shared endpoint resolution and WebSocket startup helpers for device log broadcast listeners. | ||
| */ | ||
| @NullMarked | ||
| @SuppressWarnings("removal") | ||
| public final class LogBroadcastEndpoints { | ||
| private LogBroadcastEndpoints() { | ||
| } | ||
|
|
||
| /** | ||
| * Starts server-side log streaming and connects the WebSocket client to the broadcast endpoint. | ||
| * The WebSocket scheme ({@code ws} or {@code wss}) is derived from the executor's initial server URL | ||
| * before any {@code directConnect} or server URL override so existing callers keep their original scheme. | ||
| * | ||
| * @param driver the driver executing {@code mobile: startLogsBroadcast} | ||
| * @param clientSupplier supplier for the target WebSocket client, invoked after starting server streaming | ||
| * @param host the target WebSocket host | ||
| * @param port the target WebSocket port | ||
| * @param deviceLogType the log endpoint suffix (such as {@code logcat} or {@code syslog}) | ||
| */ | ||
| public static void startBroadcast( | ||
| ExecutesMethod driver, | ||
| Supplier<StringWebSocketClient> clientSupplier, | ||
| String host, | ||
| int port, | ||
| String deviceLogType | ||
| ) { | ||
| var remoteWebDriver = (AppiumRemoteWebDriver) driver; | ||
| var executor = (AppiumCommandExecutor) remoteWebDriver.getCommandExecutor(); | ||
| CommandExecutionHelper.executeScript(driver, "mobile: startLogsBroadcast"); | ||
| URI endpoint = buildBroadcastUri( | ||
| CommandExecutorInternals.getInitialServerUrl(executor), | ||
| host, | ||
| port, | ||
| remoteWebDriver.getSessionId(), | ||
| deviceLogType | ||
| ); | ||
| clientSupplier.get().connect(endpoint); | ||
| } | ||
|
|
||
| /** | ||
| * Builds a device log broadcast WebSocket URI using {@code wss} when {@code serverUrl} uses | ||
| * {@code https}, or {@code ws} otherwise. | ||
| * | ||
| * @param serverUrl the server URL used to choose the WebSocket scheme, or {@code null} if unknown | ||
| * @param host the target WebSocket host | ||
| * @param port the target WebSocket port | ||
| * @param sessionId the active session identifier | ||
| * @param deviceLogType the log endpoint suffix (such as {@code logcat} or {@code syslog}) | ||
| * @return the WebSocket endpoint URI | ||
| */ | ||
| public static URI buildBroadcastUri( | ||
| @Nullable URL serverUrl, | ||
| String host, | ||
| int port, | ||
| @Nullable SessionId sessionId, | ||
| String deviceLogType | ||
| ) { | ||
| String scheme = serverUrl != null && "https".equalsIgnoreCase(serverUrl.getProtocol()) ? "wss" : "ws"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Restoring the host and port defaults still leaves a compatibility issue here. Suppose the session starts at
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, thanks! Updated Sessions starting on plain HTTP now stay on ws for both logcat and syslog broadcasts after direct-connecting to HTTPS, while initial HTTPS sessions keep using wss. Added regression tests covering both cases plus null initial base URL in |
||
| String endpoint = scheme + "://" + Strings.formatHost(host) + ":" + port | ||
| + "/ws/session/" + sessionId + "/appium/device/" + deviceLogType; | ||
| return URI.create(endpoint); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.