acl/lib_acl_cpp/samples/http/http_request/main.cpp

110 lines
2.2 KiB
C++
Raw Normal View History

#include <getopt.h>
2015-02-04 22:26:20 +08:00
#include "acl_cpp/lib_acl.hpp"
static void usage(const char* procname)
{
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;
bool accept_gzip = false, send_body = false;
2015-02-04 22:26:20 +08:00
acl::string addr("127.0.0.1:8194");
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;
case 'z':
accept_gzip = true;
break;
case 'B':
send_body = true;
break;
2015-02-04 22:26:20 +08:00
default:
break;
}
}
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();
//header.set_method(acl::HTTP_METHOD_POST);
2015-02-04 22:26:20 +08:00
header.set_url("/");
header.set_keep_alive(true);
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
// 鍙墍浠ュ皢 build_request 鏀惧湪 req.request 鍚庨潰锛屾槸鍥犱负
// req.request 鍐呴儴鍙兘浼氫慨鏀硅姹傚ご涓殑瀛楁
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;
}
printf("send request ok\r\n");
2015-02-04 22:26:20 +08:00
tmp.clear();
int size = 0, real_size = 0, n;
2015-02-04 22:26:20 +08:00
while (true)
{
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)
{
printf("-------------read over---------\r\n");
2015-02-04 22:26:20 +08:00
break;
}
2015-02-04 22:26:20 +08:00
size += ret;
real_size += n;
2015-02-04 22:26:20 +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;
}