Open xuexue6282 opened 1 year ago
HttpRequest.setHeader
is to set the request header, and PostRequest.getUpstreamHeaders
gets the response header, and there is no direct connection between them.
So is there any way to solve this problem? Thanks for any given idea.
--
发自新浪邮箱客户端
在 1月15日 20:56,JunXu Chen @.***> 写道:
HttpRequest.setHeader is to set the request header, and PostRequest.getUpstreamHeaders gets the response header, and there is no direct connection between them. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>
hi @christagger
Could you give a minimal example? I don't know exactly what your specific scenario is.
here is my code, i want to calculate the request-response time :
public class DemoFilter implements PluginFilter {
private final Logger logger = LoggerFactory.getLogger(DemoFilter.class);
@Override
public String name() {
return "DemoFilter";
}
@Override
public void filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) {
logger.info("DemoFilter is running");
// TODO : add a custom header "startTimestamp" here (or added in another plugin's filter() method)
request.setHeader("startTimestamp", String.valueOf(System.currentTimeMillis()));
chain.filter(request, response);
}
@Override
public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) {
// TODO : i want to get the custom header "startTimestamp" here, so i can
// calculate the request-response time
chain.postFilter(request, response);
}
/**
* If you need to fetch some Nginx variables in the current plugin, you will need to declare them in this function.
* @return a list of Nginx variables that need to be called in this plugin
*/
@Override
public List<String> requiredVars() {
List<String> vars = new ArrayList<>();
vars.add("remote_addr");
vars.add("server_port");
return vars;
}
/**
* If you need to fetch request body in the current plugin, you will need to return true in this function.
*/
@Override
public Boolean requiredBody() {
return true;
}
}
Or the more common scenario is: the custom header added in a pre-req-plugin can be passed all along the request and can be acquired in every post-resp-plugin postFilter() method.
maybe you could try to add the header to requireVars
, like:
public List<String> requiredVars() {
List<String> vars = new ArrayList<>();
vars.add("remote_addr");
vars.add("server_port");
vars.add("http_ startTimestamp");
return vars;
}
Issue description
I added an request header (startTimestamp) in an ext-plugin-pre-req plugin using HttpRequest.setHeader() method, then I want to acquire this custom header in an ext-plugin-post-resp plugin using PostRequest.getUpstreamHeaders() method. But it doesn't work. I've tried serveral ways, and none of them worked. This problem bothered me a lot, could you help me? Really thanks.
Environment