2019-07-28 10:31:56 +08:00
|
|
|
#include <getopt.h>
|
2015-02-04 22:26:20 +08:00
|
|
|
#include "acl_cpp/lib_acl.hpp"
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
{
|
2015-08-18 20:58:56 +08:00
|
|
|
printf("usage: %s -h [help] \r\n"
|
|
|
|
"\t-s server_addr[127.0.0.1:8194] \r\n"
|
|
|
|
"\t-n max_loop \r\n"
|
|
|
|
"\t-z[accept_gzip]\r\n"
|
|
|
|
"\t-B[send_body data]\r\n", procname);
|
2015-02-04 22:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
int ch, max = 10;
|
2015-08-18 20:58:56 +08:00
|
|
|
bool accept_gzip = false, send_body = false;
|
2015-02-04 22:26:20 +08:00
|
|
|
acl::string addr("127.0.0.1:8194");
|
|
|
|
|
2015-08-18 20:58:56 +08:00
|
|
|
while ((ch = getopt(argc, argv, "hs:n:zB")) > 0)
|
2015-02-04 22:26:20 +08:00
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 's':
|
|
|
|
addr = optarg;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
max = atoi(optarg);
|
|
|
|
break;
|
2015-08-18 20:58:56 +08:00
|
|
|
case 'z':
|
|
|
|
accept_gzip = true;
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
send_body = true;
|
|
|
|
break;
|
2015-02-04 22:26:20 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 20:58:56 +08:00
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
2015-02-04 22:26:20 +08:00
|
|
|
acl::string buf(1024);
|
|
|
|
for (size_t i = 0; i < 1024; i++)
|
|
|
|
buf << 'X';
|
|
|
|
|
|
|
|
acl::http_request req(addr, 10, 10);
|
|
|
|
acl::string tmp(1024);
|
|
|
|
|
|
|
|
for (int i = 0; i < max; i++)
|
|
|
|
{
|
|
|
|
acl::http_header& header = req.request_header();
|
2015-08-18 20:58:56 +08:00
|
|
|
//header.set_method(acl::HTTP_METHOD_POST);
|
2015-02-04 22:26:20 +08:00
|
|
|
header.set_url("/");
|
|
|
|
header.set_keep_alive(true);
|
2015-08-18 20:58:56 +08:00
|
|
|
header.accept_gzip(accept_gzip ? true : false);
|
|
|
|
//header.set_content_length(buf.length());
|
|
|
|
|
|
|
|
bool rc;
|
|
|
|
|
|
|
|
if (send_body)
|
|
|
|
rc = req.request(buf.c_str(), buf.length());
|
|
|
|
else
|
|
|
|
rc = req.request(NULL, 0);
|
2015-02-04 22:26:20 +08:00
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
// 只所以将 build_request 放在 req.request 后面,是因为
|
|
|
|
// req.request 内部可能会修改请求头中的字段
|
2015-08-18 20:58:56 +08:00
|
|
|
acl::string hdr;
|
|
|
|
header.build_request(hdr);
|
|
|
|
printf("request header:\r\n%s\r\n", hdr.c_str());
|
|
|
|
|
|
|
|
if (rc == false)
|
2015-02-04 22:26:20 +08:00
|
|
|
{
|
|
|
|
printf("send request error\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-18 20:58:56 +08:00
|
|
|
printf("send request ok\r\n");
|
2015-02-04 22:26:20 +08:00
|
|
|
|
|
|
|
tmp.clear();
|
|
|
|
|
2015-08-18 20:58:56 +08:00
|
|
|
int size = 0, real_size = 0, n;
|
2015-02-04 22:26:20 +08:00
|
|
|
while (true)
|
|
|
|
{
|
2015-08-18 20:58:56 +08:00
|
|
|
int ret = req.read_body(tmp, false, &n);
|
2015-02-04 22:26:20 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
printf("read_body error\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (ret == 0)
|
2015-08-18 20:58:56 +08:00
|
|
|
{
|
|
|
|
printf("-------------read over---------\r\n");
|
2015-02-04 22:26:20 +08:00
|
|
|
break;
|
2015-08-18 20:58:56 +08:00
|
|
|
}
|
|
|
|
|
2015-02-04 22:26:20 +08:00
|
|
|
size += ret;
|
2015-08-18 20:58:56 +08:00
|
|
|
real_size += n;
|
2015-02-04 22:26:20 +08:00
|
|
|
}
|
2015-08-18 20:58:56 +08:00
|
|
|
|
|
|
|
printf("read body size: %d, real_size: %d, %s\n",
|
|
|
|
size, real_size, tmp.c_str());
|
|
|
|
|
|
|
|
printf("===============================================\r\n");
|
2015-02-04 22:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|