Cookies in Apache web module
lazarus / 38-Networking and Web Programming
----------------------------------------------------------------------
Grigors · 2019-02-04 08:16
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][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i procedure
TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;
AResponse: TResponse; var Handled: Boolean);// var// c1:
TCookie;begin AResponse.Content := 'ModApa32: ' + DateTimeToStr(Now)
+ '
'; AResponse.Content := AResponse.Content + 'C1: ' +
ARequest.Cookie + '
'; AResponse.Content := AResponse.Content +
'C2: ' + ARequest.CookieFields.Text + '
'; AResponse.Content :=
AResponse.Content + 'C3: ' + ARequest.SetCookie + '
'; // c1 :=
AResponse.Cookies.Add;// c1.Name := 'mapa32';// c1.Value := 'dt:' +
DateTimeToStr(Now); Handled := True;end;
----------------------------------------------------------------------
ptvs · 2019-02-04 08:40
You have ARequest in method declaration and Request in code. Is this a
typo, or you have member named 'Request' in TFPWebModule1?
----------------------------------------------------------------------
Grigors · 2019-02-04 09:04
Sorry, I made a mistake when I beautifies code for post. Now I fix
that. The problem is still the same.
----------------------------------------------------------------------
sash · 2019-02-04 09:39
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.
----------------------------------------------------------------------
Grigors · 2019-02-04 09:49
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 · 2019-02-04 13:27
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.
----------------------------------------------------------------------
Grigors · 2019-02-07 07:57
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][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i begin
Cookie :=
MaybeP(apr_table_get(FRequest^.headers_in,PAnsiChar(HeaderCookie)));
// ParseCookies;...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!