mod_hello.pp 3.8 KB

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