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