Modify request and response headers

Having problems with building or using the JCEF Java binding? Ask your questions here.

Modify request and response headers

Postby ElAndLivia » Tue Mar 16, 2021 3:30 am

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);
}
ElAndLivia
Newbie
 
Posts: 4
Joined: Mon Mar 15, 2021 2:57 pm

Re: Modify request and response headers

Postby magreenblatt » Tue Mar 16, 2021 9:25 am

The response can be modified in CefResourceRequestHandler.onResourceResponse.
magreenblatt
Site Admin
 
Posts: 12379
Joined: Fri May 29, 2009 6:57 pm

Re: Modify request and response headers

Postby ElAndLivia » Wed Mar 17, 2021 8:28 pm

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!
ElAndLivia
Newbie
 
Posts: 4
Joined: Mon Mar 15, 2021 2:57 pm

Re: Modify request and response headers

Postby ndusart » Thu Oct 28, 2021 5:04 am

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.
ndusart
Newbie
 
Posts: 2
Joined: Thu Oct 28, 2021 4:49 am

Re: Modify request and response headers

Postby magreenblatt » Thu Oct 28, 2021 9:27 am

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.
magreenblatt
Site Admin
 
Posts: 12379
Joined: Fri May 29, 2009 6:57 pm

Re: Modify request and response headers

Postby magreenblatt » Thu Oct 28, 2021 11:59 am

magreenblatt
Site Admin
 
Posts: 12379
Joined: Fri May 29, 2009 6:57 pm

Re: Modify request and response headers

Postby ndusart » Thu Oct 28, 2021 2:10 pm

Thanks for the answer and the link :)
I will follow that.
ndusart
Newbie
 
Posts: 2
Joined: Thu Oct 28, 2021 4:49 am


Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 2 guests