mod_hello.pp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. {*******************************************************************
  2. * Test library of the Apache Pascal Headers
  3. *******************************************************************}
  4. library mod_hello;
  5. {*******************************************************************
  6. * The mode must be objfpc on this unit because the unix code uses
  7. * some extensions introduced on Free Pascal
  8. *******************************************************************}
  9. {$ifdef fpc}
  10. {$mode objfpc}{$H+}
  11. {$endif}
  12. {$IFDEF WIN32}
  13. {$DEFINE WINDOWS}
  14. {$ENDIF}
  15. {$define Apache2_2}
  16. uses SysUtils, httpd {$ifndef Apache1_3}, apr{$endif};
  17. var
  18. test_module: module; public name 'hello_module';
  19. default_module_ptr: Pmodule;
  20. const
  21. MODULE_NAME = 'mod_hello.so';
  22. {*******************************************************************
  23. * Free Pascal only supports exporting variables on Windows
  24. *******************************************************************}
  25. {$ifdef WINDOWS}
  26. exports
  27. test_module name 'test_module';
  28. {$endif}
  29. {*******************************************************************
  30. * Handles apache requests
  31. *******************************************************************}
  32. function DefaultHandler(r: Prequest_rec): Integer; cdecl;
  33. var
  34. RequestedHandler: string;
  35. begin
  36. RequestedHandler := r^.handler;
  37. { We decline to handle a request if hello-handler is not the value of r->handler }
  38. if not SameText(RequestedHandler, 'testapache-handler') then
  39. begin
  40. Result := DECLINED;
  41. Exit;
  42. end;
  43. { The following line just prints a message to the errorlog }
  44. ap_log_error(MODULE_NAME, 54, APLOG_NOERRNO or APLOG_NOTICE,
  45. {$ifndef Apache1_3}0,{$endif} r^.server,
  46. 'mod_hello: %s', [PChar('Before content is output')]);
  47. { We set the content type before doing anything else }
  48. {$ifdef Apache1_3}
  49. r^.content_type := 'text/html';
  50. // ap_send_http_header(r);
  51. {$else}
  52. ap_set_content_type(r, 'text/html');
  53. {$endif}
  54. { If the request is for a header only, and not a request for
  55. the whole content, then return OK now. We don't have to do
  56. anything else. }
  57. if (r^.header_only <> 0) then
  58. begin
  59. Result := OK;
  60. Exit;
  61. end;
  62. { Now we just print the contents of the document using the
  63. ap_rputs and ap_rprintf functions. More information about
  64. the use of these can be found in http_protocol.inc }
  65. ap_rputs('<HTML>' + LineEnding, r);
  66. ap_rputs('<HEAD>' + LineEnding, r);
  67. ap_rputs('<TITLE>Hello There</TITLE>' + LineEnding, r);
  68. ap_rputs('</HEAD>' + LineEnding, r);
  69. ap_rputs('<BODY BGCOLOR="#FFFFFF">' + LineEnding ,r);
  70. ap_rputs('<H1>Hello world</H1>' + LineEnding, r);
  71. ap_rputs('This is the first Apache Module working with the new binding from Free Pascal' + LineEnding, r);
  72. // ap_rprintf(r, '<br>A sample line generated by ap_rprintf<br>' + LineEnding, []);
  73. ap_rputs('</BODY></HTML>' + LineEnding, r);
  74. { We can either return OK or DECLINED at this point. If we return
  75. * OK, then no other modules will attempt to process this request }
  76. Result := OK;
  77. end;
  78. {*******************************************************************
  79. * Registers the hooks
  80. *******************************************************************}
  81. {$ifdef apache1_3}
  82. procedure hw_init(s: PServer_rec; p: PPool); cdecl;
  83. begin
  84. end;
  85. var
  86. hw_handlers: array[0..0] of handler_rec =
  87. (
  88. (content_type: 'hw_app'; handler: @DefaultHandler)
  89. );
  90. {$else}
  91. procedure RegisterHooks(p: Papr_pool_t); cdecl;
  92. begin
  93. ap_hook_handler(@DefaultHandler, nil, nil, APR_HOOK_MIDDLE);
  94. end;
  95. {$endif}
  96. {*******************************************************************
  97. * Library initialization code
  98. *******************************************************************}
  99. begin
  100. default_module_ptr := @test_module;
  101. FillChar(default_module_ptr^, SizeOf(default_module_ptr^), 0);
  102. {$ifdef apache1_3}
  103. STANDARD_MODULE_STUFF(test_module);
  104. with test_module do
  105. begin
  106. name := MODULE_NAME;
  107. init := @hw_init;
  108. handlers := hw_handlers;
  109. end;
  110. {$else}
  111. STANDARD20_MODULE_STUFF(test_module);
  112. with test_module do
  113. begin
  114. name := MODULE_NAME;
  115. register_hooks := @RegisterHooks;
  116. end;
  117. {$endif}
  118. end.