From bf0018ae05283af2c7972a1e422f04d4f6210622 Mon Sep 17 00:00:00 2001 From: hugy <504650082@qq.com> Date: Mon, 5 Jun 2023 14:10:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20hook=E6=B6=88=E6=81=AF=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0http=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/http_client.cc | 65 ++++++++++++++++++++++++++++++++++++++++++++++ src/http_client.h | 24 +++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/http_client.cc create mode 100644 src/http_client.h diff --git a/src/http_client.cc b/src/http_client.cc new file mode 100644 index 0000000..92cb138 --- /dev/null +++ b/src/http_client.cc @@ -0,0 +1,65 @@ +#include "pch.h" +#include "http_client.h" +namespace wxhelper { + + void HttpClient::SendRequest(std::string content) { + struct mg_mgr mgr; + Data data ; + data.done = false; + data.post_data = content; + mg_mgr_init(&mgr); + mg_http_connect(&mgr, url_.c_str(), OnHttpEvent, &data); + while (!data.done){ + mg_mgr_poll(&mgr, 500); + } + mg_mgr_free(&mgr); +} + + + void HttpClient::OnHttpEvent(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + const char * s_url = GetInstance().url_.c_str(); + Data data = *(Data*)fn_data; + if (ev == MG_EV_OPEN) { + // Connection created. Store connect expiration time in c->data + *(uint64_t *) c->data = mg_millis() + GetInstance().timeout_; + } else if (ev == MG_EV_POLL) { + if (mg_millis() > *(uint64_t *) c->data && + (c->is_connecting || c->is_resolving)) { + mg_error(c, "Connect timeout"); + } + } else if (ev == MG_EV_CONNECT) { + struct mg_str host = mg_url_host(s_url); + if (mg_url_is_ssl(s_url)) { + // no implement + } + + // Send request + int content_length = data.post_data.size(); + mg_printf(c, + "POST %s HTTP/1.0\r\n" + "Host: %.*s\r\n" + "Content-Type: application/json\r\n" + "Content-Length: %d\r\n" + "\r\n", + mg_url_uri(s_url), (int) host.len, + host.ptr, content_length); + mg_send(c, data.post_data.c_str(), content_length); + } else if (ev == MG_EV_HTTP_MSG) { + // Response is received. Print it + #ifdef _DEBUG + struct mg_http_message *hm = (struct mg_http_message *) ev_data; + printf("%.*s", (int) hm->message.len, hm->message.ptr); + #endif + c->is_closing = 1; // Tell mongoose to close this connection + data.done = true; // Tell event loop to stops + } else if (ev == MG_EV_ERROR) { + data.done = true; // Error, tell event loop to stop + } +} + +void HttpClient::SetConfig(char* url,uint64_t timeout){ + url_=url; + timeout_=timeout; +} + +} // namespace wxhelper \ No newline at end of file diff --git a/src/http_client.h b/src/http_client.h new file mode 100644 index 0000000..53c641e --- /dev/null +++ b/src/http_client.h @@ -0,0 +1,24 @@ +#ifndef WXHELPER_HTTP_CLIENT_H_ +#define WXHELPER_HTTP_CLIENT_H_ +#include "mongoose.h" +#include "singleton.h" + +namespace wxhelper { +struct Data { + bool done; + std::string post_data; +}; +class HttpClient : public Singleton { + public: + void SendRequest(std::string content); + void SetConfig(char* url,uint64_t timeout); + + static void OnHttpEvent(struct mg_connection *c, int ev, void *ev_data, + void *fn_data); + private: + std::string url_; + uint64_t timeout_; +}; + +} // namespace wxhelper +#endif \ No newline at end of file