2
0

mod_hello.pp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. {*******************************************************************
  2. * Test library of the Apache Pascal Headers
  3. *******************************************************************}
  4. library mod_hello;
  5. {$mode objfpc}{$H+}
  6. uses SysUtils, httpd24, apr;
  7. const
  8. MODULE_NAME = 'hello_module';
  9. var
  10. test_module: module;{$ifdef unix} public name MODULE_NAME;{$endif}
  11. exports
  12. test_module name MODULE_NAME;
  13. {*******************************************************************
  14. * Handles apache requests
  15. *******************************************************************}
  16. function DefaultHandler(r: Prequest_rec): Integer; cdecl;
  17. var
  18. RequestedHandler, onerow: string;
  19. begin
  20. RequestedHandler := r^.handler;
  21. { We decline to handle a request if r->handler is not the value of MODULE_NAME}
  22. if not SameText(RequestedHandler, MODULE_NAME) then
  23. begin
  24. Result := DECLINED;
  25. Exit;
  26. end;
  27. { The following line just prints a message to the errorlog }
  28. ap_log_error(MODULE_NAME, //The file in which this function is called
  29. 40, //The line number on which this function is called
  30. 0, //The module_index of the module generating this message
  31. APLOG_NOERRNO or APLOG_NOTICE, //The level of this error message
  32. 0, //The status code from the previous command
  33. r^.server, //The server on which we are logging
  34. 'mod_hello: %s', //The format string
  35. [PChar('Before content is output')]); //The arguments to use to fill out fmt.
  36. ap_set_content_type(r, 'text/html');
  37. { If the request is for a header only, and not a request for
  38. the whole content, then return OK now. We don't have to do
  39. anything else. }
  40. if (r^.header_only <> 0) then
  41. begin
  42. Result := OK;
  43. Exit;
  44. end;
  45. { Now we just print the contents of the document using the
  46. ap_rputs and ap_rprintf functions. More information about
  47. the use of these can be found in http_protocol.inc }
  48. onerow := '<HTML>' + LineEnding;
  49. ap_rwrite(PChar(onerow), length(onerow), r);
  50. onerow := '<HEAD>' + LineEnding;
  51. ap_rwrite(PChar(onerow), length(onerow), r);
  52. onerow := '<TITLE>Hello There</TITLE>' + LineEnding;
  53. ap_rwrite(PChar(onerow), length(onerow), r);
  54. onerow := '</HEAD>' + LineEnding;
  55. ap_rwrite(PChar(onerow), length(onerow), r);
  56. onerow := '<BODY BGCOLOR="#FFFFFF">' + LineEnding;
  57. ap_rwrite(PChar(onerow), length(onerow), r);
  58. onerow := '<H1>Hello world</H1>' + LineEnding;
  59. ap_rwrite(PChar(onerow), length(onerow), r);
  60. onerow := 'This is an Apache Module working with the binding from Free Pascal' + LineEnding;
  61. ap_rwrite(PChar(onerow), length(onerow), r);
  62. onerow := '</BODY></HTML>' + LineEnding;
  63. ap_rwrite(PChar(onerow), length(onerow), r);
  64. { We can either return OK or DECLINED at this point. If we return
  65. * OK, then no other modules will attempt to process this request }
  66. Result := OK;
  67. end;
  68. {*******************************************************************
  69. * Registers the hooks
  70. *******************************************************************}
  71. procedure RegisterHooks(p: Papr_pool_t); cdecl;
  72. begin
  73. ap_hook_handler(@DefaultHandler, nil, nil, APR_HOOK_MIDDLE);
  74. end;
  75. {*******************************************************************
  76. * Library initialization code
  77. *******************************************************************}
  78. begin
  79. FillChar(test_module, SizeOf(test_module),0);
  80. STANDARD20_MODULE_STUFF(test_module);
  81. with test_module do
  82. begin
  83. name := MODULE_NAME;
  84. register_hooks := @RegisterHooks;
  85. end;
  86. end.