⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 GOAHEAD WEBSERVER嵌入式的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
	p = lpwAlign(p);

/*
 *	Now start with the first item (Product Identifier)
 */
	lStyle = SS_CENTER | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
	*p++ = LOWORD(lStyle);
	*p++ = HIWORD(lStyle);
	*p++ = 0;			/* LOWORD (lExtendedStyle) */
	*p++ = 0;			/* HIWORD (lExtendedStyle) */
	*p++ = 10;			/* x */
	*p++ = 10;			/* y  */
	*p++ = 180;			/* cx */
	*p++ = 15;			/* cy */
	*p++ = 1;			/* ID */

/*
 *	Fill in class i.d., this time by name
 */
	nchar = nCopyAnsiToWideChar(p, TEXT("STATIC"));
	p += nchar;

/*
 *	Copy the text of the first item
 */
	nchar = nCopyAnsiToWideChar(p, 
		TEXT("GoAhead WebServer 2.1.8"));
	p += nchar;

/*
 *	Advance pointer over nExtraStuff WORD
 */
	*p++ = 0;  

/*
 *	Make sure the next item starts on a DWORD boundary
 */
	p = lpwAlign(p);

/*
 *	Next, the Copyright Notice.
 */
	lStyle = SS_CENTER | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
	*p++ = LOWORD(lStyle);
	*p++ = HIWORD(lStyle);
	*p++ = 0;			/* LOWORD (lExtendedStyle) */
	*p++ = 0;			/* HIWORD (lExtendedStyle) */
	*p++ = 10;			/* x */
	*p++ = 30;			/* y  */
	*p++ = 180;			/* cx */
	*p++ = 15;			/* cy */
	*p++ = 1;			/* ID */

/*
 *	Fill in class i.d. by name
 */
	nchar = nCopyAnsiToWideChar(p, TEXT("STATIC"));
	p += nchar;

/*
 *	Copy the text of the item
 */
	nchar = nCopyAnsiToWideChar(p, GOAHEAD_COPYRIGHT);
	p += nchar;

/*
 *	Advance pointer over nExtraStuff WORD
 */
	*p++ = 0;  
/*
 *	Make sure the next item starts on a DWORD boundary
 */
	p = lpwAlign(p);

/*
 *	Add third item ("Version:")
 */
	lStyle = SS_RIGHT | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
	*p++ = LOWORD(lStyle);
	*p++ = HIWORD(lStyle);
	*p++ = 0;          
	*p++ = 0;
	*p++ = 28;
	*p++ = 50;
	*p++ = 70;
	*p++ = 10;
	*p++ = 1;
	nchar = nCopyAnsiToWideChar(p, T("STATIC"));
	p += nchar;
	nchar = nCopyAnsiToWideChar(p, T("Version:"));
	p += nchar;
	*p++ = 0;

/*
 *	Add fourth Item (IDC_VERSION)
 */
	p = lpwAlign(p);
	lStyle = SS_LEFT | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
	*p++ = LOWORD(lStyle);
	*p++ = HIWORD(lStyle);
	*p++ = 0;          
	*p++ = 0;
	*p++ = 102;
	*p++ = 50;
	*p++ = 70;
	*p++ = 10;
	*p++ = IDC_VERSION;
	nchar = nCopyAnsiToWideChar(p, T("STATIC"));
	p += nchar;
	nchar = nCopyAnsiToWideChar(p, T("version"));
	p += nchar;
	*p++ = 0;
/*
 *	Add fifth item ("Build Date:")
 */
	p = lpwAlign(p);
	lStyle = SS_RIGHT | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
	*p++ = LOWORD(lStyle);
	*p++ = HIWORD(lStyle);
	*p++ = 0;          
	*p++ = 0;
	*p++ = 28;
	*p++ = 65;
	*p++ = 70;
	*p++ = 10;
	*p++ = 1;
	nchar = nCopyAnsiToWideChar(p, T("STATIC"));
	p += nchar;
	nchar = nCopyAnsiToWideChar(p, T("Build Date:"));
	p += nchar;
	*p++ = 0;
/*
 *	Add sixth item (IDC_BUILDDATE)
 */
	p = lpwAlign(p);
	lStyle = SS_LEFT | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
	*p++ = LOWORD(lStyle);
	*p++ = HIWORD(lStyle);
	*p++ = 0;          
	*p++ = 0;
	*p++ = 102;
	*p++ = 65;
	*p++ = 70;
	*p++ = 10;
	*p++ = IDC_BUILDDATE;
	nchar = nCopyAnsiToWideChar(p, T("STATIC"));
	p += nchar;
	nchar = nCopyAnsiToWideChar(p, T("Build Date"));
	p += nchar;
	*p++ = 0;
/*
 *	Add seventh item (IDOK)
 */
	p = lpwAlign(p);
	lStyle = BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
	*p++ = LOWORD(lStyle);
	*p++ = HIWORD(lStyle);
	*p++ = 0;          
	*p++ = 0;
	*p++ = 80;
	*p++ = 80;
	*p++ = 40;
	*p++ = 10;
	*p++ = IDOK;
	nchar = nCopyAnsiToWideChar(p, T("BUTTON"));
	p += nchar;
	nchar = nCopyAnsiToWideChar(p, T("OK"));
	p += nchar;
	*p++ = 0;

	hwndReturn = CreateDialogIndirect(hInstance, 
		(LPDLGTEMPLATE) pdlgtemplate, hwnd, (DLGPROC) websAboutProc);

	LocalFree(LocalHandle(pdlgtemplate));

	return 0;
}

/******************************************************************************/
/*
 *	Test Javascript binding for ASP. This will be invoked when "aspTest" is
 *	embedded in an ASP page. See web/asp.asp for usage. Set browser to 
 *	"localhost/asp.asp" to test.
 */

static int aspTest(int eid, webs_t wp, int argc, char_t **argv)
{
	char_t	*name, *address;

	if (ejArgs(argc, argv, T("%s %s"), &name, &address) < 2) {
		websError(wp, 400, T("Insufficient args\n"));
		return -1;
	}
	return websWrite(wp, T("Name: %s, Address %s"), name, address);
}

/******************************************************************************/
/*
 *	Test form for posted data (in-memory CGI). This will be called when the
 *	form in web/forms.asp is invoked. Set browser to "localhost/forms.asp" to test.
 */

static void formTest(webs_t wp, char_t *path, char_t *query)
{
	char_t	*name, *address;

	name = websGetVar(wp, T("name"), T("Joe Smith")); 
	address = websGetVar(wp, T("address"), T("1212 Milky Way Ave.")); 

	websHeader(wp);
	websWrite(wp, T("<body><h2>Name: %s, Address: %s</h2>\n"), name, address);
	websFooter(wp);
	websDone(wp, 200);
}

/******************************************************************************/
/*
 *	Home page handler
 */

static int websHomePageHandler(webs_t wp, char_t *urlPrefix, char_t *webDir,
							int arg, char_t *url, char_t *path, char_t *query)
{
/*
 *	If the empty or "/" URL is invoked, redirect default URLs to the home page
 */
	if (*url == '\0' || gstrcmp(url, T("/")) == 0) {
		websRedirect(wp, T("home.asp"));
		return 1;
	}
	return 0;
}

/******************************************************************************/
/*
 *	Default error handler.  The developer should insert code to handle
 *	error messages in the desired manner.
 */

void defaultErrorHandler(int etype, char_t *msg)
{
#if 0
	write(1, msg, gstrlen(msg));
#endif
}

/******************************************************************************/
/*
 *	Trace log. Customize this function to log trace output
 */

void defaultTraceHandler(int level, char_t *buf)
{
/*
 *	The following code would write all trace regardless of level
 *	to stdout.
 */
#if 0
	if (buf) {
		write(1, buf, gstrlen(buf));
	}
#endif
}

/******************************************************************************/
/*
 *	Returns a pointer to an allocated qualified unique temporary file name.
 *	This filename must eventually be deleted with bfree().
 */

char_t *websGetCgiCommName()
{
	char_t	*pname1, *pname2;

	pname1 = tempnam(NULL, T("cgi"));
	pname2 = bstrdup(B_L, pname1);
	free(pname1);
	return pname2;
}

/******************************************************************************/
/*
 *	Convert a table of strings into a single block of memory.
 *	The input table consists of an array of null-terminated strings,
 *	terminated in a null pointer.
 *	Returns the address of a block of memory allocated using the balloc() 
 *	function.  The returned pointer must be deleted using bfree().
 *	Returns NULL on error.
 */

static unsigned char *tableToBlock(char **table)
{
    unsigned char	*pBlock;		/*  Allocated block */
    char			*pEntry;		/*  Pointer into block      */
    size_t			sizeBlock;		/*  Size of table           */
    int				index;			/*  Index into string table */

    a_assert(table);

/*  
 *	Calculate the size of the data block.  Allow for final null byte. 
 */
    sizeBlock = 1;                    
    for (index = 0; table[index]; index++) {
        sizeBlock += strlen(table[index]) + 1;
	}

/*
 *	Allocate the data block and fill it with the strings                   
 */
    pBlock = balloc(B_L, sizeBlock);

	if (pBlock != NULL) {
		pEntry = (char *) pBlock;

        for (index = 0; table[index]; index++) {
			strcpy(pEntry, table[index]);
			pEntry += strlen(pEntry) + 1;
		}

/*		
 *		Terminate the data block with an extra null string                
 */
		*pEntry = '\0';              
	}

	return pBlock;
}


/******************************************************************************/
/*
 *	Create a temporary stdout file and launch the CGI process.
 *	Returns a handle to the spawned CGI process.
 */

int websLaunchCgiProc(char_t *cgiPath, char_t **argp, char_t **envp, 
					  char_t *stdIn, char_t *stdOut)
{
    STARTUPINFO			newinfo;	
	SECURITY_ATTRIBUTES security;
    PROCESS_INFORMATION	procinfo;		/*  Information about created proc   */
    DWORD				dwCreateFlags;
    char_t				*cmdLine;
	char_t				**pArgs;
	BOOL				bReturn;
	int					i, nLen;
	unsigned char		*pEnvData;

/*
 *	Replace directory delimiters with Windows-friendly delimiters
 */
	nLen = gstrlen(cgiPath);
	for (i = 0; i < nLen; i++) {
		if (cgiPath[i] == '/') {
			cgiPath[i] = '\\';
		}
	}
/*
 *	Calculate length of command line
 */
	nLen = 0;
	pArgs = argp;
	while (pArgs && *pArgs && **pArgs) {
		nLen += gstrlen(*pArgs) + 1;
		pArgs++;
	}

/*
 *	Construct command line
 */
	cmdLine = balloc(B_L, sizeof(char_t) * nLen);
	a_assert (cmdLine);
	gstrcpy(cmdLine, "");

	pArgs = argp;
	while (pArgs && *pArgs && **pArgs) {
		gstrcat(cmdLine, *pArgs);
		gstrcat(cmdLine, T(" "));
		pArgs++;
	}

 /*
 *	Create the process start-up information 
 */
	memset (&newinfo, 0, sizeof(newinfo));
    newinfo.cb          = sizeof(newinfo);
    newinfo.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    newinfo.wShowWindow = SW_HIDE;
    newinfo.lpTitle     = NULL;

/*
 *	Create file handles for the spawned processes stdin and stdout files
 */
	security.nLength = sizeof(SECURITY_ATTRIBUTES);
	security.lpSecurityDescriptor = NULL;
	security.bInheritHandle = TRUE;

/*
 *	Stdin file should already exist.
 */
	newinfo.hStdInput = CreateFile(stdIn, GENERIC_READ,
		FILE_SHARE_READ, &security, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

/*
 *	Stdout file is created and file pointer is reset to start.
 */
	newinfo.hStdOutput = CreateFile(stdOut, GENERIC_READ | GENERIC_WRITE,
		FILE_SHARE_READ + FILE_SHARE_WRITE, &security, OPEN_ALWAYS,
		FILE_ATTRIBUTE_NORMAL, NULL);
	SetFilePointer (newinfo.hStdOutput, 0, NULL, FILE_END);

/*
 *	Stderr file is set to Stdout.
 */
	newinfo.hStdError =	newinfo.hStdOutput;

	dwCreateFlags = CREATE_NEW_CONSOLE;
	pEnvData = tableToBlock(envp);

/*
 *	CreateProcess returns errors sometimes, even when the process was    
 *	started correctly.  The cause is not evident.  For now: we detect    
 *	an error by checking the value of procinfo.hProcess after the call.  
 */
    procinfo.hProcess = NULL;
    bReturn = CreateProcess(
        NULL,				/*  Name of executable module        */
        cmdLine,			/*  Command line string              */
        NULL,				/*  Process security attributes      */
        NULL,				/*  Thread security attributes       */
        TRUE,				/*  Handle inheritance flag          */
        dwCreateFlags,		/*  Creation flags                   */
        pEnvData,			/*  New environment block            */
        NULL,				/*  Current directory name           */
        &newinfo,			/*  STARTUPINFO                      */
        &procinfo);			/*  PROCESS_INFORMATION              */

	if (procinfo.hThread != NULL)  {
		CloseHandle(procinfo.hThread);
	}

	if (newinfo.hStdInput) {
		CloseHandle(newinfo.hStdInput);
	}

	if (newinfo.hStdOutput) {
		CloseHandle(newinfo.hStdOutput);
	}

	bfree(B_L, pEnvData);
	bfree(B_L, cmdLine);

	if (bReturn == 0) {
		return -1;
	} else {
		return (int) procinfo.hProcess;
	}
}

/******************************************************************************/
/*
 *	Check the CGI process.  Return 0 if it does not exist; non 0 if it does.
 */

int websCheckCgiProc(int handle)
{
	int		nReturn;
	DWORD	exitCode;

	nReturn = GetExitCodeProcess((HANDLE)handle, &exitCode);
/*
 *	We must close process handle to free up the window resource, but only
 *  when we're done with it.
 */
	if ((nReturn == 0) || (exitCode != STILL_ACTIVE)) {
		CloseHandle((HANDLE)handle);
		return 0;
	}

	return 1;
}

/******************************************************************************/

#ifdef B_STATS
static void memLeaks() 
{
	int		fd;

	if ((fd = gopen(T("leak.txt"), O_CREAT | O_TRUNC | O_WRONLY)) >= 0) {
		bstats(fd, printMemStats);
		close(fd);
	}
}

/******************************************************************************/
/*
 *	Print memory usage / leaks  
 */

static void printMemStats(int handle, char_t *fmt, ...)
{
	va_list		args;
	char_t		buf[256];

	va_start(args, fmt);
	vsprintf(buf, fmt, args);
	va_end(args);
	write(handle, buf, strlen(buf));
}
#endif

/******************************************************************************/
/*
 *	Center window on screen
 */

#define RCWIDTH(rc) ((rc).right - (rc).left)
#define RCHEIGHT(rc) ((rc).bottom - (rc).top)

static void centerWindowOnDisplay(HWND hwndCenter)
{
	int			xLeft, yTop, cxDisp, cyDisp;
	RECT		rcDlg;

	a_assert(IsWindow(hwndCenter));

/*
 *	Get Window Size
 */
	GetWindowRect(hwndCenter, &rcDlg);

/*
 *	Get monitor width and height
 */
	cxDisp = GetSystemMetrics(SM_CXFULLSCREEN);
	cyDisp = GetSystemMetrics(SM_CYFULLSCREEN);

/*
 *	Find dialog's upper left based on screen size
 */
	xLeft = cxDisp / 2 - RCWIDTH(rcDlg) / 2;
	yTop = cyDisp / 2 - RCHEIGHT(rcDlg) / 2;

/*
 *	If the dialog is outside the screen, move it inside
 */
	if (xLeft < 0) {
		xLeft = 0;
	} else if (xLeft + RCWIDTH(rcDlg) > cxDisp) {
		xLeft = cxDisp - RCWIDTH(rcDlg);
	}

	if (yTop < 0) {
		yTop = 0;
	} else if (yTop + RCHEIGHT(rcDlg) > cyDisp) {
		yTop = cyDisp - RCHEIGHT(rcDlg);
	}

/*
 *	Move the window
 */
	SetWindowPos(hwndCenter, HWND_TOP, xLeft, yTop, -1, -1,
		SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}

/******************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -