httpd-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dr...@hyperreal.org
Subject cvs commit: apache-2.0/htdocs/manual/developer hooks.html modules.html
Date Sat, 15 Jan 2000 20:13:51 GMT
dreid       00/01/15 12:13:51

  Added:       htdocs/manual/developer hooks.html modules.html
  Log:
  The remaining files for the new documentation.
  
  Revision  Changes    Path
  1.1                  apache-2.0/htdocs/manual/developer/hooks.html
  
  Index: hooks.html
  ===================================================================
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
  <html>
  <head>
  <title>Apache 2.0 Hook Functions</title>
  </head>
  
  <!-- Background white, links blue (unvisited), navy (visited), red (active) -->
  <BODY
   BGCOLOR="#FFFFFF"
   TEXT="#000000"
   LINK="#0000FF"
   VLINK="#000080"
   ALINK="#FF0000"
  >
  
  <H1 align="center">Apache Hook Functions</H1>
  
  <P>In general, a hook function is one that Apache will call at some
  point during the processing of a request. Modules can provide
  functions that are called, and specify when they get called in
  comparison to other modules.</P>
  
  <H2>Creating a hook function</H2>
  
  <P>In order to create a new hook, four things need to be done:</P>
  
  <H3>Declare the hook function</H3>
  
  <P>Use the DECLARE_HOOK macro, which needs to be given the name of the
  hook, the return type of the hook function and the arguments. For
  example, if the hook returns an <TT>int</TT> and takes a
  <TT>request_rec *</TT> and an <TT>int</TT> and is called
  "do_something", then declare it like this:</P>
  
  <TT>DECLARE_HOOK(int,do_something,(request_rec *r,int n))</TT>
  
  <P>This should go in a header which modules will include if they want
  to use the hook.</P>
  
  <H3>Create the hook structure</H3>
  
  <P>Each source file that exports a hook has a private structure which
  is used to record the module functions that use the hook. This is
  declared as follows:</P>
  
  <PRE>
  HOOK_STRUCT(
              HOOK_LINK(do_something)
              ...
             )
  </PRE>
  
  <H3>Implement the hook caller</H3>
  
  <P>The source file that exports the hook has to implement a function
  that will call the hook. There are currently three possible ways to do
  this. In all cases, the calling function is called
  <TT>ap_run_<I>hookname</I>()</TT>.</P>
  
  <H4>Void hooks</H4>
  
  <P>If the return value of a hook is <TT>void</TT>, then all the hooks
are
  called, and the caller is implemented like this:</P>
  
  <TT>IMPLEMENT_HOOK_VOID(do_something,(request_rec *r,int
  n),(r,n))</TT>
  
  <P>The second and third arguments are the dummy argument declaration and
  the dummy arguments as they will be used when calling the hook. In
  other words, this macro expands to something like this:</P>
  
  <PRE>
  void ap_run_do_something(request_rec *r,int n)
  {
      ...
      do_something(r,n);
  }
  </PRE>
  
  <H4>Hooks that return a value</H4>
  
  <P>If the hook returns a value, then it can either be run until the first
  hook that does something interesting, like so:</P>
  
  <TT>IMPLEMENT_HOOK_RUN_FIRST(int,do_something,(request_rec *r,int n),(r,n),DECLINED)</TT>
  
  <P>The first hook that <I>doesn't</I> return <TT>DECLINED</TT>
stops
  the loop and its return value is returned from the hook caller. Note
  that <TT>DECLINED</TT> is the tradition Apache hook return meaning "I
  didn't do anything", but it can be whatever suits you.</P>
  
  <P>Alternatively, all hooks can be run until an error occurs. This
  boils down to permitting <I>two</I> return values, one of which means
  "I did something, and it was OK" and the other meaning "I did
  nothing". The first function that returns a value other than one of
  those two stops the loop, and its return is the return value. Declare
  these like so:</P>
  
  <TT>IMPLEMENT_HOOK_RUN_ALL(int,do_something,(request_rec *r,int
  n),(r,n),OK,DECLINED)</TT>
  
  <P>Again, <TT>OK</TT> and <TT>DECLINED</TT> are the traditional
  values. You can use what you want.</P>
  
  <H3>Call the hook callers</H3>
  
  <P>At appropriate moments in the code, call the hook caller, like
  so:</P>
  
  <PRE>
      int n,ret;
      request_rec *r;
  
      ret=ap_run_do_something(r,n);
  </PRE>
  
  <H2>Hooking the hook</H2>
  
  <P>A module that wants a hook to be called needs to do two
  things.</P>
  
  <H3>Implement the hook function</H3>
  
  <P>Include the appropriate header, and define a static function of the
  correct type:</P>
  
  <PRE>
  static int my_something_doer(request_rec *r,int n)
  {
      ...
      return OK;
  }
  </PRE>
  
  <H3>Add a hook registering function</H3>
  
  <P>During initialisation, Apache will call each modules hook
  registering function, which is included in the module structure:</P>
  
  <PRE>
  static void my_register_hooks()
  {
      ap_hook_do_something(my_something_doer,NULL,NULL,HOOK_MIDDLE);
  }
  
  mode MODULE_VAR_EXPORT my_module =
  {
      ...
      my_register_hooks       /* register hooks */
  };
  </PRE>
  
  <H3>Controlling hook calling order</H3>
  
  <P>In the example above, we didn't use the three arguments in the hook
  registration function that control calling order. There are two
  mechanisms for doing this. The first, rather crude, method, allows us
  to specify roughly where the hook is run relative to other
  modules. The final argument control this. There are three possible
  values:</P>
  
  <PRE>
  HOOK_FIRST
  HOOK_MIDDLE
  HOOK_LAST
  </PRE>
  
  <P>All modules using any particular value may be run in any order
  relative to each other, but, of course, all modules using
  <TT>HOOK_FIRST</TT> will be run before <TT>HOOK_MIDDLE</TT> which
are
  before <TT>HOOK_LAST</TT>. Modules that don't care when they are run
  should use <TT>HOOK_MIDDLE</TT>. <I>(I spaced these out so people
  could do stuff like <TT>HOOK_FIRST-2</TT> to get in slightly earlier,
  but is this wise? - Ben)</I></P>
  
  <P>Note that there are two more values, <TT>HOOK_REALLY_FIRST</TT> and
  <TT>HOOK_REALLY_LAST</TT>. These should only be used by the hook
  exporter.</P>
  
  <P>The other method allows finer control. When a module knows that it
  must be run before (or after) some other modules, it can specify them
  by name. The second (third) argument is a NULL-terminated array of
  strings consisting of the names of modules that must be run before
  (after) the current module. For example, suppose we want "mod_xyz.c"
  and "mod_abc.c" to run before we do, then we'd hook as follows:</P>
  
  <PRE>
  static void register_hooks()
  {
      static const char * const aszPre[]={ "mod_xyz.c", "mod_abc.c", NULL };
  
      ap_hook_do_something(my_something_doer,aszPre,NULL,HOOK_MIDDLE);
  }
  </PRE>
  
  <P>Note that the sort used to achieve this is stable, so ordering set
  by <TT>HOOK_<I>ORDER</I></TT> is preserved, as far as is
  possible.</P>
  
  <I>Ben Laurie, 15th August 1999</I>
  </body>
  </html>
  
  
  
  1.1                  apache-2.0/htdocs/manual/developer/modules.html
  
  Index: modules.html
  ===================================================================
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
  <HTML>
  <HEAD>
  <TITLE>Converting Modules from Apache 1.3 to Apache 2.0</TITLE>
  </HEAD>
  
  <!-- Background white, links blue (unvisited), navy (visited), red (active) -->
  <BODY
   BGCOLOR="#FFFFFF"
   TEXT="#000000"
   LINK="#0000FF"
   VLINK="#000080"
   ALINK="#FF0000"
  >
  
  <H1 ALIGN="CENTER">From Apache 1.3 to Apache 2.0<br>Modules</H1>
  
  <p>
  This is a first attempt at writing the lessons I learned when trying to convert the mod_mmap_static
module to Apache 2.0.  It's by no means definitive and probably won't even be correct in some
ways, but it's a start.
  </p>
  <hr>
  <h2>The easier changes...
  </h2>
  
  <h3>Cleanup Routines</h3>
  <p>
  These now need to be of type ap_status_t and return a value of that type.  Normally the
return value will be APR_SUCCESS unless there is some need to signal an error in the cleanup.
 Be aware that even though you signal an error not all code yet checks and acts upon the error.
  </p>
  
  <h3>Initialisation Routines</h3>
  
  <p>
  These should now be renamed to better signify where they sit in the overall process.  So
the name gets a small change from mmap_init to mmap_post_config.  The arguments passed have
undergone a radical change and now look like
  </p>
  <ul style="list-style:none">
  <li>ap_context_t *p,</li>
  <li>ap_context_t *plog,</li>
  <li>ap_context_t *ptemp,</li>
  <li>server_rec *s</li>
  </ul>
  <p>
  Throughout Apache the old pools have been replced by the ap_context_t, though their use
remains remarkably similar.
  </p>
  
  <h3>Data Types</h3>
  <p>
  A lot of the data types have been moved into the APR.  This means that some have had a name
change, such as the one shown above.  The following is a brief list of some of the changes
that you are likely to have to make.
  <ul style="list-style:none">
  <li>pool becomes ap_context_t</li>
  <li>table becomes ap_table_t</li>
  </ul>
  <hr>
  
  <h2>
  The <em>messier</em> changes...
  </h2>
  <h3>Register Hooks</h3>
  <p>
  The new architecture uses a series of hooks to provide for calling your functions.  These
you'll need to add to your module by way of a new function, static void register_hooks(void).
 The function is really reasonably straightforward once you understand what needs to be done.
 Each function that needs calling at some stage in the processing of a request needs to be
registered, handlers do not.  There are a number of phases where functions can be added, and
for each you can specify with a high degree of control the relative order that the function
will be called in.
  </p>
  <p>
  This is the code that was added to mod_mmap_static
  </p>
  <pre>
  static void register_hooks(void)
  {
      static const char * const aszPre[]={ "http_core.c",NULL };
      ap_hook_post_config(mmap_post_config,NULL,NULL,HOOK_MIDDLE);
      ap_hook_translate_name(mmap_static_xlat,aszPre,NULL,HOOK_LAST);
  };
  </pre>
  <p>
  This registers 2 functions that need to be called, one in the post_config stage (virtually
every module will need this one) and one for the translate_name phase.  note that while there
are different function names the format of each is identical.  So what is the format?
  </p>
  <p><strong>
  ap_hook_[phase_name](function_name, predecessors, successors, position);
  </strong></p>
  <p>
  There are 3 hook positions defined...
  </p>
  <ul style="list-style:none">
  <li>HOOK_FIRST</li>
  <li>HOOK_MIDDLE</li>
  <li>HOOK_LAST</li>
  </ul>
  <p>
  To define the position you use the position and then modify it with the predecessors and
successors.  each of the modifiers can be a list of functions that should be called, either
before the function is run (predecessors) or after the function has run (successors).
  </p>
  <p>
  In the mod_mmap_static case I didn't care about the post_config stage, but the mmap_static_xlat
MUST be called after the core module had done it's name translation, hence the use of the
aszPre to define a modifier to the position HOOK_LAST.
  </p>
  <h3>Module Definition</h3>
  
  <p>
  There are now a lot fewer stages to worry about when creating your module definition.  The
old defintion looked like
  </p>
  <pre>
  module MODULE_VAR_EXPORT [module_name]_module =
  {
      STANDARD_MODULE_STUFF,
      /* initializer */
      /* dir config creater */
      /* dir merger --- default is to override */
      /* server config */
      /* merge server config */
      /* command handlers */
      /* handlers */
      /* filename translation */
      /* check_user_id */
      /* check auth */
      /* check access */
      /* type_checker */
      /* fixups */
      /* logger */
      /* header parser */
      /* child_init */
      /* child_exit */
      /* post read-request */
  };
  </pre>
  <p>
  The new structure is a great deal simpler...
  </p>
  <pre>
  module MODULE_VAR_EXPORT [module_name]_module =
  {
      STANDARD20_MODULE_STUFF,
      /* create per-directory config structures */
      /* merge per-directory config structures  */
      /* create per-server config structures    */
      /* merge per-server config structures     */
      /* command handlers */
      /* handlers */
      /* register hooks */
   };
  </pre>
  <p>
  Some of these read directly across, some don't.  I'll try to summarise what should be done
below.
  </p>
  <p>
  The stages that read directly across :
  </p>
  <ul style="list-style:none">
  <li>
  /* dir config creater */ ==> /* create per-directory config structures */
  </li>
  <li>
  /* server config */ ==> /* create per-server config structures    */
  </li>
  <li>
  /* dir merger */ ==> /* merge per-directory config structures  */
  </li>
  <li>
  /* merge server config */ ==> /* merge per-server config structures     */
  </li>
  <li>
  /* command table */ ==> /* command ap_table_t */
  </li>
  <li>
  /* handlers */ ==> /* handlers */
  </li>
  </ul>
  <P>
  The remainder of the old functions should be registered as hooks.  There are the following
hook stages defined so far...
  </P>
  
  <ul style="list-style:none">
  <li>
  ap_hook_post_config  <em>(this is where the old _init routines get registered)</em>
  </li>
  <li>
  ap_hook_http_method
  </li>
  <li>
  ap_hook_open_logs
  </li>
  <li>
  ap_hook_auth_checker
  </li>
  <li>
  ap_hook_default_port
  </li>
  <li>
  ap_hook_access_checker
  </li>
  <li>
  ap_hook_process_connection
  </li>
  <li>
  ap_hook_child_init_hook
  </li>
  
  
  </body>
  </html>
  
  
  
  
  
  
  
  

Mime
View raw message