2019-07-28 10:31:56 +08:00
|
|
|
|
#include "stdafx.h"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include "http_download.h"
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>̶߳<DFB3>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> DOWN_CTX <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
typedef enum
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
CTX_T_REQ_HDR, // Ϊ HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>
|
|
|
|
|
CTX_T_RES_HDR, // Ϊ HTTP <20><>Ӧͷ<D3A6><CDB7><EFBFBD><EFBFBD>
|
|
|
|
|
CTX_T_CONTENT_LENGTH, // Ϊ HTTP <20><>Ӧ<EFBFBD><D3A6><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>
|
|
|
|
|
CTX_T_PARTIAL_LENGTH, // Ϊ HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
CTX_T_END
|
|
|
|
|
} ctx_t;
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>̶߳<DFB3>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD><DDB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳̽<DFB3><CCBD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
struct DOWN_CTX
|
|
|
|
|
{
|
|
|
|
|
ctx_t type;
|
|
|
|
|
long long int length;
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ؼ<EFBFBD><D8BC><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><EFBFBD>뼶<EFBFBD><EBBCB6>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
static double stamp_sub(const struct timeval *from,
|
|
|
|
|
const struct timeval *sub_by)
|
|
|
|
|
{
|
|
|
|
|
struct timeval res;
|
|
|
|
|
|
|
|
|
|
memcpy(&res, from, sizeof(struct timeval));
|
|
|
|
|
|
|
|
|
|
res.tv_usec -= sub_by->tv_usec;
|
|
|
|
|
if (res.tv_usec < 0)
|
|
|
|
|
{
|
|
|
|
|
--res.tv_sec;
|
|
|
|
|
res.tv_usec += 1000000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.tv_sec -= sub_by->tv_sec;
|
|
|
|
|
return (res.tv_sec * 1000.0 + res.tv_usec/1000.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>̴߳<DFB3><CCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
void http_download::rpc_run()
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
acl::http_request req(addr_); // HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>Ϣ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
req.request_header().set_url(url_.c_str())
|
|
|
|
|
.set_content_type("text/html")
|
|
|
|
|
.set_host(addr_.c_str())
|
|
|
|
|
.set_method(acl::HTTP_METHOD_GET);
|
|
|
|
|
|
|
|
|
|
req.request_header().build_request(req_hdr_);
|
|
|
|
|
DOWN_CTX* ctx = new DOWN_CTX;
|
|
|
|
|
ctx->type = CTX_T_REQ_HDR;
|
2019-07-28 10:31:56 +08:00
|
|
|
|
rpc_signal(ctx); // ֪ͨ<CDA8><D6AA><EFBFBD>߳<EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
struct timeval begin, end;;
|
|
|
|
|
gettimeofday(&begin, NULL);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
if (req.request(NULL, 0) == false)
|
|
|
|
|
{
|
|
|
|
|
logger_error("send request error");
|
|
|
|
|
error_ = false;
|
|
|
|
|
gettimeofday(&end, NULL);
|
|
|
|
|
total_spent_ = stamp_sub(&end, &begin);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl::http_client* conn = req.get_client();
|
|
|
|
|
assert(conn);
|
|
|
|
|
|
|
|
|
|
(void) conn->get_respond_head(&res_hdr_);
|
|
|
|
|
ctx = new DOWN_CTX;
|
|
|
|
|
ctx->type = CTX_T_RES_HDR;
|
2019-07-28 10:31:56 +08:00
|
|
|
|
rpc_signal(ctx); // ֪ͨ<CDA8><D6AA><EFBFBD>߳<EFBFBD> HTTP <20><>Ӧͷ<D3A6><CDB7><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
ctx = new DOWN_CTX;
|
|
|
|
|
ctx->type = CTX_T_CONTENT_LENGTH;
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
ctx->length = conn->body_length(); // <20><><EFBFBD><EFBFBD> HTTP <20><>Ӧ<EFBFBD><D3A6><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>峤<EFBFBD><E5B3A4>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
content_length_ = ctx->length;
|
2019-07-28 10:31:56 +08:00
|
|
|
|
rpc_signal(ctx); // ֪ͨ<CDA8><D6AA><EFBFBD>߳<EFBFBD> HTTP <20><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
acl::string buf(8192);
|
|
|
|
|
int real_size;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><> HTTP <20><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
int ret = req.read_body(buf, true, &real_size);
|
|
|
|
|
if (ret <= 0)
|
|
|
|
|
{
|
|
|
|
|
ctx = new DOWN_CTX;
|
|
|
|
|
ctx->type = CTX_T_END;
|
|
|
|
|
ctx->length = ret;
|
2019-07-28 10:31:56 +08:00
|
|
|
|
rpc_signal(ctx); // ֪ͨ<CDA8><D6AA><EFBFBD>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
ctx = new DOWN_CTX;
|
|
|
|
|
ctx->type = CTX_T_PARTIAL_LENGTH;
|
|
|
|
|
ctx->length = real_size;
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ֪ͨ<CDA8><D6AA><EFBFBD>̵߳<DFB3>ǰ<EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD>صĴ<D8B5>С
|
2014-11-19 00:25:21 +08:00
|
|
|
|
rpc_signal(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ع<EFBFBD><D8B9><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
gettimeofday(&end, NULL);
|
|
|
|
|
total_spent_ = stamp_sub(&end, &begin);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD>̵߳<DFB3> rpc_onover <20><><EFBFBD>̽<EFBFBD><CCBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
http_download::http_download(const char* addr, const char* url,
|
|
|
|
|
rpc_callback* callback)
|
|
|
|
|
: addr_(addr)
|
|
|
|
|
, url_(url)
|
|
|
|
|
, callback_(callback)
|
|
|
|
|
, error_(false)
|
|
|
|
|
, total_read_(0)
|
|
|
|
|
, content_length_(0)
|
|
|
|
|
, total_spent_(0)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>̴߳<DFB3><CCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD><CCA3>յ<EFBFBD><D5B5><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD>Ϣ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
void http_download::rpc_onover()
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
logger("http download(%s) over, <20><> %I64d <20>ֽڣ<D6BD><DAA3><EFBFBD>ʱ %.3f <20><><EFBFBD><EFBFBD>",
|
2014-11-19 00:25:21 +08:00
|
|
|
|
url_.c_str(), total_read_, total_spent_);
|
|
|
|
|
callback_->OnDownloadOver(total_read_, total_spent_);
|
2019-07-28 10:31:56 +08:00
|
|
|
|
delete this; // <20><><EFBFBD>ٱ<EFBFBD><D9B1><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>̴߳<DFB3><CCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD><CCA3>յ<EFBFBD><D5B5><EFBFBD><EFBFBD>̵߳<DFB3>֪ͨ<CDA8><D6AA>Ϣ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
void http_download::rpc_wakeup(void* ctx)
|
|
|
|
|
{
|
|
|
|
|
DOWN_CTX* down_ctx = (DOWN_CTX*) ctx;
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3>д<EFBFBD><D0B4><EFBFBD><EFBFBD>IJ<EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD>ؽν<D7B6><CEBD>д<EFBFBD><D0B4><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
switch (down_ctx->type)
|
|
|
|
|
{
|
|
|
|
|
case CTX_T_REQ_HDR:
|
|
|
|
|
callback_->SetRequestHdr(req_hdr_.c_str());
|
|
|
|
|
break;
|
|
|
|
|
case CTX_T_RES_HDR:
|
|
|
|
|
callback_->SetResponseHdr(res_hdr_.c_str());
|
|
|
|
|
break;
|
|
|
|
|
case CTX_T_CONTENT_LENGTH:
|
|
|
|
|
break;
|
|
|
|
|
case CTX_T_PARTIAL_LENGTH:
|
|
|
|
|
total_read_ += down_ctx->length;
|
|
|
|
|
callback_->OnDownloading(content_length_, total_read_);
|
|
|
|
|
break;
|
|
|
|
|
case CTX_T_END:
|
|
|
|
|
logger("%s: read over", addr_.c_str());
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
logger_error("%s: ERROR", addr_.c_str());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3>ж<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
delete down_ctx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|