HPlogo Configuring and Managing MPE/iX Internet Services > Chapter 9 HP WebWise MPE/iX Secure Web Server

Sample Module Code (mod_hw)

MPE documents

Complete PDF
Table of Contents
Glossary
Index

E0802 Edition 6 ♥
E0701 Edition 5
E0400 Edition 4

This section contains source code for the sample DSO module discussed in the previous sections, mod_hw.so. The module source code consists of two files, mod_hw.c and hw.c. Mod_hw.c contains the module structure and hw.c contains a function called by mod_hw.c.

mod_hw.c


Mod_hw.c is a simple Apache module. It calls pow() (in the math library, /lib/libm) and helloworld() in hw.c. This example is designed to illustrate the use of external function calls.

  shell/iX>  cat mod_hw.c
  #include <stdlib.h>
  #include <math.h>
  #include "httpd.h"
  #include "http_config.h"
  #include "http_core.h"
  #include "http_log.h"
  #include "http_protocol.h"
  /* here's the content handler */
  static int hw_handler(request_rec *r) {
  double root = 12;
  double power = 2;
     r->content_type = "text/html";
     ap_send_http_header(r);

     helloworld(r);

     ap_rprintf(r, "\nresult of %.2lf**%.2lf is %.2lf",
        root,power,pow(root,power));
     return OK;
  }/* Make the name of the content handler known to Apache */
  static handler_rec hw_handlers[] =
  {    {"hw-handler", hw_handler},
      {NULL}
  };/* Tell Apache what phases of the transaction we handle */
  module MODULE_VAR_EXPORT hw_module =
  {   STANDARD_MODULE_STUFF,
     NULL,            /* module initializer                 */
     NULL,            /* per-directory config creator       */
     NULL,            /* dir config merger                  */
     NULL,            /* server config creator              */
     NULL,            /* server config merger               */
     NULL,            /* command table                      */
     hw_handlers,     /* [7]  content handlers              */
     NULL,            /* [2]  URI-to-filename translation   */
     NULL,            /* [5]  check/validate user_id        */
     NULL,            /* [6]  check user_id is valid *here* */
     NULL,            /* [4]  check access by host address  */
     NULL,            /* [7]  MIME type checker/setter      */
     NULL,            /* [8]  fixups                        */
     NULL,            /* [9]  logger                        */
     NULL,            /* [3]  header parser                 */
     NULL,            /* process initialization             */
     NULL,            /* process exit/cleanup               */
     NULL             /* [1]  post read_request handling    */
  };

hw.c


This file defines a function called helloworld().

  shell/iX> cat hw.c
  #include "httpd.h"
  #include "http_config.h"
  #include "http_core.h"
  #include "http_log.h"
  #include "http_protocol.h"
  helloworld(request_rec *r)
  {
     ap_rputs("<HTML>\n", r);
     ap_rputs("<HEADER>\n", r);
     ap_rputs("<TITLE>Hello There</TITLE>\n", r);
     ap_rputs("</HEADER>\n", r);
     ap_rputs("<BODY>\n", r);
     ap_rprintf(r, "<H1>Hello World!</H1>\n");
     ap_rputs("</BODY>\n", r);
     ap_rputs("</HTML>\n", r);
  }

APXS Default Makefile (mod_hw)


This is the Makefile auto-generated by apxs -g -n hw.

  ##
  ##  Makefile -- Build procedure for sample hw Apache module
  ##  Autogenerated via ``apxs -n hw -g''.
  ##
  #   the used tools
  APXS=apxs
  APACHECTL=apachectl
  #   additional defines, includes and libraries
  #DEF=-Dmy_define=my_value
  #INC=-Imy/include/dir
  #LIB=-Lmy/lib/dir -lmylib
  #   the default target
  all: mod_hw.so
  #   compile the shared object file
  mod_hw.so: mod_hw.c
          $(APXS) -c $(DEF) $(INC) $(LIB) mod_hw.c
  #   install the shared object file into Apache
  install: all
          $(APXS) -i -a -n 'hw' mod_hw.so
  #   cleanup
  clean:
           -rm -f mod_hw.o mod_hw.so
  #   simple test
  test: reload
            lynx -mime_header http://localhost/hw
  #   install and activate shared object by reloading Apache to
  #   force a reload of the shared object file
  reload: install restart
  #   the general Apache start/restart/stop
  #   procedures
  start:
            $(APACHECTL) start
  restart:
            $(APACHECTL) restart
  stop:
            $(APACHECTL) stop

Modified APXS Makefile (mod_hw)


This Makefile is a modified version of the apxs auto-generated Makefile. It shows how to call gcc for compiling and LinkEditor for linking. The APXS variable was also changed to contain a fully qualified path to apxs. Apxs is used for getting the correct defines and includes. It is also used for installing the new module in the libexec/ directory.

Make sure to use tabs (instead of spaces) when adding callci and gcc to the MakeFile.

  shell/iX> cat Makefile
  ##
  ##  Makefile -- Build procedure for sample hw Apache module
  ##  Autogenerated via ``apxs -n hw -g''.
  ##
  ##  3/01 Modified Makefile to replace apxs by gcc and linkedit
  ##       for compile and link
  #   the used tools
APXS=/APACHE/PUB/bin/apxs APACHECTL=/APACHE/PUB/bin/apachectl # defines, includes and libraries DEF=`$(APXS) -q CFLAGS` INC=-I`$(APXS) -q INCLUDEDIR` LIB=/lib/libm.a,/lib/libc.a # the default target all: mod_hw.so # link the shared object file mod_hw.so: mod_hw.o hw.o callci "linkedit 'buildxl xl=./mod_hw.so;limit=5'" callci "linkedit 'addxl from=./mod_hw.o,./hw.o \ ;to=./mod_hw.so; rl=$(LIB);merge;share'" # compile the object files mod_hw.o: mod_hw.c gcc -o mod_hw.o $(DEF) $(INC) -c mod_hw.c hw.o: hw.c gcc -o hw.o $(DEF) $(INC) -c hw.c # install the shared object file into Apache install: all $(APXS) -i -a -n 'hw' mod_hw.so # cleanup clean: -rm -f mod_hw.o mod_hw.so hw.o# simple test test: reload lynx -mime_header http://localhost/hw # install and activate shared object by reloading Apache to # force a reload of the shared object file reload: install restart # the general Apache start/restart/stop # procedures start: $(APACHECTL) start restart: $(APACHECTL) restart stop: $(APACHECTL) stop

Extended Apache Programming Interface (EAPI)


Apache 1.3.9 and later are built with an extended set of Apache APIs. This means that Apache 1.3.9 and later expects these EAPIs to be built into any DSO they call. This EAPI feature is included in Apache so that a DSO can be used by either Apache or WebWise, since WebWise requires EAPI for its SSL functionality.

When creating DSOs, you must compile with the -DEAPI option. This will include the necessary EAPI header files. These header files are distributed with Apache 1.3.9 and later and reside in the /APACHE/PUB/include directory.

DSOs created without -DEAPI may operate successfully but may generate a warning message in the error_log file.




Testing a DSO


Stopping the Web Server