-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathHTTPServer.h
172 lines (141 loc) · 3.84 KB
/
HTTPServer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Copyright(c) 2021-2025 jvde.github@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <list>
#include <thread>
#include <mutex>
#include <time.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#define SOCKET int
#define closesocket close
#endif
#ifdef __ANDROID__
#include <netinet/in.h>
#endif
#include "Stream.h"
#include "Common.h"
#include "AIS.h"
#include "Keys.h"
#include "TCP.h"
#include "Utilities.h"
#include "Library/ZIP.h"
namespace IO
{
class SSEConnection
{
protected:
bool running = false;
TCP::ServerConnection *connection;
int _id = 0;
public:
SSEConnection(TCP::ServerConnection *c, int id) : connection(c), _id(id) { c->setVerbosity(false); }
~SSEConnection() { Close(); }
int getID()
{
return _id;
}
void Start()
{
if (!connection)
return;
running = true;
connection->Lock();
std::string headers = "HTTP/1.1 200 OK\r\n";
headers += "Content-Type: text/event-stream\r\n";
headers += "Cache-Control: no-cache\r\n";
headers += "X-Accel-Buffering: no\r\n";
headers += "Connection: keep-alive\r\n\r\n\r\n";
connection->SendDirect(headers.c_str(), headers.length());
}
bool isConnected()
{
return connection && connection->isConnected();
}
void Close()
{
if (connection)
{
connection->SendDirect("\r\n", 1);
connection->Unlock();
connection->Close();
connection = nullptr;
}
}
void SendEvent(const std::string &eventName, const std::string &eventData, const std::string &eventId = "")
{
if (connection && running)
{
std::string eventStr = "event: " + eventName + "\r\n";
if (!eventId.empty())
{
eventStr += "id: " + eventId + "\r\n";
}
eventStr += "data: " + eventData + "\r\n\r\n";
connection->SendDirect(eventStr.c_str(), eventStr.length());
}
}
};
class HTTPServer : public TCP::Server
{
std::array<std::string, 4> sse_topic = {"aiscatcher", "nmea", "nmea", "log"};
public:
virtual void Request(TCP::ServerConnection &c, const std::string &msg, bool accept_gzip);
void Response(TCP::ServerConnection &c, std::string type, const std::string &content, bool gzip = false, bool cache = false);
void Response(TCP::ServerConnection &c, std::string type, char *data, int len, bool gzip = false, bool cache = false);
void ResponseRaw(TCP::ServerConnection &c, std::string type, char *data, int len, bool gzip = false, bool cache = false);
void cleanupSSE()
{
for (auto it = sse.begin(); it != sse.end();)
{
if (!it->isConnected())
{
it->Close();
it = sse.erase(it);
}
else
{
++it;
}
}
}
IO::SSEConnection *upgradeSSE(TCP::ServerConnection &c, int id)
{
cleanupSSE();
sse.emplace_back(&c, id);
auto &connection = sse.back();
connection.Start();
return &connection;
}
void sendSSE(int id, const std::string &event, const std::string &data)
{
for (auto it = sse.begin(); it != sse.end(); ++it)
{
if (it->getID() == id)
it->SendEvent(sse_topic[MIN(id, 3)], data);
}
cleanupSSE();
}
private:
std::string ret, header;
std::list<IO::SSEConnection> sse;
void Parse(const std::string &s, std::string &get, bool &accept_gzip);
void processClients();
ZIP zip;
};
}