Recent

Author Topic: Problem with apr_table_do in Apache module [SOLVED]  (Read 2238 times)

Tor

  • Newbie
  • Posts: 6
Problem with apr_table_do in Apache module [SOLVED]
« on: September 14, 2016, 11:10:59 am »
hi,
I played around with creating apache modules with freepascal. When I tried using apr_table_do I get a segmentation fault, when I load the page, without it everything works fine.

Code: Pascal  [Select][+][-]
  1. library mod_test;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, sysutils,httpd24, apr24;
  7.  
  8. var
  9.  test_module: module; {$ifdef Unix} public name 'test_module'; {$endif}
  10.  default_module_ptr: Pmodule;
  11.  
  12. const
  13.   MODULE_NAME = 'mod_test.so';
  14.  
  15. exports
  16.  test_module name 'test_module';
  17.  
  18. function ap_rputs(const str: string; const r: Prequest_rec): Integer;
  19. begin
  20.   Result := ap_rwrite(@str[1],length(str),r);
  21. end;
  22.  
  23. //this function will be iterated over the table and will do absolutely nothing
  24. function test_function(rec: Pointer; const key, value: PChar): Integer; cdecl;
  25. begin
  26.   Result:=0;
  27. end;
  28.  
  29. function DefaultHandler(r: Prequest_rec): Integer; cdecl;
  30. var
  31.   RequestedHandler: string;
  32. begin
  33.   RequestedHandler := r^.handler;
  34.  
  35.   if not SameText(RequestedHandler, 'test-handler') then
  36.   begin
  37.     Result := DECLINED;
  38.     Exit;
  39.   end;
  40.  
  41.     ap_set_content_type(r, 'text/html');
  42.   if (r^.header_only <> 0) then
  43.   begin
  44.     Result := OK;
  45.     Exit;
  46.   end;
  47.  
  48.   ap_rputs('<HTML>' + LineEnding, r);
  49.   ap_rputs('<HEAD>' + LineEnding, r);
  50.   ap_rputs('<TITLE>Hello There</TITLE>' + LineEnding, r);
  51.   ap_rputs('</HEAD>' + LineEnding, r);
  52.   ap_rputs('<BODY BGCOLOR="#FFFFFF">' + LineEnding ,r);
  53.   ap_rputs('<H1>Hello world</H1>' + LineEnding, r);
  54.   ap_rputs('This is the first Apache Module working with the new binding from Free Pascal<br><br>' + LineEnding, r);
  55.  
  56.   apr_table_do(@test_function,r,r^.headers_in); //When I comment this out, everything works fine
  57.  
  58.   ap_rputs('</BODY></HTML>' + LineEnding, r);
  59.  
  60.   Result := OK;
  61. end;
  62.  
  63. procedure RegisterHooks(p: Papr_pool_t); cdecl;
  64. begin
  65.   ap_hook_handler(@DefaultHandler, nil, nil, APR_HOOK_MIDDLE);
  66. end;
  67.  
  68.  
  69. begin
  70.   default_module_ptr := @test_module;
  71.   FillChar(default_module_ptr^, SizeOf(default_module_ptr^), 0);
  72.  
  73.   STANDARD20_MODULE_STUFF(test_module);
  74.   with test_module do
  75.     begin
  76.       name := MODULE_NAME;
  77.       register_hooks := @RegisterHooks;
  78.     end;
  79. end.
  80.  
  81.  


What am I doing wrong?
« Last Edit: September 14, 2016, 03:46:34 pm by Tor »

Tor

  • Newbie
  • Posts: 6
Re: Problem with apr_table_do in Apache module
« Reply #1 on: September 14, 2016, 03:46:15 pm »
I found the solution.

instead of

Code: Pascal  [Select][+][-]
  1. apr_table_do(@test_function,r,r^.headers_in);

it needs to be

Code: Pascal  [Select][+][-]
  1. apr_table_do(@test_function,r,r^.headers_in,nil);

 

TinyPortal © 2005-2018