Search in sources :

Example 1 with Script

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;
}
Also used : URL(java.net.URL) Script(net.sourceforge.htmlunit.corejs.javascript.Script) StringUtils(org.apache.commons.lang3.StringUtils) FormEncodingType(com.gargoylesoftware.htmlunit.FormEncodingType) ArrayList(java.util.ArrayList) WebConnection(com.gargoylesoftware.htmlunit.WebConnection) ISO_8859_1(java.nio.charset.StandardCharsets.ISO_8859_1) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) HttpMethod(com.gargoylesoftware.htmlunit.HttpMethod) OutputStream(java.io.OutputStream) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) EOFException(java.io.EOFException) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) WebResponseData(com.gargoylesoftware.htmlunit.WebResponseData) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) Log(org.apache.commons.logging.Log) Pattern(java.util.regex.Pattern) LogFactory(org.apache.commons.logging.LogFactory) InputStream(java.io.InputStream) Script(net.sourceforge.htmlunit.corejs.javascript.Script) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) ArrayList(java.util.ArrayList) IOException(java.io.IOException) EOFException(java.io.EOFException) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) WebResponseData(com.gargoylesoftware.htmlunit.WebResponseData)

Example 2 with Script

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);
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) Script(net.sourceforge.htmlunit.corejs.javascript.Script) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) Test(org.junit.jupiter.api.Test)

Example 3 with Script

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);
    }
}
Also used : AbstractJavaScriptConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.AbstractJavaScriptConfiguration) WindowOrWorkerGlobalScopeMixin(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScopeMixin) URL(java.net.URL) Script(net.sourceforge.htmlunit.corejs.javascript.Script) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) FF(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF) EDGE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE) ClassConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) FF_ESR(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) MimeType(com.gargoylesoftware.htmlunit.util.MimeType) Context(net.sourceforge.htmlunit.corejs.javascript.Context) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) CHROME(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME) IOException(java.io.IOException) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) JsxClass(com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass) List(java.util.List) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) IE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE) WindowOrWorkerGlobalScope(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScope) Log(org.apache.commons.logging.Log) WebClient(com.gargoylesoftware.htmlunit.WebClient) LogFactory(org.apache.commons.logging.LogFactory) Undefined(net.sourceforge.htmlunit.corejs.javascript.Undefined) Function(net.sourceforge.htmlunit.corejs.javascript.Function) JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL(com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) EventTarget(com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) Script(net.sourceforge.htmlunit.corejs.javascript.Script) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) URL(java.net.URL) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 4 with Script

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;
}
Also used : Script(net.sourceforge.htmlunit.corejs.javascript.Script) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) Charset(java.nio.charset.Charset) IOException(java.io.IOException) WebClient(com.gargoylesoftware.htmlunit.WebClient) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) Cache(com.gargoylesoftware.htmlunit.Cache)

Example 5 with Script

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);
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) Script(net.sourceforge.htmlunit.corejs.javascript.Script) ArrayList(java.util.ArrayList) Evaluator(net.sourceforge.htmlunit.corejs.javascript.Evaluator) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) ErrorReporter(net.sourceforge.htmlunit.corejs.javascript.ErrorReporter) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) Test(org.junit.jupiter.api.Test)

Aggregations

Script (net.sourceforge.htmlunit.corejs.javascript.Script)5 ContextAction (net.sourceforge.htmlunit.corejs.javascript.ContextAction)4 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)3 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)3 IOException (java.io.IOException)3 Context (net.sourceforge.htmlunit.corejs.javascript.Context)3 ContextFactory (net.sourceforge.htmlunit.corejs.javascript.ContextFactory)3 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)2 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)2 JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL (com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL)1 Cache (com.gargoylesoftware.htmlunit.Cache)1 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 FormEncodingType (com.gargoylesoftware.htmlunit.FormEncodingType)1 HttpMethod (com.gargoylesoftware.htmlunit.HttpMethod)1 WebConnection (com.gargoylesoftware.htmlunit.WebConnection)1 WebResponseData (com.gargoylesoftware.htmlunit.WebResponseData)1