<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eas.server.httpservlet;

import com.eas.util.BinaryUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;
import org.mozilla.javascript.UniqueTag;

/**
 *
 * @author vv
 */
public class HttpScriptContext extends ScriptableObject {

    private final static String REQUEST_PROP_NAME = "request";//NOI18N
    private final static String RESPONSE_PROP_NAME = "response";//NOI18N
    private final static String HTTP_JS_CLASS_NAME = "Http";//NOI18N
    private Request request;
    private Response response;

    public static HttpScriptContext getInstance(Scriptable scope, HttpServletRequest aHttpRequest, HttpServletResponse aHttpResponse) {
        HttpScriptContext sc = new HttpScriptContext();
        ScriptRuntime.setBuiltinProtoAndParent(sc, scope, TopLevel.Builtins.Object);
        sc.request = Request.getInstance(sc, aHttpRequest);
        sc.response = Response.getInstance(sc, aHttpResponse);
        sc.defineProperty(REQUEST_PROP_NAME, HttpScriptContext.class, READONLY);
        sc.defineProperty(RESPONSE_PROP_NAME, HttpScriptContext.class, READONLY);
        return sc;
    }

    public Request getRequest() {
        return request;
    }

    public Response getResponse() {
        return response;
    }

    @Override
    public String getClassName() {
        return HTTP_JS_CLASS_NAME;
    }

    public static class Request extends ScriptableObject {

        private final static String AUTH_TYPE_PROP_NAME = "authType";//NOI18N
        private final static String CHARACTER_ENCODING_PROP_NAME = "characterEncoding";//NOI18N
        private final static String CONTENT_LENGTH_PROP_NAME = "contentLength";//NOI18N
        private final static String CONTENT_TYPE_PROP_NAME = "contentType";//NOI18N
        private final static String BODY_PROP_NAME = "body";//NOI18N
        private final static String BODY_BUFFER_PROP_NAME = "bodyBuffer";//NOI18N
        private final static String CONTEXT_PATH_PROP_NAME = "contextPath";//NOI18N
        private final static String COOKIES_PROP_NAME = "cookies";//NOI18N
        private final static String HEADERS_PROP_NAME = "headers";//NOI18N
        private final static String LOCAL_ADDR_PROP_NAME = "localAddr";//NOI18N
        private final static String LOCAL_NAME_PROP_NAME = "localName";//NOI18N
        private final static String LOCAL_PORT_PROP_NAME = "localPort";//NOI18N
        private final static String METHOD_PROP_NAME = "method";//NOI18N
        private final static String PARAMS_PROP_NAME = "params";//NOI18N
        private final static String PATH_INFO_PROP_NAME = "pathInfo";//NOI18N
        private final static String PATH_TRANSLATED_PROP_NAME = "pathTranslated";//NOI18N
        private final static String PROTOCOL_PROP_NAME = "protocol";//NOI18N
        private final static String QUERY_STRING_PROP_NAME = "queryString";//NOI18N
        private final static String REMOTE_ADDR_PROP_NAME = "remoteAddr";//NOI18N
        private final static String REMOTE_HOST_PROP_NAME = "remoteHost";//NOI18N
        private final static String REMOTE_PORT_PROP_NAME = "remotePort";//NOI18N
        private final static String REQUEST_URI_PROP_NAME = "requestURI";//NOI18N
        private final static String REQUEST_URL_PROP_NAME = "requestURL";//NOI18N
        private final static String SCHEME_PROP_NAME = "scheme";//NOI18N
        private final static String SERVER_NAME_PROP_NAME = "serverName";//NOI18N
        private final static String SERVER_PORT_PROP_NAME = "serverPort";//NOI18N
        private final static String SECURE_PROP_NAME = "secure";//NOI18N
        private final static String REQUEST_JS_CLASS_NAME = "Request";//NOI18N
        private HttpServletRequest httpRequest;
        private Cookies cookies;
        private RequestHeaders headers;
        private Params params;

        public static Request getInstance(Scriptable scope, HttpServletRequest aHttpRequest) {
            Request r = new Request();
            r.httpRequest = aHttpRequest;
            r.defineProperty(AUTH_TYPE_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(CHARACTER_ENCODING_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(CONTENT_LENGTH_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(CONTENT_TYPE_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(BODY_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(BODY_BUFFER_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(CONTEXT_PATH_PROP_NAME, HttpScriptContext.Request.class, READONLY);

            r.cookies = Cookies.getInstance(scope, r.httpRequest);
            r.defineProperty(COOKIES_PROP_NAME, HttpScriptContext.Request.class, READONLY);

            r.headers = RequestHeaders.getInstance(scope, aHttpRequest);
            r.defineProperty(HEADERS_PROP_NAME, HttpScriptContext.Request.class, READONLY);

            r.defineProperty(LOCAL_ADDR_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(LOCAL_NAME_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(LOCAL_PORT_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(METHOD_PROP_NAME, HttpScriptContext.Request.class, READONLY);

            r.params = Params.getInstance(scope, r.httpRequest);
            r.defineProperty(PARAMS_PROP_NAME, HttpScriptContext.Request.class, READONLY);

            r.defineProperty(PATH_INFO_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(PATH_TRANSLATED_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(PROTOCOL_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(QUERY_STRING_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(REMOTE_ADDR_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(REMOTE_HOST_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(REMOTE_PORT_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(REQUEST_URI_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(REQUEST_URL_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(SCHEME_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(SERVER_NAME_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(SERVER_PORT_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(SECURE_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            ScriptRuntime.setBuiltinProtoAndParent(r, scope, TopLevel.Builtins.Object);
            return r;
        }

        public String getAuthType() {
            return httpRequest.getAuthType();
        }

        public String getCharacterEncoding() {
            return httpRequest.getCharacterEncoding();
        }

        public int getContentLength() {
            return httpRequest.getContentLength();
        }

        public String getContentType() {
            return httpRequest.getContentType();
        }

        public String getBody() throws IOException {
            String encoding = httpRequest.getCharacterEncoding();
            if (encoding == null || encoding.isEmpty()) {
                Logger.getLogger(HttpScriptContext.class.getName()).log(Level.WARNING, "Missing character encoding. Falling back to utf-8.");
                encoding = "utf-8";
            }
            if (Charset.isSupported(encoding)) {
                try (InputStream is = httpRequest.getInputStream()) {
                    byte[] data = BinaryUtils.readStream(is, -1);
                    return new String(data, httpRequest.getCharacterEncoding());
                }
            } else {
                throw new IOException(String.format("Character encoding %s is not supported.", encoding));
            }
        }

        public byte[] getBodyBuffer() throws IOException {
            try (InputStream is = httpRequest.getInputStream()) {<commit_msg>Bug fix<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eas.server.httpservlet;

import com.eas.util.BinaryUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;
import org.mozilla.javascript.UniqueTag;

/**
 *
 * @author vv
 */
public class HttpScriptContext extends ScriptableObject {

    private final static String REQUEST_PROP_NAME = "request";//NOI18N
    private final static String RESPONSE_PROP_NAME = "response";//NOI18N
    private final static String HTTP_JS_CLASS_NAME = "Http";//NOI18N
    private Request request;
    private Response response;

    public static HttpScriptContext getInstance(Scriptable scope, HttpServletRequest aHttpRequest, HttpServletResponse aHttpResponse) {
        HttpScriptContext sc = new HttpScriptContext();
        ScriptRuntime.setBuiltinProtoAndParent(sc, scope, TopLevel.Builtins.Object);
        sc.request = Request.getInstance(sc, aHttpRequest);
        sc.response = Response.getInstance(sc, aHttpResponse);
        sc.defineProperty(REQUEST_PROP_NAME, HttpScriptContext.class, READONLY);
        sc.defineProperty(RESPONSE_PROP_NAME, HttpScriptContext.class, READONLY);
        return sc;
    }

    public Request getRequest() {
        return request;
    }

    public Response getResponse() {
        return response;
    }

    @Override
    public String getClassName() {
        return HTTP_JS_CLASS_NAME;
    }

    public static class Request extends ScriptableObject {

        private final static String AUTH_TYPE_PROP_NAME = "authType";//NOI18N
        private final static String CHARACTER_ENCODING_PROP_NAME = "characterEncoding";//NOI18N
        private final static String CONTENT_LENGTH_PROP_NAME = "contentLength";//NOI18N
        private final static String CONTENT_TYPE_PROP_NAME = "contentType";//NOI18N
        private final static String BODY_PROP_NAME = "body";//NOI18N
        private final static String BODY_BUFFER_PROP_NAME = "bodyBuffer";//NOI18N
        private final static String CONTEXT_PATH_PROP_NAME = "contextPath";//NOI18N
        private final static String COOKIES_PROP_NAME = "cookies";//NOI18N
        private final static String HEADERS_PROP_NAME = "headers";//NOI18N
        private final static String LOCAL_ADDR_PROP_NAME = "localAddr";//NOI18N
        private final static String LOCAL_NAME_PROP_NAME = "localName";//NOI18N
        private final static String LOCAL_PORT_PROP_NAME = "localPort";//NOI18N
        private final static String METHOD_PROP_NAME = "method";//NOI18N
        private final static String PARAMS_PROP_NAME = "params";//NOI18N
        private final static String PATH_INFO_PROP_NAME = "pathInfo";//NOI18N
        private final static String PATH_TRANSLATED_PROP_NAME = "pathTranslated";//NOI18N
        private final static String PROTOCOL_PROP_NAME = "protocol";//NOI18N
        private final static String QUERY_STRING_PROP_NAME = "queryString";//NOI18N
        private final static String REMOTE_ADDR_PROP_NAME = "remoteAddr";//NOI18N
        private final static String REMOTE_HOST_PROP_NAME = "remoteHost";//NOI18N
        private final static String REMOTE_PORT_PROP_NAME = "remotePort";//NOI18N
        private final static String REQUEST_URI_PROP_NAME = "requestURI";//NOI18N
        private final static String REQUEST_URL_PROP_NAME = "requestURL";//NOI18N
        private final static String SCHEME_PROP_NAME = "scheme";//NOI18N
        private final static String SERVER_NAME_PROP_NAME = "serverName";//NOI18N
        private final static String SERVER_PORT_PROP_NAME = "serverPort";//NOI18N
        private final static String SECURE_PROP_NAME = "secure";//NOI18N
        private final static String REQUEST_JS_CLASS_NAME = "Request";//NOI18N
        private HttpServletRequest httpRequest;
        private Cookies cookies;
        private RequestHeaders headers;
        private Params params;

        public static Request getInstance(Scriptable scope, HttpServletRequest aHttpRequest) {
            Request r = new Request();
            r.httpRequest = aHttpRequest;
            r.defineProperty(AUTH_TYPE_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(CHARACTER_ENCODING_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(CONTENT_LENGTH_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(CONTENT_TYPE_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(BODY_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(BODY_BUFFER_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(CONTEXT_PATH_PROP_NAME, HttpScriptContext.Request.class, READONLY);

            r.cookies = Cookies.getInstance(scope, r.httpRequest);
            r.defineProperty(COOKIES_PROP_NAME, HttpScriptContext.Request.class, READONLY);

            r.headers = RequestHeaders.getInstance(scope, aHttpRequest);
            r.defineProperty(HEADERS_PROP_NAME, HttpScriptContext.Request.class, READONLY);

            r.defineProperty(LOCAL_ADDR_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(LOCAL_NAME_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(LOCAL_PORT_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(METHOD_PROP_NAME, HttpScriptContext.Request.class, READONLY);

            r.params = Params.getInstance(scope, r.httpRequest);
            r.defineProperty(PARAMS_PROP_NAME, HttpScriptContext.Request.class, READONLY);

            r.defineProperty(PATH_INFO_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(PATH_TRANSLATED_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(PROTOCOL_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(QUERY_STRING_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(REMOTE_ADDR_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(REMOTE_HOST_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(REMOTE_PORT_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(REQUEST_URI_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(REQUEST_URL_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(SCHEME_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(SERVER_NAME_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(SERVER_PORT_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            r.defineProperty(SECURE_PROP_NAME, HttpScriptContext.Request.class, READONLY);
            ScriptRuntime.setBuiltinProtoAndParent(r, scope, TopLevel.Builtins.Object);
            return r;
        }

        public String getAuthType() {
            return httpRequest.getAuthType();
        }

        public String getCharacterEncoding() {
            return httpRequest.getCharacterEncoding();
        }

        public int getContentLength() {
            return httpRequest.getContentLength();
        }

        public String getContentType() {
            return httpRequest.getContentType();
        }

        public String getBody() throws IOException {
            String encoding = httpRequest.getCharacterEncoding();
            if (encoding == null || encoding.isEmpty()) {
                Logger.getLogger(HttpScriptContext.class.getName()).log(Level.WARNING, "Missing character encoding. Falling back to utf-8.");
                encoding = "utf-8";
            }
            if (Charset.isSupported(encoding)) {
                try (InputStream is = httpRequest.getInputStream()) {
                    byte[] data = BinaryUtils.readStream(is, -1);
                    return new String(data, encoding);
                }
            } else {
                throw new IOException(String.format("Character encoding %s is not supported.", encoding));
            }
        }

        public byte[] getBodyBuffer() throws IOException {
            try (InputStream is = httpRequest.getInputStream()) {
{'fraction_non_alphanumeric': 0.08316256830601093, 'fraction_numerical': 0.007399817850637523, 'mean_word_length': 3.373661936768733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6966660', 'n_tokens_mistral': 4868, 'n_tokens_neox': 4670, 'n_words': 991}