Recent

Author Topic: Cookies in Apache web module  (Read 1722 times)

Grigors

  • Newbie
  • Posts: 4
Cookies in Apache web module
« on: February 04, 2019, 09:16:17 am »
I'm trying to retrieve Cookie on OnRequest from TFPWebModule, but without success.
I always get empty strings. What I miss? Please help.

Here is my test code:

Code: Pascal  [Select]
  1. procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;
  2.   AResponse: TResponse; var Handled: Boolean);
  3. // var
  4. //   c1: TCookie;
  5. begin
  6.   AResponse.Content := 'ModApa32: ' + DateTimeToStr(Now) + '<br>';
  7.   AResponse.Content := AResponse.Content + 'C1: ' + ARequest.Cookie + '<br>';
  8.   AResponse.Content := AResponse.Content + 'C2: ' + ARequest.CookieFields.Text + '<br>';
  9.   AResponse.Content := AResponse.Content + 'C3: ' + ARequest.SetCookie + '<br>';
  10.  
  11. //  c1 := AResponse.Cookies.Add;
  12. //  c1.Name := 'mapa32';
  13. //  c1.Value := 'dt:' + DateTimeToStr(Now);
  14.  
  15.   Handled := True;
  16. end;                
  17.  
« Last Edit: February 04, 2019, 09:58:22 am by Grigors »

ptvs

  • Newbie
  • Posts: 4
Re: Cookies in Apache web module
« Reply #1 on: February 04, 2019, 09:40:27 am »
You have ARequest in method declaration and Request in code. Is this a typo, or you have member named 'Request' in TFPWebModule1?

Grigors

  • Newbie
  • Posts: 4
Re: Cookies in Apache web module
« Reply #2 on: February 04, 2019, 10:04:08 am »
Sorry, I made a mistake when I beautifies code for post. Now I fix that. The problem is still the same.

sash

  • Full Member
  • ***
  • Posts: 225
Re: Cookies in Apache web module
« Reply #3 on: February 04, 2019, 10:39:19 am »
Did you check headers at client side (in browser), does it really contains cookies?
Because I don't see a code which is actually setting cookies.
Lazarus 1.8.4 FPC 3.0.4 x86_64-linux-gtk2 -- Ubuntu 18.04 XFCE

Grigors

  • Newbie
  • Posts: 4
Re: Cookies in Apache web module
« Reply #4 on: February 04, 2019, 10:49:47 am »
Yes, I checked client side headers. There is cookies I sent before in TResponse (commented code). Other HTTP variables (like URL, Query, PathInfo, etc.) reads OK. I Also, I tried FreeSpider module without any problems with cookies. On server side I use Apache 2.2.

sash

  • Full Member
  • ***
  • Posts: 225
Re: Cookies in Apache web module
« Reply #5 on: February 04, 2019, 02:27:20 pm »
Did you try cookies in standalone fphttp server? Is it working?
I'm just guessing, but please check cookie domain, path and expiration.
Maybe some other apache mods is meddling with it.
Lazarus 1.8.4 FPC 3.0.4 x86_64-linux-gtk2 -- Ubuntu 18.04 XFCE

Grigors

  • Newbie
  • Posts: 4
Re: Cookies in Apache web module
« Reply #6 on: February 07, 2019, 08:57:46 am »
My solution

Because it looks like cookes is excluded from parsing HTTP request header I added this:

in file "...\source\packages\fcl-web\src\base\custapache.pp"
procedure TApacheRequest.InitFromRequest;   

commented out ParseCookies and in first line added:

Code: Pascal  [Select]
  1. begin
  2.   Cookie := MaybeP(apr_table_get(FRequest^.headers_in,PAnsiChar(HeaderCookie)));
  3.   // ParseCookies;
  4. ...
  5. end;

Now Arequest.Cookie, Arequest.CookieFields and Arequest.GetHTTPVariable(hvCookie) works in OnRequest events.

This is for Apache 2.2. For Apache 2.4 there is custapache24.pp something diferent in this section. I not tried this.
There may be a better solution. I do not like what I had to touch the file in packages\fcl-web.

Thanks Ptvs and Sash!