Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

CEF4 doesn't send the Referer header

Post Reply
vitaliy_zh
Posts: 10
Joined: Thu Apr 02, 2020 2:28 pm

CEF4 doesn't send the Referer header

Post by vitaliy_zh »

Hi all,

I have a problem with the Referer header when I send a request. No matter what I do, the header is not set. Has anyone managed to set the Referer header for the request manually?

Here's what I've tried:

Code: Select all

procedure TMainForm.FormShow(Sender: TObject);
begin
  Chromium1.SendReferrer := true;
  
  if not (Chromium1.CreateBrowser(CEFWindowParent1, '')) then
  TimerChromium1.Enabled := True;
end;

procedure TMainForm.Chromium1BeforeResourceLoad(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest; const callback: ICefRequestCallback; out Result: TCefReturnValue);
begin
  request.SetHeaderByName('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', true);
  request.SetHeaderByName('Accept-Encoding', 'gzip, deflate, br', true);
  request.SetHeaderByName('Accept-Language', 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7', true);
  request.SetHeaderByName('User-Agent', CURRENT_USER_AGENT, true);
  request.SetHeaderByName('Connection', 'keep-alive', true);

  request.SetHeaderByName('Referer', CURRENT_REFERER, true);  // <--- Doesn't work
  request.SetReferrer(CURRENT_REFERER, REFERRER_POLICY_CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE); // <--- Doesn't work
end;
What's wrong?
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CEF4 doesn't send the Referer header

Post by salvadordf »

Hi,

Try using the TChromium.CustomHeaderName and TChromium.CustomHeaderValue properties.

If you prefer to do it manually you then use this code :
https://github.com/salvadordf/CEF4Delphi/blob/25edba76e8ef339fb9128ff5e06a354d2614f8ce/demos/Delphi_VCL/ResponseFilterBrowser/uResponseFilterBrowser.pas#L515

There are some situations where that header could be lost :
  • The app blocks new tabs or popup windows to open them with a simple TChromium.LoadURL. If this is the case the POST information is lost and also the referer header.
  • The app sets TChromium.SendReferrer to false.
  • Some bug in Chromium.
Post Reply