which comes into play when we need to load the module:
</p>
<highlight language="config">
-LoadModule example_module modules/mod_example.so
+LoadModule example_module "modules/mod_example.so"
</highlight>
<p>
In essence, this tells the server to open up <code>mod_example.so</code> and look for a module
the server to do just that:
</p>
<highlight language="config">
-AddHandler example-handler .sum
+AddHandler example-handler ".sum"
</highlight>
<p>
What this tells the server is the following: <em>Whenever we receive a request
for a URI ending in .sum, we are to let all modules know that we are
looking for whoever goes by the name of "example-handler" </em>.
Thus, when a request is being served that ends in .sum, the server will let all
-modules know, that this request should be served by "example-handler
-". As you will see later, when we start building mod_example, we will
+modules know, that this request should be served by "example-handler".
+As you will see later, when we start building mod_example, we will
check for this handler tag relayed by <code>AddHandler</code> and reply to
the server based on the value of this tag.
</p>
static int example_handler(request_rec *r)
{
/* Set the appropriate content type */
- ap_set_content_type(r, "text/html");
+ ap_set_content_type(r, "text/html");
/* Print out the IP address of the client connecting to us: */
- ap_rprintf(r, "<h2>Hello, %s!</h2>", r->useragent_ip);
+ ap_rprintf(r, "<h2>Hello, %s!</h2>", r->useragent_ip);
/* If we were reached through a GET or a POST request, be happy, else sad. */
- if ( !strcmp(r->method, "POST") || !strcmp(r->method, "GET") ) {
- ap_rputs("You used a GET or a POST method, that makes us happy!<br/>", r);
+ if ( !strcmp(r->method, "POST") || !strcmp(r->method, "GET") ) {
+ ap_rputs("You used a GET or a POST method, that makes us happy!<br/>", r);
}
else {
- ap_rputs("You did not use POST or GET, that makes us sad :(<br/>", r);
+ ap_rputs("You did not use POST or GET, that makes us sad :(<br/>", r);
}
/* Lastly, if there was a query string, let's print that too! */
if (r->args) {
- ap_rprintf(r, "Your query string was: %s", r->args);
+ ap_rprintf(r, "Your query string was: %s", r->args);
}
return OK;
}
const char *digestType;
- /* Check that the "example-handler" handler is being called. */
- if (!r->handler || strcmp(r->handler, "example-handler")) return (DECLINED);
+ /* Check that the "example-handler" handler is being called. */
+ if (!r->handler || strcmp(r->handler, "example-handler")) return (DECLINED);
/* Figure out which file is being requested by removing the .sum from it */
filename = apr_pstrdup(r->pool, r->filename);
ap_parse_form_data(r, NULL, &POST, -1, 8192);
/* Set the appropriate content type */
- ap_set_content_type(r, "text/html");
+ ap_set_content_type(r, "text/html");
/* Print a title and some general information */
- ap_rprintf(r, "<h2>Information on %s:</h2>", filename);
- ap_rprintf(r, "<b>Size:</b> %u bytes<br/>", finfo.size);
+ ap_rprintf(r, "<h2>Information on %s:</h2>", filename);
+ ap_rprintf(r, "<b>Size:</b> %u bytes<br/>", finfo.size);
/* Get the digest type the client wants to see */
- digestType = apr_table_get(GET, "digest");
- if (!digestType) digestType = "MD5";
+ digestType = apr_table_get(GET, "digest");
+ if (!digestType) digestType = "MD5";
rc = apr_file_open(&file, filename, APR_READ, APR_OS_DEFAULT, r->pool);
if (rc == APR_SUCCESS) {
/* Are we trying to calculate the MD5 or the SHA1 digest? */
- if (!strcasecmp(digestType, "md5")) {
+ if (!strcasecmp(digestType, "md5")) {
/* Calculate the MD5 sum of the file */
union {
char chr[16];
apr_md5_final(digest.chr, &md5);
/* Print out the MD5 digest */
- ap_rputs("<b>MD5: </b><code>", r);
+ ap_rputs("<b>MD5: </b><code>", r);
for (n = 0; n < APR_MD5_DIGESTSIZE/4; n++) {
- ap_rprintf(r, "%08x", digest.num[n]);
+ ap_rprintf(r, "%08x", digest.num[n]);
}
- ap_rputs("</code>", r);
+ ap_rputs("</code>", r);
/* Print a link to the SHA1 version */
- ap_rputs("<br/><a href='?digest=sha1'>View the SHA1 hash instead</a>", r);
+ ap_rputs("<br/><a href='?digest=sha1'>View the SHA1 hash instead</a>", r);
}
else {
/* Calculate the SHA1 sum of the file */
apr_sha1_final(digest.chr, &sha1);
/* Print out the SHA1 digest */
- ap_rputs("<b>SHA1: </b><code>", r);
+ ap_rputs("<b>SHA1: </b><code>", r);
for (n = 0; n < APR_SHA1_DIGESTSIZE/4; n++) {
- ap_rprintf(r, "%08x", digest.num[n]);
+ ap_rprintf(r, "%08x", digest.num[n]);
}
- ap_rputs("</code>", r);
+ ap_rputs("</code>", r);
/* Print a link to the MD5 version */
- ap_rputs("<br/><a href='?digest=md5'>View the MD5 hash instead</a>", r);
+ ap_rputs("<br/><a href='?digest=md5'>View the MD5 hash instead</a>", r);
}
apr_file_close(file);
</p>
<highlight language="config">
RewriteEngine On
-RewriteCond %{REQUEST_URI} ^/foo/bar
-RewriteRule ^/foo/bar/(.*)$ /foobar?page=$1
+RewriteCond "%{REQUEST_URI}" "^/foo/bar"
+RewriteRule "^/foo/bar/(.*)$" "/foobar?page=$1"
</highlight>
<p>
Each of these configuration directives are handled by a separate function,
static int example_handler(request_rec *r)
{
- if (!r->handler || strcmp(r->handler, "example-handler")) return(DECLINED);
- ap_set_content_type(r, "text/plain");
- ap_rprintf(r, "Enabled: %u\n", config.enabled);
- ap_rprintf(r, "Path: %s\n", config.path);
- ap_rprintf(r, "TypeOfAction: %x\n", config.typeOfAction);
+ if (!r->handler || strcmp(r->handler, "example-handler")) return(DECLINED);
+ ap_set_content_type(r, "text/plain");
+ ap_rprintf(r, "Enabled: %u\n", config.enabled);
+ ap_rprintf(r, "Path: %s\n", config.path);
+ ap_rprintf(r, "TypeOfAction: %x\n", config.typeOfAction);
return OK;
}
static void register_hooks(apr_pool_t *pool)
{
config.enabled = 1;
- config.path = "/foo/bar";
+ config.path = "/foo/bar";
config.typeOfAction = 0x00;
ap_hook_handler(example_handler, NULL, NULL, APR_HOOK_LAST);
}
our configuration:
</p>
<highlight language="config">
-<Location /example>
+<Location "/example">
SetHandler example-handler
</Location>
</highlight>
Our directive handlers:
==============================================================================
*/
-/* Handler for the "exampleEnabled" directive */
+/* Handler for the "exampleEnabled" directive */
const char *example_set_enabled(cmd_parms *cmd, void *cfg, const char *arg)
{
- if(!strcasecmp(arg, "on")) config.enabled = 1;
+ if(!strcasecmp(arg, "on")) config.enabled = 1;
else config.enabled = 0;
return NULL;
}
-/* Handler for the "examplePath" directive */
+/* Handler for the "examplePath" directive */
const char *example_set_path(cmd_parms *cmd, void *cfg, const char *arg)
{
config.path = arg;
return NULL;
}
-/* Handler for the "exampleAction" directive */
+/* Handler for the "exampleAction" directive */
/* Let's pretend this one takes one argument (file or db), and a second (deny or allow), */
/* and we store it in a bit-wise manner. */
const char *example_set_action(cmd_parms *cmd, void *cfg, const char *arg1, const char *arg2)
{
- if(!strcasecmp(arg1, "file")) config.typeOfAction = 0x01;
+ if(!strcasecmp(arg1, "file")) config.typeOfAction = 0x01;
else config.typeOfAction = 0x02;
- if(!strcasecmp(arg2, "deny")) config.typeOfAction += 0x10;
+ if(!strcasecmp(arg2, "deny")) config.typeOfAction += 0x10;
else config.typeOfAction += 0x20;
return NULL;
}
*/
static const command_rec example_directives[] =
{
- AP_INIT_TAKE1("exampleEnabled", example_set_enabled, NULL, RSRC_CONF, "Enable or disable mod_example"),
- AP_INIT_TAKE1("examplePath", example_set_path, NULL, RSRC_CONF, "The path to whatever"),
- AP_INIT_TAKE2("exampleAction", example_set_action, NULL, RSRC_CONF, "Special action value!"),
+ AP_INIT_TAKE1("exampleEnabled", example_set_enabled, NULL, RSRC_CONF, "Enable or disable mod_example"),
+ AP_INIT_TAKE1("examplePath", example_set_path, NULL, RSRC_CONF, "The path to whatever"),
+ AP_INIT_TAKE2("exampleAction", example_set_action, NULL, RSRC_CONF, "Special action value!"),
{ NULL }
};
/*
*/
static int example_handler(request_rec *r)
{
- if(!r->handler || strcmp(r->handler, "example-handler")) return(DECLINED);
- ap_set_content_type(r, "text/plain");
- ap_rprintf(r, "Enabled: %u\n", config.enabled);
- ap_rprintf(r, "Path: %s\n", config.path);
- ap_rprintf(r, "TypeOfAction: %x\n", config.typeOfAction);
+ if(!r->handler || strcmp(r->handler, "example-handler")) return(DECLINED);
+ ap_set_content_type(r, "text/plain");
+ ap_rprintf(r, "Enabled: %u\n", config.enabled);
+ ap_rprintf(r, "Path: %s\n", config.path);
+ ap_rprintf(r, "TypeOfAction: %x\n", config.typeOfAction);
return OK;
}
static void register_hooks(apr_pool_t *pool)
{
config.enabled = 1;
- config.path = "/foo/bar";
+ config.path = "/foo/bar";
config.typeOfAction = 3;
ap_hook_handler(example_handler, NULL, NULL, APR_HOOK_LAST);
}
configuration set up for mod_rewrite:
</p>
<highlight language="config">
-<Directory "/var/www">
- RewriteCond %{HTTP_HOST} ^example.com$
- RewriteRule (.*) http://www.example.com/$1
+<Directory "/var/www">
+ RewriteCond "%{HTTP_HOST}" "^example.com$"
+ RewriteRule "(.*)" "http://www.example.com/$1"
</Directory>
-<Directory "/var/www/sub">
- RewriteRule ^foobar$ index.php?foobar=true
+<Directory "/var/www/sub">
+ RewriteRule "^foobar$" "index.php?foobar=true"
</Directory>
</highlight>
<p>
where you have a parent configuration and a child, such as the following:
</p>
<highlight language="config">
-<Directory "/var/www">
+<Directory "/var/www">
ExampleEnabled On
- ExamplePath /foo/bar
+ ExamplePath "/foo/bar"
ExampleAction file allow
</Directory>
-<Directory "/var/www/subdir">
+<Directory "/var/www/subdir">
ExampleAction file deny
</Directory>
</highlight>
how the module works:
</p>
<highlight language="config">
-<Location "/a">
+<Location "/a">
SetHandler example-handler
ExampleEnabled on
- ExamplePath "/foo/bar"
+ ExamplePath "/foo/bar"
ExampleAction file allow
</Location>
-<Location "/a/b">
+<Location "/a/b">
ExampleAction file deny
ExampleEnabled off
</Location>
-<Location "/a/b/c">
+<Location "/a/b/c">
ExampleAction db deny
- ExamplePath "/foo/bar/baz"
+ ExamplePath "/foo/bar/baz"
ExampleEnabled on
</Location>
</highlight>
static const command_rec directives[] =
{
- AP_INIT_TAKE1("exampleEnabled", example_set_enabled, NULL, ACCESS_CONF, "Enable or disable mod_example"),
- AP_INIT_TAKE1("examplePath", example_set_path, NULL, ACCESS_CONF, "The path to whatever"),
- AP_INIT_TAKE2("exampleAction", example_set_action, NULL, ACCESS_CONF, "Special action value!"),
+ AP_INIT_TAKE1("exampleEnabled", example_set_enabled, NULL, ACCESS_CONF, "Enable or disable mod_example"),
+ AP_INIT_TAKE1("examplePath", example_set_path, NULL, ACCESS_CONF, "The path to whatever"),
+ AP_INIT_TAKE2("exampleAction", example_set_action, NULL, ACCESS_CONF, "Special action value!"),
{ NULL }
};
*/
static int example_handler(request_rec *r)
{
- if(!r->handler || strcmp(r->handler, "example-handler")) return(DECLINED);
+ if(!r->handler || strcmp(r->handler, "example-handler")) return(DECLINED);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
example_config *config = (example_config *) ap_get_module_config(r->per_dir_config, &example_module);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
- ap_set_content_type(r, "text/plain");
- ap_rprintf(r, "Enabled: %u\n", config->enabled);
- ap_rprintf(r, "Path: %s\n", config->path);
- ap_rprintf(r, "TypeOfAction: %x\n", config->typeOfAction);
- ap_rprintf(r, "Context: %s\n", config->context);
+ ap_set_content_type(r, "text/plain");
+ ap_rprintf(r, "Enabled: %u\n", config->enabled);
+ ap_rprintf(r, "Path: %s\n", config->path);
+ ap_rprintf(r, "TypeOfAction: %x\n", config->typeOfAction);
+ ap_rprintf(r, "Context: %s\n", config->context);
return OK;
}
/*
=======================================================================================================================
- Handler for the "exampleEnabled" directive
+ Handler for the "exampleEnabled" directive
=======================================================================================================================
*/
const char *example_set_enabled(cmd_parms *cmd, void *cfg, const char *arg)
if(conf)
{
- if(!strcasecmp(arg, "on"))
+ if(!strcasecmp(arg, "on"))
conf->enabled = 1;
else
conf->enabled = 0;
/*
=======================================================================================================================
- Handler for the "examplePath" directive
+ Handler for the "examplePath" directive
=======================================================================================================================
*/
const char *example_set_path(cmd_parms *cmd, void *cfg, const char *arg)
/*
=======================================================================================================================
- Handler for the "exampleAction" directive ;
+ Handler for the "exampleAction" directive ;
Let's pretend this one takes one argument (file or db), and a second (deny or allow), ;
and we store it in a bit-wise manner.
=======================================================================================================================
if(conf)
{
{
- if(!strcasecmp(arg1, "file"))
+ if(!strcasecmp(arg1, "file"))
conf->typeOfAction = 0x01;
else
conf->typeOfAction = 0x02;
- if(!strcasecmp(arg2, "deny"))
+ if(!strcasecmp(arg2, "deny"))
conf->typeOfAction += 0x10;
else
conf->typeOfAction += 0x20;
*/
void *create_dir_conf(apr_pool_t *pool, char *context)
{
- context = context ? context : "Newly created configuration";
+ context = context ? context : "Newly created configuration";
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
example_config *cfg = apr_pcalloc(pool, sizeof(example_config));
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
example_config *base = (example_config *) BASE;
example_config *add = (example_config *) ADD;
- example_config *conf = (example_config *) create_dir_conf(pool, "Merged configuration");
+ example_config *conf = (example_config *) create_dir_conf(pool, "Merged configuration");
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
conf->enabled = (add->enabled == 0) ? base->enabled : add->enabled;
int i;
for (i = 0; &formData[i]; i++) {
if (formData[i].key && formData[i].value) {
- ap_rprintf(r, "%s = %s\n", formData[i].key, formData[i].value);
+ ap_rprintf(r, "%s = %s\n", formData[i].key, formData[i].value);
} else if (formData[i].key) {
- ap_rprintf(r, "%s\n", formData[i].key);
+ ap_rprintf(r, "%s\n", formData[i].key);
} else if (formData[i].value) {
- ap_rprintf(r, "= %s\n", formData[i].value);
+ ap_rprintf(r, "= %s\n", formData[i].value);
} else {
break;
}
fields = apr_table_elts(r->headers_in);
e = (apr_table_entry_t *) fields->elts;
for(i = 0; i < fields->nelts; i++) {
- ap_rprintf(r, "%s: %s\n", e[i].key, e[i].val);
+ ap_rprintf(r, "%s: %s\n", e[i].key, e[i].val);
}
return OK;
}
/*~~~~~~~~~~~~~~~~*/
if(util_read(r, &buffer, &size) == OK) {
- ap_rprintf(r, "We read a request body that was %" APR_OFF_T_FMT " bytes long", size);
+ ap_rprintf(r, "We read a request body that was %" APR_OFF_T_FMT " bytes long", size);
}
return OK;
}