http.h

main 64 lines · 1.7 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1#ifndef HTTP_H
2#define HTTP_H
3
4#include <framework/global.h>
5#include "result.h"
6
7class WebsocketSession;
8
9class Http {
10public:
11 Http() : m_ios(), m_guard(boost::asio::make_work_guard(m_ios)) {}
12
13 void init();
14 void terminate();
15
16 int get(const std::string& url, int timeout = 5);
17 int post(const std::string& url, const std::string& data, int timeout = 5, bool isJson = false);
18 int download(const std::string& url, std::string path, int timeout = 5);
19 int ws(const std::string& url, int timeout = 5);
20 bool wsSend(int operationId, std::string message);
21 bool wsClose(int operationId);
22
23 bool cancel(int id);
24
25 const std::map<std::string, HttpResult_ptr>& downloads() {
26 return m_downloads;
27 }
28 void clearDownloads() {
29 m_downloads.clear();
30 }
31 HttpResult_ptr getFile(std::string path) {
32 if (!path.empty() && path[0] == '/')
33 path = path.substr(1);
34 auto it = m_downloads.find(path);
35 if (it == m_downloads.end())
36 return nullptr;
37 return it->second;
38 }
39
40 void setUserAgent(const std::string& userAgent)
41 {
42 m_userAgent = userAgent;
43 }
44
45private:
46 bool m_working = false;
47 int m_operationId = 1;
48 int m_speed = 0;
49 size_t m_lastSpeedUpdate = 0;
50 std::thread m_thread;
51 boost::asio::io_context m_ios;
52 boost::asio::executor_work_guard<boost::asio::io_context::executor_type> m_guard;
53 std::map<int, HttpResult_ptr> m_operations;
54#ifndef __EMSCRIPTEN__
55 std::map<int, std::shared_ptr<WebsocketSession>> m_websockets;
56#endif
57 std::map<std::string, HttpResult_ptr> m_downloads;
58 std::string m_userAgent = "Mozilla/5.0";
59};
60
61extern Http g_http;
62
63#endif // ! HTTP_H
64
Top