mod_hello.pp 3.9 KB

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