You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Richard Jones <ri...@annexia.org> on 2004/01/04 15:55:05 UTC

mod_acquire - Zope-like acquisition for Apache

Attached below is a highly experimental module which I hacked together
in an hour or so to add "acquisition" (a la Zope) to Apache.

It's mainly for comment.  Is it the correct approach?  Are there other
Apache modules already available which can add acquisition?

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
http://www.winwinsales.co.uk/ - CRM improvement consultancy

----------------------------------------------------------------------
/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2004 Merjis Ltd.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 */

/*
 * mod_acquire: Implement Zope-like acquisition for Apache.
 *
 * By Richard W.M. Jones <ri...@annexia.org>.
 * Copyright (C) 2004 Merjis Ltd.  http://www.merjis.com/
 */

#include "httpd.h"
#include "http_config.h"
#include "http_request.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_main.h"
#include "http_log.h"
#include "util_script.h"

typedef struct {
  int acquisition;		/* Set if enabled for this directory. */
} acquire_dir_config;

module acquire_module;

static void *
create_acquire_dir_config (pool *p, char *dummy)
{
  acquire_dir_config *new =
    (acquire_dir_config *) ap_palloc(p, sizeof (acquire_dir_config));

  new->acquisition = 0;
  return new;
}

static void *
merge_acquire_dir_configs (pool *p, void *basev, void *addv)
{
  acquire_dir_config *base = (acquire_dir_config *) basev;
  acquire_dir_config *add = (acquire_dir_config *) addv;
  acquire_dir_config *new =
    (acquire_dir_config *) ap_palloc (p, sizeof (acquire_dir_config));

  new->acquisition = add->acquisition || base->acquisition;
  return new;
}

static const char *
acquire (cmd_parms *cmd, acquire_dir_config *m, int enabled)
{
  m->acquisition = enabled;
  return NULL;
}

static const command_rec acquire_cmds[] =
{
  {"Acquire", acquire, NULL, ACCESS_CONF | RSRC_CONF, FLAG,
   "enable acquisition in this scope"},
  {NULL}
};

static char *
rindex_from (char *str, char *from, char c)
{
  while (str <= from)
    {
      if (*from == c) return from;
      from--;
    }
  return 0;
}

static int
no_file (request_rec *r)
{
  return
    r->uri[strlen (r->uri) - 1] != '/' &&
    (r->finfo.st_mode == 0 || (r->path_info && *r->path_info));
}

static char *
uri_above (request_rec *r)
{
  char *pi, *pj, *new_uri;
  int i, j;

  pi = rindex (r->uri, '/');
  if (pi == 0 || pi == r->uri) return 0;
  i = pi - r->uri;

  pj = rindex_from (r->uri, pi-1, '/');
  if (pj == 0) return 0;
  j = pj - r->uri;

  new_uri = ap_pstrdup (r->pool, r->uri);
  memmove (new_uri + j, new_uri + i, strlen (new_uri + i) + 1);
  return new_uri;
}

static int
acquire_fixups (request_rec *r)
{
  acquire_dir_config *conf = (acquire_dir_config *)
    ap_get_module_config (r->per_dir_config, &acquire_module);

  if (! conf->acquisition) return DECLINED;

  /* If the file exists, the core default_handler or another handler
   * will serve it.
   */
  if (! no_file (r)) return DECLINED;

  /* Otherwise, it's a missing file, so a candidate for acquisition. */
  r->handler = "acquire-handler";
  return OK;
}

static int
acquire_handler (request_rec *r)
{
  char *new_uri;

  /* This is a candidate for acquisition (file not found) so do it. */
  new_uri = uri_above (r);
  if (!new_uri) return DECLINED;

  /* XXX ap_escape_uri? */
  /* XXX ap_internal_redirect_handler? */
  ap_internal_redirect (new_uri, r);
  return OK;
}

static const handler_rec acquire_handlers[] =
{
    {"acquire-handler", acquire_handler},
    {NULL}
};

module acquire_module =
{
    STANDARD_MODULE_STUFF,
    NULL,			/* initializer */
    create_acquire_dir_config,	/* dir config creater */
    merge_acquire_dir_configs,	/* dir merger --- default is to override */
    NULL,			/* server config */
    NULL,			/* merge server config */
    acquire_cmds,		/* command table */
    acquire_handlers,		/* handlers */
    NULL,			/* filename translation */
    NULL,			/* check_user_id */
    NULL,			/* check auth */
    NULL,			/* check access */
    NULL,			/* type_checker */
    acquire_fixups,		/* fixups */
    NULL,			/* logger */
    NULL,			/* header parser */
    NULL,			/* child_init */
    NULL,			/* child_exit */
    NULL			/* post read-request */
};