Page 1 of 1

Modify request and response headers

PostPosted: Tue Mar 16, 2021 3:30 am
by ElAndLivia
I'm trying to modify the request and response headers during the normal network transaction. I've found it possible to modify request headers via onBeforeResourceLoad, but I'm not sure if jcef can modify response headers. Could anyone tell me how to modify them?

Here is my code to modify request headers. If I have made any mistakes please tell me, thank you!
Code: Select all
public void modifyRequestHeaders() {
   class JcefRequestHandler extends CefRequestHandlerAdapter{
      @Override
      public boolean onBeforeResourceLoad(CefBrowser browser, CefFrame frame, CefRequest request) {
         if (request.getURL().indexOf("islocal=true") > 0) {
            HashMap<String, String> headerMap = new HashMap<String, String>();
            request.getHeaderMap(headerMap);
              String key = null, value = null;
              for (Map.Entry<String, String> header : headerMap.entrySet()) {
                 if (header.getKey().toLowerCase().contains("cookies")) {
                    key = header.getKey();
                    value = header.getValue();
                 }
              }
              if (key != null) {
                 headerMap.put("Cookie", value);
              }
              headerMap.remove(key);
              request.setHeaderMap(headerMap);
         }
         return false;
      }
   }
      
   JcefRequestHandler requestHandler = new JcefRequestHandler();
   client.addRequestHandler(requestHandler);
}

Re: Modify request and response headers

PostPosted: Tue Mar 16, 2021 9:25 am
by magreenblatt
The response can be modified in CefResourceRequestHandler.onResourceResponse.

Re: Modify request and response headers

PostPosted: Wed Mar 17, 2021 8:28 pm
by ElAndLivia
magreenblatt wrote:The response can be modified in CefResourceRequestHandler.onResourceResponse.

Thank you for your reply!
But I'm still confused about CefResourceRequestHandler.onResourceResponse's parameter declaration:
response : The request response. Cannot be modified in this callback. Instance only valid within the scope of this method.

I've tried to modify the response in the method like this:
Code: Select all
class RequestHandler extends CefResourceRequestHandlerAdapter implements CefRequestHandler{
        @Override
        public boolean onResourceResponse(
                CefBrowser browser, CefFrame frame, CefRequest request, CefResponse response) {
              HashMap<String, String> headerMap = new HashMap<String, String>();
              response.getHeaderMap(headerMap);
              /* modify headerMap */
              response.setHeaderMap(headerMap);
           }
           return false;
        }

   @Override
   public CefResourceRequestHandler getResourceRequestHandler(CefBrowser browser, CefFrame frame,
      CefRequest request, boolean isNavigation, boolean isDownload, String requestInitiator,
         BoolRef disableDefaultHandling) {
      return this;
   }

   /* other methods */
}
client.addRequestHandler(new RequestHandler());

But if I create a new headerMap and execute response.getHeaderMap(headerMap) again, the headerMap seems to be just the same as before without any change. Am I using the onResourceResponse method in a wrong way? Thank you!

Re: Modify request and response headers

PostPosted: Thu Oct 28, 2021 5:04 am
by ndusart
CefResponseImpl is always read-only and all modifications are discarded in all callbacks of CefResourceRequestHandler.

I think you would have to implement a CefResourceHandler to make the request yourself and modify the headers before returning them to the caller.

@magreenblatt, any reason why we cannot modify response in onResourceResponse ? It seems counter-intuitive even for you. It would greatly simplify this kind of response processing.

Re: Modify request and response headers

PostPosted: Thu Oct 28, 2021 9:27 am
by magreenblatt
magreenblatt wrote:The response can be modified in CefResourceRequestHandler.onResourceResponse.

Indeed, this is incorrect currently. Sorry about that.

ndusart wrote:@magreenblatt, any reason why we cannot modify response in onResourceResponse ? It seems counter-intuitive even for you. It would greatly simplify this kind of response processing.

It should be technically possible to modify headers in this callback, hence the confusion above.

Re: Modify request and response headers

PostPosted: Thu Oct 28, 2021 11:59 am
by magreenblatt

Re: Modify request and response headers

PostPosted: Thu Oct 28, 2021 2:10 pm
by ndusart
Thanks for the answer and the link :)
I will follow that.