httpserver.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #ifndef HTTPSERVER_H
  2. #define HTTPSERVER_H
  3. #include <QObject>
  4. #include <QTcpServer>
  5. #include <QDateTime>
  6. #include <QTimer>
  7. #include <map>
  8. #include <queue>
  9. constexpr int MSECS_TO_AUTOREMOVE_MESSAGES_FROM_BUFFER {15000}; // 15 secs
  10. constexpr int MAX_MESSAGE_LENGTH_WITHOUT_WBR = 30;
  11. constexpr int MAX_NICKNAME_LENGTH_WITHOUT_WBR = 20;
  12. constexpr int BUFFER_SIZE = 2048;
  13. const QString HTTP_ACTUAL_ETAG {__DATE__ __TIME__};
  14. const QString ANCHOR_SUFFIX {"msg"};
  15. const QString RED_1 {"#753d08"};
  16. const QString RED_2 {"#b83e3e"};
  17. const QString GREEN_1 {"#60940c"};
  18. const QString GREEN_2 {"#27940c"};
  19. const QString LOCAL_TIME_MARKER_FOR_MAIN_PAGE {"%LOCAL_TIME%"};
  20. const QString DAILY_REQUESTS_COUNTER_VALUE_FOR_MAIN_PAGE {"%DAILY_REQUESTS%"};
  21. class NickColorist
  22. {
  23. public:
  24. const QString& getGreenColor(bool next = true);
  25. const QString& getRedColor(bool next = true);
  26. private:
  27. const QString* m_currentGreen = &GREEN_1;
  28. const QString* m_currentRed = &RED_1;
  29. };
  30. class Message : public QObject
  31. {
  32. Q_OBJECT
  33. public:
  34. Message(const QString& s, const QString& t, qint64 timestamp, QObject *parent = nullptr);
  35. const QString getSender();
  36. const QString getText();
  37. private:
  38. QString m_sender;
  39. QString m_text;
  40. qint64 m_timestamp;
  41. QTimer m_selfKiller;
  42. signals:
  43. void outDated(qint64);
  44. };
  45. class MessagePool : public QObject
  46. {
  47. Q_OBJECT
  48. public:
  49. MessagePool(QObject *parent = nullptr);
  50. void saveNewMessage(const QString& nick, const QString& text);
  51. const std::multimap<qint64, Message*>* getMessages();
  52. qint64 getLastPing();
  53. private:
  54. qint64 m_lastPing;
  55. std::multimap<qint64, Message*> m_messages; // timestamp, message
  56. public slots:
  57. void messageToDelete(qint64 timestamp);
  58. };
  59. class RequestCounter
  60. {
  61. public:
  62. void operator++();
  63. quint64* value();
  64. private:
  65. quint64 m_requestsCounter {0};
  66. QDate m_lastUpdateDate {QDate::currentDate()};
  67. };
  68. //////////////////////////////////////////////////
  69. class HttpServer : public QObject
  70. {
  71. Q_OBJECT
  72. public:
  73. explicit HttpServer(const QString& address, quint16 port,
  74. const QString& logFolder, const QString& serviceName, const QString& serviceEmoji,
  75. bool ajaxIsDisabled, QObject *parent = nullptr);
  76. ~HttpServer();
  77. private:
  78. QString convertToClickableLink(const QString &httpLine);
  79. std::pair<QString,QString> splitUserNameAndMessage(const QString& rawLine);
  80. QString getRequestPath(const QString &req);
  81. QString getWordFromPath(const QString& path);
  82. void writeMainPage(QTcpSocket* socket, bool isHeadRequest);
  83. void writeCustomPicture(QTcpSocket* socket, QString& urlPath, bool isHeadRequest);
  84. void writeRegularPage(QTcpSocket* socket, QString& urlPath, bool isHeadRequest);
  85. void writeRealTimeChatPage(QTcpSocket* socket, QString& urlPath, bool isHeadRequest);
  86. void writeAboutServerPage(QTcpSocket* socket, QString& urlPath, bool isHeadRequest);
  87. void writeAjaxAnswer(QTcpSocket* socket, QString& urlPath, bool isHeadRequest);
  88. void writeErrorPage(QTcpSocket*, const QString& text = "NOT FOUND");
  89. void writeErrorJson(QTcpSocket*, const QString& text = "");
  90. void removeBrakelineSymbols(QString& line);
  91. inline void replaceTag(QString& page, const QString& tag, const QString& payload);
  92. void consoleLog(const QString &message);
  93. void debugLog(const QString &url);
  94. QTcpServer* m_TcpServer;
  95. QString m_serviceButton;
  96. QString m_serviceName;
  97. QString m_dataFolder;
  98. bool m_ajaxIsDisabled;
  99. RequestCounter m_requestCounterPlainInfo;
  100. RequestCounter m_requestCounterAjax;
  101. std::map<QString, std::map<QString, QStringList>> m_servers; // server, {channel, users}
  102. std::map<QString, std::map<QString, QString>> m_channelsTopic; // server, {channel, topic}
  103. std::map<QString, bool> m_serversOnline;
  104. std::map<QString, QString> m_botNick;
  105. QMap<QString, MessagePool*> m_messageCache; // server+channel
  106. const QString HEADER_HTML = "\
  107. HTTP/1.1 200 OK\r\n\
  108. Content-Type: text/html; charset=utf-8\r\n\
  109. Content-Length: {{SIZE}}\r\n\r\n";
  110. const QString HEADER_TEXT = "\
  111. HTTP/1.1 200 OK\r\n\
  112. Content-Type: text/plain; charset=utf-8\r\n\
  113. Content-Length: {{SIZE}}\r\n\r\n";
  114. const QString HEADER_CSS = "\
  115. HTTP/1.1 200 OK\r\n\
  116. Content-Type: text/css\r\n\
  117. Cache-Control: public, max-age=31536000\r\n\
  118. ETag: \""+HTTP_ACTUAL_ETAG+"\"\r\n\
  119. Content-Length: {{SIZE}}\r\n\r\n";
  120. const QString HEADER_SVG = "\
  121. HTTP/1.1 200 OK\r\n\
  122. Content-Type: image/svg+xml\r\n\
  123. Cache-Control: public, max-age=31536000\r\n\
  124. ETag: \""+HTTP_ACTUAL_ETAG+"\"\r\n\
  125. Content-Length: {{SIZE}}\r\n\r\n";
  126. const QString HEADER_ICO = "\
  127. HTTP/1.1 200 OK\r\n\
  128. Content-Type: image/ico\r\n\
  129. Cache-Control: public, max-age=31536000\r\n\
  130. ETag: \""+HTTP_ACTUAL_ETAG+"\"\r\n\
  131. Content-Length: {{SIZE}}\r\n\r\n";
  132. const QString HEADER_IMG = "\
  133. HTTP/1.1 200 OK\r\n\
  134. Content-Type: image/{{TYPE}}\r\n\
  135. Cache-Control: public, max-age=31536000\r\n\
  136. ETag: \""+HTTP_ACTUAL_ETAG+"\"\r\n\
  137. Content-Length: {{SIZE}}\r\n\r\n";
  138. const QString HEADER_JS = "\
  139. HTTP/1.1 200 OK\r\n\
  140. Content-Type: text/javascript; charset=utf-8\r\n\
  141. Cache-Control: public, max-age=31536000\r\n\
  142. ETag: \""+HTTP_ACTUAL_ETAG+"\"\r\n\
  143. Content-Length: {{SIZE}}\r\n\r\n";
  144. const QString HEADER_JSON = "\
  145. HTTP/1.1 200 OK\r\n\
  146. Content-Type: application/json; charset=utf-8\r\n\
  147. Content-Length: {{SIZE}}\r\n\r\n";
  148. const QString HEADER_MP3 = "\
  149. HTTP/1.1 200 OK\r\n\
  150. Content-Type: audio/mpeg;\r\n\
  151. ETag: \""+HTTP_ACTUAL_ETAG+"\"\r\n\
  152. Content-Length: {{SIZE}}\r\n\r\n";
  153. const QString HEADER_304 = "HTTP/1.1 304 Not Modified\r\nContent-Length: 0\r\n\r\n";
  154. const QString HEADER_404 = "HTTP/1.1 404 Not found\r\n\r\n";
  155. const QString HTML_SERVER_SECTION = "\
  156. <div class=\"left_menu__item\">\n\
  157. <div class=\"left_menu__item_title\">\n\
  158. <span style=\"font-size: 17px;\">{{ONLINE_STATUS}}</span>\n\
  159. <a class=\"left_menu__item_title_name\" href=\"{{ABOUT_SERVER}}\">{{SERVER_NAME}}</a>\n\
  160. </div>\n\
  161. <div class=\"left_menu__item_rooms\">\n\
  162. {{CHANNELS}}\
  163. </div>\n\
  164. </div>\n";
  165. const QString HTML_SERVER_ONLINE_MARKER = "&#9989;";
  166. const QString HTML_SERVER_OFFLINE_MARKER = "&#10060;";
  167. const QString HTML_SERVER_SECTION_CHANNEL = "\
  168. <a href=\"{{CHANNEL_LINK}}\" class=\"left_menu__item_link\">{{CHANNEL_NAME}}</a>\n";
  169. const QString HTML_SERVER_SECTION_CHANNEL_SELECTED = "\
  170. <a href=\"{{CHANNEL_LINK}}\" class=\"left_menu__item_link__selected\">{{CHANNEL_NAME}}</a>\n";
  171. const QString HTML_ONLINE_POINT = "\
  172. <div class=\"main_middle__online_point\">\n\
  173. {{NICKNAME}}\n\
  174. </div>\n";
  175. const QString HTML_PAYLOAD_LIST_POINT_FOLDER = "\
  176. <a href=\"{{POINT_LINK}}\" class=\"main_payload__block\">\n\
  177. <div class=\"main_payload__block_folder\">\n\
  178. </div>\n\
  179. <div class=\"main_payload__block_text\">\n\
  180. {{POINT_CONTENT}}\n\
  181. </div>\n\
  182. </a>\n";
  183. const QString HTML_PAYLOAD_LIST_POINT_MESSAGE = "\
  184. <a href=\"{{POINT_LINK}}\" class=\"main_payload__block\">\n\
  185. <div class=\"main_payload__block_message\">\n\
  186. </div>\n\
  187. <div class=\"main_payload__block_text\">\n\
  188. {{POINT_CONTENT}}\n\
  189. </div>\n\
  190. </a>\n";
  191. const QString HTML_PAYLOAD_LIST_CHAT_MESSAGE = "\
  192. <div class=\"main_payload__chat\">\n\
  193. <a name=\"{{ANCHOR}}\" href=\"{{ANCHOR}}\" class=\"main_payload__chat_username\" style=\"color: {{COLOR}}\">\n\
  194. {{USERNAME}}\n\
  195. </a>\n\
  196. <span class=\"main_payload__chat_mail\">\n\
  197. {{MESSAGE_TEXT}}\n\
  198. </span>\n\
  199. </div>\n";
  200. const QString HTML_PAYLOAD_ADDITIONAL_ARROWS = "\
  201. <a href=\"{{CURRENT_DATA_LOG}}\" title=\"Jump to today logs\" class=\"main_header__title_arrows\"></a>\n";
  202. const QString HTML_PAYLOAD_ADDITIONAL_NOTIFY = "\
  203. <div onclick=\"sound()\" id=\"musicanotes\" class=\"main_header__title_notify\"></div>\n";
  204. const QString HTML_PAYLOAD_MIDDLEPATH_ARROW_PREV = "\
  205. <a href=\"{{LINK}}\" title=\"Previous day\" class=\"main_middle__path_aleft\"></a>\n";
  206. const QString HTML_PAYLOAD_MIDDLEPATH_ARROW_PREV_FALSE = "\
  207. <a class=\"main_middle__path_aleft_f\"></a>\n";
  208. const QString HTML_PAYLOAD_MIDDLEPATH_ARROW_NEXT = "\
  209. <a href=\"{{LINK}}\" title=\"Next day\" class=\"main_middle__path_aright\"></a>\n";
  210. const QString HTML_PAYLOAD_MIDDLEPATH_ARROW_NEXT_FALSE = "\
  211. <a class=\"main_middle__path_aright_f\"></a>\n";
  212. const QString HTML_PAYLOAD_ERROR = "\
  213. <div class=\"main_payload__error\">\n\
  214. <span style=\"color: red; display: block; font-size: 24px;\">{{ERROR_TITLE}}</span><br>\n\
  215. {{ERROR_TEXT}}\n\
  216. </div>";
  217. const QString HTML_PAYLOAD_ABOUT = "\
  218. <div class=\"main_payload__about\">\n\
  219. <span style=\"color: green; display: block; font-size: 24px; text-align: center;\">{{ABOUT_TITLE}}</span><br>\n\
  220. {{ABOUT_TEXT}}\n\
  221. </div>";
  222. const QString HTML_LEFT_MENU_MAIN_POINT = "\
  223. <div class=\"left_menu__mainitem\">\n\
  224. <div class=\"left_menu__item_title\">\n\
  225. <span style=\"font-size: 17px;\">{{EMOJI}}</span>\n\
  226. <a class=\"left_menu__item_title_name\" href=\"/\">{{SERVICE_NAME}}</a>\n\
  227. </div>\n\
  228. </div>\n";
  229. private slots:
  230. void acceptor();
  231. void reader();
  232. public slots:
  233. void ircBotFirstInfo(QString server, QStringList channels); // needed for permanent offline servers
  234. void ircUsersOnline(QString server, QString channel, QStringList users);
  235. void ircChannelTopic(QString server, QString channel, QString topic);
  236. void ircServerOnline(QString server, quint8 status /*0 or 1*/);
  237. void ircBotNick(QString server, QString nickname);
  238. void ircMessageCache(QString server, QString channel, QString nick, QString text);
  239. };
  240. #endif // HTTPSERVER_H