home *** CD-ROM | disk | FTP | other *** search
- // Lst15_10.cpp
-
- #include <stdio.h>
- #include <afx.h>
-
- // Global Cache for Post
- // You can only read from
- // Standard In Once
-
- TCHAR *szCache=NULL;
-
- TCHAR ConvertHex(TCHAR cHigh, TCHAR cLow)
- {
- static const TCHAR szHex[] = _T("0123456789ABCDEF");
-
- LPCTSTR pszLow;
- LPCTSTR pszHigh;
- TCHAR cValue;
-
- // Find the Values in the Hex String
- pszHigh = _tcschr(szHex, (TCHAR) _totupper(cHigh));
- pszLow = _tcschr(szHex, (TCHAR) _totupper(cLow));
-
- // If both Values Exist Then Calculate the Value
- // Based off of the string
- if (pszHigh && pszLow)
- {
- cValue = (TCHAR) (((pszHigh - szHex) << 4) + (pszLow - szHex));
- return (cValue);
- }
-
- return('?');
- }
-
- // Returns the String
- LPVOID TranslateCGI(LPTSTR pszString)
- {
- LPTSTR pszIndex = pszString;
- LPTSTR pszReturn = pszString;
-
- // unescape special characters
-
- while (*pszIndex)
- {
- // Translate '+' to Spaces
- if (*pszIndex == _T('+'))
- *pszReturn++ = _T(' ');
-
- // Translate Hex Strings to characters
- else if (*pszIndex == _T('%'))
- {
- *pszReturn++=ConvertHex(pszIndex[1], pszIndex[2]);
- pszIndex+=2;
- }
-
- // or just copy the character
- else
- *pszReturn++ = *pszIndex;
- pszIndex++;
- }
-
- // Terminate the End
- *pszReturn = '\0';
-
- return (LPVOID) pszString;
- }
-
- DWORD GetValue(LPTSTR szCGI, LPTSTR szName, LPTSTR szValue, DWORD dwValueSize)
- {
- LPTSTR szIndex;
- LPTSTR szEnd;
- DWORD dwReturnSize=0;
-
- // Find The Name in the Query String
- szIndex=_tcsstr(szCGI,szName);
-
- // Error: The Name part of the Name value pair doesn't exist
- if (!szIndex)
- return (0);
-
- // Increase the pointer passed the Name and Get to the Value
- szIndex+=_tcslen(szName)+1;
-
- // Find the End of the Value by looking for the '&'
- szEnd=_tcschr(szCGI,_T('&'));
-
- // if we find a '&' set it as the end
- if (szEnd)
- (*szEnd)='\0';
-
- // Remove the CGI Syntax
- TranslateCGI(szIndex);
-
- // Calculate the Value Size
- dwReturnSize=_tcslen(szIndex);
-
- // Chop the Value if bigger then the Allocation of Value
- if (dwReturnSize>dwValueSize)
- szIndex[dwValueSize]=_T('\0');
-
- // Assign the Value if there is allocated space
- // if no space has been allocated then the caller
- // is just looking for the string size
- if (szValue)
- _tcscpy(szValue,szIndex);
-
- // If we are going to return the size of
- // Allocated space we new might as well
- // include the Null
- return (dwReturnSize+1);
- }
-
- // Returns The Length of szValue on successful execution else returns 0
- DWORD GetMethod(LPTSTR szName, LPTSTR szValue, DWORD dwValueSize)
- {
- DWORD dwBufferSize=0;
- DWORD dwReturnSize=0;
- LPTSTR szQuery=NULL;
-
- // Call GetEnvironmentVariable To get the buffer size
- dwBufferSize=GetEnvironmentVariable(_T("QUERY_STRING"),szQuery,dwBufferSize);
-
- // Error: QUERY_STRING doesn't exist
- if (!dwBufferSize)
- return(0);
-
- // Allocate the Need Space
- szQuery = new TCHAR[dwBufferSize];
-
- // Call Again
- dwBufferSize=GetEnvironmentVariable(_T("QUERY_STRING"),szQuery,dwBufferSize);
-
- // Get the Value From the Query String
- dwReturnSize=GetValue(szQuery,szName,szValue,dwValueSize);
-
- delete szQuery;
-
- return (dwReturnSize);
- }
-
- // Returns The Content Length on successful execution else returns 0
- DWORD GetContentLength()
- {
- DWORD dwBufferSize=0;
- LPTSTR szContentLength=NULL;
- DWORD dwContentLength;
-
- // Call GetEnvironmentVariable to get the buffer size
- dwBufferSize=GetEnvironmentVariable(_T("CONTENT_LENGTH"),szContentLength,dwBufferSize);
-
- // Error: CONTENT_LENGTH doesn't exist
- if (!dwBufferSize)
- return(0);
-
- // Allocate the Need Space
- szContentLength = new TCHAR[dwBufferSize];
-
- // Call Again
- dwBufferSize=GetEnvironmentVariable(_T("CONTENT_LENGTH"),szContentLength,dwBufferSize);
-
- // Change the String to a usable form
- dwContentLength=(DWORD)_ttoi(szContentLength);
-
- delete szContentLength;
-
- return(dwContentLength);
- }
-
- // Returns The Length of szValue on successful execution else returns 0
- DWORD PostMethod(LPTSTR szName, LPTSTR szValue, DWORD dwValueSize)
- {
- DWORD dwBufferSize;
- LPTSTR szContentType=NULL;
- DWORD dwContentTypeSize=0;
- LPTSTR szPost;
- DWORD dwReturnSize=0;
- UINT nCount;
-
- if (szCache)
- {
- dwBufferSize=_tcslen(szCache);
- // Allocate Some Memory for szPost Plus the NULL
- szPost=new TCHAR[dwBufferSize+1];
- _tcscpy(szPost,szCache);
- }
- else
- {
- // Look at the CONTENT_TYPE to see if it is a POST
-
- // Call GetEnvironmentVariable to get the buffer size
- dwContentTypeSize=GetEnvironmentVariable(_T("CONTENT_TYPE"),szContentType,dwContentTypeSize);
-
- // Error: CONTENT_TYPE doesn't exist
- if (!dwContentTypeSize)
- return(0);
-
- // Allocate the Need Space
- szContentType = new TCHAR[dwContentTypeSize];
-
- // Call Again
- GetEnvironmentVariable(_T("CONTENT_TYPE"),szContentType,dwContentTypeSize);
-
- if (!_tcscmp(szContentType,_T("application/x-www-form-urlencoded")))
- {
- // Figure out the Size of the String
- dwBufferSize=GetContentLength();
-
- if (!dwBufferSize)
- return(0);
-
- // Declare the Memory for the String plus the NULL
- szPost = new TCHAR[dwBufferSize+1];
-
- nCount=0;
-
- // Read the Standard In
- while (!feof(stdin) && (nCount<dwBufferSize))
- {
- szPost[nCount++]=(TCHAR)_fgetchar();
- }
-
- szPost[nCount]=_T('\0');
-
- // Cache the CGI String
- szCache=new TCHAR[dwBufferSize+1];
- _tcscpy(szCache,szPost);
- }
- else
- {
- // Not a POST so return unsuccessfull
- return(0);
- }
- }
-
- // We now have the CGI String, Lets Get the Value
-
- // Get the Value From the Post String
- dwReturnSize=GetValue(szPost,szName,szValue,dwValueSize);
-
- delete szPost;
-
- // If we are going to return the size of
- // Allocated space we new might as well
- // include the Null
- return (dwReturnSize);
-
- return(0);
- };
-
- // Returns The Length of szValue on successful execution else returns 0
- DWORD GetParameter (LPTSTR szName, LPTSTR szValue, DWORD dwValueSize)
- {
- DWORD dwBufferSize=0;
- LPTSTR szRequestMethod=NULL;
-
- // Look at the enviorment variable REQUEST_METHOD
-
- // Call GetEnvironmentVariable to get the buffer size
- dwBufferSize=GetEnvironmentVariable(_T("REQUEST_METHOD"),szRequestMethod,dwBufferSize);
-
- // Error: REQUEST_METHOD doesn't exist
- if (!dwBufferSize)
- return(0);
-
- // Allocate the Need Space
- szRequestMethod = new TCHAR[dwBufferSize];
-
- // Call Again
- dwBufferSize=GetEnvironmentVariable(_T("REQUEST_METHOD"),szRequestMethod,dwBufferSize);
-
- // It's has to be POST or GET
- if (!_tcscmp(szRequestMethod,_T("GET")))
- {
- delete szRequestMethod;
- return(GetMethod(szName,szValue,dwValueSize));
- }
-
- if (!_tcscmp(szRequestMethod,_T("POST")))
- {
- delete szRequestMethod;
- return(PostMethod(szName,szValue,dwValueSize));
- }
-
- delete szRequestMethod;
-
- return(0);
- };
-
- int main( int argc, char *argv[ ], char *envp[ ] )
- {
-
- DWORD dwValueSize=0;
- LPTSTR szValue=NULL;
-
- // Header
-
- _tprintf(_T("Status: 200\r\n"));
- _tprintf(_T("Content-type: text/html\r\n"));
- _tprintf(_T("\r\n"));
-
- // Body
-
- _tprintf(_T("<HTML><BODY>\n"));
-
- // Find out how big the name parameters is going to be
- dwValueSize=GetParameter(_T("Name"),szValue,dwValueSize);
-
- // Allocate enough space for The Value of Name
- szValue=new TCHAR[dwValueSize];
-
- // Get The Value Again this time with a big enough buffer
- dwValueSize=GetParameter(_T("Name"),szValue,dwValueSize);
-
- // Display the Name
- if (dwValueSize)
- _tprintf(_T("Name : %s\n"),szValue);
-
- delete szValue;
-
- _tprintf(_T("<BR>\n"));
-
- // Do it all Again for Age
- dwValueSize=GetParameter(_T("Age"),szValue,dwValueSize);
- szValue=new TCHAR[dwValueSize];
- dwValueSize=GetParameter(_T("Age"),szValue,dwValueSize);
-
- if (dwValueSize)
- _tprintf(_T("Age : %s\n"),szValue);
-
- _tprintf(_T("</BODY></HTML>\n"));
-
- delete szValue;
-
- // Clean the Cache
-
- if (szCache)
- delete szCache;
-
- return(0);
- }
-