|
43 | 43 | import java.util.concurrent.ConcurrentHashMap; |
44 | 44 |
|
45 | 45 | import org.apache.commons.lang3.StringUtils; |
| 46 | +import org.apache.commons.lang3.Strings; |
46 | 47 | import org.apache.commons.logging.Log; |
47 | 48 | import org.apache.commons.logging.LogFactory; |
48 | 49 | import org.htmlunit.Cache; |
@@ -938,7 +939,7 @@ public ScriptResult executeJavaScript(String sourceCode, final String sourceName |
938 | 939 | return new ScriptResult(JavaScriptEngine.UNDEFINED); |
939 | 940 | } |
940 | 941 |
|
941 | | - if (StringUtils.startsWithIgnoreCase(sourceCode, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) { |
| 942 | + if (Strings.CI.startsWith(sourceCode, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) { |
942 | 943 | sourceCode = sourceCode.substring(JavaScriptURLConnection.JAVASCRIPT_PREFIX.length()).trim(); |
943 | 944 | if (sourceCode.startsWith("return ")) { |
944 | 945 | sourceCode = sourceCode.substring("return ".length()); |
@@ -1425,23 +1426,45 @@ private void executeRefreshIfNeeded() throws IOException { |
1425 | 1426 | } |
1426 | 1427 | } |
1427 | 1428 |
|
1428 | | - final int timeRounded = (int) time; |
1429 | | - checkRecursion(); |
1430 | | - getWebClient().getRefreshHandler().handleRefresh(this, url, timeRounded); |
| 1429 | + processRefresh(url, time); |
1431 | 1430 | } |
1432 | 1431 |
|
1433 | | - private void checkRecursion() { |
1434 | | - final StackTraceElement[] elements = new Exception().getStackTrace(); |
1435 | | - if (elements.length > 500) { |
1436 | | - for (int i = 0; i < 500; i++) { |
1437 | | - if (!elements[i].getClassName().startsWith("org.htmlunit.")) { |
1438 | | - return; |
1439 | | - } |
1440 | | - } |
| 1432 | + // this is different from what is done in org.htmlunit.WebClient.loadWebResponseFromWebConnection(WebRequest, int) |
| 1433 | + // because there we are directly replacing the response before loading the response into the window |
| 1434 | + // here we are replacing the page in the window (maybe after some time) |
| 1435 | + private void processRefresh(final URL url, final double time) throws IOException { |
| 1436 | + final WebClient webClient = getWebClient(); |
| 1437 | + |
| 1438 | + final int refreshLimit = webClient.getOptions().getPageRefreshLimit(); |
| 1439 | + if (refreshLimit == 0) { |
1441 | 1440 | final WebResponse webResponse = getWebResponse(); |
1442 | | - throw new FailingHttpStatusCodeException("Too much redirect for " |
| 1441 | + throw new FailingHttpStatusCodeException("Too many redirects for " |
1443 | 1442 | + webResponse.getWebRequest().getUrl(), webResponse); |
1444 | 1443 | } |
| 1444 | + |
| 1445 | + if (refreshLimit >= 0) { |
| 1446 | + final StackTraceElement[] elements = new Exception().getStackTrace(); |
| 1447 | + int count = 0; |
| 1448 | + final int elementCountLimit = refreshLimit > 50 ? 400 : refreshLimit > 10 ? 80 : 5; |
| 1449 | + final int elementCount = elements.length; |
| 1450 | + |
| 1451 | + if (elementCount > elementCountLimit) { |
| 1452 | + for (int i = 0; i < elementCount; i++) { |
| 1453 | + if ("processRefresh".equals(elements[i].getMethodName()) |
| 1454 | + && "org.htmlunit.html.HtmlPage".equals(elements[i].getClassName())) { |
| 1455 | + count++; |
| 1456 | + if (count >= refreshLimit) { |
| 1457 | + final WebResponse webResponse = getWebResponse(); |
| 1458 | + throw new FailingHttpStatusCodeException( |
| 1459 | + "Too many redirects (>= " + count + ") for " |
| 1460 | + + webResponse.getWebRequest().getUrl(), webResponse); |
| 1461 | + } |
| 1462 | + } |
| 1463 | + } |
| 1464 | + } |
| 1465 | + } |
| 1466 | + |
| 1467 | + webClient.getRefreshHandler().handleRefresh(this, url, (int) time); |
1445 | 1468 | } |
1446 | 1469 |
|
1447 | 1470 | /** |
|
0 commit comments