package com.pannk.mms.common.filters;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by wolf on 20-11-6.
 */
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper{

    HttpServletRequest oriRequest;

    private final static HTMLFilter HTML_FILTER = new HTMLFilter();

    public XssHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
        this.oriRequest = request;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        if (!MediaType.APPLICATION_JSON_VALUE.equalsIgnoreCase(super.getHeader(HttpHeaders.CONTENT_TYPE))){
            return super.getInputStream();
        }
        String json = IOUtils.toString(super.getInputStream(),"UTF-8");
        if (StringUtils.isBlank(json)){
            return super.getInputStream();
        }

        json = xssEncode(json);
        final ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes("UTF-8"));
        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return true;
            }

            @Override
            public boolean isReady() {
                return true;
            }

            @Override
            public void setReadListener(ReadListener listener) {

            }

            @Override
            public int read() throws IOException {
                return bis.read();
            }
        };
    }

    @Override
    public String getParameter(String name) {
        String value = super.getParameter(xssEncode(name));
        if (StringUtils.isNotBlank(value)){
            value = xssEncode(value);
        }
        value = SQLFilter.sqlInject(value);
        return StringEscapeUtils.unescapeHtml(value);
    }

    @Override
    public String[] getParameterValues(String name) {
        String[] parameters = super.getParameterValues(name);
        if (parameters==null||parameters.length==0){
            return null;
        }
        for (int i = 0; i < parameters.length; i++) {
            parameters[i] = xssEncode(parameters[i]);
            parameters[i] = SQLFilter.sqlInject(parameters[i]);
            parameters[i] = StringEscapeUtils.unescapeHtml(parameters[i]);
        }
        return parameters;
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        Map<String,String[]> map = new LinkedHashMap<>();
        Map<String,String[]> parameters = super.getParameterMap();
        for (String key:parameters.keySet()){
            String[] values = parameters.get(key);
            for (int i = 0; i < values.length; i++) {
                values[i] = xssEncode(values[i]);
                values[i] = SQLFilter.sqlInject(values[i]);
                values[i] = StringEscapeUtils.unescapeHtml(values[i]);
            }
            map.put(key,values);
        }
        return map;
    }

    @Override
    public String getHeader(String name) {
        String value = super.getHeader(xssEncode(name));
        if (StringUtils.isNotBlank(value)){
            value = xssEncode(value);
        }
        value = SQLFilter.sqlInject(value);
        return StringEscapeUtils.unescapeHtml(value);
    }

    private String xssEncode(String json) {
        return HTML_FILTER.filter(json);
    }

    public HttpServletRequest getOriRequest() {
        return oriRequest;
    }

    public static HttpServletRequest getOriRequest(HttpServletRequest request){
        if (request instanceof XssHttpServletRequestWrapper){
            return ((XssHttpServletRequestWrapper)request).getOriRequest();
        }
        return request;
    }
}
