org.openqa.selenium.devtools.v85.V85Network Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenium-devtools-v85 Show documentation
Show all versions of selenium-devtools-v85 Show documentation
Selenium automates browsers. That's it! What you do with that power is entirely up to you.
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. 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 org.openqa.selenium.devtools.v85;
import static java.net.HttpURLConnection.HTTP_OK;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.DevToolsException;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.idealized.Network;
import org.openqa.selenium.devtools.v85.fetch.Fetch;
import org.openqa.selenium.devtools.v85.fetch.model.AuthChallengeResponse;
import org.openqa.selenium.devtools.v85.fetch.model.AuthRequired;
import org.openqa.selenium.devtools.v85.fetch.model.HeaderEntry;
import org.openqa.selenium.devtools.v85.fetch.model.RequestPattern;
import org.openqa.selenium.devtools.v85.fetch.model.RequestPaused;
import org.openqa.selenium.devtools.v85.fetch.model.RequestStage;
import org.openqa.selenium.devtools.v85.network.model.Request;
import org.openqa.selenium.internal.Either;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
public class V85Network extends Network {
private static final Logger LOG = Logger.getLogger(V85Network.class.getName());
public V85Network(DevTools devTools) {
super(devTools);
}
@Override
protected Command setUserAgentOverride(UserAgent userAgent) {
return org.openqa.selenium.devtools.v85.network.Network.setUserAgentOverride(
userAgent.userAgent(), userAgent.acceptLanguage(), userAgent.platform(), Optional.empty());
}
@Override
protected Command enableNetworkCaching() {
return org.openqa.selenium.devtools.v85.network.Network.setCacheDisabled(false);
}
@Override
protected Command disableNetworkCaching() {
return org.openqa.selenium.devtools.v85.network.Network.setCacheDisabled(true);
}
@Override
protected Command enableFetchForAllPatterns() {
return Fetch.enable(
Optional.of(
List.of(
new RequestPattern(
Optional.of("*"), Optional.empty(), Optional.of(RequestStage.REQUEST)),
new RequestPattern(
Optional.of("*"), Optional.empty(), Optional.of(RequestStage.RESPONSE)))),
Optional.of(true));
}
@Override
protected Command disableFetch() {
return Fetch.disable();
}
@Override
protected Event authRequiredEvent() {
return Fetch.authRequired();
}
@Override
protected String getUriFrom(AuthRequired authRequired) {
return authRequired.getAuthChallenge().getOrigin();
}
@Override
protected Command continueWithAuth(
AuthRequired authRequired, UsernameAndPassword credentials) {
return Fetch.continueWithAuth(
authRequired.getRequestId(),
new AuthChallengeResponse(
AuthChallengeResponse.Response.PROVIDECREDENTIALS,
Optional.of(credentials.username()),
Optional.ofNullable(credentials.password())));
}
@Override
protected Command cancelAuth(AuthRequired authRequired) {
return Fetch.continueWithAuth(
authRequired.getRequestId(),
new AuthChallengeResponse(
AuthChallengeResponse.Response.CANCELAUTH, Optional.empty(), Optional.empty()));
}
@Override
public Event requestPausedEvent() {
return Fetch.requestPaused();
}
@Override
public Either createSeMessages(RequestPaused pausedReq) {
if (pausedReq.getResponseStatusCode().isPresent()) {
String body;
boolean bodyIsBase64Encoded;
try {
Fetch.GetResponseBodyResponse base64Body =
devTools.send(Fetch.getResponseBody(pausedReq.getRequestId()));
body = base64Body.getBody();
bodyIsBase64Encoded =
base64Body.getBase64Encoded() != null && base64Body.getBase64Encoded();
} catch (DevToolsException e) {
// Redirects don't seem to have bodies
int code = pausedReq.getResponseStatusCode().orElse(HTTP_OK);
if (code >= 300 && code <= 399) {
LOG.warning("Unable to get body for request id " + pausedReq.getRequestId());
}
body = null;
bodyIsBase64Encoded = false;
}
List> headers = new ArrayList<>();
pausedReq
.getResponseHeaders()
.ifPresent(
resHeaders ->
resHeaders.forEach(
header ->
headers.add(
new AbstractMap.SimpleEntry<>(header.getName(), header.getValue()))));
HttpResponse res =
createHttpResponse(pausedReq.getResponseStatusCode(), body, bodyIsBase64Encoded, headers);
return Either.right(res);
}
Request cdpReq = pausedReq.getRequest();
HttpRequest req =
createHttpRequest(
cdpReq.getMethod(), cdpReq.getUrl(), cdpReq.getHeaders(), cdpReq.getPostData());
return Either.left(req);
}
@Override
protected boolean hasErrorResponse(RequestPaused pausedReq) {
return pausedReq.getResponseErrorReason().isPresent();
}
@Override
protected String getRequestId(RequestPaused pausedReq) {
return pausedReq.getRequestId().toString();
}
@Override
protected Command continueWithoutModification(RequestPaused pausedRequest) {
return Fetch.continueRequest(
pausedRequest.getRequestId(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());
}
@Override
protected Command continueRequest(RequestPaused pausedReq, HttpRequest req) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (InputStream is = req.getContent().get()) {
is.transferTo(bos);
} catch (IOException e) {
return continueWithoutModification(pausedReq);
}
List headers = new ArrayList<>();
req.forEachHeader((name, value) -> headers.add(new HeaderEntry(name, value)));
return Fetch.continueRequest(
pausedReq.getRequestId(),
Optional.of(req.getUri()),
Optional.of(req.getMethod().toString()),
Optional.of(Base64.getEncoder().encodeToString(bos.toByteArray())),
Optional.of(headers));
}
@Override
protected Command fulfillRequest(RequestPaused pausedReq, HttpResponse res) {
List headers = new ArrayList<>();
res.forEachHeader((name, value) -> headers.add(new HeaderEntry(name, value)));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (InputStream is = res.getContent().get()) {
is.transferTo(bos);
} catch (IOException e) {
bos.reset();
}
return Fetch.fulfillRequest(
pausedReq.getRequestId(),
res.getStatus(),
Optional.of(headers),
Optional.empty(),
Optional.of(Base64.getEncoder().encodeToString(bos.toByteArray())),
Optional.empty());
}
}