package com.zrlog.web.util;

import javax.servlet.http.HttpServletRequest;

/**
 * 存放与Web相关的工具代码
 */
public class WebTools {

    /**
     * 处理由于浏览器使用透明代理，或者是WebServer运行在诸如 nginx/apache 这类 HttpServer后面的情况，通过获取请求头真实IP地址
     *
     * @param request
     * @return
     */
    public static String getRealIp(HttpServletRequest request) {
        //bae env
        if (request.getHeader("clientip") != null) {
            return request.getHeader("clientip");
        }
        String ip = request.getHeader("X-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public static String getRealScheme(HttpServletRequest request) {
        if (request != null) {
            String scheme = request.getHeader("X-Forwarded-Protocol");
            if (scheme == null) {
                scheme = request.getScheme();
            }
            return scheme;
        }
        return "http";
    }

    public static String htmlEncode(String source) {
        if (source == null) {
            return "";
        }
        String html;
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < source.length(); i++) {
            char c = source.charAt(i);
            switch (c) {
                case '<':
                    buffer.append("&lt;");
                    break;
                case '>':
                    buffer.append("&gt;");
                    break;
                case '&':
                    buffer.append("&amp;");
                    break;
                case '"':
                    buffer.append("&quot;");
                    break;
                case 10:
                case 13:
                    break;
                default:
                    buffer.append(c);
            }
        }
        html = buffer.toString();
        return html;
    }
}

@@ -1276,11 +1276,8 @@
 roto
-col
 %22);%0A
@@ -1350,16 +1350,217 @@
 .get
-Scheme()
+Header(%22X-Forwarded-Protocol%22);%0A            %7D%0A            if (scheme != null) %7B%0A                //%E6%A3%80%E6%9F%A5%E6%98%AF%E5%90%A6%E5%90%88%E6%B3%95%0A                if (%22http%22.equals(scheme) %7C%7C %22https%22.equals(scheme)) %7B%0A                    return scheme
 ;%0A  
@@ -1561,32 +1561,36 @@
 me;%0A            
+    
 %7D%0A            re
@@ -1583,38 +1583,90 @@
             
-return scheme;
+%7D else %7B%0A                return request.getScheme();%0A            %7D
 %0A        %7D%0A 

optimize get http scheme