2015-02-04 22:26:20 +08:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include <getopt.h>
|
|
|
|
|
2015-08-18 20:58:56 +08:00
|
|
|
static void handle_request(acl::socket_stream* conn)
|
2015-02-04 22:26:20 +08:00
|
|
|
{
|
|
|
|
acl::string buf(1024);
|
|
|
|
for (size_t i = 0; i < 1024; i++)
|
|
|
|
buf << 'X';
|
|
|
|
|
|
|
|
acl::http_response res(conn);
|
|
|
|
acl::string tmp(1024);
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (res.read_header() == false)
|
|
|
|
{
|
|
|
|
printf("read_header error\r\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-18 20:58:56 +08:00
|
|
|
printf("read header ok\r\n");
|
2015-02-04 22:26:20 +08:00
|
|
|
|
|
|
|
tmp.clear();
|
|
|
|
if (res.get_body(tmp) == false)
|
|
|
|
{
|
|
|
|
printf("read_body error\r\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-18 20:58:56 +08:00
|
|
|
printf("request body's size: %d\r\n", (int) tmp.length());
|
2015-02-04 22:26:20 +08:00
|
|
|
|
|
|
|
acl::http_header& header = res.response_header();
|
|
|
|
header.set_status(200);
|
|
|
|
header.set_keep_alive(true);
|
2015-08-18 20:58:56 +08:00
|
|
|
header.set_transfer_gzip(true);
|
|
|
|
header.set_chunked(true);
|
|
|
|
// header.set_content_length(buf.length());
|
|
|
|
|
|
|
|
acl::string hdr;
|
|
|
|
header.build_response(hdr);
|
|
|
|
printf("response header:\r\n%s\r\n", hdr.c_str());
|
2015-02-04 22:26:20 +08:00
|
|
|
|
|
|
|
if (res.response(buf.c_str(), buf.length()) == false)
|
|
|
|
{
|
|
|
|
printf("response error\r\n");
|
|
|
|
break;
|
|
|
|
}
|
2015-08-18 20:58:56 +08:00
|
|
|
if (res.response(NULL, 0) == false)
|
|
|
|
{
|
|
|
|
printf("response trailer error\r\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("response body ok\r\n");
|
|
|
|
printf("===============================================\r\n");
|
2015-02-04 22:26:20 +08:00
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->close();
|
2015-08-18 20:58:56 +08:00
|
|
|
printf(">>>> close client <<<<\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
{
|
|
|
|
printf("usage: %s -h [help] -s listen_addr[127.0.0.1:8194]\r\n", procname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
acl::string addr("0.0.0.0:8194");
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "hs:")) > 0)
|
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 's':
|
|
|
|
addr = optarg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acl::server_socket server;
|
|
|
|
if (server.open(addr) == false)
|
|
|
|
{
|
|
|
|
printf("listen error, addr: %s\r\n", addr.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("listen %s ok\r\n", addr.c_str());
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
acl::socket_stream* conn = server.accept();
|
|
|
|
if (conn == NULL)
|
|
|
|
{
|
|
|
|
printf("accept error\r\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("accept one: %s\r\n", conn->get_peer());
|
|
|
|
|
|
|
|
handle_request(conn);
|
|
|
|
}
|
2015-02-04 22:26:20 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|