acl/lib_acl_cpp/samples/ssl/https_request/https_request.cpp

121 lines
2.5 KiB
C++
Raw Normal View History

2020-07-02 15:44:44 +08:00
#include "stdafx.h"
#include "https_request.h"
https_request::https_request(acl::sslbase_conf* ssl_conf, const char* addr,
2023-09-03 21:20:35 +08:00
const char* host, const char* url, bool debug)
2020-07-02 15:44:44 +08:00
: request_(addr)
, host_(host)
, url_(url)
, to_charset_("utf-8")
2023-09-03 21:20:35 +08:00
, debug_(debug)
2020-07-02 15:44:44 +08:00
{
printf("server addr: %s\r\n", addr);
printf("host: %s\r\n", host);
printf("url: %s\r\n", url);
printf("ssl_conf: %p\r\n", ssl_conf);
2020-07-02 15:44:44 +08:00
printf("\r\n");
if (ssl_conf) {
request_.set_ssl(ssl_conf);
}
}
https_request::~https_request(void)
{
}
void* https_request::run(void)
{
acl::http_header& hdr = request_.request_header();
hdr.set_url(url_)
.set_host(host_)
.set_content_type("text/plain")
.set_keep_alive(true);
if (!request_.request(NULL, 0)) {
printf("send request error\r\n");
return NULL;
}
2023-09-03 21:20:35 +08:00
acl::http_client* conn = request_.get_client();
conn->print_header("http response header");
int http_status = request_.http_status();
printf(">>>http status=%d\r\n", http_status);
2020-07-02 15:44:44 +08:00
const char* ptr = request_.header_value("Content-Type");
if (ptr == NULL || *ptr == 0) {
printf("Content-Type empty!\r\n");
return NULL;
}
acl::http_ctype ctype;
ctype.parse(ptr);
// <20><>Ӧͷ<D3A6><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
const char* stype = ctype.get_stype();
bool ret;
if (stype == NULL) {
ret = do_plain(request_);
} else if (strcasecmp(stype, "xml") == 0) {
ret = do_xml(request_);
} else if (strcasecmp(stype, "json") == 0) {
ret = do_json(request_);
} else {
ret = do_plain(request_);
}
if (ret) {
printf("%s(%d): read ok!\r\n", __FILE__, __LINE__);
} else {
printf("read error\r\n");
}
return NULL;
}
bool https_request::do_plain(acl::http_request& req)
{
2023-09-03 21:20:35 +08:00
printf("Begin get body in plain\r\n");
2020-07-02 15:44:44 +08:00
acl::string body;
if (!req.get_body(body, to_charset_)) {
logger_error("get http body error");
return false;
}
2023-09-03 21:20:35 +08:00
if (debug_) {
printf("plain body:\r\n(%s)\r\n", body.c_str());
} else {
printf("plain body length: %zd\r\n", body.size());
}
2020-07-02 15:44:44 +08:00
return true;
}
bool https_request::do_xml(acl::http_request& req)
{
acl::xml1 body;
if (req.get_body(body, to_charset_)) {
logger_error("get http body error");
return false;
}
printf(">>>xml body:\r\n[%s]\r\n", body.to_string());
return true;
}
bool https_request::do_json(acl::http_request& req)
{
acl::json body;
if (!req.get_body(body, to_charset_)) {
logger_error("get http body error");
return false;
}
printf(">>>json body:\r\n[%s]\r\n", body.to_string().c_str());
return true;
}