2019-07-28 10:31:56 +08:00
|
|
|
|
// http_aio.cpp : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Ӧ<CCA8>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㡣
|
2019-05-24 18:12:08 +08:00
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
ACL_ASTREAM *stream;
|
2019-05-27 11:15:45 +08:00
|
|
|
|
HTTP_HDR_REQ *hdr_req;
|
2019-05-24 18:12:08 +08:00
|
|
|
|
HTTP_HDR_RES *hdr_res;
|
|
|
|
|
HTTP_RES *http_res;
|
|
|
|
|
} CTX;
|
|
|
|
|
|
2019-05-26 23:55:02 +08:00
|
|
|
|
static char __server_addr[128];
|
2019-05-24 18:12:08 +08:00
|
|
|
|
static bool __stop = false;
|
|
|
|
|
|
|
|
|
|
static int on_close(ACL_ASTREAM *stream acl_unused, void *context)
|
|
|
|
|
{
|
|
|
|
|
CTX *ctx = (CTX *) context;
|
|
|
|
|
|
2019-05-27 11:15:45 +08:00
|
|
|
|
http_hdr_req_free(ctx->hdr_req);
|
2019-05-27 11:20:20 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> HTTP <20><>Ӧͷʱ<CDB7><CAB1>HTTP <20><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>Ӧ<EFBFBD><D3A6>
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ӧͷ<D3A6><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ͷţ<CDB7><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD>Ӧͷʱ<CDB7><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// <20><>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>Ӧͷ<D3A6><CDB7><EFBFBD><EFBFBD>
|
2019-05-27 11:20:20 +08:00
|
|
|
|
if (ctx->http_res) {
|
|
|
|
|
http_res_free(ctx->http_res);
|
|
|
|
|
} else if (ctx->hdr_res) {
|
|
|
|
|
http_hdr_res_free(ctx->hdr_res);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-24 18:12:08 +08:00
|
|
|
|
acl_myfree(ctx);
|
|
|
|
|
|
|
|
|
|
printf("%s(%d)\n", __FUNCTION__, __LINE__);
|
|
|
|
|
__stop = true;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int on_timeout(ACL_ASTREAM *stream acl_unused, void *context acl_unused)
|
|
|
|
|
{
|
|
|
|
|
printf("%s(%d)\n", __FUNCTION__, __LINE__);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int read_respond_body_ready(int status, char *data, int dlen,
|
|
|
|
|
void *arg acl_unused)
|
|
|
|
|
{
|
|
|
|
|
ACL_VSTRING *buf = acl_vstring_alloc(256);
|
|
|
|
|
|
|
|
|
|
acl_vstring_memcpy(buf, data, dlen);
|
|
|
|
|
ACL_VSTRING_TERMINATE(buf);
|
|
|
|
|
printf("status=%d >>%s", status, acl_vstring_str(buf));
|
|
|
|
|
acl_vstring_free(buf);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int on_http_hdr(int status, void *arg)
|
|
|
|
|
{
|
|
|
|
|
CTX *ctx = (CTX*) arg;
|
|
|
|
|
|
|
|
|
|
printf("%s: status=%d\r\n", __FUNCTION__, status);
|
|
|
|
|
|
|
|
|
|
http_hdr_print(&ctx->hdr_res->hdr, "---respond---");
|
2019-05-26 23:55:02 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><>Ӧ<EFBFBD><D3A6>
|
2019-05-24 18:12:08 +08:00
|
|
|
|
ctx->http_res = http_res_new(ctx->hdr_res);
|
2019-05-26 23:55:02 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>첽<EFBFBD><ECB2BD>ȡ HTTP <20><><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>Ӧ<EFBFBD><D3A6>
|
2019-05-24 18:12:08 +08:00
|
|
|
|
http_res_body_get_async(ctx->http_res, ctx->stream,
|
|
|
|
|
read_respond_body_ready, ctx, 0);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int on_connect(ACL_ASTREAM *stream, void *context)
|
|
|
|
|
{
|
|
|
|
|
const char *addr = (const char*) context;
|
|
|
|
|
|
|
|
|
|
if (stream == NULL) {
|
|
|
|
|
int err = acl_last_error();
|
|
|
|
|
printf("connect %s failed, errno=%d, %s\r\n", addr, err,
|
|
|
|
|
err < 0 ? acl_dns_serror(err) : acl_last_serror());
|
|
|
|
|
__stop = true;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf(">>>> connect %s ok!\r\n", addr);
|
|
|
|
|
|
|
|
|
|
CTX *ctx = (CTX*) acl_mycalloc(1, sizeof(CTX));
|
|
|
|
|
ctx->stream = stream;
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ
|
2019-05-27 11:15:45 +08:00
|
|
|
|
ctx->hdr_req = http_hdr_req_create("/", "GET", "HTTP/1.1");
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
2019-05-27 11:15:45 +08:00
|
|
|
|
http_hdr_entry_replace(&ctx->hdr_req->hdr, "Connection", "Close", 1);
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-05-27 11:15:45 +08:00
|
|
|
|
http_hdr_put_str(&ctx->hdr_req->hdr, "Content-Type", "text/plain");
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>
|
2019-05-27 11:15:45 +08:00
|
|
|
|
http_hdr_put_str(&ctx->hdr_req->hdr, "Host", __server_addr);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD>ʱ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2019-05-24 18:12:08 +08:00
|
|
|
|
acl_aio_add_timeo_hook(stream, on_timeout, ctx);
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>ùرջص<D5BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2019-05-24 18:12:08 +08:00
|
|
|
|
acl_aio_add_close_hook(stream, on_close, ctx);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̬<EFBFBD>ڴ沢<DAB4><E6B2A2> HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-05-27 11:15:45 +08:00
|
|
|
|
ACL_VSTRING *buf = acl_vstring_alloc(1024);
|
|
|
|
|
http_hdr_build_request(ctx->hdr_req, buf);
|
2019-05-24 18:12:08 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>
|
2019-05-27 11:15:45 +08:00
|
|
|
|
acl_aio_writen(stream, acl_vstring_str(buf), ACL_VSTRING_LEN(buf));
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>ͷŶ<CDB7>̬<EFBFBD>ڴ<EFBFBD>
|
2019-05-27 11:15:45 +08:00
|
|
|
|
acl_vstring_free(buf);
|
2019-05-24 18:12:08 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>
|
2019-05-24 18:12:08 +08:00
|
|
|
|
ctx->hdr_res = http_hdr_res_new();
|
2019-05-26 23:55:02 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>첽<EFBFBD><ECB2BD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6> HTTP ͷ
|
2019-05-24 18:12:08 +08:00
|
|
|
|
http_hdr_res_get_async(ctx->hdr_res, stream, on_http_hdr, ctx, 0);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void usage(const char *procname)
|
|
|
|
|
{
|
|
|
|
|
printf("usage: %s -h[help]\r\n"
|
|
|
|
|
" -s server_addr\r\n"
|
|
|
|
|
" -N name_server\r\n"
|
|
|
|
|
" -t connect_timeout\r\n"
|
|
|
|
|
" -T dns_timeout\r\n",
|
|
|
|
|
procname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
|
|
|
|
ACL_AIO *aio = acl_aio_create(ACL_EVENT_SELECT);
|
2019-05-26 23:55:02 +08:00
|
|
|
|
char name_server[128];
|
2019-05-24 18:12:08 +08:00
|
|
|
|
int ch, connect_timeout = 5, dns_timeout = 1;
|
|
|
|
|
|
|
|
|
|
acl_aio_set_keep_read(aio, 1);
|
|
|
|
|
|
2019-05-26 23:55:02 +08:00
|
|
|
|
snprintf(__server_addr, sizeof(__server_addr), "www.baidu.com:80");
|
2019-05-24 18:12:08 +08:00
|
|
|
|
snprintf(name_server, sizeof(name_server), "8.8.8.8:53");
|
|
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "hs:N:t:T:")) > 0) {
|
|
|
|
|
switch (ch) {
|
|
|
|
|
case 'h':
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return 0;
|
|
|
|
|
case 's':
|
2019-05-26 23:55:02 +08:00
|
|
|
|
snprintf(__server_addr, sizeof(__server_addr), "%s", optarg);
|
2019-05-24 18:12:08 +08:00
|
|
|
|
break;
|
|
|
|
|
case 'N':
|
|
|
|
|
snprintf(name_server, sizeof(name_server), "%s", optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 't':
|
|
|
|
|
connect_timeout = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'T':
|
|
|
|
|
dns_timeout = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
2019-05-24 18:12:08 +08:00
|
|
|
|
acl_aio_set_dns(aio, name_server, dns_timeout);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8> WEB <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-05-26 23:55:02 +08:00
|
|
|
|
if (acl_aio_connect_addr(aio, __server_addr, connect_timeout,
|
|
|
|
|
on_connect, __server_addr) == -1) {
|
2019-05-24 18:12:08 +08:00
|
|
|
|
|
2019-05-26 23:55:02 +08:00
|
|
|
|
printf("connect %s error\r\n", __server_addr);
|
2019-05-24 18:12:08 +08:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>첽<EFBFBD>¼<EFBFBD>ѭ<EFBFBD><D1AD>
|
2019-05-24 18:12:08 +08:00
|
|
|
|
while (!__stop) {
|
|
|
|
|
acl_aio_loop(aio);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acl_aio_free(aio);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|