This commit is contained in:
azw 2023-09-13 14:15:59 +00:00
parent ae2a2bcddb
commit 5954fb5fcb
2 changed files with 45 additions and 27 deletions

View File

@ -9,24 +9,38 @@
namespace f8
{
JsonHttpRequest::JsonHttpRequest()
JsonHttpRequest::JsonHttpRequest(unsigned long saddr,
const std::string url,
const std::string& query_str,
a8::CommonCbProc cb)
{
params = std::make_shared<a8::XObject>();
resp_xobj = a8::MutableXObject::CreateObject();
saddr_ = saddr;
url_ = url;
query_str_ = query_str;
cb_ = cb;
params_ = std::make_shared<a8::XObject>();
resp_xobj_ = a8::MutableXObject::CreateObject();
params_->ReadFromUrlQueryString(query_str);
GetResp()->SetVal("errcode", 0);
GetResp()->SetVal("errmsg", "");
}
JsonHttpRequest::~JsonHttpRequest()
{
if (context && free_context) {
free_context(context);
Response();
}
void JsonHttpRequest::Response()
{
if (!resped_) {
if (cb_) {
std::string response;
resp_xobj_->ToJsonStr(response);
cb_(a8::Args({a8::HttpResponse(response)}));
}
resped_ = true;
}
}
std::string JsonHttpRequest::Response()
{
std::string response;
resp_xobj->ToJsonStr(response);
return a8::HttpResponse(response);
}
}

View File

@ -9,24 +9,28 @@ namespace a8
namespace f8
{
struct JsonHttpRequest
class JsonHttpRequest
{
bool pending = false;
unsigned long saddr = 0;
int socket_handle = 0;
time_t create_time = 0;
time_t handle_time = 0;
std::string query_str;
std::shared_ptr<a8::XObject> params;
std::shared_ptr<a8::MutableXObject> resp_xobj;
private:
unsigned long saddr_ = 0;
std::string url_;
std::string query_str_;
std::shared_ptr<a8::XObject> params_;
std::shared_ptr<a8::MutableXObject> resp_xobj_;
a8::CommonCbProc cb_;
bool resped_ = false;
int async_pending_count = 0;
void* context = nullptr;
void (*free_context)(void*) = nullptr;
JsonHttpRequest();
public:
JsonHttpRequest(unsigned long saddr,
const std::string url,
const std::string& query_str,
a8::CommonCbProc cb);
~JsonHttpRequest();
std::string Response();
const std::string GetUrl() { return this->url_; }
std::shared_ptr<a8::XObject> GetParams() { return this->params_; }
std::shared_ptr<a8::MutableXObject> GetResp() { return this->resp_xobj_; }
void Response();
};
}