2019-07-28 10:31:56 +08:00
|
|
|
|
#include <iostream>
|
2019-06-05 17:45:12 +08:00
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <unistd.h>
|
2019-06-11 23:16:03 +08:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include "lib_acl.h"
|
2019-06-05 17:45:12 +08:00
|
|
|
|
#include "acl_cpp/lib_acl.hpp"
|
|
|
|
|
|
2019-06-13 22:05:25 +08:00
|
|
|
|
static acl::atomic_long __aio_refer = 0;
|
2019-06-11 23:16:03 +08:00
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2019-06-13 22:05:25 +08:00
|
|
|
|
#ifndef USE_AIO_OSTREAM
|
|
|
|
|
|
|
|
|
|
class pipe_ostream : public acl::aio_ostream
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
pipe_ostream(acl::aio_handle& handle, int fd)
|
|
|
|
|
: aio_stream(&handle), aio_ostream(&handle, fd)
|
|
|
|
|
, handle_(handle)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~pipe_ostream(void)
|
|
|
|
|
{
|
|
|
|
|
if (--__aio_refer == 0) {
|
|
|
|
|
printf("%s: stop aio engine now!\r\n", __FUNCTION__);
|
|
|
|
|
handle_.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// @override
|
|
|
|
|
void destroy(void)
|
|
|
|
|
{
|
|
|
|
|
printf("pipe_ostream: will be destroied!\r\n");
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
acl::aio_handle& handle_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
class pipe_writer : public acl::aio_callback
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
pipe_writer(acl::aio_handle& handle, int fd)
|
|
|
|
|
: handle_(handle)
|
|
|
|
|
{
|
|
|
|
|
#ifdef USE_AIO_OSTREAM
|
2019-08-07 14:36:09 +08:00
|
|
|
|
out_ = new acl::aio_ostream(&handle_, fd);
|
2019-06-13 22:05:25 +08:00
|
|
|
|
#else
|
2019-08-07 14:36:09 +08:00
|
|
|
|
out_ = new pipe_ostream(handle_, fd);
|
2019-06-13 22:05:25 +08:00
|
|
|
|
#endif
|
|
|
|
|
++__aio_refer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void start(void)
|
|
|
|
|
{
|
|
|
|
|
out_->add_write_callback(this);
|
|
|
|
|
out_->add_close_callback(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acl::aio_ostream& get_ostream(void) const
|
|
|
|
|
{
|
|
|
|
|
return *out_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// @override
|
|
|
|
|
bool add_write_callback(void)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
|
|
|
|
void close_callback(void)
|
|
|
|
|
{
|
|
|
|
|
printf("pipe_writer->being closed!\r\n");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
acl::aio_handle& handle_;
|
|
|
|
|
#ifdef USE_AIO_OSTREAM
|
|
|
|
|
acl::aio_ostream* out_;
|
|
|
|
|
#else
|
|
|
|
|
pipe_ostream* out_;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
~pipe_writer(void)
|
|
|
|
|
{
|
|
|
|
|
printf("%s: writer pipe will deleted!\r\n", __FUNCTION__);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-11 23:16:03 +08:00
|
|
|
|
class pipe_reader : public acl::aio_callback
|
|
|
|
|
{
|
|
|
|
|
public:
|
2019-06-13 22:05:25 +08:00
|
|
|
|
pipe_reader(acl::aio_handle& handle, int fd, pipe_writer& out)
|
2019-06-11 23:16:03 +08:00
|
|
|
|
: handle_(handle)
|
2019-06-13 22:05:25 +08:00
|
|
|
|
, out_(out)
|
2019-06-11 23:16:03 +08:00
|
|
|
|
{
|
|
|
|
|
in_ = new acl::aio_istream(&handle, fd);
|
2019-06-13 22:05:25 +08:00
|
|
|
|
++__aio_refer;
|
2019-06-11 23:16:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void start(void)
|
|
|
|
|
{
|
|
|
|
|
in_->add_read_callback(this);
|
|
|
|
|
in_->add_close_callback(this);
|
|
|
|
|
in_->read();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// @override
|
|
|
|
|
bool read_callback(char* data, int len)
|
|
|
|
|
{
|
2019-06-13 22:05:25 +08:00
|
|
|
|
printf("pipe_reader <- %s", data);
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
// transfer the data to thread_reader
|
|
|
|
|
out_.get_ostream().write(data, len);
|
2019-06-11 23:16:03 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
|
|
|
|
void close_callback(void)
|
|
|
|
|
{
|
2019-06-13 22:05:25 +08:00
|
|
|
|
printf("pipe_reader: being closed!\r\n");
|
2019-06-11 23:16:03 +08:00
|
|
|
|
fflush(stdout);
|
2019-06-13 22:05:25 +08:00
|
|
|
|
|
|
|
|
|
printf("pipe_reader: close pipe_out\r\n");
|
|
|
|
|
out_.get_ostream().close();
|
|
|
|
|
|
2019-06-11 23:16:03 +08:00
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
acl::aio_handle& handle_;
|
|
|
|
|
acl::aio_istream* in_;
|
2019-06-13 22:05:25 +08:00
|
|
|
|
pipe_writer& out_;
|
2019-06-11 23:16:03 +08:00
|
|
|
|
|
|
|
|
|
~pipe_reader(void)
|
|
|
|
|
{
|
2019-06-13 22:05:25 +08:00
|
|
|
|
printf("%s: reader pipe will be deleted!\r\n", __FUNCTION__);
|
|
|
|
|
if (--__aio_refer == 0) {
|
2019-06-11 23:16:03 +08:00
|
|
|
|
printf("%s: stop aio engine now!\r\n", __FUNCTION__);
|
|
|
|
|
handle_.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2019-06-13 22:05:25 +08:00
|
|
|
|
class thread_writer : public acl::thread
|
2019-06-11 23:16:03 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
2019-06-13 22:05:25 +08:00
|
|
|
|
thread_writer(int fd)
|
2019-06-11 23:16:03 +08:00
|
|
|
|
{
|
|
|
|
|
out_ = new acl::socket_stream;
|
|
|
|
|
out_->open(fd);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-13 22:05:25 +08:00
|
|
|
|
~thread_writer(void) {}
|
|
|
|
|
|
2019-06-11 23:16:03 +08:00
|
|
|
|
protected:
|
|
|
|
|
// @override
|
|
|
|
|
void* run(void)
|
|
|
|
|
{
|
2019-06-13 22:05:25 +08:00
|
|
|
|
acl::string buf("hello world!\r\n");
|
2019-06-11 23:16:03 +08:00
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
|
sleep(1);
|
2019-06-13 22:05:25 +08:00
|
|
|
|
printf("\r\n");
|
|
|
|
|
printf("thread_writer -> %s", buf.c_str());
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
out_->write(buf);
|
2019-06-11 23:16:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-13 22:05:25 +08:00
|
|
|
|
printf("thread_writer: close out socket_stream\r\n");
|
|
|
|
|
delete out_;
|
2019-06-11 23:16:03 +08:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
acl::socket_stream* out_;
|
2019-06-13 22:05:25 +08:00
|
|
|
|
};
|
2019-06-11 23:16:03 +08:00
|
|
|
|
|
2019-06-13 22:05:25 +08:00
|
|
|
|
class thread_reader : public acl::thread
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
thread_reader(int fd)
|
2019-06-11 23:16:03 +08:00
|
|
|
|
{
|
2019-06-13 22:05:25 +08:00
|
|
|
|
in_ = new acl::socket_stream;
|
|
|
|
|
in_->open(fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~thread_reader(void) {}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// @override
|
|
|
|
|
void* run(void)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
|
acl::string buf;
|
|
|
|
|
if (!in_->read(buf, false)) {
|
|
|
|
|
printf("thread_reader: read over!\r\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
printf("thread_reader <- %s", buf.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("thread_reader: close in socket_stream\r\n");
|
|
|
|
|
delete in_;
|
|
|
|
|
|
|
|
|
|
return NULL;
|
2019-06-11 23:16:03 +08:00
|
|
|
|
}
|
2019-06-13 22:05:25 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
acl::socket_stream* in_;
|
2019-06-11 23:16:03 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2019-06-05 17:45:12 +08:00
|
|
|
|
class http_aio_client : public acl::http_aclient
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
http_aio_client(acl::aio_handle& handle, acl::polarssl_conf* ssl_conf,
|
|
|
|
|
const char* host)
|
|
|
|
|
: http_aclient(handle, ssl_conf)
|
|
|
|
|
, host_(host)
|
2019-06-06 13:46:23 +08:00
|
|
|
|
, debug_(false)
|
|
|
|
|
, compressed_(false)
|
2019-06-10 13:00:25 +08:00
|
|
|
|
, ws_mode_(false)
|
2019-06-05 17:45:12 +08:00
|
|
|
|
{
|
2019-06-13 22:05:25 +08:00
|
|
|
|
++__aio_refer;
|
2019-06-05 17:45:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~http_aio_client(void)
|
|
|
|
|
{
|
2019-06-11 23:16:03 +08:00
|
|
|
|
printf("delete http_aio_client!\r\n");
|
2019-06-13 22:05:25 +08:00
|
|
|
|
if (--__aio_refer == 0) {
|
2019-06-11 23:16:03 +08:00
|
|
|
|
printf("%s: stop aio engine now!\r\n", __FUNCTION__);
|
|
|
|
|
handle_.stop();
|
|
|
|
|
}
|
2019-06-05 17:45:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-06 13:46:23 +08:00
|
|
|
|
http_aio_client& enable_debug(bool on)
|
|
|
|
|
{
|
|
|
|
|
debug_ = on;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 13:00:25 +08:00
|
|
|
|
http_aio_client& enable_websocket(bool on)
|
|
|
|
|
{
|
|
|
|
|
ws_mode_ = on;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 17:45:12 +08:00
|
|
|
|
protected:
|
|
|
|
|
// @override
|
2019-06-06 13:46:23 +08:00
|
|
|
|
void destroy(void)
|
2019-06-05 17:45:12 +08:00
|
|
|
|
{
|
2019-06-06 13:46:23 +08:00
|
|
|
|
printf("http_aio_client will be deleted!\r\n");
|
|
|
|
|
fflush(stdout);
|
2019-06-05 17:45:12 +08:00
|
|
|
|
|
2019-06-06 13:46:23 +08:00
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
|
|
|
|
bool on_connect(void)
|
|
|
|
|
{
|
2019-06-10 16:08:21 +08:00
|
|
|
|
printf("--------------- connect server ok ------------\r\n");
|
2019-06-05 17:45:12 +08:00
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
2019-06-10 13:00:25 +08:00
|
|
|
|
if (ws_mode_) {
|
2019-06-10 16:08:21 +08:00
|
|
|
|
printf(">>> begin ws_handshake\r\n");
|
2019-06-10 13:00:25 +08:00
|
|
|
|
this->ws_handshake();
|
|
|
|
|
} else {
|
2019-06-10 16:08:21 +08:00
|
|
|
|
printf(">>> begin send http request\r\n");
|
2019-06-10 13:00:25 +08:00
|
|
|
|
this->send_request(NULL, 0);
|
|
|
|
|
}
|
2019-06-05 17:45:12 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
|
|
|
|
void on_disconnect(void)
|
|
|
|
|
{
|
|
|
|
|
printf("disconnect from server\r\n");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 17:42:07 +08:00
|
|
|
|
// @override
|
|
|
|
|
void on_ns_failed(void)
|
|
|
|
|
{
|
|
|
|
|
printf("dns lookup failed\r\n");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 17:45:12 +08:00
|
|
|
|
// @override
|
|
|
|
|
void on_connect_timeout(void)
|
|
|
|
|
{
|
2019-06-06 13:46:23 +08:00
|
|
|
|
printf("connect timeout\r\n");
|
|
|
|
|
fflush(stdout);
|
2019-06-05 17:45:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
|
|
|
|
void on_connect_failed(void)
|
|
|
|
|
{
|
|
|
|
|
printf("connect failed\r\n");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 21:57:24 +08:00
|
|
|
|
// @override
|
2019-08-08 16:13:05 +08:00
|
|
|
|
bool on_read_timeout(void)
|
2019-08-07 21:57:24 +08:00
|
|
|
|
{
|
|
|
|
|
printf("read timeout\r\n");
|
2019-08-08 16:13:05 +08:00
|
|
|
|
return true;
|
2019-08-07 21:57:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 17:42:07 +08:00
|
|
|
|
protected:
|
2019-06-05 17:45:12 +08:00
|
|
|
|
// @override
|
|
|
|
|
bool on_http_res_hdr(const acl::http_header& header)
|
|
|
|
|
{
|
|
|
|
|
acl::string buf;
|
|
|
|
|
header.build_response(buf);
|
|
|
|
|
|
2019-06-06 13:46:23 +08:00
|
|
|
|
compressed_ = header.is_transfer_gzip();
|
|
|
|
|
|
2019-06-05 17:45:12 +08:00
|
|
|
|
printf("---------------response header-----------------\r\n");
|
|
|
|
|
printf("[%s]\r\n", buf.c_str());
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
|
|
|
|
bool on_http_res_body(char* data, size_t dlen)
|
|
|
|
|
{
|
2019-06-10 16:08:21 +08:00
|
|
|
|
if (debug_ && (!compressed_ || this->is_unzip_body())) {
|
2019-06-05 17:45:12 +08:00
|
|
|
|
(void) write(1, data, dlen);
|
|
|
|
|
} else {
|
|
|
|
|
printf(">>>read body: %ld\r\n", dlen);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
2019-06-06 13:46:23 +08:00
|
|
|
|
bool on_http_res_finish(bool success)
|
2019-06-05 17:45:12 +08:00
|
|
|
|
{
|
|
|
|
|
printf("---------------response over-------------------\r\n");
|
2019-06-06 13:46:23 +08:00
|
|
|
|
printf("http finish: keep_alive=%s, success=%s\r\n",
|
|
|
|
|
keep_alive_ ? "true" : "false",
|
|
|
|
|
success ? "ok" : "failed");
|
2019-06-05 17:45:12 +08:00
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
return keep_alive_;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 13:00:25 +08:00
|
|
|
|
protected:
|
|
|
|
|
// @override
|
|
|
|
|
bool on_ws_handshake(void)
|
|
|
|
|
{
|
|
|
|
|
printf(">>> websocket handshake ok\r\n");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
char buf[128];
|
|
|
|
|
snprintf(buf, sizeof(buf), "hello, myname is zsx\r\n");
|
|
|
|
|
size_t len = strlen(buf);
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (!this->ws_send_text(buf, len)) {
|
2019-06-10 13:00:25 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD> websocket <20>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-06-10 13:00:25 +08:00
|
|
|
|
this->ws_read_wait(0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
|
|
|
|
void on_ws_handshake_failed(int status)
|
|
|
|
|
{
|
|
|
|
|
printf(">>> websocket handshake failed, status=%d\r\n", status);
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
|
|
|
|
bool on_ws_frame_text(void)
|
|
|
|
|
{
|
|
|
|
|
printf(">>> got frame text type\r\n");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
|
|
|
|
bool on_ws_frame_binary(void)
|
|
|
|
|
{
|
|
|
|
|
printf(">>> got frame binaray type\r\n");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
|
|
|
|
void on_ws_frame_closed(void)
|
|
|
|
|
{
|
|
|
|
|
printf(">>> got frame closed type\r\n");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
|
|
|
|
bool on_ws_frame_data(char* data, size_t dlen)
|
|
|
|
|
{
|
|
|
|
|
(void) write(1, data, dlen);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
|
|
|
|
bool on_ws_frame_finish(void)
|
|
|
|
|
{
|
|
|
|
|
printf(">>> frame finish\r\n");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 17:45:12 +08:00
|
|
|
|
private:
|
|
|
|
|
acl::string host_;
|
2019-06-06 13:46:23 +08:00
|
|
|
|
bool debug_;
|
|
|
|
|
bool compressed_;
|
2019-06-10 13:00:25 +08:00
|
|
|
|
bool ws_mode_;
|
2019-06-05 17:45:12 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
|
{
|
|
|
|
|
printf("usage: %s -h[help]\r\n"
|
|
|
|
|
" -s server_addr\r\n"
|
2019-08-14 16:36:23 +08:00
|
|
|
|
" -H host[default: www.baidu.com]\r\n"
|
|
|
|
|
" -L url[default: /]\r\n"
|
2019-06-06 13:46:23 +08:00
|
|
|
|
" -D [if in debug mode, default: false]\r\n"
|
2019-06-05 17:45:12 +08:00
|
|
|
|
" -c cocorrent\r\n"
|
2019-06-06 13:46:23 +08:00
|
|
|
|
" -t connect_timeout[default: 5]\r\n"
|
|
|
|
|
" -i rw_timeout[default: 5]\r\n"
|
|
|
|
|
" -Z [enable_gzip, default: false]\r\n"
|
2019-08-07 21:41:43 +08:00
|
|
|
|
" -U [enable_unzip response, default: false]\r\n"
|
2019-06-06 13:46:23 +08:00
|
|
|
|
" -K [keep_alive, default: false]\r\n"
|
2019-06-05 17:45:12 +08:00
|
|
|
|
" -S polarssl_lib_path[default: none]\n"
|
|
|
|
|
" -N name_server[default: 8.8.8.8:53]\r\n"
|
2019-06-10 13:00:25 +08:00
|
|
|
|
" -W [if using websocket, default: no]\r\n"
|
2019-06-05 17:45:12 +08:00
|
|
|
|
, procname);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 18:29:40 +08:00
|
|
|
|
static void add_dns(std::vector<acl::string>& name_servers, const char* s)
|
|
|
|
|
{
|
|
|
|
|
acl::string buf(s);
|
|
|
|
|
const std::vector<acl::string>& tokens = buf.split2(",; \t");
|
|
|
|
|
for (std::vector<acl::string>::const_iterator cit = tokens.begin();
|
|
|
|
|
cit != tokens.end(); ++cit) {
|
|
|
|
|
|
|
|
|
|
name_servers.push_back(*cit);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 17:45:12 +08:00
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
2019-06-06 13:46:23 +08:00
|
|
|
|
acl::polarssl_conf* ssl_conf = NULL;
|
|
|
|
|
int ch, conn_timeout = 5, rw_timeout = 5;
|
2019-08-13 18:29:40 +08:00
|
|
|
|
std::vector<acl::string> name_servers;
|
|
|
|
|
acl::string addr("127.0.0.1:80");
|
2019-08-14 16:36:23 +08:00
|
|
|
|
acl::string host("www.baidu.com"), url("/"), ssl_lib_path;
|
2019-06-06 13:46:23 +08:00
|
|
|
|
bool enable_gzip = false, keep_alive = false, debug = false;
|
2019-08-07 21:41:43 +08:00
|
|
|
|
bool ws_enable = false, enable_unzip = false;
|
2019-06-05 17:45:12 +08:00
|
|
|
|
|
2019-08-14 16:36:23 +08:00
|
|
|
|
while ((ch = getopt(argc, argv, "hs:S:N:H:L:t:i:ZUKDW")) > 0) {
|
2019-06-05 17:45:12 +08:00
|
|
|
|
switch (ch) {
|
|
|
|
|
case 'h':
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return (0);
|
|
|
|
|
case 's':
|
|
|
|
|
addr = optarg;
|
|
|
|
|
break;
|
|
|
|
|
case 'S':
|
|
|
|
|
ssl_lib_path = optarg;
|
|
|
|
|
break;
|
2019-08-14 16:36:23 +08:00
|
|
|
|
case 'L':
|
|
|
|
|
url = optarg;
|
|
|
|
|
break;
|
2019-06-05 17:45:12 +08:00
|
|
|
|
case 'N':
|
2019-08-13 18:29:40 +08:00
|
|
|
|
add_dns(name_servers, optarg);
|
2019-06-05 17:45:12 +08:00
|
|
|
|
break;
|
|
|
|
|
case 'H':
|
|
|
|
|
host = optarg;
|
|
|
|
|
break;
|
2019-06-06 13:46:23 +08:00
|
|
|
|
case 't':
|
|
|
|
|
conn_timeout = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'i':
|
|
|
|
|
rw_timeout = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'Z':
|
|
|
|
|
enable_gzip = true;
|
|
|
|
|
break;
|
2019-06-10 16:08:21 +08:00
|
|
|
|
case 'U':
|
2019-08-07 21:41:43 +08:00
|
|
|
|
enable_unzip = true;
|
2019-06-10 16:08:21 +08:00
|
|
|
|
break;
|
2019-06-06 13:46:23 +08:00
|
|
|
|
case 'K':
|
|
|
|
|
keep_alive = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'D':
|
|
|
|
|
debug = true;
|
|
|
|
|
break;
|
2019-06-10 13:00:25 +08:00
|
|
|
|
case 'W':
|
|
|
|
|
ws_enable = true;
|
|
|
|
|
break;
|
2019-06-05 17:45:12 +08:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 18:29:40 +08:00
|
|
|
|
if (name_servers.empty()) {
|
|
|
|
|
name_servers.push_back("8.8.8.8:53");
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 17:45:12 +08:00
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SSL <20><><EFBFBD>ӿ⣬<D3BF><E2A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SSL <20><><EFBFBD><EFBFBD>ģʽ
|
2019-06-05 17:45:12 +08:00
|
|
|
|
if (!ssl_lib_path.empty()) {
|
|
|
|
|
if (access(ssl_lib_path.c_str(), R_OK) == 0) {
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> libpolarssl.so <20><>ȫ·<C8AB><C2B7>
|
2019-06-11 09:11:14 +08:00
|
|
|
|
acl::polarssl_conf::set_libpath(ssl_lib_path);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD> libpolarssl.so <20><>
|
2019-06-11 09:11:14 +08:00
|
|
|
|
acl::polarssl_conf::load();
|
2019-06-16 20:10:40 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB> SSL <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-06-06 13:46:23 +08:00
|
|
|
|
ssl_conf = new acl::polarssl_conf;
|
2019-06-05 17:45:12 +08:00
|
|
|
|
} else {
|
|
|
|
|
printf("disable ssl, %s not found\r\n",
|
|
|
|
|
ssl_lib_path.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> AIO <20>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>
|
2019-06-05 17:45:12 +08:00
|
|
|
|
acl::aio_handle handle(acl::ENGINE_KERNEL);
|
|
|
|
|
|
2019-06-11 23:16:03 +08:00
|
|
|
|
int fds[2];
|
2019-06-13 22:05:25 +08:00
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2019-06-11 23:16:03 +08:00
|
|
|
|
int ret = acl_sane_socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
printf("acl_sane_socketpair error %s\r\n", acl::last_serror());
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-13 22:05:25 +08:00
|
|
|
|
thread_reader* thread_in = new thread_reader(fds[0]);
|
|
|
|
|
pipe_writer* pipe_out = new pipe_writer(handle, fds[1]);
|
|
|
|
|
|
|
|
|
|
thread_in->start();
|
|
|
|
|
pipe_out->start();
|
|
|
|
|
|
|
|
|
|
ret = acl_sane_socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
printf("acl_sane_socketpair error %s\r\n", acl::last_serror());
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pipe_reader* pipe_in = new pipe_reader(handle, fds[0], *pipe_out);
|
|
|
|
|
thread_writer* thread_out = new thread_writer(fds[1]);
|
2019-06-11 23:16:03 +08:00
|
|
|
|
|
2019-06-13 22:05:25 +08:00
|
|
|
|
pipe_in->start();
|
|
|
|
|
thread_out->start();
|
2019-06-11 23:16:03 +08:00
|
|
|
|
|
2019-06-13 22:05:25 +08:00
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2019-06-11 23:16:03 +08:00
|
|
|
|
|
2019-08-13 18:29:40 +08:00
|
|
|
|
for (std::vector<acl::string>::const_iterator cit = name_servers.begin();
|
|
|
|
|
cit != name_servers.end(); ++cit) {
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> DNS <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
handle.set_dns((*cit).c_str(), 5);
|
|
|
|
|
}
|
2019-06-05 17:45:12 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6> WEB <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-06-06 13:46:23 +08:00
|
|
|
|
http_aio_client* conn = new http_aio_client(handle, ssl_conf, host);
|
|
|
|
|
if (!conn->open(addr, conn_timeout, rw_timeout)) {
|
|
|
|
|
printf("connect %s error\r\n", addr.c_str());
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
delete conn;
|
2019-06-05 17:45:12 +08:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
(*conn).enable_debug(debug) // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>õ<EFBFBD><C3B5>Է<EFBFBD>ʽ
|
|
|
|
|
.enable_websocket(ws_enable); // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD> websocket
|
2019-08-07 21:41:43 +08:00
|
|
|
|
conn->unzip_body(enable_unzip); // <20><><EFBFBD><EFBFBD> HTTP <20>Ƿ<EFBFBD><C7B7>Զ<EFBFBD><D4B6><EFBFBD>ѹ
|
2019-06-06 13:46:23 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>Ҳ<EFBFBD>ɽ<EFBFBD><C9BD>˹<EFBFBD><CBB9>̷<EFBFBD><CCB7><EFBFBD> conn->on_connect() <20><>
|
2019-06-06 13:46:23 +08:00
|
|
|
|
acl::http_header& head = conn->request_header();
|
2019-08-14 16:36:23 +08:00
|
|
|
|
head.set_url(url)
|
2019-06-06 13:46:23 +08:00
|
|
|
|
.set_content_length(0)
|
|
|
|
|
.set_host(host)
|
|
|
|
|
.accept_gzip(enable_gzip)
|
|
|
|
|
.set_keep_alive(keep_alive);
|
|
|
|
|
|
|
|
|
|
acl::string buf;
|
|
|
|
|
head.build_request(buf);
|
|
|
|
|
printf("---------------request header-----------------\r\n");
|
|
|
|
|
printf("[%s]\r\n", buf.c_str());
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʼ AIO <20>¼<EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-06-05 17:45:12 +08:00
|
|
|
|
while (true) {
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> false <20><><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ټ<EFBFBD><D9BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>˳<EFBFBD>
|
2019-06-05 17:45:12 +08:00
|
|
|
|
if (!handle.check()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handle.check();
|
2019-06-06 13:46:23 +08:00
|
|
|
|
delete ssl_conf;
|
2019-06-13 22:05:25 +08:00
|
|
|
|
|
|
|
|
|
thread_out->wait(NULL);
|
|
|
|
|
thread_in->wait(NULL);
|
|
|
|
|
|
|
|
|
|
delete thread_out;
|
|
|
|
|
delete thread_in;
|
|
|
|
|
|
2019-06-06 13:46:23 +08:00
|
|
|
|
return 0;
|
2019-06-05 17:45:12 +08:00
|
|
|
|
}
|