Skip to content

Commit

Permalink
2020-10-19 修复Request#continueRequest的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyong committed Oct 19, 2020
1 parent c2f2de6 commit 6684650
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,28 @@ public static void main(String[] args) throws Exception {
arrayList.add("--disable-setuid-sandbox");
Browser browser = Puppeteer.launch(options);
Page page = browser.newPage();
page.emulate(Device.IPHONE_X);
// System.out.println("sdaad");

PageNavigateOptions options1 = new PageNavigateOptions();
//如果不设置 domcontentloaded 算页面导航完成的话,那么goTo方法会超时,因为图片请求被拦截了,页面不会达到loaded阶段
options1.setWaitUntil(Collections.singletonList("domcontentloaded"));
page.goTo("https://www.baidu.com/?tn=98012088_10_dg&ch=3",options1);

page.setRequestInterception(true);
//拦截请求
// page.onRequest(request -> {
// if ("image".equals(request.resourceType()) || "media".equals(request.resourceType())) {
// //遇到多媒体或者图片资源请求,拒绝,加载页面加载
// System.out.println(request.url()+": "+request.resourceType()+": abort");
// request.abort();
// } else {//其他资源放行
// request.continueRequest();
// }
// });
// page.setRequestInterception(true);

page.goTo("https://item.taobao.com/item.htm?id=541605195654",options1);
page.onRequest(request -> {
if ("image".equals(request.resourceType())) {
//遇到多媒体或者图片资源请求,拒绝,加载页面加载
System.out.println(request.url()+": "+request.resourceType()+": abort");
request.abort();
} else if(request.url().contains("wd=31")){
request.continueRequest();
}
else{//其他资源放行
request.continueRequest();
}
});


page.goTo("https://www.baidu.com/s?cl=3&tn=baidutop10&fr=top1000&wd=31%E7%9C%81%E5%8C%BA%E5%B8%82%E6%96%B0%E5%A2%9E%E5%A2%83%E5%A4%96%E8%BE%93%E5%85%A513%E4%BE%8B&rsv_idx=2&rsv_dl=fyb_n_homepage&hisfilter=1",options1);

}
}
13 changes: 0 additions & 13 deletions example/src/main/resources/testSelect.html

This file was deleted.

141 changes: 78 additions & 63 deletions src/main/java/com/ruiyun/jvppeteer/core/page/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,24 +197,32 @@ public String failure() {
* @param headers 请求头
* @return Future
*/
public Future<JsonNode> continueRequest(String url, String method, String postData, Map<String, String> headers) {
return Helper.commonExecutor().submit(() -> {
// Request interception is not supported for data: urls.
if (url().startsWith("data:"))
return null;
ValidateUtil.assertArg(isAllowInterception(), "Request Interception is not enabled!");
ValidateUtil.assertArg(!isInterceptionHandled(), "Request is already handled!");
setInterceptionHandled(true);
Map<String, Object> params = new HashMap<>();
params.put("requestId", interceptionId());
public JsonNode continueRequest(String url, String method, String postData, Map<String, String> headers) {
// Request interception is not supported for data: urls.
if (url().startsWith("data:"))
return null;

ValidateUtil.assertArg(isAllowInterception(), "Request Interception is not enabled!");
ValidateUtil.assertArg(!isInterceptionHandled(), "Request is already handled!");

setInterceptionHandled(true);
Map<String, Object> params = new HashMap<>();
params.put("requestId", interceptionId());

if(StringUtil.isNotEmpty(url)) {
params.put("url", url);
}
if(StringUtil.isNotEmpty(method)) {
params.put("method", method);
params.put("postData", postData);
if (headers != null && headers.size() > 0) {
params.put("headers", headersArray(headers));
}
return client.send("Fetch.continueRequest", params, true);
});
}
if(StringUtil.isNotEmpty(postData)){
params.put("postData", new String(Base64.getEncoder().encode(postData.getBytes()),StandardCharsets.UTF_8));
}

if (headers != null && headers.size() > 0) {
params.put("headers", headersArray(headers));
}
return client.send("Fetch.continueRequest", params, true);
}

/**
Expand All @@ -232,40 +240,43 @@ public void continueRequest() {
* @param body 响应体
* @return Future
*/
public Future<JsonNode> respond(int status, Map<String, String> headers, String contentType, String body) {
return Helper.commonExecutor().submit(() -> {
// Mocking responses for dataURL requests is not currently supported.
if (url().startsWith("data:"))
return null;
ValidateUtil.assertArg(allowInterception, "Request Interception is not enabled!");
ValidateUtil.assertArg(!interceptionHandled, "Request is already handled!");
setInterceptionHandled(true);
byte[] responseBody = null;
if (StringUtil.isNotEmpty(body)) {
responseBody = body.getBytes(StandardCharsets.UTF_8);
}
Map<String, String> responseHeaders = new HashMap<>();
if (headers != null && headers.size() > 0) {
for (Map.Entry<String, String> entry : headers.entrySet())
responseHeaders.put(entry.getKey().toLowerCase(), entry.getValue());
}
if (StringUtil.isNotEmpty(contentType)){
responseHeaders.put("content-type", contentType);
}
public JsonNode respond(int status, Map<String, String> headers, String contentType, String body) {
// Mocking responses for dataURL requests is not currently supported.
if (url().startsWith("data:"))
return null;

ValidateUtil.assertArg(allowInterception, "Request Interception is not enabled!");
ValidateUtil.assertArg(!interceptionHandled, "Request is already handled!");

setInterceptionHandled(true);
byte[] responseBody = null;
if (StringUtil.isNotEmpty(body)) {
responseBody = body.getBytes(StandardCharsets.UTF_8);
}
Map<String, String> responseHeaders = new HashMap<>();

if (responseBody != null && !responseHeaders.containsKey("content-length")){
responseHeaders.put("content-length", String.valueOf(responseBody.length));
}
Map<String, Object> params = new HashMap<>();
params.put("requestId", interceptionId);
params.put("responseCode", status);
params.put("responsePhrase", STATUS_TEXTS.get(status));
params.put("responseHeaders", headersArray(responseHeaders));
if (responseBody != null) {
params.put("body", Base64.getDecoder().decode(responseBody));
}
return client.send("Fetch.fulfillRequest", params, true);
});
if (headers != null && headers.size() > 0) {
for (Map.Entry<String, String> entry : headers.entrySet())
responseHeaders.put(entry.getKey().toLowerCase(), entry.getValue());
}

if (StringUtil.isNotEmpty(contentType)){
responseHeaders.put("content-type", contentType);
}

if (responseBody != null && !responseHeaders.containsKey("content-length")){
responseHeaders.put("content-length", String.valueOf(responseBody.length));
}

Map<String, Object> params = new HashMap<>();
params.put("requestId", interceptionId);
params.put("responseCode", status);
params.put("responsePhrase", STATUS_TEXTS.get(status));
params.put("responseHeaders", headersArray(responseHeaders));
if (responseBody != null) {
params.put("body", Base64.getDecoder().decode(responseBody));
}
return client.send("Fetch.fulfillRequest", params, true);
}

/**
Expand All @@ -274,20 +285,21 @@ public Future<JsonNode> respond(int status, Map<String, String> headers, String
* @param errorCode errorCode错误码
* @return Future
*/
public Future<JsonNode> abort(ErrorCode errorCode) {
return Helper.commonExecutor().submit(() -> {
// Request interception is not supported for data: urls.
if (url().startsWith("data:"))
return null;
String errorReason = errorCode.getName();
ValidateUtil.assertArg(allowInterception, "Request Interception is not enabled!");
ValidateUtil.assertArg(!interceptionHandled, "Request is already handled!");
setInterceptionHandled(true);
Map<String, Object> params = new HashMap<>();
params.put("requestId", interceptionId);
params.put("errorReason", errorReason);
return client.send("Fetch.failRequest", params, true);
});
public JsonNode abort(ErrorCode errorCode) {
// Request interception is not supported for data: urls.
if (url().startsWith("data:"))
return null;

String errorReason = errorCode.getName();
ValidateUtil.assertArg(allowInterception, "Request Interception is not enabled!");
ValidateUtil.assertArg(!interceptionHandled, "Request is already handled!");

setInterceptionHandled(true);
Map<String, Object> params = new HashMap<>();
params.put("requestId", interceptionId);
params.put("errorReason", errorReason);
return client.send("Fetch.failRequest", params, true);

}

private List<HeaderEntry> headersArray(Map<String, String> headers) {
Expand All @@ -301,6 +313,9 @@ private List<HeaderEntry> headersArray(Map<String, String> headers) {
return result;
}

/**
* 截断请求
*/
public void abort() {
this.abort(ErrorCode.FAILED);
}
Expand Down

0 comments on commit 6684650

Please sign in to comment.