"Hello World" in the C API

Step 1

Use apxs> to create a skeleton module named mod_hello:


(~/build) 56% apxs -g -n hello
Creating [DIR]  hello
Creating [FILE] hello/Makefile
Creating [FILE] hello/mod_hello.c

Step 2

Modify the automatically generated mod_hello.c file:


#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
#include "http_core.h"  /* <== added this extra include file */

/* The content handler */
/*  modified this */
static int hello_handler(request_rec *r)
{
  const char* remote_host;
  const char* user_agent;

  r->content_type = "text/html";      
  ap_send_http_header(r);
  if (r->header_only) return OK;

  remote_host = ap_get_remote_host(r->connection,
				   r->per_dir_config,
				   REMOTE_NAME);
  user_agent = ap_table_get(r->headers_in,"User-Agent");

  ap_rputs("<HTML><HEAD><TITLE>Greetings</TITLE></HEAD></BODY>\n",r);
  ap_rputs("<H1>Greetings, Earthling</H1>\n",r);
  ap_rprintf(r,"<P>Hello to you, <b>%s</b>.</P>\n",remote_host);
  ap_rprintf(r,
	     "<P>You are the lucky user of a <i>%s</i> browser.</P><hr>\n",
	     user_agent);

  if (r->path_info && strlen(r->path_info) > 0)
    ap_rprintf(r, "<P>Path info = <b>%s</b></P><hr>\n", r->path_info);

  if (r->args && strlen(r->args) >0 )
    ap_rprintf(r, "<P>Query string = <b>%s</b></P><hr>\n", r->args);
    
  ap_rputs("</BODY></HTML>\n",r);

  return OK;
}

/*  the rest is autogenerated */

/* Dispatch list of content handlers */
static const handler_rec hello_handlers[] = { 
    { "hello", hello_handler }, 
    { NULL, NULL }
};

/* Dispatch list for API hooks */
module MODULE_VAR_EXPORT hello_module = {
    STANDARD_MODULE_STUFF, 
    NULL,                  /* module initializer                  */
    NULL,                  /* create per-dir    config structures */
    NULL,                  /* merge  per-dir    config structures */
    NULL,                  /* create per-server config structures */
    NULL,                  /* merge  per-server config structures */
    NULL,                  /* table of config file commands       */
    hello_handlers,       /* [#8] MIME-typed-dispatched handlers */
    NULL,                  /* [#1] URI to filename translation    */
    NULL,                  /* [#4] validate user id from request  */
    NULL,                  /* [#5] check if the user is ok _here_ */
    NULL,                  /* [#3] check access by host address   */
    NULL,                  /* [#6] determine MIME type            */
    NULL,                  /* [#7] pre-run fixups                 */
    NULL,                  /* [#9] log a transaction              */
    NULL,                  /* [#2] header parser                  */
    NULL,                  /* child_init                          */
    NULL,                  /* child_exit                          */
    NULL                   /* [#0] post read-request              */
};

Step 3

Run make and install the .so file into Apache's libexec directory:


(~/build/hello) 64% make
apxs -c    mod_hello.c
gcc -DLINUX=2 -DMOD_PERL -DUSE_PERL_SSI -D_REENTRANT -Dbool=char -DHAS_BOOL -I/usr/local/include -DPERL_THREADS -DUSE_HSREGEX -fpic -DSHARED_MODULE -I/usr/local/apache/include  -c mod_hello.c
ld -Bshareable -o mod_hello.so mod_hello.o -L/usr/local/lib -lm -lcrypt -lndbm -lnsl -lndbm -lgdbm -ldbm -ldb -ldl -lm -lrt -lpthread -lc -lcrypt
(~/build/hello) 65% make install
apxs -i -a -n 'hello' mod_hello.so
cp mod_hello.so /usr/local/apache/libexec/mod_hello.so

Step 4

Modify httpd.conf to load the module and to install it as the content handler for the URL of your choice:


# httpd.conf
LoadModule speling_module     libexec/mod_speling.so
LoadModule proxy_module       libexec/libproxy.so
LoadModule rewrite_module     libexec/mod_rewrite.so
....
LoadModule   hello_module	libexec/mod_hello.so

<Location /hello_demo>
  SetHandler hello
</Location>

http://localhost/hello_demo

http://localhost/hello_demo/extra/stuff

http://localhost/hello_demo/extra/stuff?query=string

<< Previous
Contents >> Next >>


Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Wed Nov 3 15:24:09 EST 1999