/*
* Copyright (C) 2020 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.fizzgate.util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.server.ServerWebExchange;
import com.fizzgate.config.SystemConfig;
import com.fizzgate.filter.FilterResult;
import com.fizzgate.plugin.auth.ApiConfig;
import com.fizzgate.plugin.auth.AuthPluginFilter;
import com.fizzgate.proxy.Route;
import com.fizzgate.util.Consts;
import com.fizzgate.util.JacksonUtils;
import com.fizzgate.util.NettyDataBufferUtils;
import com.fizzgate.util.Result;
import com.fizzgate.util.ThreadContext;
import com.fizzgate.util.Utils;
import reactor.core.publisher.Mono;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author hongqiaowei
*/
public abstract class WebUtils {
// TODO: don't log in this class
private static final Logger log = LoggerFactory.getLogger(WebUtils.class);
private static final String clientService = "cs@";
private static final String xForwardedFor = "X-Forwarded-For";
private static final String unknown = "unknown";
private static final String loopBack = "127.0.0.1";
private static final String binaryAddress = "0:0:0:0:0:0:0:1";
private static final String directResponse = "dr@";
private static final String response = " response ";
private static final String originIp = "oi@";
private static final String clientRequestPath = "crp@";
private static final String clientRequestPathPrefix = "crpp@";
private static final String clientRequestQuery = "crq@";
private static String gatewayPrefix = SystemConfig.DEFAULT_GATEWAY_PREFIX;
private static List<String> appHeaders = Stream.of(SystemConfig.FIZZ_APP_ID) .collect(Collectors.toList());
private static List<String> signHeaders = Stream.of(SystemConfig.FIZZ_SIGN) .collect(Collectors.toList());
private static List<String> timestampHeaders = Stream.of(SystemConfig.FIZZ_TIMESTAMP).collect(Collectors.toList());
public static final String TRACE_ID = "traid@";
public static final String BACKEND_SERVICE = "bs@";
public static final String FILTER_CONTEXT = "fc@";
public static final String APPEND_HEADERS = "ahs@";
public static final String PREV_FILTER_RESULT = "pfr@";
public static final String BACKEND_PATH = "bp@";
public static final String ROUTE = "rout@";
public static boolean LOG_RESPONSE_BODY = false;
public static Set<String> LOG_HEADER_SET = Collections.emptySet();
public static final String ADMIN_REQUEST = "ar@";
public static final String FIZZ_REQUEST = "fr@";
public static final String FAV_REQUEST = "fa@";
public static final String BODY_ENCRYPT = "b-ecyt";
public static final String ORIGINAL_ERROR = "origerr@";
public static final String IGNORE_PLUGIN = "ignPlg@";
private WebUtils() {
}
public static boolean ignorePlugin(ServerWebExchange exchange) {
return exchange.getAttributes().containsKey(IGNORE_PLUGIN);
}
public static boolean isFavReq(ServerWebExchange exchange) {
return exchange.getAttribute(FAV_REQUEST) != null;
}
public static boolean isAdminReq(ServerWebExchange exchange) {
return exchange.getAttribute(ADMIN_REQUEST) != null;
}
public static boolean isFizzReq(ServerWebExchange exchange) {
return exchange.getAttribute(FIZZ_REQUEST) != null;
}
public static void setGatewayPrefix(String p) {
gatewayPrefix = p;
}
public static void setAppHeaders(List<String> hdrs) {
appHeaders = hdrs;
}
public static void setSignHeaders(List<String> hdrs) {
signHeaders = hdrs;
}
public static void setTimestampHeaders(List<String> hdrs) {
timestampHeaders = hdrs;
}
public static String getHeaderValue(ServerWebExchange exchange, String header) {
return exchange.getRequest().getHeaders().getFirst(header);
}
public static List<String> getHeaderValues(ServerWebExchange exchange, String header) {
return exchange.getRequest().getHeaders().get(header);
}
public static boolean isDedicatedLineRequest(ServerWebExchange exchange) {
String v = exchange.getRequest().getHeaders().getFirst(SystemConfig.FIZZ_DL_ID);
return v != null;
}
public static String getAppId(ServerWebExchange exchange) {
HttpHeaders headers = exchange.getRequest().getHeaders();
for (int i = 0; i < appHeaders.size(); i++) {
String v = headers.getFirst(appHeaders.get(i));
if (v != null) {
return v;
}
}
return null;
}
public static String getTimestamp(ServerWebExchange exchange) {
HttpHeaders headers = exchange.getRequest().getHeaders();
for (int i = 0; i < timestampHeaders.size(); i++) {
String v = headers.getFirst(timestampHeaders.get(i));
if (v != null) {
return v;
}
}
return null;
}
public static String getSign(ServerWebExchange exchange) {
HttpHeaders headers = exchange.getRequest().getHeaders();
for (int i = 0; i < signHeaders.size(); i++) {
String v = headers.getFirst(signHeaders.get(i));
if (v != null) {
return v;
}
}
return null;
}
public static String getDedicatedLineId(ServerWebExchange exchange) {
return getHeaderValue(exchange, SystemConfig.FIZZ_DL_ID);
}
public static String getDedicatedLineTimestamp(ServerWebExchange exchange) {
return getHeaderValue(exchange, SystemConfig.FIZZ_DL_TS);
}
p
Java程序员-张凯
- 粉丝: 1w+
- 资源: 7527
最新资源
- 基于java+ssm+mysql的校园二手物品交易平台开题报告.docx
- 疫苗预约小程序ssm-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 英语互助小程序springboot-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 学习资料库小程序设计ssm-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 基于java+ssm+mysql的校园兼职管理系统开题报告.docx
- 运动健康小程序SpringBoot-微信小程序毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- MATLAB代码:含电动汽车参与园区综合能源系统优化调度模型 关键词:电动汽车 改进粒子群 综合能源 优化调度 园区 参考文档:加好友获取 仿真平台:MATLAB 主要内容:代码主要做的是一个含有
- 基于YOLO的摔倒检测研究
- 使用python卷积神经网络做交通信号灯识别的自动驾驶,并在unity进行3d可视化 另有全景图像配准算法源码,改进yolov5目标检测识别源码,体感交互切水果等 计算机硕士
- 内容分发网络(CDN)的工作原理、应用场景与发展前景解析
- 基于STM32F407开发调试,Modbus TCP服务器源程序 采用LWIP网络通讯库,外部PHY采用LAN8720 使用 modbus poll工具调试通过 该工程可直接作为模板开发 源码
- MATLAB代码:基于蒙特卡洛抽样的电动汽车充电负荷计算 关键词:电动汽车 蒙特卡洛模拟 抽样 充放电负荷 参考文档:《主动配电网多源协同运行优化研究-乔珊》完全复现 仿真平台:MATLAB 优
- 《CDN技术分享》PDF
- simplorer与Maxwell电机联合仿真,包含搭建好的Simplorer电机场路耦合主电路与控制算法(矢量控制SVPWM),包含电路与算法搭建的详细教程视频 仿真文件可复制,可将教程中的电机模
- 四旋翼无人机PID模型,飞行器本体模型,位置控制,姿态控制,控制分配和电机控制
- 改进人工大猩猩优化算法(CGTO,2021年提出的优化算法较新),自己研究的改进方法,没有任何成果产出,有没有一款较新的智能算法且效果好一些的呢,那么今天它来了,CGTO通过改进勘探阶段公式,混沌映射
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈