- 构造request请求行
- 请求行的请求方法:GET,HEAD,OPTIONS,TRACE
- 请求行的URL:http://127.0.0.1:3000/index.html
- 请求行协议版本号:HTTP/0.9、HTTP/1.0、HTTP/1.1
- 构造request请求头
- 第一个头部关键字:值
User-Agent: WebBench User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。 比如你在做移动APP开发时,有个网站在你手机端APP打开之后和直接在浏览器中打开之后看到的东西不一样,网页会根据UA判断你是app打开的还是直接浏览器打开的,如果没有找到事先商定的UA信息就认定这个链接是在浏览器打开的,如果查询到事先商定的UA就是用app打开的,两种情况可以让你看到不同的东西,做不同的操作,比如有些东西开发者只想让你使用app打开才能看见。
- 第二个头部关键字:值
Host: 127.0.0.1:3000 Host 请求头指明了服务器的域名(对于虚拟主机来说),以及(可选的)服务器监听的TCP端口号。
Host: <host>:<port>
Host: developer.cdn.mozilla.net
- 第三个头部关键字:值
Pragma: no-cache
缓存是一种保存资源副本并在下次请求时直接使用该副本的技术。当 web 缓存发现请求的资源已经被存储,它会拦截请求,返回该资源的拷贝,而不会去源服务器重新下载。这里设置没有缓存 - 第四个头部关键字:值
Connection: close
决定当前的事务完成后,是否会关闭网络连接。如果该值是“keep-alive”,网络连接就是持久的,不会关闭,使得对同一个服务器的请求可以继续在该连接上完成。
- 第一个头部关键字:值
- 具体代码如下:
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," "); if(NULL==strstr(url,"://")) { fprintf(stderr, "\n%s: is not a valid URL.\n",url); exit(2); } if(strlen(url)>1500) { fprintf(stderr,"URL is too long.\n"); exit(2); } if(proxyhost==NULL) if (0!=strncasecmp("http://",url,7)) { fprintf(stderr,"\nOnly HTTP protocol is directly supported, set --proxy for others.\n"); exit(2); } /* protocol/host delimiter */ i=strstr(url,"://")-url+3; /* printf("%d\n",i); */ if(strchr(url+i,'/')==NULL) { fprintf(stderr,"\nInvalid URL syntax - hostname don't ends with '/'.\n"); exit(2); } if(proxyhost==NULL) { /* get port from hostname */ if(index(url+i,':')!=NULL && index(url+i,':')<index(url+i,'/')) { 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); 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"); 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"); } if(force_reload && proxyhost!=NULL) { strcat(request,"Pragma: no-cache\r\n"); } if(http10>1) strcat(request,"Connection: close\r\n"); /* add empty line at end */ if(http10>0) strcat(request,"\r\n"); // printf("Req=%s\n",request); }
TKeed之webbench测压之request报文的构建
最新推荐文章于 2023-07-19 12:40:19 发布