How to Implement CGI technology in WebServer

Source: Internet
Author: User
Tags readfile
Talk In WebServer How to Implement CGITechnologyMany people are interested in the implementation of cgi technology in WebServer. However, in some open-source software such as Apache, due to the large scale of software, there are many related modules, it is difficult to understand how CGI is implemented directly. Let's talk about the implementation method of CGI technology. To implement CGI technology, the key is to redirect the application output from the screen to the SOCKET when other applications are executed. After the application output is redirected, CGI implementation is simple. The following uses windows as an example to implement cgi technology. The following cgi_writeclient () function redirects the output result of the CGI program to the socket. HANDLE cgi_exec (HANDLE hPipeWrite, char * exefile, char * using line, char * lpszCurrentPath) {BOOL bSucceed; STARTUPINFO sui; PROCESS_INFORMATION pi; BOOL bTest; memset (& sui, 0, sizeof (STARTUPINFO); sui. cb = sizeof (STARTUPINFO); sui. dwXSize = 0; sui. dwXSize = 0; sui. wShowWindow = SW_HIDE; sui. dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; sui. hStdInput = NULL; sui. hStdOutput = hPipeWrite; sui. hStdErr Or = hPipeWrite; bSucceed = CreateProcess (exefile, cmdline, NULL, NULL, TRUE, 0, // CREATE_NEW_PROCESS_GROUP, NULL, lpszCurrentPath, // lpszPath, // "// Rose // c //", // NULL, // "d:/ip_progs/namepipe/child", & sui, & pi ); if (bSucceed) {CloseHandle (pi. hThread); return pi. hProcess;} // ShowErrorMessage ("CreateProcess ()"); return FALSE;} int cgi_writeclient (HTTPRequest * hr) {char * pszPath; HANDLE hProcess; BOOL bProcessDead = FALSE; HANDLE hPipeRead, hPipeWrite; SECURITY_ATTRIBUTES SecAttrib; int ret; int BytesInPipe; char * buf; DWORD dwRead; SecAttrib. nLength = sizeof (SECURITY_ATTRIBUTES); SecAttrib. lpSecurityDescriptor = NULL; SecAttrib. bInheritHandle = TRUE; if (hr-> http_mod! = HTTP_GET) {return MODULE_RUN_CONTINUE;} if (hr-> mod! = MOD_LOCAL) {return MODULE_RUN_CONTINUE;} pszPath = MemAlloc (hr-> hMem, strlen (pszCgiRoot) + strlen (hr-> http_url) + 10); if (! PszPath) {return MODULE_RUN_CONTINUE;}/* decide if it is the cgi path */strcpy (pszPath, hr-> http_url); if (pszPath [strlen (pszPath) -1] = '/') {if (* (hr-> http_url) = '/') {strcat (pszPath, hr-> http_url + 1 );} else {strcat (pszPath, hr-> http_url) ;}} else {strcpy (pszPath, pszrot); strcat (pszPath, hr-> http_url); ReplaceChar (pszPath, '//', '/');} if (strnicmp (pszPath, pszCgi Root, strlen (pszCgiRoot ))! = 0) {return MODULE_RUN_CONTINUE;} ret = CreatePipe (& hPipeRead, & hPipeWrite, & SecAttrib, 65536); buf = MemAlloc (hr-> hMem, 1024 ); hProcess = cgi_exec (hPipeWrite, NULL, pszPath, NULL); while (! BProcessDead) {int dwRet; dwRet = WaitForSingleObject (hProcess, 1); if (dwRet! = WAIT_TIMEOUT) {bProcessDead = TRUE;} if (! PeekNamedPipe (hPipeRead, NULL, 0, NULL, & BytesInPipe, NULL) {break;} if (BytesInPipe! = 0) {memset (buf, 0, sizeof (buf); dwRead = 0; if (ReadFile (hPipeRead, buf, 1024, & dwRead, NULL )) {if (dwRead> 0) {send (hr-> s, buf, dwRead, 0) ;}}} do {if (! PeekNamedPipe (hPipeRead, NULL, 0, NULL, & BytesInPipe, NULL) {break;} if (BytesInPipe! = 0) {memset (buf, 0, sizeof (buf); dwRead = 0; if (ReadFile (hPipeRead, buf, 1024, & dwRead, NULL )) {if (dwRead> 0) {send (hr-> s, buf, dwRead, 0) ;}}} while (BytesInPipe! = 0); MemFree (hr-> hMem, buf); CloseHandle (hProcess); CloseHandle (hPipeRead); CloseHandle (hPipeWrite); return MODULE_RUN_ENDSESSION;} in the above Code, cgi_exec is used to create a process and direct the process output to the MPs queue. Then, in the cgi_writeclient () function, create the MPs queue and call cgi_exec () the function creates a cgi process and directs its output to the newly created MPs queue. Then, it calls the WaitForSingleObject () function to wait for the CGI process to run. Note that data is not read from the MPs queue until the process ends. During the running of the cgi process, some data is read from the pipeline every other time, And the send () function is called to send to the socket. After the cgi process ends, all the remaining data is read from the pipeline and sent to the socket. In this way, the cgi function is implemented.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.