...16// under the License.17package org.openqa.selenium.devtools.v86;18import com.google.common.collect.ImmutableList;19import org.openqa.selenium.UsernameAndPassword;20import org.openqa.selenium.devtools.Command;21import org.openqa.selenium.devtools.DevTools;22import org.openqa.selenium.devtools.Event;23import org.openqa.selenium.devtools.idealized.Network;24import org.openqa.selenium.devtools.v86.fetch.Fetch;25import org.openqa.selenium.devtools.v86.fetch.model.AuthChallengeResponse;26import org.openqa.selenium.devtools.v86.fetch.model.AuthRequired;27import org.openqa.selenium.devtools.v86.fetch.model.HeaderEntry;28import org.openqa.selenium.devtools.v86.fetch.model.RequestPattern;29import org.openqa.selenium.devtools.v86.fetch.model.RequestPaused;30import org.openqa.selenium.devtools.v86.network.model.Request;31import org.openqa.selenium.remote.http.Contents;32import org.openqa.selenium.remote.http.HttpRequest;33import org.openqa.selenium.remote.http.HttpResponse;34import java.util.ArrayList;35import java.util.Base64;36import java.util.List;37import java.util.Optional;38public class V86Network extends Network<AuthRequired, RequestPaused> {39 public V86Network(DevTools devTools) {40 super(devTools);41 }42 @Override43 protected Command<Void> enableNetworkCaching() {44 return org.openqa.selenium.devtools.v86.network.Network.setCacheDisabled(false);45 }46 @Override47 protected Command<Void> disableNetworkCaching() {48 return org.openqa.selenium.devtools.v86.network.Network.setCacheDisabled(true);49 }50 @Override51 protected Command<Void> enableFetchForAllPatterns() {52 return Fetch.enable(53 Optional.of(ImmutableList.of(new RequestPattern(Optional.of("*"), Optional.empty(), Optional.empty()))),54 Optional.of(true));55 }56 @Override57 protected Command<Void> disableFetch() {58 return Fetch.disable();59 }60 @Override61 protected Event<AuthRequired> authRequiredEvent() {62 return Fetch.authRequired();63 }64 @Override65 protected String getUriFrom(AuthRequired authRequired) {66 return authRequired.getAuthChallenge().getOrigin();67 }68 @Override69 protected Command<Void> continueWithAuth(AuthRequired authRequired, UsernameAndPassword credentials) {70 return Fetch.continueWithAuth(71 authRequired.getRequestId(),72 new AuthChallengeResponse(73 AuthChallengeResponse.Response.PROVIDECREDENTIALS,74 Optional.of(credentials.username()),75 Optional.ofNullable(credentials.username())));76 }77 @Override78 protected Command<Void> cancelAuth(AuthRequired authRequired) {79 return Fetch.continueWithAuth(80 authRequired.getRequestId(),81 new AuthChallengeResponse(AuthChallengeResponse.Response.CANCELAUTH, Optional.empty(), Optional.empty()));82 }83 @Override84 protected Event<RequestPaused> requestPausedEvent() {85 return Fetch.requestPaused();86 }87 @Override88 protected Optional<HttpRequest> createHttpRequest(RequestPaused pausedRequest) {89 if (pausedRequest.getResponseErrorReason().isPresent() || pausedRequest.getResponseStatusCode().isPresent()) {90 return Optional.empty();91 }92 Request cdpRequest = pausedRequest.getRequest();93 return Optional.of(createHttpRequest(94 cdpRequest.getMethod(),95 cdpRequest.getUrl(),96 cdpRequest.getHeaders(),97 cdpRequest.getPostData()));98 }99 @Override100 protected Command<Void> continueWithoutModification(RequestPaused pausedRequest) {101 return Fetch.continueRequest(102 pausedRequest.getRequestId(),103 Optional.empty(),104 Optional.empty(),105 Optional.empty(),106 Optional.empty());107 }108 @Override109 protected Command<Void> createResponse(RequestPaused pausedRequest, HttpResponse response) {110 List<HeaderEntry> headers = new ArrayList<>();111 response.getHeaderNames().forEach(112 name -> response.getHeaders(name).forEach(value -> headers.add(new HeaderEntry(name, value))));113 byte[] bytes = Contents.bytes(response.getContent());114 String body = bytes.length > 0 ? Base64.getEncoder().encodeToString(bytes) : null;115 return Fetch.fulfillRequest(116 pausedRequest.getRequestId(),117 response.getStatus(),118 Optional.of(headers),119 Optional.empty(),120 Optional.ofNullable(body),121 Optional.empty());122 }123}...