org.cobraparser.html.js.XMLHttpRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Cobra Show documentation
Show all versions of Cobra Show documentation
Cobra is the rendering engine designed for LoboBrowser
package org.cobraparser.html.js;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.jdt.annotation.NonNull;
import org.cobraparser.html.js.Window.JSRunnableTask;
import org.cobraparser.js.AbstractScriptableDelegate;
import org.cobraparser.js.JavaScript;
import org.cobraparser.ua.NetworkRequest;
import org.cobraparser.ua.UserAgentContext;
import org.cobraparser.ua.UserAgentContext.Request;
import org.cobraparser.ua.UserAgentContext.RequestKind;
import org.cobraparser.util.DOMExceptions;
import org.cobraparser.util.Urls;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
public class XMLHttpRequest extends AbstractScriptableDelegate {
// TODO: See reference:
// http://www.xulplanet.com/references/objref/XMLHttpRequest.html
private static final Logger logger = Logger.getLogger(XMLHttpRequest.class.getName());
private final NetworkRequest request;
private final UserAgentContext pcontext;
private final Scriptable scope;
private final URL codeSource;
// TODO: This is a quick hack
private final Window window;
public XMLHttpRequest(final UserAgentContext pcontext, final URL codeSource, final Scriptable scope, final Window window) {
this.request = pcontext.createHttpRequest();
this.pcontext = pcontext;
this.scope = scope;
this.codeSource = codeSource;
this.window = window;
}
public void abort() {
request.abort();
}
// excluded as per https://dvcs.w3.org/hg/xhr/raw-file/default/xhr-1/Overview.html
private static final List excludedResponseHeadersLowerCase = Arrays.asList(
"set-cookie",
"set-cookie2"
);
@NotGetterSetter
public String getAllResponseHeaders() {
// TODO: Need to also filter out based on CORS
return request.getAllResponseHeaders(excludedResponseHeadersLowerCase);
}
public int getReadyState() {
return request.getReadyState();
}
public byte[] getResponseBytes() {
return request.getResponseBytes();
}
public String getResponseHeader(final String headerName) {
// TODO: Need to also filter out based on CORS
if (excludedResponseHeadersLowerCase.contains(headerName.toLowerCase())) {
return request.getResponseHeader(headerName);
} else {
return null;
}
}
public String getResponseText() {
return request.getResponseText();
}
public Document getResponseXML() {
return request.getResponseXML();
}
public int getStatus() {
return request.getStatus();
}
public String getStatusText() {
return request.getStatusText();
}
private @NonNull URL getFullURL(final String relativeUrl) throws MalformedURLException {
return Urls.createURL(this.codeSource, relativeUrl);
}
public void open(final String method, final String url, final boolean asyncFlag, final String userName, final String password)
throws IOException {
final String adjustedMethod = checkAndAdjustMethod(method);
try {
request.open(adjustedMethod, this.getFullURL(url), asyncFlag, userName, password);
} catch (final MalformedURLException mfe) {
throw ScriptRuntime.typeError("url malformed");
}
}
private static String[] prohibitedMethods = {
"CONNECT", "TRACE", "TRACK"
};
private static String[] upperCaseMethods = {
"DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT"
};
private static String checkAndAdjustMethod(final String method) {
for (final String p : prohibitedMethods) {
if (p.equalsIgnoreCase(method)) {
throw DOMExceptions.ExtendedError.SecurityError.createException();
}
}
for (final String u : upperCaseMethods) {
if (u.equalsIgnoreCase(method)) {
return u;
}
}
return method;
}
public void open(final String method, final String url, final boolean asyncFlag, final String userName) throws IOException {
final String adjustedMethod = checkAndAdjustMethod(method);
request.open(adjustedMethod, this.getFullURL(url), asyncFlag, userName);
}
public void open(final String method, final String url, final boolean asyncFlag) throws IOException {
final String adjustedMethod = checkAndAdjustMethod(method);
request.open(adjustedMethod, this.getFullURL(url), asyncFlag);
}
public void open(final String method, final String url) throws IOException {
final String adjustedMethod = checkAndAdjustMethod(method);
request.open(adjustedMethod, this.getFullURL(url));
}
public void send(final String content) throws IOException {
final Optional urlOpt = request.getURL();
if (urlOpt.isPresent()) {
final URL url = urlOpt.get();
if (isSameOrigin(url, codeSource)) {
// final URLPermission urlPermission = new URLPermission(url.toExternalForm());
// final SocketPermission socketPermission = new SocketPermission(url.getHost() + ":" + Urls.getPort(url), "connect,resolve");
// final StoreHostPermission storeHostPermission = StoreHostPermission.forURL(url);
final PrivilegedExceptionAction