473,322 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

C ISAPI Code (Redirect)...

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:
Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <httpfilt.h>
  5. #include <time.h>
  6. #include <string.h>
  7.  
  8.  
  9. #ifdef _DEBUG
  10. #define TO_FILE  // uncomment out to use a log file
  11. #ifdef TO_FILE
  12. #define DEST ghFile
  13. #define DebugMsg(x) WriteToFile x;
  14. HANDLE ghFile;
  15. #define LOGFILE "W:\\Temp\\URLRedirector.log"
  16. void WriteToFile (HANDLE hFile, char *szFormat, ...) {
  17.     char szBuf[1024];
  18.     DWORD dwWritten;
  19.     va_list list;
  20.     va_start (list, szFormat);
  21.     vsprintf (szBuf, szFormat, list);
  22.     hFile = CreateFile (LOGFILE, GENERIC_WRITE, 
  23.                         0, NULL, OPEN_ALWAYS, 
  24.                         FILE_ATTRIBUTE_NORMAL, NULL);
  25.     if (hFile != INVALID_HANDLE_VALUE) {
  26.         SetFilePointer (hFile, 0, NULL, FILE_END);
  27.         WriteFile (hFile, szBuf, lstrlen (szBuf), &dwWritten, NULL);
  28.         CloseHandle (hFile);
  29.     }
  30.     va_end (list);
  31. }
  32. #endif
  33. #endif
  34.  
  35. BOOL WINAPI __stdcall GetFilterVersion(HTTP_FILTER_VERSION *pVer)
  36. {
  37.     /* Specify the types and order of notification */
  38.  
  39.     pVer->dwFlags = (SF_NOTIFY_ORDER_HIGH | SF_NOTIFY_SECURE_PORT | SF_NOTIFY_NONSECURE_PORT
  40.                      | SF_NOTIFY_PREPROC_HEADERS | SF_NOTIFY_END_OF_NET_SESSION);
  41.  
  42.     pVer->dwFilterVersion = HTTP_FILTER_REVISION;
  43.  
  44.     strcpy(pVer->lpszFilterDesc, "URL Redirector, Version 1.0");
  45.  
  46.     return TRUE;
  47. }
  48.  
  49. DWORD WINAPI __stdcall HttpFilterProc(HTTP_FILTER_CONTEXT *pfc, DWORD NotificationType, VOID *pvData)
  50. {
  51.     CHAR *pPhysPath;
  52.     PHTTP_FILTER_URL_MAP pURLMap;
  53.     PHTTP_FILTER_PREPROC_HEADERS pHeaderInfo;
  54.     CHAR szReferrer[255], szServer[255], szURL[255], szNewURL[255];
  55.     DWORD dwRSize = sizeof(szReferrer);
  56.     DWORD dwSSize = sizeof(szServer);
  57.     DWORD dwUSize = sizeof(szURL);
  58.     int iTmp, iTmp2;
  59.     CHAR *pos, tmp[255], tmp2[255];
  60.  
  61.     switch (NotificationType) {
  62.  
  63.         case SF_NOTIFY_PREPROC_HEADERS :
  64.             pHeaderInfo = (PHTTP_FILTER_PREPROC_HEADERS)pvData;
  65.  
  66.             if (pfc->GetServerVariable(pfc, "HTTP_REFERER", szReferrer, &dwRSize))
  67.             {
  68.                 DebugMsg(( DEST,
  69.                            "Referrer: %s\r\n", szReferrer ));
  70.  
  71.                 if (pfc->GetServerVariable(pfc, "SERVER_NAME", szServer, &dwSSize))
  72.                     DebugMsg(( DEST,
  73.                                "Server Name: %s\r\n", szServer ));
  74.  
  75.                 if (pHeaderInfo->GetHeader(pfc, "URL", szURL, &dwUSize))
  76.                     DebugMsg(( DEST,
  77.                                "URL: %s\r\n", szURL ));
  78.  
  79.                 iTmp = strnstr(szReferrer, szServer, strlen(szReferrer));
  80.                 if(iTmp > 0)
  81.                 {
  82.                     //Referred is our own server...
  83.                     strcpy(tmp, szReferrer + iTmp);
  84.                     DebugMsg(( DEST,
  85.                                "tmp: %s - %d\r\n", tmp, strlen(tmp) ));
  86.                     pos = strchr(tmp+1, '/');
  87.                     DebugMsg(( DEST,
  88.                                "pos: %s - %d\r\n", pos, strlen(pos) ));
  89.  
  90.                     iTmp2 = strlen(tmp) - strlen(pos) + 1;
  91.  
  92.                     strncpy(tmp2, tmp, iTmp2);
  93.                     tmp2[iTmp2] = '\0';
  94.                     DebugMsg(( DEST,
  95.                                "tmp2: %s\r\n", tmp2));
  96.  
  97.                     if(strncmp(szURL, tmp2, iTmp2) != 0)
  98.                     {
  99.                         //First paths don't match, create new URL...
  100.                         strncpy(szNewURL, tmp2, iTmp2-1);
  101.                         strcat(szNewURL, szURL);
  102.                         DebugMsg(( DEST,
  103.                                    "newURL: %s\r\n", szNewURL));
  104.                         pHeaderInfo->SetHeader(pfc, "URL", szNewURL);
  105.                         return SF_STATUS_REQ_HANDLED_NOTIFICATION;
  106.                     }
  107.                 }
  108.             }
  109.  
  110.             break;
  111.  
  112.         default :
  113.  
  114.             break;    
  115.     }
  116.  
  117.     return SF_STATUS_REQ_NEXT_NOTIFICATION;
  118. }
  119.  
  120.  
  121. /* simple function to compare two strings and return the position at which the compare ended */
  122. static int strnstr ( const char *string, const char *strCharSet, int n)
  123. {
  124.     int len = (strCharSet  != NULL ) ? ((int)strlen(strCharSet )) : 0 ;
  125.     int ret, I, J, found;
  126.  
  127.     if ( 0 == n || 0 == len )
  128.     {
  129.         return -1;
  130.     }
  131.  
  132.     ret = -1;
  133.     found = 0;
  134.     for (I = 0 ; I <= n - len && found != 1 ; I++)
  135.     {
  136.         J = 0 ;
  137.         for ( ; J < len ; J++ )
  138.         {
  139.             if (toupper(string[I + J]) != toupper(strCharSet [J]))
  140.             {
  141.                 break; // Exit For(J)
  142.             }
  143.         }
  144.  
  145.         if ( J == len)
  146.         {
  147.             ret = I + (J);
  148.             found = 1;
  149.         } 
  150.     }
  151.  
  152.     return ret;
  153. }
  154.  
Mar 13 '10 #1
0 1431

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
3
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...
3
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)...
2
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...
2
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...
8
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...
2
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. ...
4
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...
6
by: trooperbill | last post by:
.... and if so how (using basic asp, not .net)? thanks Mark www.iosilver - Silver Jewellery
0
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 :)...
0
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...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.