I wrote this ISAPI filter to rewrite the URL because we had some sites that moved locations... Basically the filter looks at the referrer, and if it's the local server, it looks at the requested URL and compared it to the full referrer. If the first path is identical, nothing is done, however if not, it takes the first path from the full referrer and prepends it to the URL. For example: /Content/imgs/img.jpg from a referrer of http://myserver/wr/apps/default.htm would be rewritten as /wr/Content/imgs/img.jpg.
I would like someone to look over it and school me a bit as I'm sure I need it. This was my first C program ever and I'm pretty sure there are mistakes that need to be fixed (even though it works now). For example, memory leaks???
Here's the code: - #include <windows.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <httpfilt.h>
-
#include <time.h>
-
#include <string.h>
-
-
-
#ifdef _DEBUG
-
#define TO_FILE // uncomment out to use a log file
-
#ifdef TO_FILE
-
#define DEST ghFile
-
#define DebugMsg(x) WriteToFile x;
-
HANDLE ghFile;
-
#define LOGFILE "W:\\Temp\\URLRedirector.log"
-
void WriteToFile (HANDLE hFile, char *szFormat, ...) {
-
char szBuf[1024];
-
DWORD dwWritten;
-
va_list list;
-
va_start (list, szFormat);
-
vsprintf (szBuf, szFormat, list);
-
hFile = CreateFile (LOGFILE, GENERIC_WRITE,
-
0, NULL, OPEN_ALWAYS,
-
FILE_ATTRIBUTE_NORMAL, NULL);
-
if (hFile != INVALID_HANDLE_VALUE) {
-
SetFilePointer (hFile, 0, NULL, FILE_END);
-
WriteFile (hFile, szBuf, lstrlen (szBuf), &dwWritten, NULL);
-
CloseHandle (hFile);
-
}
-
va_end (list);
-
}
-
#endif
-
#endif
-
-
BOOL WINAPI __stdcall GetFilterVersion(HTTP_FILTER_VERSION *pVer)
-
{
-
/* Specify the types and order of notification */
-
-
pVer->dwFlags = (SF_NOTIFY_ORDER_HIGH | SF_NOTIFY_SECURE_PORT | SF_NOTIFY_NONSECURE_PORT
-
| SF_NOTIFY_PREPROC_HEADERS | SF_NOTIFY_END_OF_NET_SESSION);
-
-
pVer->dwFilterVersion = HTTP_FILTER_REVISION;
-
-
strcpy(pVer->lpszFilterDesc, "URL Redirector, Version 1.0");
-
-
return TRUE;
-
}
-
-
DWORD WINAPI __stdcall HttpFilterProc(HTTP_FILTER_CONTEXT *pfc, DWORD NotificationType, VOID *pvData)
-
{
-
CHAR *pPhysPath;
-
PHTTP_FILTER_URL_MAP pURLMap;
-
PHTTP_FILTER_PREPROC_HEADERS pHeaderInfo;
-
CHAR szReferrer[255], szServer[255], szURL[255], szNewURL[255];
-
DWORD dwRSize = sizeof(szReferrer);
-
DWORD dwSSize = sizeof(szServer);
-
DWORD dwUSize = sizeof(szURL);
-
int iTmp, iTmp2;
-
CHAR *pos, tmp[255], tmp2[255];
-
-
switch (NotificationType) {
-
-
case SF_NOTIFY_PREPROC_HEADERS :
-
pHeaderInfo = (PHTTP_FILTER_PREPROC_HEADERS)pvData;
-
-
if (pfc->GetServerVariable(pfc, "HTTP_REFERER", szReferrer, &dwRSize))
-
{
-
DebugMsg(( DEST,
-
"Referrer: %s\r\n", szReferrer ));
-
-
if (pfc->GetServerVariable(pfc, "SERVER_NAME", szServer, &dwSSize))
-
DebugMsg(( DEST,
-
"Server Name: %s\r\n", szServer ));
-
-
if (pHeaderInfo->GetHeader(pfc, "URL", szURL, &dwUSize))
-
DebugMsg(( DEST,
-
"URL: %s\r\n", szURL ));
-
-
iTmp = strnstr(szReferrer, szServer, strlen(szReferrer));
-
if(iTmp > 0)
-
{
-
//Referred is our own server...
-
strcpy(tmp, szReferrer + iTmp);
-
DebugMsg(( DEST,
-
"tmp: %s - %d\r\n", tmp, strlen(tmp) ));
-
pos = strchr(tmp+1, '/');
-
DebugMsg(( DEST,
-
"pos: %s - %d\r\n", pos, strlen(pos) ));
-
-
iTmp2 = strlen(tmp) - strlen(pos) + 1;
-
-
strncpy(tmp2, tmp, iTmp2);
-
tmp2[iTmp2] = '\0';
-
DebugMsg(( DEST,
-
"tmp2: %s\r\n", tmp2));
-
-
if(strncmp(szURL, tmp2, iTmp2) != 0)
-
{
-
//First paths don't match, create new URL...
-
strncpy(szNewURL, tmp2, iTmp2-1);
-
strcat(szNewURL, szURL);
-
DebugMsg(( DEST,
-
"newURL: %s\r\n", szNewURL));
-
pHeaderInfo->SetHeader(pfc, "URL", szNewURL);
-
return SF_STATUS_REQ_HANDLED_NOTIFICATION;
-
}
-
}
-
}
-
-
break;
-
-
default :
-
-
break;
-
}
-
-
return SF_STATUS_REQ_NEXT_NOTIFICATION;
-
}
-
-
-
/* simple function to compare two strings and return the position at which the compare ended */
-
static int strnstr ( const char *string, const char *strCharSet, int n)
-
{
-
int len = (strCharSet != NULL ) ? ((int)strlen(strCharSet )) : 0 ;
-
int ret, I, J, found;
-
-
if ( 0 == n || 0 == len )
-
{
-
return -1;
-
}
-
-
ret = -1;
-
found = 0;
-
for (I = 0 ; I <= n - len && found != 1 ; I++)
-
{
-
J = 0 ;
-
for ( ; J < len ; J++ )
-
{
-
if (toupper(string[I + J]) != toupper(strCharSet [J]))
-
{
-
break; // Exit For(J)
-
}
-
}
-
-
if ( J == len)
-
{
-
ret = I + (J);
-
found = 1;
-
}
-
}
-
-
return ret;
-
}
-
0 1431 Sign in to post your reply or Sign up for a free account.
Similar topics
by: Irene |
last post by:
Hi,
I have an asp page that allows a user to search for info in a DB and
add info to a DB. The search uses "ADODB.Connection" objects in the
page, but the add will use a call to an isapi dll...
|
by: PCC |
last post by:
I need to be able to redirect web requests from certain parties to specific
content. In the old days I would have used ISAPI to do this. Now days I am
wondering if I should be doing this using...
|
by: Lionel Schiepers |
last post by:
I rencently tested an web application with ISAPI extensions that internally
use managed extensions but the call to the ISAPI failed. I receive the
following message: "A dynamic link library (DLL)...
|
by: Norton |
last post by:
I had a person tell me the other day that a person would not be able to beat
the efficiency of an ISAPI dll, especially by using handlers/modules. Of
course, they could only say the reason was that...
|
by: Jon Maz |
last post by:
Hi All,
I've been looking into options for URL Rewriting in .net, and to be honest,
I haven't seen anything that's easier than the old Classic Asp solution with
an ISAPI filter redirecting to an...
|
by: daberelay |
last post by:
Hi,
I'm using IHttpModule class (added to GAC and machine.config) to get
requests of non existing pages in my web server and redirect them to
different pages, using the...
|
by: yachtIT |
last post by:
HI MS,
I have read from official microsoft msdn that an HTTPhandler in .NET can do
anything a c++ isapi extension can do. Only a .NET HTTPhandler has a much
more easy programming model.
...
|
by: Jeeran |
last post by:
We use an ISAPI filter to convert long urls into short clean ones.
For example:
"Site.com/user/john/"
Is re-written as:
"Site.com/user/userinfo.aspx?uid=john"
Now, "userinfo.aspx" contains a...
|
by: trooperbill |
last post by:
.... and if so how (using basic asp, not .net)?
thanks
Mark
www.iosilver - Silver Jewellery
|
by: |
last post by:
Hi
here is the situation
we have IIS site with multiple host headers
based on host header we need to send to different URL's within the server
my current solution (hate to call it naive :)...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |