📄 cgiapp.cpp
字号:
// ----- cgi.cpp -----
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#define FOREVER 1
#define BUFF_SIZE 1024
#define LARGE_BUFF_SIZE 2048
#define _islower(_c) ((_c >='a' && _c <= 'z') ? 1 : 0)
#define _isupper(_c) ((_c >='A' && _c <= 'Z') ? 1 : 0)
#define _toupper(_c) ((_islower(_c)) ? _c -'a'+'A' : _c)
#define _tolower(_c) ((_isupper(_c)) ? _c -'A'+'a' : _c)
#define _isdigit(_c) ((_c >='0' && _c <= '9') ? 1 : 0)
#define _isascii(_c) ( (unsigned)(_c) < 0x80 )
#define _toascii(_c) ( (_c) & 0x7f )
#define _isspace(_c) (_c==' ' || _c=='\t' || _c=='\n')
char *szTheBreak = "<BR>\r\n";
HANDLE g_hDllInstance;
char *szBuf,
*szServerName = NULL,
*szServerSoftware = NULL,
*szGatewayInterface = NULL,
*szServerProtocol = NULL,
*szServerPort = NULL,
*szDocumentRoot = NULL,
*szRequestMethod = NULL,
*szHttpUserAgent = NULL,
*szHttpAccept = NULL,
*szHttpReferer = NULL,
*szHttpCookie = NULL,
*szScriptName = NULL,
*szQueryString = NULL,
*szRemoteHost = NULL,
*szRemoteAddr = NULL,
*szRemoteUser = NULL,
*szContentType = NULL,
*szContentLength = NULL,
*szAuthType = NULL,
*szContentFileName = NULL,
*szOutputFileName = NULL;
unsigned long ulContentLength = 0;
void cleanUp(void);
BOOL processQuery(void);
BOOL getEnvironment(LPCTSTR lpFileName);
void checkEnvironment(HANDLE hFile);
void writeEnvironmentVari(HANDLE hOutFile);
char * strdup(const char *s);
WCHAR * strwdup(const char *s);
int stricmp(const char *str1, const char *str2);
int strnicmp(const char *str1, const char *str2, size_t maxlen);
void WriteFileSpecial(char *buffer, HANDLE hFile);
char * getEnvData(char *environ, char *nameP);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPWSTR pszCmdLine,
int nCmdShow)
{
// MSG msg;
g_hDllInstance = hInst;
if(!pszCmdLine) // no command line (i.e file name)
return 0;
if(!getEnvironment(pszCmdLine)) // file exists?
return 0; // no!
processQuery();
cleanUp();
return 0;
}
BOOL processQuery(void)
{
WCHAR *wszOutputFileName = NULL, *wszContentFileName = NULL;
HANDLE hInFile, hOutFile;
DWORD dwRead = 0;
if(!szOutputFileName)
return FALSE;
if(!strlen(szOutputFileName))
return FALSE;
wszOutputFileName = strwdup(szOutputFileName);
hOutFile=CreateFile(wszOutputFileName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hOutFile==INVALID_HANDLE_VALUE) {
delete [] wszOutputFileName;
return FALSE;
}
//
// The following is example code, supply your own here
// This code:
// 1. Returns the environment variables
// 2. Reads data from the server (i.e POST) and returns it
//
WriteFileSpecial("Content-Type: text/plain\r\n", hOutFile);
// WriteFileSpecial("Set-Cookie: Customer=Wile_E_Coyote; path=/;\r\n", hOutFile); // cookie example
WriteFileSpecial("<HTML><HEAD>\r\n", hOutFile);
WriteFileSpecial("<TITLE>vxWeb CGI Test</TITLE></HEAD>\r\n", hOutFile);
WriteFileSpecial("<BODY><P>", hOutFile);
writeEnvironmentVari(hOutFile);
szBuf = NULL;
if (ulContentLength > 0 && szContentFileName)
{
DWORD dwActualRead = 0;
szBuf = new char[ulContentLength + 1];
wszContentFileName = strwdup((const char*)szContentFileName);
hInFile = CreateFile(wszContentFileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if(hInFile!=INVALID_HANDLE_VALUE) {
char szTemp[25];
while (dwRead < ulContentLength)
{
ReadFile(hInFile, &szBuf[dwRead], ulContentLength - dwRead, &dwActualRead, NULL);
dwRead += dwActualRead;
WriteFileSpecial("Read ", hOutFile);
_ltoa(dwActualRead, szTemp, 10);
WriteFileSpecial(szTemp, hOutFile);
WriteFileSpecial(" bytes from ", hOutFile);
WriteFileSpecial(szContentFileName, hOutFile);
WriteFileSpecial("\r\n", hOutFile);
if (!dwActualRead)
break;
}
CloseHandle(hInFile);
}
delete [] wszContentFileName;
}
// Now the the things we read.
if (szBuf)
{
DWORD dwWritten;
WriteFileSpecial("<BR>Here's The Data<BR>\r\n", hOutFile);
WriteFile(hOutFile, szBuf, dwRead, &dwWritten, NULL);
delete [] szBuf;
}
WriteFileSpecial("</P></BODY></HTML>\r\n", hOutFile);
// End Example code
CloseHandle(hOutFile);
if(wszOutputFileName)
delete [] wszOutputFileName;
return TRUE;
}
void writeEnvironmentVari(HANDLE hOutFile)
{
WriteFileSpecial("<h2>The Environment Variables</h2>\r\n", hOutFile);
WriteFileSpecial("SERVER_NAME = ", hOutFile);
if(szServerName)
WriteFileSpecial(szServerName, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("SERVER_SOFTWARE = ", hOutFile);
if(szServerSoftware)
WriteFileSpecial(szServerSoftware, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("GATEWAY_INTERFACE = ", hOutFile);
if(szGatewayInterface)
WriteFileSpecial(szGatewayInterface, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("SERVER_PROTOCOL = ", hOutFile);
if(szServerProtocol)
WriteFileSpecial(szServerProtocol, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("SERVER_PORT = ", hOutFile);
if(szServerPort)
WriteFileSpecial(szServerPort, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("DOCUMENT_ROOT = ", hOutFile);
if(szDocumentRoot)
WriteFileSpecial(szDocumentRoot, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("REQUEST_METHOD = ", hOutFile);
if(szRequestMethod)
WriteFileSpecial(szRequestMethod, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("HTTP_USER_AGENT = ", hOutFile);
if(szHttpUserAgent)
WriteFileSpecial(szHttpUserAgent, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("HTTP_ACCEPT = ", hOutFile);
if(szHttpAccept)
WriteFileSpecial(szHttpAccept, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("HTTP_REFERER = ", hOutFile);
if(szHttpReferer)
WriteFileSpecial(szHttpReferer, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("HTTP_COOKIE = ", hOutFile);
if(szHttpCookie)
WriteFileSpecial(szHttpCookie, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("SCRIPT_NAME = ", hOutFile);
if(szScriptName)
WriteFileSpecial(szScriptName, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("QUERY_STRING = ", hOutFile);
if(szQueryString)
WriteFileSpecial(szQueryString, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("REMOTE_HOST = ", hOutFile);
if(szRemoteHost)
WriteFileSpecial(szRemoteHost, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("REMOTE_ADDR = ", hOutFile);
if(szRemoteAddr)
WriteFileSpecial(szRemoteAddr, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("REMOTE_USER = ", hOutFile);
if(szRemoteUser)
WriteFileSpecial(szRemoteUser, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("CONTENT_TYPE = ", hOutFile);
if(szContentType)
WriteFileSpecial(szContentType, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("CONTENT_LENGTH = ", hOutFile);
if(szContentLength)
WriteFileSpecial(szContentLength, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("AUTH_TYPE = ", hOutFile);
if(szAuthType)
WriteFileSpecial(szAuthType, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("VX_CONTENT_FILE = ", hOutFile);
if(szContentFileName)
WriteFileSpecial(szContentFileName, hOutFile);
WriteFileSpecial(szTheBreak, hOutFile);
WriteFileSpecial("VX_OUTPUT_FILE = ", hOutFile);
WriteFileSpecial(szOutputFileName, hOutFile);
WriteFileSpecial("<BR><BR>\r\n", hOutFile);
}
void cleanUp(void)
{
if (szServerName)
delete [] szServerName;
if (szServerSoftware)
delete [] szServerSoftware;
if (szGatewayInterface)
delete [] szGatewayInterface;
if (szServerProtocol)
delete [] szServerProtocol;
if (szServerPort)
delete [] szServerPort;
if (szDocumentRoot)
delete [] szDocumentRoot;
if (szRequestMethod)
delete [] szRequestMethod;
if (szHttpUserAgent)
delete [] szHttpUserAgent;
if (szHttpAccept)
delete [] szHttpAccept;
if (szHttpReferer)
delete [] szHttpReferer;
if (szHttpCookie)
delete [] szHttpCookie;
if (szScriptName)
delete [] szScriptName;
if (szQueryString)
delete [] szQueryString;
if (szRemoteHost)
delete [] szRemoteHost;
if (szRemoteAddr)
delete [] szRemoteAddr;
if (szAuthType)
delete [] szAuthType;
if (szRemoteUser)
delete [] szRemoteUser;
if (szContentType)
delete [] szContentType;
if (szContentLength)
delete [] szContentLength;
if (szContentFileName)
delete [] szContentFileName;
if (szOutputFileName)
delete [] szOutputFileName;
}
BOOL getEnvironment(LPCTSTR lpFileName)
{
HANDLE hFile;
hFile = CreateFile(lpFileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if(hFile==INVALID_HANDLE_VALUE)
return FALSE;
checkEnvironment(hFile);
CloseHandle(hFile);
return TRUE;
}
void checkEnvironment(HANDLE hFile)
{
char szFileData[LARGE_BUFF_SIZE];
char *szBuf;
DWORD dwLen;
ReadFile(hFile, szFileData, LARGE_BUFF_SIZE, &dwLen, NULL);
if(!dwLen)
return;
if ((szBuf = getEnvData(szFileData, "SERVER_NAME")) != NULL)
{
szServerName = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "SERVER_SOFTWARE")) != NULL)
{
szServerSoftware = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "GATEWAY_INTERFACE")) != NULL)
{
szGatewayInterface = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "SERVER_PROTOCOL")) != NULL)
{
szServerProtocol = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "SERVER_PORT")) != NULL)
{
szServerPort = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "DOCUMENT_ROOT")) != NULL)
{
szDocumentRoot = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "REQUEST_METHOD")) != NULL)
{
szRequestMethod = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "HTTP_USER_AGENT")) != NULL)
{
szHttpUserAgent = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "HTTP_ACCEPT")) != NULL)
{
szHttpAccept = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "HTTP_REFERER")) != NULL)
{
szHttpReferer = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "HTTP_COOKIE")) != NULL)
{
szHttpCookie = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "SCRIPT_NAME")) != NULL)
{
szScriptName = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "QUERY_STRING")) != NULL)
{
szQueryString = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "REMOTE_HOST")) != NULL)
{
szRemoteHost = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "REMOTE_ADDR")) != NULL)
{
szRemoteAddr = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "REMOTE_USER")) != NULL)
{
szRemoteUser = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "CONTENT_TYPE")) != NULL)
{
szContentType = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "CONTENT_LENGTH")) != NULL)
{
szContentLength = strdup(szBuf);
ulContentLength = atol(szContentLength);
}
if ((szBuf = getEnvData(szFileData, "AUTH_TYPE")) != NULL)
{
szAuthType = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "VX_CONTENT_FILE")) != NULL)
{
szContentFileName = strdup(szBuf);
}
if ((szBuf = getEnvData(szFileData, "VX_OUTPUT_FILE")) != NULL)
{
szOutputFileName = strdup(szBuf);
}
}
//**************************************************************
char * strdup(const char *s)
{
char *p;
unsigned int n;
n = strlen(s) + 1;
if ((p = (char *)new char[n]) != NULL)
memcpy(p, s, n);
return (p);
}
WCHAR * strwdup(const char *s)
{
WCHAR *p;
unsigned int n;
n = (strlen(s) + 1);
if ((p = (WCHAR *)new WCHAR[n]) != NULL)
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, s, -1, p, n);
return (p);
}
int stricmp(const char *str1, const char *str2)
{
unsigned char c1, c2;
while ((c1 = (char)_toupper(*str1)) == (c2 = (char)_toupper(*str2)) && c1 != '\0')
{
str1++;
str2++;
}
return (c1 - c2);
}
int strnicmp(const char *str1, const char *str2, size_t maxlen)
{
while (_toupper(*str1) == _toupper(*str2) && *str1 != '\0' && maxlen != 0)
{
maxlen--;
str1++;
str2++;
}
if (maxlen == 0)
return (0);
else
return (_toupper(*str1) - _toupper(*str2));
}
void WriteFileSpecial(char *buffer, HANDLE hFile)
{
DWORD dwLen;
DWORD dwWritten;
dwLen=strlen(buffer);
WriteFile(hFile, buffer, dwLen, &dwWritten, NULL);
}
char * getEnvData(char *environ, char *nameP)
{
char *envP;
int len, iEnvLen;
len = strlen(nameP); /* save length of name */
envP = environ;
while(FOREVER) {
if (strnicmp(envP,nameP,len) == 0 && (envP)[len] == '=')
break;
iEnvLen=strlen(envP);
if(!iEnvLen) {
envP=NULL;
break;
}
envP=envP+iEnvLen+1;
}
if (envP)
return ((envP)+len+1); /* point past the '=' */
else
return (NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -