2022-01-19 23:47:53 +08:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "http_transfer.h"
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
http_transfer::http_transfer(acl::http_method_t method, request_t* req,
|
|
|
|
response_t* res, int port)
|
2022-01-24 18:36:08 +08:00
|
|
|
: port_(port)
|
|
|
|
, method_(method)
|
|
|
|
, req_(req)
|
2022-01-19 23:47:53 +08:00
|
|
|
, res_(res)
|
2022-01-24 18:36:08 +08:00
|
|
|
, client_(NULL)
|
2022-03-31 18:35:14 +08:00
|
|
|
{
|
|
|
|
box_ = new acl::fiber_tbox<bool>;
|
|
|
|
|
|
|
|
acl::socket_stream& sin = req_->getSocketStream();
|
|
|
|
req_in_.open(sin.sock_handle());
|
|
|
|
|
|
|
|
acl::socket_stream& sout = res_->getSocketStream();
|
|
|
|
res_out_.open(sout.sock_handle());
|
|
|
|
res_client_ = res_->getClient();
|
|
|
|
}
|
2022-01-19 23:47:53 +08:00
|
|
|
|
2022-01-24 18:36:08 +08:00
|
|
|
http_transfer::~http_transfer(void) {
|
2022-03-31 18:35:14 +08:00
|
|
|
req_in_.unbind_sock();
|
|
|
|
res_out_.unbind_sock();
|
|
|
|
|
2022-01-24 18:36:08 +08:00
|
|
|
delete client_;
|
2022-03-31 18:35:14 +08:00
|
|
|
delete box_;
|
2022-01-19 23:47:53 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 18:36:08 +08:00
|
|
|
void http_transfer::wait(bool* keep_alive) {
|
2022-03-31 18:35:14 +08:00
|
|
|
bool* res = box_->pop();
|
2022-01-24 18:36:08 +08:00
|
|
|
assert(res);
|
|
|
|
*keep_alive = *res;
|
|
|
|
delete res;
|
2022-01-19 23:47:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void http_transfer::run(void) {
|
2022-01-24 18:36:08 +08:00
|
|
|
bool* res = new bool;
|
|
|
|
switch (method_) {
|
|
|
|
case acl::HTTP_METHOD_GET:
|
|
|
|
*res = transfer_get();
|
|
|
|
break;
|
|
|
|
case acl::HTTP_METHOD_POST:
|
|
|
|
*res = transfer_post();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logger_error("not support method: %d", (int) method_);
|
|
|
|
*res = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
box_->push(res);
|
2022-01-24 18:36:08 +08:00
|
|
|
}
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
bool http_transfer::open_peer(request_t* req, acl::socket_stream* conn)
|
2022-01-24 18:36:08 +08:00
|
|
|
{
|
2022-03-31 18:35:14 +08:00
|
|
|
const char* host = req->getRemoteHost();
|
2022-01-24 18:36:08 +08:00
|
|
|
if (host == NULL || *host == 0) {
|
|
|
|
logger_error("no Host in request head");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
acl::string buf(host);
|
|
|
|
|
|
|
|
char* ptr = strrchr(buf.c_str(), ':');
|
|
|
|
if (ptr != NULL && *(ptr + 1) != 0) {
|
|
|
|
*ptr++ = 0;
|
|
|
|
int port = atoi(ptr);
|
|
|
|
if (port > 0 && port < 65535) {
|
|
|
|
port_ = port;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acl::string addr;
|
|
|
|
addr.format("%s|%d", buf.c_str(), port_);
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
if (!conn->open(addr, 0, 0)) {
|
2022-03-16 18:14:45 +08:00
|
|
|
logger_error("connect %s error %s",
|
|
|
|
addr.c_str(), acl::last_serror());
|
|
|
|
return false;
|
2022-01-24 18:36:08 +08:00
|
|
|
}
|
|
|
|
|
2022-03-16 18:14:45 +08:00
|
|
|
logger("connect %s ok", addr.c_str());
|
|
|
|
|
|
|
|
bool is_request = true, unzip = false, fixed_stream = true;
|
2022-03-31 18:35:14 +08:00
|
|
|
client_ = new acl::http_client(conn, is_request, unzip, fixed_stream);
|
2022-03-16 18:14:45 +08:00
|
|
|
return true;
|
2022-01-24 18:36:08 +08:00
|
|
|
}
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
bool http_transfer::transfer_request_head(acl::socket_stream* conn) {
|
2022-01-24 18:36:08 +08:00
|
|
|
acl::string header;
|
2022-03-31 18:35:14 +08:00
|
|
|
req_->sprint_header(header, NULL);
|
2022-01-24 18:36:08 +08:00
|
|
|
if (header.empty()) {
|
|
|
|
logger_error("http request head empty");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
header += "\r\n";
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
if (conn->write(header) == -1) {
|
2022-01-24 18:36:08 +08:00
|
|
|
logger_error("write request header error");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:14:45 +08:00
|
|
|
printf(">>>send head: [%s]\r\n", header.c_str());
|
2022-01-24 18:36:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
bool http_transfer::transfer_request_body(acl::socket_stream* conn) {
|
|
|
|
long long length = req_->getContentLength();
|
2022-01-24 18:36:08 +08:00
|
|
|
if (length <= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long n = 0;
|
|
|
|
char buf[8192];
|
|
|
|
|
|
|
|
while (n < length) {
|
2022-03-31 18:35:14 +08:00
|
|
|
int ret = req_in_.read(buf, sizeof(buf), false);
|
2022-01-24 18:36:08 +08:00
|
|
|
if (ret == -1) {
|
|
|
|
logger_error("read request body error");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
if (conn->write(buf, ret) == -1) {
|
2022-01-24 18:36:08 +08:00
|
|
|
logger_error("send request body error");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
n += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool http_transfer::transfer_get(void) {
|
2022-03-31 18:35:14 +08:00
|
|
|
if (!open_peer(req_, &conn_)) {
|
2022-01-24 18:36:08 +08:00
|
|
|
logger_error("open server error");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
if (!transfer_request_head(&conn_)) {
|
2022-01-24 18:36:08 +08:00
|
|
|
logger_error("transfer_request_head error");
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return transfer_response();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool http_transfer::transfer_post(void) {
|
2022-03-31 18:35:14 +08:00
|
|
|
if (!open_peer(req_, &conn_)) {
|
2022-01-24 18:36:08 +08:00
|
|
|
logger_error("open server error");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
if (!transfer_request_head(&conn_)) {
|
2022-01-24 18:36:08 +08:00
|
|
|
logger_error("transfer_request_head error");
|
|
|
|
return false;
|
2022-03-31 18:35:14 +08:00
|
|
|
} else if (!transfer_request_body(&conn_)) {
|
2022-01-24 18:36:08 +08:00
|
|
|
logger_error("transfer_request_body error");
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return transfer_response();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool http_transfer::transfer_response(void) {
|
|
|
|
assert(client_);
|
|
|
|
if (!client_->read_head()) {
|
|
|
|
logger_error("read response head error");
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-31 18:35:14 +08:00
|
|
|
|
|
|
|
bool keep_alive = false; // xxxx
|
2022-03-16 18:14:45 +08:00
|
|
|
client_->header_update("Connection", "Close");
|
2022-01-24 18:36:08 +08:00
|
|
|
|
|
|
|
acl::string header;
|
|
|
|
client_->sprint_header(header, NULL);
|
|
|
|
if (header.empty()) {
|
|
|
|
logger_error("response header empty");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
header += "\r\n";
|
|
|
|
|
|
|
|
printf("response head:\r\n[%s]\r\n", header.c_str());
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
//acl::ostream* out = &res_->getOutputStream();
|
|
|
|
if (res_out_.write(header) == -1) {
|
2022-01-24 18:36:08 +08:00
|
|
|
logger_error("send response head error");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
//acl::http_client* out_client = res_->getClient();
|
|
|
|
//assert(out_client);
|
2022-03-16 18:14:45 +08:00
|
|
|
|
2022-01-24 18:36:08 +08:00
|
|
|
long long length = client_->body_length();
|
|
|
|
if (length == 0) {
|
2022-03-31 18:35:14 +08:00
|
|
|
return client_->is_server_keep_alive() && keep_alive;
|
2022-01-24 18:36:08 +08:00
|
|
|
}
|
|
|
|
|
2022-03-16 18:14:45 +08:00
|
|
|
HTTP_HDR_RES* hdr_res = client_->get_respond_head(NULL);
|
|
|
|
assert(hdr_res);
|
|
|
|
bool chunked = hdr_res->hdr.chunked ? true : false;
|
|
|
|
|
2022-01-24 18:36:08 +08:00
|
|
|
char buf[8192];
|
2022-03-16 18:14:45 +08:00
|
|
|
|
2022-01-24 18:36:08 +08:00
|
|
|
while (true) {
|
|
|
|
int ret = client_->read_body(buf, sizeof(buf));
|
|
|
|
if (ret <= 0) {
|
|
|
|
break;
|
2022-03-16 18:14:45 +08:00
|
|
|
} else if (chunked) {
|
2022-03-31 18:35:14 +08:00
|
|
|
if (!res_client_->write_chunk(res_out_, buf, ret)) {
|
2022-03-16 18:14:45 +08:00
|
|
|
logger_error("send response body error");
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-31 18:35:14 +08:00
|
|
|
} else if (res_out_.write(buf, ret) == -1) {
|
2022-01-24 18:36:08 +08:00
|
|
|
logger_error("send response body error");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:14:45 +08:00
|
|
|
if (chunked) {
|
2022-03-31 18:35:14 +08:00
|
|
|
if (!res_client_->write_chunk_trailer(res_out_)) {
|
2022-03-16 18:14:45 +08:00
|
|
|
logger_error("write chunked trailer error");
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-19 23:47:53 +08:00
|
|
|
}
|
|
|
|
|
2022-03-16 18:14:45 +08:00
|
|
|
return client_->is_server_keep_alive() && false;
|
2022-01-19 23:47:53 +08:00
|
|
|
}
|