acl/lib_fiber/samples-gui/HttpGet/HttpClient.cpp

142 lines
2.7 KiB
C++
Raw Normal View History

2021-09-13 00:19:49 +08:00
#include "pch.h"
#include "HttpGetDlg.h"
#include "HttpClient.h"
CHttpClient::CHttpClient(CHttpGetDlg& hWin, const CString& url)
2021-09-13 16:55:48 +08:00
: m_hWin(&hWin)
, m_box(NULL)
2021-09-13 00:19:49 +08:00
, m_url(url)
{
}
2021-09-13 16:55:48 +08:00
CHttpClient::CHttpClient(acl::fiber_tbox<CHttpMsg>& box, const CString& url)
: m_hWin(NULL)
, m_box(&box)
, m_url(url)
{
}
CHttpClient::~CHttpClient() {}
2021-09-13 00:19:49 +08:00
void CHttpClient::run()
{
acl::http_url hu;
if (!hu.parse(m_url.GetString())) {
2021-09-13 16:55:48 +08:00
SetError("Invalid url=%s", m_url.GetString());
SetEnd();
2021-09-13 00:19:49 +08:00
return;
}
const char* domain = hu.get_domain();
unsigned short port = hu.get_port();
CString addr;
addr.Format("%s|%d", domain, port);
acl::http_request request(addr.GetString());
CString url;
url.Format("%s", hu.get_url_path());
const char* params = hu.get_url_params();
if (*params != 0) {
url.AppendFormat("?%s", params);
}
acl::http_header& header = request.request_header();
header.set_url(url.GetString()).accept_gzip(true).set_host(domain);
acl::string head;
header.build_request(head);
2021-09-13 16:55:48 +08:00
SetRequestHead(head.c_str());
2021-09-13 00:19:49 +08:00
if (!request.request(NULL, 0)) {
2021-09-13 16:55:48 +08:00
SetError("Send request to %s error: %s",
2021-09-13 00:19:49 +08:00
addr.GetString(), acl::last_serror());
2021-09-13 16:55:48 +08:00
SetEnd();
2021-09-13 00:19:49 +08:00
return;
}
acl::http_client* client = request.get_client();
head.clear();
client->sprint_header(head);
2021-09-13 16:55:48 +08:00
SetResponseHead(head);
2021-09-13 00:19:49 +08:00
long long length = request.body_length();
2021-09-13 16:55:48 +08:00
SetBodyTotalLength(length);
2021-09-13 00:19:49 +08:00
length = 0;
char buf[8192];
while (true) {
int ret = request.read_body(buf, sizeof(buf));
if (ret <= 0) {
break;
}
length += ret;
2021-09-13 16:55:48 +08:00
SetBodyLength(length);
}
SetBodyLength(length);
SetEnd();
}
void CHttpClient::SetError(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
CString buf;
buf.FormatV(fmt, ap);
va_end(ap);
if (m_box) {
CHttpMsg* msg = new CHttpMsg(buf.GetString(), HTTP_MSG_ERR);
m_box->push(msg);
} else if (m_hWin) {
m_hWin->SetError("%s", buf.GetString());
}
}
void CHttpClient::SetRequestHead(const char* data)
{
if (m_box) {
CHttpMsg* msg = new CHttpMsg(data, HTTP_MSG_REQ);
m_box->push(msg);
} else if (m_hWin) {
m_hWin->SetRequestHead(data);
2021-09-13 00:19:49 +08:00
}
2021-09-13 16:55:48 +08:00
}
void CHttpClient::SetResponseHead(const char* data)
{
if (m_box) {
CHttpMsg* msg = new CHttpMsg(data, HTTP_MSG_RES);
m_box->push(msg);
} else if (m_hWin) {
m_hWin->SetResponseHead(data);
}
}
void CHttpClient::SetBodyTotalLength(long long length)
{
if (m_box) {
CHttpMsg* msg = new CHttpMsg(length, HTTP_MSG_TOTAL_LENGTH);
m_box->push(msg);
} else if (m_hWin) {
m_hWin->SetBodyTotalLength(length);
}
}
2021-09-13 00:19:49 +08:00
2021-09-13 16:55:48 +08:00
void CHttpClient::SetBodyLength(long long length)
{
if (m_box) {
CHttpMsg* msg = new CHttpMsg(length, HTTP_MSG_LENGTH);
m_box->push(msg);
} else if (m_hWin) {
m_hWin->SetBodyLength(length);
}
}
void CHttpClient::SetEnd() {
if (m_box) {
m_box->push(NULL);
}
2021-09-13 00:19:49 +08:00
}