Skip to content

Commit

Permalink
Remove upgrade headers
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Oct 11, 2023
1 parent f593eb2 commit 924838f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 19 deletions.
9 changes: 7 additions & 2 deletions src/main/java/io/appium/java_client/AppiumClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package io.appium.java_client;

import io.appium.java_client.internal.filters.AppiumHttp11EnforcerFilter;
import io.appium.java_client.internal.filters.AppiumIdempotencyFilter;
import io.appium.java_client.internal.filters.AppiumUserAgentFilter;
import org.openqa.selenium.Credentials;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.http.ClientConfig;
Expand All @@ -35,7 +38,9 @@
public class AppiumClientConfig extends ClientConfig {
private final boolean directConnect;

private static final Filter DEFAULT_FILTER = new AppiumUserAgentFilter();
private static final Filter DEFAULT_FILTERS = new AppiumUserAgentFilter()
.andThen(new AppiumIdempotencyFilter())
.andThen(new AppiumHttp11EnforcerFilter());

private static final Duration DEFAULT_READ_TIMEOUT = Duration.ofMinutes(10);

Expand Down Expand Up @@ -77,7 +82,7 @@ public static AppiumClientConfig defaultConfig() {
null,
DEFAULT_CONNECTION_TIMEOUT,
DEFAULT_READ_TIMEOUT,
DEFAULT_FILTER,
DEFAULT_FILTERS,
null,
null,
null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.filters;

import org.openqa.selenium.remote.http.Filter;
import org.openqa.selenium.remote.http.HttpHandler;

import java.util.List;

public class AppiumHttp11EnforcerFilter implements Filter {
private static final List<String> HTTP2_UPGRADE_HEADERS = List.of(
"Upgrade", "HTTP2-Settings", "Connection"
);

@Override
public HttpHandler apply(HttpHandler next) {
return req -> {
var upgrade = req.getHeader("Upgrade");
if (upgrade != null && upgrade.trim().equalsIgnoreCase("h2c")) {
HTTP2_UPGRADE_HEADERS.forEach(x -> req.removeHeader(x));
}
return next.execute(req);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.filters;

import org.openqa.selenium.remote.http.Filter;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpMethod;

import java.util.UUID;

public class AppiumIdempotencyFilter implements Filter {
// https://github.com/appium/appium-base-driver/pull/400
private static final String IDEMPOTENCY_KEY_HEADER = "X-Idempotency-Key";

@Override
public HttpHandler apply(HttpHandler next) {
return req -> {
if (req.getMethod() == HttpMethod.POST && req.getUri().endsWith("/session")) {
req.setHeader(IDEMPOTENCY_KEY_HEADER, UUID.randomUUID().toString().toLowerCase());
}
return next.execute(req);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.appium.java_client;
package io.appium.java_client.internal.filters;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.net.HttpHeaders;
Expand Down Expand Up @@ -82,7 +82,6 @@ public static String buildUserAgent(@Nullable String userAgent) {

@Override
public HttpHandler apply(HttpHandler next) {

return req -> {
req.setHeader(HttpHeaders.USER_AGENT, buildUserAgent(req.getHeader(HttpHeaders.USER_AGENT)));
return next.execute(req);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.net.HttpHeaders;
import io.appium.java_client.AppiumClientConfig;
import io.appium.java_client.AppiumUserAgentFilter;
import io.appium.java_client.internal.ReflectionHelpers;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriverException;
Expand Down Expand Up @@ -48,16 +46,13 @@
import java.net.URL;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.throwIfUnchecked;
import static java.util.Optional.ofNullable;
import static org.openqa.selenium.remote.DriverCommand.NEW_SESSION;

public class AppiumCommandExecutor extends HttpCommandExecutor {
// https://github.com/appium/appium-base-driver/pull/400
private static final String IDEMPOTENCY_KEY_HEADER = "X-Idempotency-Key";

private final Optional<DriverService> serviceOptional;

Expand Down Expand Up @@ -179,14 +174,7 @@ private Response createSession(Command command) throws IOException {
throw new SessionNotCreatedException("Session already exists");
}

ProtocolHandshake.Result result = new AppiumProtocolHandshake().createSession(
getClient().with(httpHandler -> req -> {
req.setHeader(HttpHeaders.USER_AGENT,
AppiumUserAgentFilter.buildUserAgent(req.getHeader(HttpHeaders.USER_AGENT)));
req.setHeader(IDEMPOTENCY_KEY_HEADER, UUID.randomUUID().toString().toLowerCase());
return httpHandler.execute(req);
}), command
);
ProtocolHandshake.Result result = new AppiumProtocolHandshake().createSession(getClient(), command);
Dialect dialect = result.getDialect();
if (!(dialect.getCommandCodec() instanceof W3CHttpCommandCodec)) {
throw new SessionNotCreatedException("Only W3C sessions are supported. "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.appium.java_client.internal;

import io.appium.java_client.AppiumUserAgentFilter;
import io.appium.java_client.internal.filters.AppiumUserAgentFilter;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.appium.java_client.internal;

import io.appium.java_client.AppiumUserAgentFilter;
import io.appium.java_client.internal.filters.AppiumUserAgentFilter;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand Down

0 comments on commit 924838f

Please sign in to comment.