DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

ISAPI URL Rewrite

06.29.2007
| 5124 views |
  • submit to reddit
        // description of your code here
#include <windows.h>
#include <httpfilt.h>

#define MAX_URL_LEN 4096

BOOL WINAPI GetFilterVersion(HTTP_FILTER_VERSION * pVer)
{
      pVer->dwFlags = (SF_NOTIFY_SECURE_PORT | SF_NOTIFY_NONSECURE_PORT | SF_NOTIFY_PREPROC_HEADERS | SF_NOTIFY_ORDER_HIGH);
      pVer->dwFilterVersion = HTTP_FILTER_REVISION;
      strcpy_s(pVer->lpszFilterDesc,9,"Blah blah blah");
      return TRUE;
}

void ReMapURLs(CHAR* pUrl,HTTP_FILTER_CONTEXT* pfc,PHTTP_FILTER_PREPROC_HEADERS pHeaders)
{
      if (pUrl[0] != '/') return;

      CHAR *iUrl = 0;
      BOOL doSet = FALSE;

      char *sOldUrls[] = { "/test/", "/TEST/" };
      char *sNewUrls[] = { "/go/", "/GO/" };

      for (int i=0; i<2; i++)
      {
            if (iUrl = strstr(pUrl,sOldUrls[i]))
            {
                  doSet = TRUE;
                  memcpy(iUrl,sNewUrls[i],strlen(sNewUrls[i]));
            }
      }

      if (doSet) pHeaders->SetHeader(pfc, "url", pUrl);
}

DWORD WINAPI HttpFilterProc(HTTP_FILTER_CONTEXT *pfc,DWORD NotificationType,VOID * pvData)
{
      PHTTP_FILTER_PREPROC_HEADERS pHeaders;
      DWORD cUrlOrig = MAX_URL_LEN;
      DWORD cUrl = cUrlOrig;
      CHAR rgUrl[MAX_URL_LEN];
      CHAR *pUrl;
      BOOL result;

      switch ( NotificationType )
      {
            case SF_NOTIFY_PREPROC_HEADERS:
                  pHeaders = (PHTTP_FILTER_PREPROC_HEADERS) pvData;
                  result = pHeaders->GetHeader(pfc, "url", rgUrl, &cUrl);

                  if (!result && cUrl > cUrlOrig)
                  {
                        pUrl = (CHAR*)LocalAlloc(0, cUrl);
                        result = pHeaders->GetHeader(pfc, "url", pUrl, &cUrl);
                        if (!result)
                        {
                              LocalFree(pUrl);
                              break;
                        }
                        ReMapURLs(pUrl, pfc, pHeaders);
                        LocalFree(pUrl);
                  }
                  else
                        ReMapURLs(rgUrl, pfc, pHeaders);
                  break;
            default:
                  break;
      }
      return SF_STATUS_REQ_NEXT_NOTIFICATION;
}