Skip to content

Commit

Permalink
fix: correctly resolve OOPIF response bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyong920 committed Nov 22, 2024
1 parent 286aced commit ca19d06
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
39 changes: 26 additions & 13 deletions src/main/java/com/ruiyun/jvppeteer/core/NetworkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void addClient(CDPSession client) {
client.on(CDPSession.CDPSessionEvent.Network_loadingFinished, loadingFinished);
listeners.put(CDPSession.CDPSessionEvent.Network_loadingFinished, loadingFinished);

Consumer<LoadingFailedEvent> loadingFailed = this::onLoadingFailed;
Consumer<LoadingFailedEvent> loadingFailed = event -> this.onLoadingFailed(client,event);
client.on(CDPSession.CDPSessionEvent.Network_loadingFailed, loadingFailed);
listeners.put(CDPSession.CDPSessionEvent.Network_loadingFailed, loadingFailed);

Expand Down Expand Up @@ -342,21 +342,22 @@ private void onResponseReceivedExtraInfo(CDPSession client, ResponseReceivedExtr
this.networkEventManager.forgetQueuedEventGroup(event.getRequestId());
this.emitResponseEvent(client, queuedEvents.getResponseReceivedEvent(), event);
if (queuedEvents.getLoadingFinishedEvent() != null) {
this.emitLoadingFinished(queuedEvents.getLoadingFinishedEvent());
this.emitLoadingFinished(client,queuedEvents.getLoadingFinishedEvent());
}
if (queuedEvents.getLoadingFailedEvent() != null) {
this.emitLoadingFailed(queuedEvents.getLoadingFailedEvent());
this.emitLoadingFailed(client,queuedEvents.getLoadingFailedEvent());
}
return;
}
this.networkEventManager.responseExtraInfo(event.getRequestId()).offer(event);
}

private void emitLoadingFailed(LoadingFailedEvent event) {
private void emitLoadingFailed(CDPSession client, LoadingFailedEvent event) {
Request request = this.networkEventManager.getRequest(event.getRequestId());
if (request == null) {
return;
}
this.maybeReassignOOPIFRequestClient(client, request);
request.setFailureText(event.getErrorText());
Response response = request.response();
if (response != null) {
Expand All @@ -366,18 +367,30 @@ private void emitLoadingFailed(LoadingFailedEvent event) {
this.emit(NetworkManagerEvent.RequestFailed, request);
}

private void emitLoadingFinished(LoadingFinishedEvent event) {
private void emitLoadingFinished(CDPSession client,LoadingFinishedEvent event) {
Request request = this.networkEventManager.getRequest(event.getRequestId());
if (request == null) {
return;
}
this.maybeReassignOOPIFRequestClient(client, request);
if (request.response() != null) {
request.response().resolveBody(null);
}
this.forgetRequest(request, true);
this.emit(NetworkManagerEvent.RequestFinished, request);
}

private void maybeReassignOOPIFRequestClient(CDPSession client, Request request) {
// Document requests for OOPIFs start in the parent frame but are adopted by their
// child frame, meaning their loadingFinished and loadingFailed events are fired on
// the child session. In this case we reassign the request CDPSession to ensure all
// subsequent actions use the correct session (e.g. retrieving response body in
// HTTPResponse).
if (client != request.client() && request.isNavigationRequest()) {
request.setClient(client);
}
}


private void onRequest(CDPSession client, RequestWillBeSentEvent event, String fetchRequestId, boolean fromMemoryCache) {
List<Request> redirectChain = new ArrayList<>();
Expand Down Expand Up @@ -412,8 +425,8 @@ private void onRequest(CDPSession client, RequestWillBeSentEvent event, String f
}

//不能阻塞 WebSocketConnectReadThread
private void handleRequestRedirect(CDPSession client, Request request, ResponsePayload responsePayload, ResponseReceivedExtraInfoEvent extraInfo) {
Response response = new Response(client, request, responsePayload, extraInfo);
private void handleRequestRedirect(CDPSession _client, Request request, ResponsePayload responsePayload, ResponseReceivedExtraInfoEvent extraInfo) {
Response response = new Response(request, responsePayload, extraInfo);
request.setResponse(response);
request.redirectChain().add(request);
response.resolveBody("Response body is unavailable for redirect responses,request url: " + request.url());
Expand All @@ -435,12 +448,12 @@ private void forgetRequest(Request request, boolean events) {
}
}

public void onLoadingFinished(CDPSession _client, LoadingFinishedEvent event) {
public void onLoadingFinished(CDPSession client, LoadingFinishedEvent event) {
QueuedEventGroup queuedEvents = this.networkEventManager.getQueuedEventGroup(event.getRequestId());
if (queuedEvents != null) {
queuedEvents.setLoadingFinishedEvent(event);
} else {
this.emitLoadingFinished(event);
this.emitLoadingFinished(client,event);
}
}

Expand All @@ -459,12 +472,12 @@ private void onResponseReceived(CDPSession client, ResponseReceivedEvent event)
this.emitResponseEvent(client, event, extraInfo);
}

public void onLoadingFailed(LoadingFailedEvent event) {
public void onLoadingFailed(CDPSession client,LoadingFailedEvent event) {
QueuedEventGroup queuedEvents = this.networkEventManager.getQueuedEventGroup(event.getRequestId());
if (queuedEvents != null) {
queuedEvents.setLoadingFailedEvent(event);
} else {
this.emitLoadingFailed(event);
this.emitLoadingFailed(client,event);
}
}

Expand All @@ -485,7 +498,7 @@ public void onRequestServedFromCache(CDPSession client, RequestServedFromCacheEv
this.emit(NetworkManagerEvent.RequestServedFromCache, request);
}

public void emitResponseEvent(CDPSession client, ResponseReceivedEvent responseReceived, ResponseReceivedExtraInfoEvent extraInfo) {
public void emitResponseEvent(CDPSession _client, ResponseReceivedEvent responseReceived, ResponseReceivedExtraInfoEvent extraInfo) {
Request request = this.networkEventManager.getRequest(responseReceived.getRequestId());
if (request == null) {
return;
Expand All @@ -497,7 +510,7 @@ public void emitResponseEvent(CDPSession client, ResponseReceivedEvent responseR
if (responseReceived.getResponse().getFromDiskCache()) {
extraInfo = null;
}
Response response = new Response(client, request, responseReceived.getResponse(), extraInfo);
Response response = new Response(request, responseReceived.getResponse(), extraInfo);
request.setResponse(response);
this.emit(NetworkManagerEvent.Response, response);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/ruiyun/jvppeteer/core/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public class Request {

private volatile String id;
private List<Request> redirectChain;

public CDPSession getClient() {
return client;
}

public void setClient(CDPSession client) {
this.client = client;
}

private Response response;
private CDPSession client;
private volatile boolean isNavigationRequest;
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/ruiyun/jvppeteer/core/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

public class Response {

private CDPSession client;
private Request request;
private final AwaitableResult<byte[]> contentResult = AwaitableResult.create();
private final AwaitableResult<String> bodyLoadedResult = AwaitableResult.create();
Expand All @@ -42,8 +41,7 @@ public class Response {
public Response() {
}

public Response(CDPSession client, Request request, ResponsePayload responsePayload, ResponseReceivedExtraInfoEvent extraInfo) {
this.client = client;
public Response(Request request, ResponsePayload responsePayload, ResponseReceivedExtraInfoEvent extraInfo) {
this.request = request;
this.remoteAddress = new RemoteAddress(responsePayload.getRemoteIPAddress(), responsePayload.getRemotePort());
this.statusText = StringUtil.isNotEmpty(this.parseStatusTextFromExtraInfo(extraInfo)) ? this.parseStatusTextFromExtraInfo(extraInfo) : responsePayload.getStatusText();
Expand Down Expand Up @@ -112,7 +110,7 @@ private void getResponseBody() {
Map<String, Object> params = ParamsFactory.create();
params.put("requestId", this.request.id());
try {
JsonNode response = this.client.send("Network.getResponseBody", params);
JsonNode response = this.request.client().send("Network.getResponseBody", params);
if (response != null) {
if (response.get("base64Encoded").asBoolean()) {
this.contentResult.onSuccess(Base64.getDecoder().decode(response.get("body").asText()));
Expand Down Expand Up @@ -169,8 +167,9 @@ public byte[] content() {
if (!this.contentResult.isDone()) {
if (StringUtil.isEmpty(this.bodyLoadedResult.get())) {
this.getResponseBody();
}else {
throw new JvppeteerException(this.bodyLoadedResult.get());
}
throw new JvppeteerException(this.bodyLoadedResult.get());
}
return this.contentResult.get();
}
Expand All @@ -180,7 +179,7 @@ public byte[] content() {
*
* @return 字节数组
*/
public byte[] getContentIgoreRedirect() {
public byte[] getContentForcibly() {
if (!this.contentResult.isDone()) {
this.getResponseBody();
}
Expand Down

0 comments on commit ca19d06

Please sign in to comment.