use of net.sourceforge.htmlunit.corejs.javascript.Script in project htmlunit by HtmlUnit.
the class DebuggingWebConnection method uncompressJavaScript.
/**
* Tries to uncompress the JavaScript code in the provided response.
* @param response the response to uncompress
* @return a new response with uncompressed JavaScript code or the original response in case of failure
*/
protected WebResponse uncompressJavaScript(final WebResponse response) {
final WebRequest request = response.getWebRequest();
final String scriptName = request.getUrl().toString();
final String scriptSource = response.getContentAsString();
// skip if it is already formatted? => TODO
final ContextFactory factory = new ContextFactory();
final ContextAction<Object> action = cx -> {
cx.setOptimizationLevel(-1);
final Script script = cx.compileString(scriptSource, scriptName, 0, null);
return cx.decompileScript(script, 4);
};
try {
final String decompileScript = (String) factory.call(action);
final List<NameValuePair> responseHeaders = new ArrayList<>(response.getResponseHeaders());
for (int i = responseHeaders.size() - 1; i >= 0; i--) {
if ("content-encoding".equalsIgnoreCase(responseHeaders.get(i).getName())) {
responseHeaders.remove(i);
}
}
final WebResponseData wrd = new WebResponseData(decompileScript.getBytes(), response.getStatusCode(), response.getStatusMessage(), responseHeaders);
return new WebResponse(wrd, response.getWebRequest().getUrl(), response.getWebRequest().getHttpMethod(), response.getLoadTime());
} catch (final Exception e) {
LOG.warn("Failed to decompress JavaScript response. Delivering as it.", e);
}
return response;
}
use of net.sourceforge.htmlunit.corejs.javascript.Script in project htmlunit-core-js by HtmlUnit.
the class DecompileTest method newObject0Arg.
/**
* As of head of trunk on 30.09.09, decompile of "new Date()" returns "new Date" without parentheses.
* @see <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=519692">Bug 519692</a>
*/
@Test
public void newObject0Arg() {
final String source = "var x = new Date().getTime();";
final ContextAction<Object> action = new ContextAction<Object>() {
@Override
public Object run(final Context cx) {
final Script script = cx.compileString(source, "my script", 0, null);
assertEquals(source, cx.decompileScript(script, 4).trim());
return null;
}
};
Utils.runWithAllOptimizationLevels(action);
}
use of net.sourceforge.htmlunit.corejs.javascript.Script in project htmlunit by HtmlUnit.
the class WorkerJob method loadAndExecute.
void loadAndExecute(final WebClient webClient, final String url, final Context context, final boolean checkMimeType) throws IOException {
final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
final URL fullUrl = page.getFullyQualifiedUrl(url);
final WebRequest webRequest = new WebRequest(fullUrl);
final WebResponse response = webClient.loadWebResponse(webRequest);
if (checkMimeType && !MimeType.isJavascriptMimeType(response.getContentType())) {
throw Context.reportRuntimeError("NetworkError: importScripts response is not a javascript response");
}
final String scriptCode = response.getContentAsString();
final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) webClient.getJavaScriptEngine();
final DedicatedWorkerGlobalScope thisScope = this;
final ContextAction<Object> action = cx -> {
final Script script = javaScriptEngine.compile(page, thisScope, scriptCode, fullUrl.toExternalForm(), 1);
// script might be null here e.g. if there is a syntax error
if (script != null) {
return javaScriptEngine.execute(page, thisScope, script);
}
return null;
};
final ContextFactory cf = javaScriptEngine.getContextFactory();
if (context != null) {
action.run(context);
} else {
final JavaScriptJob job = new WorkerJob(cf, action, "loadAndExecute " + url);
owningWindow_.getWebWindow().getJobManager().addJob(job, page);
}
}
use of net.sourceforge.htmlunit.corejs.javascript.Script in project htmlunit by HtmlUnit.
the class HtmlPage method loadJavaScriptFromUrl.
/**
* Loads JavaScript from the specified URL. This method may return {@code null} if
* there is a problem loading the code from the specified URL.
*
* @param url the URL of the script
* @param scriptCharset the charset from the script tag
* @return the content of the file, or {@code null} if we ran into a compile error
* @throws IOException if there is a problem downloading the JavaScript file
* @throws FailingHttpStatusCodeException if the request's status code indicates a request
* failure and the {@link WebClient} was configured to throw exceptions on failing
* HTTP status codes
*/
private Object loadJavaScriptFromUrl(final URL url, final Charset scriptCharset) throws IOException, FailingHttpStatusCodeException {
final WebRequest referringRequest = getWebResponse().getWebRequest();
final WebClient client = getWebClient();
final WebRequest request = new WebRequest(url);
// copy all headers from the referring request
request.setAdditionalHeaders(new HashMap<>(referringRequest.getAdditionalHeaders()));
// at least overwrite this headers
final BrowserVersion browserVersion = client.getBrowserVersion();
request.setAdditionalHeader(HttpHeader.ACCEPT, client.getBrowserVersion().getScriptAcceptHeader());
if (browserVersion.hasFeature(HTTP_HEADER_SEC_FETCH)) {
request.setAdditionalHeader(HttpHeader.SEC_FETCH_SITE, "same-origin");
request.setAdditionalHeader(HttpHeader.SEC_FETCH_MODE, "no-cors");
request.setAdditionalHeader(HttpHeader.SEC_FETCH_DEST, "script");
}
request.setRefererlHeader(referringRequest.getUrl());
request.setCharset(scriptCharset);
// our cache is a bit strange;
// loadWebResponse check the cache for the web response
// AND also fixes the request url for the following cache lookups
final WebResponse response = client.loadWebResponse(request);
// now we can look into the cache with the fixed request for
// a cached script
final Cache cache = client.getCache();
final Object cachedScript = cache.getCachedObject(request);
if (cachedScript instanceof Script) {
return cachedScript;
}
client.printContentIfNecessary(response);
client.throwFailingHttpStatusCodeExceptionIfNecessary(response);
final int statusCode = response.getStatusCode();
if (statusCode == HttpStatus.SC_NO_CONTENT) {
throw new FailingHttpStatusCodeException(response);
}
if (statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_MULTIPLE_CHOICES) {
throw new IOException("Unable to download JavaScript from '" + url + "' (status " + statusCode + ").");
}
// http://www.ietf.org/rfc/rfc4329.txt
final String contentType = response.getContentType();
if (!MimeType.APPLICATION_JAVASCRIPT.equalsIgnoreCase(contentType) && !"application/ecmascript".equalsIgnoreCase(contentType)) {
// warn about obsolete or not supported content types
if ("text/javascript".equals(contentType) || "text/ecmascript".equals(contentType) || "application/x-javascript".equalsIgnoreCase(contentType)) {
getWebClient().getIncorrectnessListener().notify("Obsolete content type encountered: '" + contentType + "'.", this);
} else {
getWebClient().getIncorrectnessListener().notify("Expected content type of 'application/javascript' or 'application/ecmascript' for " + "remotely loaded JavaScript element at '" + url + "', " + "but got '" + contentType + "'.", this);
}
}
Charset scriptEncoding = Charset.forName("windows-1252");
final boolean ignoreBom;
final Charset contentCharset = EncodingSniffer.sniffEncodingFromHttpHeaders(response.getResponseHeaders());
if (contentCharset == null) {
// use info from script tag or fall back to utf-8
if (scriptCharset != null && ISO_8859_1 != scriptCharset) {
ignoreBom = true;
scriptEncoding = scriptCharset;
} else {
ignoreBom = ISO_8859_1 != scriptCharset;
}
} else if (ISO_8859_1 == contentCharset) {
ignoreBom = true;
} else {
ignoreBom = true;
scriptEncoding = contentCharset;
}
final String scriptCode = response.getContentAsString(scriptEncoding, ignoreBom && getWebClient().getBrowserVersion().hasFeature(JS_IGNORES_UTF8_BOM_SOMETIMES));
if (null != scriptCode) {
final AbstractJavaScriptEngine<?> javaScriptEngine = client.getJavaScriptEngine();
final Object script = javaScriptEngine.compile(this, scriptCode, url.toExternalForm(), 1);
if (script != null && cache.cacheIfPossible(request, response, script)) {
// no cleanup if the response is stored inside the cache
return script;
}
response.cleanUp();
return script;
}
response.cleanUp();
return null;
}
use of net.sourceforge.htmlunit.corejs.javascript.Script in project htmlunit-core-js by HtmlUnit.
the class ContextMethodsTest method captureEvalScript.
/**
* When {@link Context#compileString(String, String, int, Object)} is protected and not final,
* we can capture code passed to eval.
* @throws Exception if the test fails
*/
@Test
public void captureEvalScript() throws Exception {
final List<String> compiled = new ArrayList<>();
final ContextFactory cf = new ContextFactory() {
@Override
protected Context makeContext() {
return new Context(this) {
@Override
protected Script compileString(String source, Evaluator compiler, ErrorReporter compilationErrorReporter, String sourceName, int lineno, Object securityDomain) {
compiled.add(source);
return super.compileString(source, compiler, compilationErrorReporter, sourceName, lineno, securityDomain);
}
};
}
};
final String source = "eval('1 + 2')";
final ContextAction<Object> action = new ContextAction<Object>() {
@Override
public Object run(Context cx) {
final Scriptable scope = cx.initSafeStandardObjects();
final Script script = cx.compileString(source, "", 1, (Object) null);
return script.exec(cx, scope);
}
};
cf.call(action);
final String[] expected = { "eval('1 + 2')", "1 + 2" };
assertEquals(Arrays.asList(expected), compiled);
}
Aggregations