httpserver.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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__}; // Change it if svg, css or ico was modified
  14. class Message : public QObject
  15. {
  16. Q_OBJECT
  17. public:
  18. Message(const QString& s, const QString& t, qint64 timestamp, QObject *parent = nullptr);
  19. const QString getSender();
  20. const QString getText();
  21. private:
  22. QString m_sender;
  23. QString m_text;
  24. qint64 m_timestamp;
  25. QTimer m_selfKiller;
  26. signals:
  27. void outDated(qint64);
  28. };
  29. class MessagePool : public QObject
  30. {
  31. Q_OBJECT
  32. public:
  33. MessagePool(QObject *parent = nullptr);
  34. void saveNewMessage(const QString& nick, const QString& text);
  35. const std::multimap<qint64, Message*>* getMessages();
  36. qint64 getLastPing();
  37. private:
  38. qint64 m_lastPing;
  39. std::multimap<qint64, Message*> m_messages; // timestamp, message
  40. public slots:
  41. void messageToDelete(qint64 timestamp);
  42. };
  43. //////////////////////////////////////////////////
  44. class HttpServer : public QObject
  45. {
  46. Q_OBJECT
  47. public:
  48. explicit HttpServer(const QString& address, quint16 port,
  49. const QString& logFolder, const QString& mainChannel,
  50. bool ajaxIsDisabled, QObject *parent = nullptr);
  51. ~HttpServer();
  52. private:
  53. QString convertToClickableLink(const QString &httpLine);
  54. std::pair<QString,QString> splitUserNameAndMessage(const QString& rawLine);
  55. QString getRequestPath(const QString &req);
  56. QString getWordFromPath(const QString& path);
  57. void writeMainPage(QTcpSocket* socket, QString& urlPath, bool isHeadRequest);
  58. void writeRealTimeChatPage(QTcpSocket* socket, QString& urlPath, bool isHeadRequest);
  59. void writeAboutServerPage(QTcpSocket* socket, QString& urlPath, bool isHeadRequest);
  60. void writeAjaxAnswer(QTcpSocket* socket, QString& urlPath, bool isHeadRequest);
  61. void writeErrorPage(QTcpSocket*, const QString& text = "NOT FOUND");
  62. void writeErrorJson(QTcpSocket*, const QString& text = "");
  63. void removeBrakelineSymbols(QString& line);
  64. inline void replaceTag(QString& page, const QString& tag, const QString& payload);
  65. void consoleLog(const QString &message);
  66. void debugLog(const QString &url);
  67. QTcpServer* m_TcpServer;
  68. QString m_mainChannel;
  69. QString m_logFolder;
  70. bool m_ajaxIsDisabled;
  71. std::map<QString, std::map<QString, QStringList>> m_servers; // server, {channel, users}
  72. std::map<QString, std::map<QString, QString>> m_channelsTopic; // server, {channel, topic}
  73. std::map<QString, bool> m_serversOnline;
  74. std::map<QString, QString> m_botNick;
  75. QMap<QString, MessagePool*> m_messageCache; // server+channel
  76. const QString HEADER_HTML = "\
  77. HTTP/1.1 200 OK\r\n\
  78. Content-Type: text/html; charset=utf-8\r\n\
  79. Content-Length: {{SIZE}}\r\n\r\n";
  80. const QString HEADER_TEXT = "\
  81. HTTP/1.1 200 OK\r\n\
  82. Content-Type: text/plain; charset=utf-8\r\n\
  83. Content-Length: {{SIZE}}\r\n\r\n";
  84. const QString HEADER_CSS = "\
  85. HTTP/1.1 200 OK\r\n\
  86. Content-Type: text/css\r\n\
  87. Cache-Control: public, max-age=31536000\r\n\
  88. ETag: \""+HTTP_ACTUAL_ETAG+"\"\r\n\
  89. Content-Length: {{SIZE}}\r\n\r\n";
  90. const QString HEADER_SVG = "\
  91. HTTP/1.1 200 OK\r\n\
  92. Content-Type: image/svg+xml\r\n\
  93. Cache-Control: public, max-age=31536000\r\n\
  94. ETag: \""+HTTP_ACTUAL_ETAG+"\"\r\n\
  95. Content-Length: {{SIZE}}\r\n\r\n";
  96. const QString HEADER_ICO = "\
  97. HTTP/1.1 200 OK\r\n\
  98. Content-Type: image/ico\r\n\
  99. Cache-Control: public, max-age=31536000\r\n\
  100. ETag: \""+HTTP_ACTUAL_ETAG+"\"\r\n\
  101. Content-Length: {{SIZE}}\r\n\r\n";
  102. const QString HEADER_JS = "\
  103. HTTP/1.1 200 OK\r\n\
  104. Content-Type: text/javascript; charset=utf-8\r\n\
  105. Cache-Control: public, max-age=31536000\r\n\
  106. ETag: \""+HTTP_ACTUAL_ETAG+"\"\r\n\
  107. Content-Length: {{SIZE}}\r\n\r\n";
  108. const QString HEADER_JSON = "\
  109. HTTP/1.1 200 OK\r\n\
  110. Content-Type: application/json; charset=utf-8\r\n\
  111. Content-Length: {{SIZE}}\r\n\r\n";
  112. const QString HEADER_MP3 = "\
  113. HTTP/1.1 200 OK\r\n\
  114. Content-Type: audio/mpeg;\r\n\
  115. ETag: \""+HTTP_ACTUAL_ETAG+"\"\r\n\
  116. Content-Length: {{SIZE}}\r\n\r\n";
  117. const QString HEADER_304 = "HTTP/1.1 304 Not Modified\r\nContent-Length: 0\r\n\r\n";
  118. const QString HEADER_404 = "HTTP/1.1 404 Not found\r\n\r\n";
  119. const QString HTML_SERVER_SECTION = "\
  120. <div class=\"left_menu__item\">\n\
  121. <div class=\"left_menu__item_title\">\n\
  122. <span style=\"font-size: 17px;\">{{ONLINE_STATUS}}</span>\n\
  123. <a class=\"left_menu__item_title_name\" href=\"{{ABOUT_SERVER}}\">{{SERVER_NAME}}</a>\n\
  124. </div>\n\
  125. <div class=\"left_menu__item_rooms\">\n\
  126. {{CHANNELS}}\
  127. </div>\n\
  128. </div>\n";
  129. const QString HTML_SERVER_ONLINE_MARKER = "&#9989;";
  130. const QString HTML_SERVER_OFFLINE_MARKER = "&#10060;";
  131. const QString HTML_SERVER_SECTION_CHANNEL = "\
  132. <a href=\"{{CHANNEL_LINK}}\" class=\"left_menu__item_link\">{{CHANNEL_NAME}}</a>\n";
  133. const QString HTML_SERVER_SECTION_CHANNEL_SELECTED = "\
  134. <a href=\"{{CHANNEL_LINK}}\" class=\"left_menu__item_link__selected\">{{CHANNEL_NAME}}</a>\n";
  135. const QString HTML_ONLINE_POINT = "\
  136. <div class=\"main_middle__online_point\">\n\
  137. {{NICKNAME}}\n\
  138. </div>\n";
  139. const QString HTML_PAYLOAD_LIST_POINT_FOLDER = "\
  140. <a href=\"{{POINT_LINK}}\" class=\"main_payload__block\">\n\
  141. <div class=\"main_payload__block_folder\">\n\
  142. </div>\n\
  143. <div class=\"main_payload__block_text\">\n\
  144. {{POINT_CONTENT}}\n\
  145. </div>\n\
  146. </a>\n";
  147. const QString HTML_PAYLOAD_LIST_POINT_MESSAGE = "\
  148. <a href=\"{{POINT_LINK}}\" class=\"main_payload__block\">\n\
  149. <div class=\"main_payload__block_message\">\n\
  150. </div>\n\
  151. <div class=\"main_payload__block_text\">\n\
  152. {{POINT_CONTENT}}\n\
  153. </div>\n\
  154. </a>\n";
  155. const QString HTML_PAYLOAD_LIST_CHAT_MESSAGE = "\
  156. <div class=\"main_payload__chat\">\n\
  157. <span class=\"main_payload__chat_username\">\n\
  158. {{USERNAME}}\n\
  159. </span>\n\
  160. <span class=\"main_payload__chat_mail\">\n\
  161. {{MESSAGE_TEXT}}\n\
  162. </span>\n\
  163. </div>\n";
  164. const QString HTML_PAYLOAD_ADDITIONAL_ARROWS = "\
  165. <a href=\"{{CURRENT_DATA_LOG}}\" title=\"Jump to today logs\" class=\"main_header__title_arrows\"></a>\n";
  166. const QString HTML_PAYLOAD_ADDITIONAL_NOTIFY = "\
  167. <div onclick=\"sound()\" id=\"musicanotes\" class=\"main_header__title_notify\"></div>\n";
  168. const QString HTML_PAYLOAD_ERROR = "\
  169. <div class=\"main_payload__error\">\n\
  170. <span style=\"color: red; display: block; font-size: 24px;\">{{ERROR_TITLE}}</span><br>\n\
  171. {{ERROR_TEXT}}\n\
  172. </div>";
  173. const QString HTML_PAYLOAD_ABOUT = "\
  174. <div class=\"main_payload__about\">\n\
  175. <span style=\"color: green; display: block; font-size: 24px; text-align: center;\">{{ABOUT_TITLE}}</span><br>\n\
  176. {{ABOUT_TEXT}}\n\
  177. </div>";
  178. private slots:
  179. void acceptor();
  180. void reader();
  181. public slots:
  182. void ircBotFirstInfo(QString server, QStringList channels); // needed for permanent offline servers
  183. void ircUsersOnline(QString server, QString channel, QStringList users);
  184. void ircChannelTopic(QString server, QString channel, QString topic);
  185. void ircServerOnline(QString server, quint8 status /*0 or 1*/);
  186. void ircBotNick(QString server, QString nickname);
  187. void ircMessageCache(QString server, QString channel, QString nick, QString text);
  188. };
  189. #endif // HTTPSERVER_H