webbench源码分析之build_request函数

本文详细分析了webbench源码中的build_request函数,该函数负责根据输入的URL构建HTTP请求,包括设置请求方法(GET, HEAD等)、处理URL格式、处理代理服务器情况,并添加HTTP头信息。函数应用了多种字符串操作,确保请求的正确性和合规性。" 84205661,8213276,Struts2.5.18框架访问JSP报404错误解决方案,"['Struts框架', 'Web开发', 'Java', '数据库连接', '错误排查']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**************** 
  创建URL请求连接 
  @url:url地址 
  创建好的请求放在全局变量request中 
 ****************/  
// 此函数主要目的是要把类似于http GET请求的信息全部存储到全局变量request[REQUEST_SIZE]
// 中,其中换行操作使用"\r\n"。其中应用了大量的字符串操作函数。
// 创建url请求连接,HTTP头,创建好的请求放在全局变量request中
void build_request(const char *url)  
{
char tmp[10];  
int i;  


//请求地址和请求连接清零  
bzero(host, MAXHOSTNAMELEN);  
bzero(request, REQUEST_SIZE);  


// 协议适配
if(force_reload && proxyhost!=NULL && http10 < 1) http10=1;  
if(method==METHOD_HEAD && http10<1) http10=1;  
if(method==METHOD_OPTIONS && http10<2) http10=2;  
if(method==METHOD_TRACE && http10<2) http10=2;  


switch(method)  
{  
default:  
case METHOD_GET: strcpy(request,"GET");break;  
case METHOD_HEAD: strcpy(request,"HEAD");break; 
// 请求方法相应的不能缓存
case METHOD_OPTIONS: strcpy(request,"OPTIONS");break;  
case METHOD_TRACE: strcpy(request,"TRACE");break;  
}


// 追加空格
strcat(request, " ");


// strstr(str1, str2)用于判断str2是否是str1的子串
if(NULL == strstr(url,"://"))//找“://”在URL中的位置  
{
fprintf(stderr, "\n%s: is not a valid URL.\n",url);  
exit(2);  
}
if(strlen(url)>1500)//url是否太长
{
fprintf(stderr, "URL is too long.\n");
exit(2);
}
if(proxyhost == NULL)//代理服务器是否为空
// 未使用代理服务器的情况下,只允许HTTP协议
if (0 != strncasecmp("http://", url, 7)) //比较前7个字符串
{
fprintf(stderr,"\nOnly HTTP protocol is directly supported, set --proxy for others.\n");
exit(2);
}
/* protocol/host delimiter */
i=strstr(url,"://")-url+3; //i指向http://后第一个字母
/* printf("%d\n",i); */
// URL后必须的'/'
if(strchr(url+i, '/') == NULL) {
fprintf(stderr, "\nInvalid URL syntax - hostname don't ends with '/'.\n");
exit(2);
}
// 如果未使用代理服务器,就表示肯定是HTTP协议
if(proxyhost == NULL)
{
/* get port from hostname */
// 如果是server:port形式,解析主机和端口
if(index(url+i, ':') != NULL &&  
index(url+i, ':')<index(url+i, '/'))//判断url中是否指定了端口号
{
strncpy(host, url+i, strchr(url+i, ':')-url-i);//取出主机地址
bzero(tmp, 10);
strncpy(tmp, index(url+i,':')+1, strchr(url+i,'/')-index(url+i, ':')-1);
/* printf("tmp=%s\n",tmp); */
// 目标端口
proxyport = atoi(tmp);//端口号转换为int
if(proxyport == 0)
proxyport = 80;
}else
{
strncpy(host, url+i, strcspn(url+i, "/"));
}
// printf("Host=%s\n",host);
strcat(request+strlen(request), url+i+strcspn(url+i, "/"));
}else
{
// printf("ProxyHost=%s\nProxyPort=%d\n",proxyhost,proxyport);
/ 如若使用代理服务器 
strcat(request, url);
}
if(http10 == 1)//版本号
strcat(request, " HTTP/1.0");
else if (http10 == 2)
strcat(request, " HTTP/1.1");
// 完成如 GET/HTTP1.1后,添加"\r\n"
strcat(request, "\r\n");
if(http10 > 0)
strcat(request, "User-Agent: WebBench "PROGRAM_VERSION"\r\n");
if(proxyhost == NULL && http10 > 0)
{
strcat(request, "Host: ");
strcat(request, host);
strcat(request, "\r\n");
}
// force_reload=1和存在代理服务器,则不缓存
if(force_reload && proxyhost != NULL)
{
strcat(request, "Pragma: no-cache\r\n");
}
// 如果为HTTP1.1,则存在长连接,应将Connection置位close
if(http10 > 1)
strcat(request, "Connection: close\r\n");
/* add empty line at end */
// 最后不要忘记在请求后添加“\r\n”
if(http10 > 0) strcat(request,"\r\n");
// printf("Req=%s\n",request);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值