Skip to content

can't get any informations from request header when process request body ? (ext_proc) #15569

Closed

Description

@jefflaifu

I'm using ext_proc to implement some functions with ext_proc. An important case is that I want to get request_headers when I process request_body, but I can't. Is there any solution?

Activity

  1. added
    triage
    on Mar 19, 2021
  2. snowp commented on Mar 19, 2021

    @snowp
    Contributor
  3. added
    question
    on Mar 19, 2021
  4. gbrail commented on Mar 19, 2021

    @gbrail
    Contributor

    Glad you're interested in the new filter!

    That's why the filter uses a gRPC stream. The idea is that your external processor will first receive a request_headers message, send back a reply, and then receive a request_body message. If it needs access to the request headers while processing the body, it needs to store them. However, since the request headers and body both arrive on the same stream, it's usually fairly easy to just store whatever information you need in a local variable.

    Hope that helps, and if not then I think we need to do a better job on examples.

  5. jefflaifu commented on Mar 22, 2021

    @jefflaifu
    Author

    Got it!
    I'm new to gRPC, and not familiar with Go. I'm using Java to implement ext_proc.
    I think I am right with the code below:

    public static class ExtProcessor extends ExternalProcessorGrpc.ExternalProcessorImplBase {
        @Override
        public StreamObserver<ProcessingRequest> process(StreamObserver<ProcessingResponse> responseObserver) {
          return new StreamObserver<ProcessingRequest>() {
            private boolean bodyProcessed = false;
            private HttpHeaders requestHeaders;
    
            @Override
            public void onNext(ProcessingRequest processingRequest) {
    
              switch (processingRequest.getRequestCase()) {
                case REQUEST_HEADERS:
                  requestHeaders = processingRequest.getRequestHeaders();
                  responseObserver.onNext(ProcessingResponse.newBuilder().setRequestHeaders(HeadersResponse.newBuilder().setResponse(CommonResponse.newBuilder())).build());
                  break;
                case REQUEST_BODY:
                  String body = processingRequest.getRequestBody().getBody().toStringUtf8();
                  // TODO do processing body with the requestHeaders local varirable
                  responseObserver.onNext(ProcessingResponse.newBuilder()
                      .setRequestBody(BodyResponse.newBuilder()
                          .setResponse(CommonResponse.newBuilder()
                              .setStatus(CommonResponse.ResponseStatus.CONTINUE_AND_REPLACE)
                              .setBodyMutation(BodyMutation.newBuilder()
                                  .setBody(ByteString.copyFrom(("response for: " + body).getBytes())))))
                      .build());
                  bodyProcessed = true;
                  break;
              }
    
              if(bodyProcessed) {
                responseObserver.onCompleted();
              }
            }
    
            @Override
            public void onError(Throwable throwable) {
              throwable.printStackTrace();
            }
    
            @Override
            public void onCompleted() {
              // don't close the stream at every request, close it manually
              //responseObserver.onCompleted();
            }
          };
        }
      }
  6. removed
    triage
    on Mar 23, 2021
  7. jefflaifu commented on Mar 24, 2021

    @jefflaifu
    Author

    I got another question like this.
    If I change request_body, from json to xml, the request_body's length has been changed, but "content-length" in request_header is still the old one. So, the client can't get the request correctly. The same situation is with "change response-body but 'conent-length' in response_header is still".
    I have no idea about this. How can I change the Headers after I changed the Body? Could I get some advice?
    @gbrail Thanks.

  8. gbrail commented on Mar 24, 2021

    @gbrail
    Contributor
  9. dragonbone81 commented on Mar 31, 2021

    @dragonbone81

    @gbrail I have a similar issue except instead of content-length theres a value in the header that is calculated based off of the body and it needs to match (crc32). Since I want to modify the body I would need to change that header value. It sounds like this is not possible in the ext_proc filter. Do you know if that is a limitation of just this filter or would the other ones that can process the body (lua / WASM) also have this limitation?
    Is there a way to maybe buffer and not send out the headers right away with the async option and wait for the body to come in?

  10. gbrail commented on Mar 31, 2021

    @gbrail
    Contributor

    I'm not sure that you'd be able to do that in any Envoy filter right now -- but if someone does it then we can certainly add that capability to ext_proc.

    The problem is that you want Envoy to hold the filter chain "in place" and not transmit the headers to other filters in the chain until the body callbacks have been run. I don't know of a way to do that in Envoy's HTTP filter chain today. You can stop the filters from being further processed by returning a status like "StopAllIterationAndWatermark," but when the filter calls the "continue" function Envoy will process the headers further down or up the filter chain before sending the data, so by the time the data arrives the headers have already been sent off.

    If we can figure out a way around this then I can certainly implement a change in ext_proc.

  11. dragonbone81 commented on Mar 31, 2021

    @dragonbone81

    Is what is mentioned here in the Lua section what you mean:
    https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_filters/lua_filter#currently-supported-high-level-features

    Performing an outbound async HTTP call to an upstream host. Such a call can be performed while buffering body data so that when the call completes upstream headers can be modified.

    This seems like what I need, just not yet implemented in the ext_proc if I understand?

  12. gbrail commented on Mar 31, 2021

    @gbrail
    Contributor

    Well if it works in that filter there must be a way, so I'll read the code and figure out what they did.

  13. gbrail commented on Apr 1, 2021

    @gbrail
    Contributor

    So the short answer is just to return StopIteration rather than StopAllIterationAndWatermark from decode/encodeHeaders. But that's the short answer. ext_proc lets the processing server decide in each callback what callbacks it wants next, so if we return StopIteration then Envoy will likely call encode/decodeData before the gRPC message has come back (if it was ever sent at all) and we don't know whether to dispatch a gRPC there or not. So we need to stop iteration there too, and probably buffer, but don't want to fill the buffer if the processor isn't going to want the body. So when the gRPC response comes back we now need to consider three or four different things to determine what to do next, and so on and so on.

    The result is a state machine that changes from 2-3 states to many more. I'll have to draw it out and refactor the code and experiment and things, so this will likely take a while.

  14. dragonbone81 commented on Apr 1, 2021

    @dragonbone81

    @gbrail Thanks for taking this on so quick :) Seems like there is a path forward so far for making this work. Really liking this ext_proc filter!

  15. dragonbone81 commented on Apr 2, 2021

    @dragonbone81

    @gbrail Would implementing request trailers and response trailers in ext_proc be a solution to this? From what I've been reading it seems that's what they are for, if thats easier than adding functionality to be able to change the headers in the body.

  16. gbrail commented on Apr 3, 2021

    @gbrail
    Contributor
  17. RameshByndoor commented on Apr 15, 2021

    @RameshByndoor

    @gbrail Hi any idea why case RESPONSE_HEADERS: doesn't get called.? I have response_header_mode: "SEND" set in filter http.ext_proc

  18. gbrail commented on Apr 16, 2021

    @gbrail
    Contributor

    The PR is in progress:

    #15935

    As for why response headers aren't being sent -- they should be sent unless the processor closed the stream on the other end, or unless the gRPC timed out. In the event of a timeout an error should be returned to the client unless "failure_mode_allow" is set. Turning on "filter" tracing or debugging, for instance with the command-line option:

    --component-log-level filter:trace
    

    may also help you understand what's going on.

  19. gbrail commented on May 10, 2021

    @gbrail
    Contributor

    Support for modifying the headers along with the body when the filter is in "BUFFERED" mode was added in #15935.

  20. github-actions commented on Jun 9, 2021

    @github-actions

    This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in the next 7 days unless it is tagged "help wanted" or "no stalebot" or other activity occurs. Thank you for your contributions.

  21. added
    stale
    on Jun 9, 2021
  22. github-actions commented on Jun 16, 2021

    @github-actions

    This issue has been automatically closed because it has not had activity in the last 37 days. If this issue is still valid, please ping a maintainer and ask them to label it as "help wanted" or "no stalebot". Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/ext_procquestionstale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions