Adding a tilde to directory names (ISAPI filter)

Here is a simple example using an ISAPI filter to add a tilde to directory names. It is written in C.

/*
 * Add a tilde to directory names
 *
 * This ISAPI filter will change URLs of the form 'isp.com/USER/stuff'
 * to the form 'isp.com/~USER/stuff'
 *
 */

/* Include ISAPI definitions and constants */
#include <httpfilt.h>
#include <string.h>

/* Maximum length of a URI */
#define MAX_URI_SIZE 4065

/* This function is called when the filter gets loaded by the web server */
BOOL WINAPI
GetFilterVersion( HTTP_FILTER_VERSION *pVer )
{
  /* Set the filter version */
  pVer->dwFilterVersion = HTTP_FILTER_REVISION;

  /* Set a description for this filter */
  strncpy( pVer->lpszFilterDesc, "Tilde Insertion Filter", SF_MAX_FILTER_DESC_LEN );

  /* Ask to be notified when the HTTP headers have been processed */
  pVer->dwFlags = SF_NOTIFY_PREPROC_HEADERS;

  return TRUE;
}

/* This function is called for every HTTP request */
DWORD WINAPI
HttpFilterProc( PHTTP_FILTER_CONTEXT pfc,
		DWORD notificationType,
		VOID *pvNotificationInfo )
{
  /* This is a notification of type SF_NOTIFY_PREPROC_HEADERS, therefore the argument
   * pvNotificationInfo is of type HTTP_FILTER_PREPROC_HEADERS* */
  HTTP_FILTER_PREPROC_HEADERS* headers = (HTTP_FILTER_PREPROC_HEADERS*) pvNotificationInfo;

  /* Let the web server allocate (MAX_URI_SIZE+1) bytes of memory for us */
  char *CurrentURI = pfc->AllocMem( pfc, MAX_URI_SIZE+1, 0 );
  int URILength = MAX_URI_SIZE;

  /* Copy the header 'url' to the address (CurrentURI+1) */
  if( !headers->GetHeader( pfc, "url", CurrentURI+1, &URILength )) {
    /* The URI was not fetched successfully */
    return SF_STATUS_REQ_ERROR;
  }

  if( *(CurrentURI+2) == '~' || strrchr( CurrentURI+1, '/') == CurrentURI+1) {
    /* Don't add a tilde if it's already there or if the URI is a file in the root directory */
    return SF_STATUS_REQ_NEXT_NOTIFICATION;
  }

  /* Copy the leading slash (currently at index 1) to index 0 */
  *CurrentURI = *(CurrentURI+1);
  /* Copy a tilde character to index 1 */
  *(CurrentURI+1) = '~';

  /* Set the new header */
  if( !headers->SetHeader( pfc, "url", CurrentURI )) {
    /* The URI was not re-set successfully */
    return SF_STATUS_REQ_ERROR;
  };

  /* Return, instructing the server to notify us next time */
  return SF_STATUS_REQ_NEXT_NOTIFICATION;
}
Content Manager [Administrator] 16 December 2005  Permalink  
Download Free Trial

Recent Articles

Other Resources



www.zeus.com