问题的起因
前几天为了调试gsoap生成的server端代码(实际是一个cgi程序),一直没有找到好的调试方法,网上搜也没有找到一个切实可行的方法,于是分析了一下cgi的原理,终于找到一个调试CGI的办法。
CGI程序的本质
其实cgi就是一个没有界面的exe程序,cgi程序从stdin读取消息,从 stdout输出结果,一个cgi程序往往是部署在一个http server上,比如iis或apache,http server在接到cgi请求以后会创建一个线程,在线程里用CreatePipe创建一个匿名管道:
The CreatePipe function creates an anonymous pipe, and returns handles to the read and write ends of the pipe.
BOOL CreatePipe( PHANDLE , // read handle PHANDLE , // write handle LPSECURITY_ATTRIBUTES , // security attributes DWORD // pipe size );
该函数在创建匿名管道的同时返回两个句柄:管道读句柄hReadPipe和管道写句柄hWritePipe,通过hReadPipe和hWritePipe所指向的句柄可分别以只读、只写的方式去访问管道。
然后,创建出cgi进程,并替换cgi进程的标准输入、标准输出和标准错误句柄。cgi进程运行以后从stdin(读管道句柄)读取http server中穿过来的消息,然后根据请求执行响应的操作,最后将结果通过stdout(实际已经被与客户端对应的socket句柄替换)输出到http server或直接输出给客户端,之后cgi进程就结束了。
下面的是从msdn里的http server例子HTTPSVR中摘出来的,显示了启动一个CGI程序的过程:
UINT CGIThread( LPVOID pvParam )
{
......
HANDLE hWritePipe, hReadPipe;
// create a pipe we'll use for STDIN....
CreatePipe( &hReadPipe, &hWritePipe, &g_sa, 0 ) )
......
//创建CGI进程
PROCESS_INFORMATION pi;
STARTUPINFO si = {0};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdInput = hReadPipe;
si.hStdOutput = pReqSock->m_hFile;
si.hStdError = pReqSock->m_hFile;
bOk = CreateProcess( NULL, strCmdLine.GetBuffer(1),
NULL, NULL, TRUE,
dwCreateFlags, pEnv,
strDir, &si, &pi );
......
// send the body of the post to the stdin....
WriteFile( hWritePipe, pRequest->m_baBody.GetData(),
pRequest->m_cbBody, &dwWritten, NULL );
......
//结果输出
//由于直接将与客户端连接的socket句柄赋值给了si.hStdOutput
//所以cgi程序输出的时候直接将结果写回给客户端了
//所以http server在这里不需要再读取cgi的输出了
}
调试CGI程序
了解了CGI的本质,调试的思路就有了,就是调试一个事先执行起来的exe程序嘛,用VC6中的“Build|Start Debug|Attach to process”方法调试就搞定了。需要注意的就是cgi运行的很快,往往是当你Attach这个进程的时候,它往往已经执行完毕退出了,即便不退出可能您所关心的代码部分已经执行过了,这个问题可以通过在cgi的main函数开头添加一个死循环来解决,如下:
int main(int argc, char **argv)
{
while(true)
{
Sleep(2000);
}
//真正的cgi代码,比如:
int m, s; /* master and slave sockets */
struct soap soap;
soap_init(&soap);
......
}
这样当CGI进程起来以后,就会进入while(true)循环,这时你就可以很从容的用VC调试器 Attach到cgi进程,然后打开对应的源码,在Sleep(2000)处设置一个断点,就可以看到cgi进程乖乖进入断点了,在soap_init(&soap)处使用“Set Next Statement”命令就可以跳转到soap_init行向下执行了。
CGI实例--表单GET与POST示例
CGI接口标准包括标准输入、环境变量、标准输出三部分。
1.标准输入
CGI程序像其他可执行程序一样,可通过标准输入(stdin)从Web服务器得到输入信息,如Form中的数据,这就是所谓的向CGI程序传递数据的POST方法。这意味着在操作系统命令行状态可执行CGI程序,对CGI程序进行调试。POST方法是常用的方法,本文将以此方法为例,分析CGI程序设计的方法、过程和技巧。
2.环境变量
操作系统提供了许多环境变量,它们定义了程序的执行环境,应用程序可以存取它们。Web服务器和CGI接口又另外设置了自己的一些环境变量,用来向CGI程序传递一些重要的参数。CGI的GET方法还通过 环境变量QUERY-STRING向CGI程序传递Form中的数据。
3.标准输出
CGI程序通过标准输出(stdout)将输出信息传送给Web服务器。传送给Web服务器的信息可以用各种格式,通常是以纯文本或者HTML文本的形式,这样我们就可以在命令行状态调试CGI程序,并且得到它们的输出。
GET方法:做一个加法运算,需要接收两个参数。
文件get.c如下:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *data;
char a[10],b[10];
printf("Content-Type:text/html\n\n");
printf("<HTML>\n");
printf("<HEAD>\n<TITLE >Get Method</TITLE>\n</HEAD>\n");
printf("<BODY>\n");
printf("<div style=\"font-size:12px\">\n");
data = getenv("QUERY_STRING");
if(sscanf(data,"a=%[^&]&b=%s",a,b)!=2){
printf("<DIV STYLE=\"COLOR:RED\">Error parameters should be entered!</DIV>\n");
}
else{
printf("<DIV STYLE=\"COLOR:GREEN; font-size:15px;font-weight:bold\">a + b = %d</DIV>\n",atoi(a)+atoi(b));
}
printf("<HR COLOR=\"blue\" align=\"left\" width=\"100\">");
printf("<input type=\"button\" value=\"Back CGI\" οnclick=\"javascript:window.location='../cgi.html'\">");
printf("</div>\n");
printf("</BODY>\n");
printf("</HTML>\n");
return 0;
}
POST方法:做一个乘法运算,需要接收两个参数
文件post.c如下:
#include <stdio.h>
#include <stdlib.h>
int main(void){
int len;
char *lenstr,poststr[20];
char m[10],n[10];
printf("Content-Type:text/html\n\n");
printf("<HTML>\n");
printf("<HEAD>\n<TITLE >post Method</TITLE>\n</HEAD>\n");
printf("<BODY>\n");
printf("<div style=\"font-size:12px\">\n");
lenstr=getenv("CONTENT_LENGTH");
if(lenstr == NULL)
printf("<DIV STYLE=\"COLOR:RED\">Error parameters should be entered!</DIV>\n");
else{
len=atoi(lenstr);
fgets(poststr,len+1,stdin);
if(sscanf(poststr,"m=%[^&]&n=%s",m,n)!=2){
printf("<DIV STYLE=\"COLOR:RED\">Error: Parameters are not right!</DIV>\n");
}
else{
printf("<DIV STYLE=\"COLOR:GREEN; font-size:15px;font-weight:bold\">m * n = %d</DIV>\n",atoi(m)*atoi(n));
}
}
printf("<HR COLOR=\"blue\" align=\"left\" width=\"100\">");
printf("<input type=\"button\" value=\"Back CGI\" οnclick=\"javascript:window.location='../cgi.html'\">");
printf("</div>\n");
printf("</BODY>\n");
printf("</HTML>\n");
fflush(stdout);
return 0;
}
再附上html测试文件cgi.html:
<html>
<head>
<title>CGI Testing</title>
</head>
<body>
<table width="200" height="180" border="0" style="font-size:12px">
<tr><td>
<div style="font-weight:bold; font-size:15px">Method: GET</div>
<div>please input two number:<div>
<form method="get" action="./cgi-bin/get">
<input type="txt" size="3" name="a">+
<input type="txt" size="3" name="b">=
<input type="submit" value="sum">
</form>
</td></tr>
<tr><td>
<div style="font-weight:bold; font-size:15px">Method: POST</div>
<div>please input two number:<div>
<form method="post" action="./cgi-bin/post">
<input type="txt" size="3" name="m">*
<input type="txt" size="3" name="n">=
<input type="submit" value="resu">
</form>
</td></tr>
<tr><td><inputtype="button" value="Back Home"οnclick='javascript:window.location="./index.html"'></td></tr>
</table>
</body>
</html>
几点简要说明:
(1) printf("Content-Type:text/html\n\n");
此行通过标准输出将字符串″Contenttype:text/plain\n\n″传送给Web服务器。它是一个MIME头信息,它告诉Web服务器随后的输出是以纯ASCII文本的形式。请注意在这个头信息中有两个换行符,这是因为Web服务器需要在实际的文本信息开始之前先看见一个空行。
(2) data = getenv("QUERY_STRING");
CGI定义:当GET方法提交的表单被发送到服务器端后,表单中的数据被保存在服务器上一个叫做QUERY_STRING的环境变量中。这种表单的处理相对简单,只要读取环境变量就可以了。
(3) sscanf(data,"a=%[^&]&b=%s",a,b)!=2
这个是关于sscanf函数的使用问题,自己可以上网搜索一下,这里不再详述!
(4)atoi(a)+atoi(b)
atoi函数的功能是将字符型成整型,只有转换之后才可以进行加法运算!
(5) lenstr=getenv("CONTENT_LENGTH");
Web服务器在调用使用POST方法的CGI程序时设置此环境变量,它的文本值表示Web服务器传送给CGI程序的输入中的字符数目,因此需要使用函数atoi() 将此环境变量的值转换成整数,并赋给变量len(下面有定义)。
(6) fgets(poststr,len+1,stdin);
这个是关于fgets函数的使用问题,自己可以上网搜索一下,这里不再详述!
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- oldu.cn 版权所有 浙ICP备2024123271号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务