apache / apisix-java-plugin-runner

APISIX Plugin Runner in Java
https://apisix.apache.org/
Apache License 2.0
128 stars 95 forks source link

request help: how should i get the custom header (this header was added beforehand in an ext-plugin-pre-req plugin.filter method) in ext-plugin-post-resp plugin??? #226

Open xuexue6282 opened 1 year ago

xuexue6282 commented 1 year ago

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

nic-chen commented 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.

xuexue6282 commented 1 year ago

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: @.***>

nic-chen commented 1 year ago

hi @christagger

Could you give a minimal example? I don't know exactly what your specific scenario is.

xuexue6282 commented 1 year ago

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.

nic-chen commented 1 year ago

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;
    }