Anklang-0.3.0.dev797+g4e3241f3 anklang-0.3.0.dev797+g4e3241f3
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
websocket.cc
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#include "websocket.hh"
3#include "platform.hh"
4#include "strings.hh"
5#include "formatter.hh"
6#include "path.hh"
7#include "blob.hh"
8#include "utils.hh"
9#include "regex.hh"
10#include "randomhash.hh"
11#include "internal.hh"
12#include "main.hh"
13#include <regex>
14#include <fstream>
15#include <map>
16#include <vector>
17
18// POSIX Network Headers
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <fcntl.h>
23#include <poll.h>
24#include <unistd.h>
25
26#include <websocketpp/config/core.hpp>
27#include <websocketpp/server.hpp>
28
29#define IODEBUG(...) while (0) { printerr (__VA_ARGS__); break; }
30
31namespace Ase {
32
33struct WebSocketServerImpl;
34using WebSocketServerImplP = std::shared_ptr<WebSocketServerImpl>;
35
36// Use core config with sync concurrency so send() is thread-safe
37struct CustomServerConfig : public websocketpp::config::core {
38 static const size_t connection_read_buffer_size = 16384;
39};
40
41using WppServer = websocketpp::server<CustomServerConfig>;
42using WppConnectionP = WppServer::connection_ptr;
43using WppConnection = WppConnectionP::element_type;
44using WppHdl = websocketpp::connection_hdl;
45
46static inline bool
47operator!= (const WppHdl &a, const WppHdl &b)
48{
49 return a.owner_before (b) || b.owner_before (a);
50}
51static inline bool operator== (const WppHdl &a, const WppHdl &b) { return !(a != b); }
52
53// State for main_loop integration
55 int fd;
56 WppConnectionP con;
57 std::string out_buffer;
58 std::string local_ip;
59 int local_port = 0;
60 std::string remote_ip;
61 int remote_port = 0;
62 Ase::LoopID source_id = Ase::LoopID::INVALID;
63};
64
65// == WebSocketServerImpl ==
69 WppServer wppserver_;
70 String server_url_, dir_, token_, see_other_;
72 ConVec opencons_;
73 RegexVector ignores_;
74 MakeConnection make_con_;
75 int logflags_ = 0;
76 int listen_port_ = 0;
78 int listen_fd_ = -1;
79 Ase::LoopID listen_source_id_ = Ase::LoopID::INVALID;
80
81 void update_client_sources();
82 bool io_handler (PollFD &pfd);
83
84 void setup (const String &host, int port);
85 void run ();
86 WebSocketConnectionP make_connection (WppHdl hdl);
87 WebSocketConnection* force_con (WppHdl);
88 friend class WebSocketServer;
89public:
90 int listen_port () const override { return listen_port_; }
91 String url () const override { return server_url_; }
92 WppConnectionP wppconp (WppHdl hdl)
93 {
94 websocketpp::lib::error_code ec;
95 return wppserver_.get_con_from_hdl (hdl, ec); // nullptr if (ec)
96 }
97 void
98 http_dir (const String &path) override
99 {
100 assert_return (listen_fd_ < 0);
101 dir_ = Path::normalize (path);
102 aliases_.clear();
103 ignores_.clear();
104
105 // compile .aseignore
106 String cxxline;
107 std::ifstream aseignore;
108 aseignore.open (Path::join (dir_, ".aseignore"));
109 while (!aseignore.fail() && std::getline (aseignore, cxxline))
110 if (!cxxline.empty())
111 ignores_.push_back (std::regex (cxxline));
112 }
113 void
114 http_alias (const String &webdir, const String &path) override
115 {
116 assert_return (!dir_.empty());
117 String aliaspath = Path::normalize (Path::abspath (path, dir_));
118 aliases_.push_back (std::make_pair (webdir, aliaspath));
119 // sort by URL length, longest URLs come first
120 std::stable_sort (aliases_.begin(), aliases_.end(), [] (const auto &a, const auto &b) {
121 return a.first.size() > b.first.size();
122 });
123 }
124 String
125 map_url (const String &urlpath) override
126 {
127 if (dir_.empty())
128 return "";
129
130 // decode URL, also uncovers '.' and '/'
131 String absurl = string_url_decode (urlpath);
132
133 // normalize '.' and '..' dirs
134 absurl = Path::simplify_abspath (absurl);
135
136 // ignore urls
137 for (const auto &ignorepat : ignores_)
138 if (std::regex_search (absurl, ignorepat))
139 return "";
140
141 // map URL to sorted aliases, prefers longest match
142 for (const auto &pair : aliases_)
143 if (absurl == pair.first)
144 return pair.second;
145 else if (absurl.size() > pair.first.size() &&
146 absurl[pair.first.size()] == '/' &&
147 strncmp (absurl.c_str(), pair.first.c_str(), pair.first.size()) == 0)
148 return Path::join (pair.second, absurl.c_str() + pair.first.size());
149
150 // fallback to root
151 return Path::join (dir_, absurl);
152 }
153 void
154 listen (const String &host, int port, const UnlistenCB &ulcb) override
155 {
156 assert_return (listen_fd_ < 0);
157 setup (host, port);
158 // Register listen fd with main_loop
159 listen_source_id_ = main_loop->exec_io_handler ([this] (PollFD &pfd) {
160 // Handle new connections
161 if (pfd.revents & PollFD::IN) {
162 sockaddr_in client_addr;
163 socklen_t client_len = sizeof (client_addr);
164 int client_fd = accept (listen_fd_, (sockaddr*) &client_addr, &client_len);
165 if (client_fd >= 0) {
166 fcntl (client_fd, F_SETFL, O_NONBLOCK);
167 sockaddr_in local_addr;
168 socklen_t local_len = sizeof (local_addr);
169 getsockname (client_fd, (sockaddr*) &local_addr, &local_len);
170 WppConnectionP con = wppserver_.get_connection();
171 ClientState cs;
172 cs.fd = client_fd;
173 cs.con = con;
174 char ip_str[INET_ADDRSTRLEN];
175 inet_ntop (AF_INET, &(local_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
176 cs.local_ip = ip_str;
177 cs.local_port = ntohs (local_addr.sin_port);
178 inet_ntop (AF_INET, &(client_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
179 cs.remote_ip = ip_str;
180 cs.remote_port = ntohs (client_addr.sin_port);
181 // Wire up the iostream transport's write handler
182 con->set_write_handler ([this, client_fd] (WppHdl hdl, char const *data, size_t len)
183 {
184 auto it = clients_.find (client_fd);
185 if (it != clients_.end()) {
186 it->second.out_buffer.append (data, len);
187 main_loop->wakeup();
188 }
189 IODEBUG ("%s:%u:%s: fd=%d write_handler: len=%d\n", __FILE__, __LINE__, __func__, it != clients_.end() ? it->first : -1, len);
190 return websocketpp::lib::error_code();
191 });
192 clients_[client_fd] = cs;
193 IODEBUG ("%s:%u:%s: accept(%d)\n", __FILE__, __LINE__, __func__, client_fd);
194 con->start();
195 // Update client sources to include the new connection
196 update_client_sources();
197 }
198 }
199 return true; // keep handler active
200 }, listen_fd_, "r", LoopPriority::NORMAL);
201 }
202 void
203 see_other (const String &uri) override
204 {
205 // TODO: not thread-safe
206 see_other_ = uri;
207 }
208 void
209 shutdown () override
210 {
211 // Cancel loop sources
212 reset();
213 main_loop->cancel (&listen_source_id_);
214 ::close (listen_fd_);
215 listen_fd_ = -1;
216 }
217 void ws_opened (WebSocketConnectionP);
218 void ws_closed (WebSocketConnectionP);
219 void reset () override;
220 WebSocketServerImpl (const MakeConnection &make_con, int logflags, const std::string &token) :
221 token_ (token), make_con_ (make_con), logflags_ (logflags)
222 {}
223};
224
225void
226WebSocketServerImpl::setup (const String &host, int port)
227{
228 wppserver_.set_user_agent (user_agent());
229 wppserver_.set_validate_handler ([this] (WppHdl hdl)
230 {
231 WebSocketConnectionP conp = make_connection (hdl);
232 WppConnectionP cp = this->wppconp (hdl);
233 return_unless (conp && cp, false);
234 const bool is_authenticated = conp->authenticated (token_);
235 if (is_authenticated) {
236 const int index = conp->validate();
237 if (index >= 0) {
238 const std::vector<std::string> &subprotocols = cp->get_requested_subprotocols();
239 if (size_t (index) < subprotocols.size())
240 {
241 cp->select_subprotocol (subprotocols[index]);
242 return true;
243 }
244 else if (subprotocols.size() == 0 && index == 0)
245 return true;
246 }
247 }
248 cp->set_status (websocketpp::http::status_code::forbidden);
249 conp->log_status (cp->get_response_code());
250 return false;
251 });
252 wppserver_.set_http_handler ([this] (WppHdl hdl)
253 {
254 WebSocketConnectionP conp = make_connection (hdl);
255 if (conp)
256 conp->http_request();
257 });
258 wppserver_.clear_access_channels (websocketpp::log::alevel::all);
259 wppserver_.clear_error_channels (websocketpp::log::elevel::all);
260
261 // Setup POSIX listening socket
262 listen_fd_ = socket (AF_INET, SOCK_STREAM, 0);
263 if (listen_fd_ < 0)
264 fatal_error ("failed to create socket for: %s:%d: %s", host, port);
265 int opt = 1;
266 setsockopt (listen_fd_, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
267
268 sockaddr_in addr{};
269 addr.sin_family = AF_INET;
270 if (host.empty())
271 addr.sin_addr.s_addr = htonl (INADDR_ANY);
272 else
273 {
274 addr.sin_addr.s_addr = inet_addr (host.c_str());
275 if (addr.sin_addr.s_addr == INADDR_NONE)
276 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
277 }
278 addr.sin_port = htons (port);
279
280 if (bind (listen_fd_, (sockaddr*) &addr, sizeof (addr)) < 0)
281 fatal_error ("failed to bind to %s:%d", host, port);
282
283 if (::listen (listen_fd_, 128) < 0)
284 fatal_error ("failed to listen on: %s:%d: %s", host, port);
285 fcntl (listen_fd_, F_SETFL, O_NONBLOCK);
286
287 if (port == 0) {
288 socklen_t len = sizeof (addr);
289 if (getsockname (listen_fd_, (sockaddr*) &addr, &len) == 0)
290 listen_port_ = ntohs (addr.sin_port);
291 } else
292 listen_port_ = port;
293 String fullurl = string_format ("http://%s:%d/", host.empty() ? "127.0.0.1" : host.c_str(), listen_port_);
294 server_url_ = fullurl;
295}
296
297bool
298WebSocketServerImpl::io_handler (PollFD &pfd)
299{
300 const int fd = pfd.fd;
301 IODEBUG ("%s:%u:%s: io-ready(%d): r=%d w=%d other=0x%x\n", __FILE__, __LINE__, __func__, fd,
302 pfd.revents & PollFD::IN, pfd.revents & PollFD::OUT,
303 pfd.revents & ~(PollFD::IN | PollFD::OUT));
304 auto it = clients_.find (fd);
305 if (it == clients_.end())
306 return false; // client gone
307 auto &client = it->second;
308 if (client.out_buffer.size()) {
309 ssize_t n = ::send (fd, client.out_buffer.data(), client.out_buffer.size(), 0);
310 IODEBUG ("%s:%u:%s: send(%d,%d)\n", __FILE__, __LINE__, __func__, fd, n);
311 if (n > 0)
312 client.out_buffer.erase (0, n);
313 else if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
314 client.con->fatal_error();
315 }
316 if ((pfd.revents & PollFD::IN) &&
317 client.con->get_state() != websocketpp::session::state::closed) {
318 char buf[8192];
319 ssize_t n = ::recv (fd, buf, sizeof (buf), 0);
320 IODEBUG ("%s:%u:%s: recv(%d)=%d\n", __FILE__, __LINE__, __func__, fd, n);
321 if (n > 0) {
322 try {
323 client.con->read_all (buf, n);
324 } catch (...) {
325 client.con->fatal_error(); // websocketpp error
326 }
327 }
328 else if (n == 0)
329 client.con->eof();
330 else if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
331 client.con->fatal_error(); // read EIO
332 }
333 }
334 if (pfd.revents & (PollFD::ERR | PollFD::HUP | PollFD::NVAL)) // 0x8 0x10 0x20
335 client.con->fatal_error(); // fd EIO
336 if (client.con->get_state() == websocketpp::session::state::closed)
337 IODEBUG ("%s:%u:%s: closed fd=%d ob=%d io_errors=0x%x\n", __FILE__, __LINE__, __func__, fd, client.out_buffer.size(), pfd.revents & (PollFD::ERR | PollFD::HUP | PollFD::NVAL));
338 if (client.out_buffer.size())
339 pfd.events |= PollFD::OUT;
340 else
341 pfd.events &= ~PollFD::OUT;
342 const bool closed = client.out_buffer.empty() && client.con->get_state() == websocketpp::session::state::closed;
343 return !closed; // keep handler if not closed
344}
345
346// Update client sources when clients connect/disconnect
347void
348WebSocketServerImpl::update_client_sources()
349{
350 // Register missing client fds
351 for (auto &[fd, client] : clients_)
352 if (client.source_id == Ase::LoopID::INVALID)
353 client.source_id = main_loop->exec_io_handler ([this] (PollFD &pfd)
354 {
355 return this->io_handler (pfd);
356 }, fd, "r", LoopPriority::NORMAL);
357}
358
359// == WebSocketConnection ==
362 WppServer &wppserver;
363 WppHdl hdl;
364 String nickname_;
365 bool opened = false;
366 WppConnectionP wppconp() { return server->wppconp (hdl); }
367 friend struct WebSocketServerImpl;
368};
369
370void
371WebSocketConnection::log_status (int intstatus, const String &filepath)
372{
373 const auto status = websocketpp::http::status_code::value (intstatus);
374 WppConnectionP cp = internals_.wppconp();
375 if (!cp || !(logflags_ & 16))
376 return;
377 using namespace AnsiColors;
378 const bool rejected = (status >= 400 && status <= 499) || status == 303;
379 const String C1 = rejected ? color (FG_RED) : "";
380 const String C0 = rejected ? color (RESET) : "";
381 String resource = cp->get_resource();
382 resource = Re::sub ("\\btoken=[^;?]+", "token=<………>", resource);
383 log (string_format ("%s%d %s %s%s%s",
384 C1, status,
385 cp->get_request().get_method(), resource, C0,
386 filepath.empty() ? "" : " [" + filepath + "]"));
387}
388
389WebSocketConnection::WebSocketConnection (Internals &internals, int logflags) :
390 internals_ (*new (internals_mem_) Internals (internals)), logflags_ (logflags)
391{
392 static_assert (sizeof (Internals) <= sizeof (internals_mem_));
393 assert_return (internals_.server != nullptr);
394}
395
396WebSocketConnection::~WebSocketConnection()
397{
398 internals_.~Internals();
399}
400
401bool
402WebSocketConnection::is_open () const
403{
404 return internals_.opened;
405}
406
407bool
409{
410 assert_return (!message.empty(), false);
411 WppConnectionP cp = internals_.wppconp();
412 return_unless (cp, false);
413 websocketpp::lib::error_code ec;
414 // Find client fd for debug logging
415 int fd = -1;
416 for (const auto &[f, client] : internals_.server->clients_)
417 if (client.con == cp)
418 { fd = f; break; }
419 IODEBUG ("%s:%u:%s: fd=%d message=%d\n", __FILE__, __LINE__, __func__, fd, message.size());
420 internals_.wppserver.send (internals_.hdl, message, websocketpp::frame::opcode::text, ec);
421 if (ec)
422 {
423 if (logflags_ > 0)
424 log (string_format ("Error: %s: %s", __func__, ec.message()));
425 websocketpp::lib::error_code ec2;
426 cp->close (websocketpp::close::status::going_away, "", ec2);
427 return false;
428 }
429 return true;
430}
431
432bool
434{
435 WppConnectionP cp = internals_.wppconp();
436 return_unless (cp, false);
437 websocketpp::lib::error_code ec;
438 internals_.wppserver.send (internals_.hdl, blob, websocketpp::frame::opcode::binary, ec);
439 if (ec)
440 {
441 if (logflags_ > 0)
442 log (string_format ("Error: %s: %s", __func__, ec.message()));
443 websocketpp::lib::error_code ec2;
444 cp->close (websocketpp::close::status::going_away, "", ec2);
445 return false; // invalid connection or send failed
446 }
447 if (ASE_UNLIKELY (logflags_ & 256))
448 {
449 String hex;
450 for (size_t i = 0; i < blob.size(); i++)
451 {
452 if (i && 0 == i % 16)
453 hex += "\n ";
454 else if (0 == i % 8)
455 hex += " ";
456 hex += string_format (" %02x", blob[i]);
457 }
458 log (string_format ("⇜ Blob: len=%d hash=%016x\n%s", blob.size(), fnv1a_consthash64 (blob.data(), blob.size()), hex));
459 }
460 return true; // connection alive and message queued
461}
462
464WebSocketConnection::get_info ()
465{
466 WppConnectionP cp = internals_.wppconp();
467 const websocketpp::http::parser::request &rq = cp->get_request();
468 auto headers = std::make_shared<websocketpp::http::parser::header_list> (rq.get_headers());
469 Info info;
470 info.header = [headers] (const String &header) {
471 const auto it = headers->find (header);
472 return it == headers->end() ? "" : it->second;
473 };
474 if (0)
475 for (auto it : *headers)
476 IODEBUG ("%s: %s\n", it.first, it.second);
477
478 // Lookup IP/Port from our manual ClientState map
479 for (const auto &[fd, client] : internals_.server->clients_)
480 if (client.con == cp) {
481 info.local = client.local_ip;
482 info.lport = client.local_port;
483 info.remote = client.remote_ip;
484 info.rport = client.remote_port;
485 break;
486 }
487 info.subs = cp->get_requested_subprotocols();
488 return info;
489}
490
491String
492WebSocketConnection::nickname ()
493{
494 if (internals_.nickname_.empty())
495 {
496 Info info = get_info();
497 const String ua = info.header ("User-Agent");
498 String s = info.local + ":" + string_from_int (info.lport) + "\n" +
499 info.remote + ":" + string_from_int (info.rport * 0) + "\n" +
500 ua + "\n" +
501 info.header ("Accept-Encoding") + "\n" +
502 info.header ("Accept-Language") + "\n" +
503 info.header ("sec-ch-ua") + "\n" +
504 info.header ("sec-ch-ua-mobile") + "\n" +
505 info.header ("sec-gpc") + "\n";
506 String hh;
507 uint64_t hash = string_hash64 (s);
508 if (Re::search (R"(\bFirefox/)", ua) >= 0)
509 hh = "FF";
510 else if (Re::search (R"(\bElectron/)", ua) >= 0)
511 hh = "El";
512 else if (Re::search (R"(\bChrome-Lighthouse\b)", ua) >= 0)
513 hh = "Lh";
514 else if (Re::search (R"(\bChrome/)", ua) >= 0)
515 hh = "CR";
516 else if (Re::search (R"(\bSafari/)", ua) >= 0)
517 hh = "Sa";
518 else if (Re::search (R"(\bWget/)", ua) >= 0)
519 hh = "Wg";
520 else if (Re::search (R"(\bw3m/)", ua) >= 0)
521 hh = "W3";
522 else if (Re::search (R"(\bLynx/)", ua) >= 0)
523 hh = "Ly";
524 else if (Re::search (R"(\bPython-urllib/)", ua) >= 0)
525 hh = "Py";
526 else
527 hh = "NA";
528 internals_.nickname_ = string_format ("%s-%08x:%x", hh, uint32_t (hash ^ (hash >> 32)), info.rport);
529 }
530 return internals_.nickname_;
531}
532
533int WebSocketConnection::validate () { return -1; }
534void WebSocketConnection::failed () { /*if (logflags_ & 2) log (__func__);*/ }
535void WebSocketConnection::opened () { if (logflags_ & 4) log (__func__); }
536void WebSocketConnection::message (const String &message) { if (logflags_ & 8) log (__func__); }
537void WebSocketConnection::closed () { if (logflags_ & 4) log (__func__); }
538void WebSocketConnection::log (const String &message) { printerr ("%s\n", message);}
539
540bool
542{
543 WppConnectionP cp = internals_.wppconp();
544 if (!cp)
545 return false;
546 if (token.empty())
547 return true;
548 const String cookie_header = cp->get_request().get_header ("Cookie");
549 const String cookie_token = Re::grep ("\\bsession_auth=([^;?]+)", cookie_header, 1);
550 return cookie_token == token;
551}
552
553enum CacheType {
554 CACHE_NEVER,
555 CACHE_AUTO,
556 CACHE_BRIEFLY,
557 CACHE_FOREVER,
558};
559
560void
562{
563 if (internals_.server->dir_.empty())
564 return;
565 WppConnectionP cp = internals_.wppconp();
566 const auto &parts = string_split (cp->get_resource(), "?");
567 const String query = cp->get_uri()->get_query();
568 String filepath;
569
570 // Helpers
571 auto set_response = [] (WppConnectionP cp, websocketpp::http::status_code::value status, const String &title, const String &msg)
572 {
573 const String body =
574 string_format ("<!DOCTYPE html>\n"
575 "<html><head><title>%u %s</title></head>\n<body>\n"
576 "<h1>%s</h1>\n\n"
577 "%s\n"
578 "<hr><address>%s</address>\n"
579 "<hr></body></html>\n", status, title, title,
580 msg.size() ? "<p>" + msg + "</p>" : "",
581 WebSocketServer::user_agent());
582 cp->set_body (body);
583 cp->replace_header ("Content-Type", "text/html; charset=utf-8");
584 cp->set_status (status);
585 };
586
587 // GET ~auth
588 if (cp->get_request().get_method() == "GET" && parts[0] == "/~auth" &&
589 (authenticated (internals_.server->token_) ||
590 Re::search ("\\btoken=" + internals_.server->token_ + "\\b", query) >= 0)) {
591 const std::string target = string_format ("http://localhost:%u/", cp->get_port());
592 cp->replace_header ("Set-Cookie", "session_auth=" + internals_.server->token_ + "; Path=/; HttpOnly");
593 cp->replace_header ("Location", target);
594 set_response (cp, websocketpp::http::status_code::found, // 302 Found
595 "Found", "Redirecting to: <tt>" + target + "</tt>");
596 log_status (cp->get_response_code());
597 return;
598 }
599
600 // Validate Cookie session_auth
601 if (!authenticated (internals_.server->token_)) {
602 const std::string see_other = internals_.server->see_other_;
603 if (see_other.empty())
604 set_response (cp, websocketpp::http::status_code::method_not_allowed, // 405 Method Not Allowed
605 "Method Not Allowed", "Authentication required.");
606 else {
607 if (string_startswith (see_other, "http"))
608 cp->replace_header ("Location", see_other);
609 set_response (cp, websocketpp::http::status_code::see_other, // 303 See Other
610 "See Other", "<a href=\"" + see_other + "\">" + see_other + "</a>");
611 }
612 log_status (cp->get_response_code());
613 return;
614 }
615
616 // find file for URL
617 filepath = internals_.server->map_url (parts[0]);
618
619 // map directories to index.html
620 if (!filepath.empty() && Path::check (filepath, "dx"))
621 filepath = Path::join (filepath, "index.html");
622
623 // GET
624 if (cp->get_request().get_method() == "GET") {
625 // serve existing files
626 const String accept_encoding = cp->get_request().get_header ("Accept-Encoding");
627 const bool canzip = nullptr != string_find_word (accept_encoding.c_str(), "gzip");
628 bool fp = false, fz = false;
629 if (!filepath.empty() && ((fp = Path::check (filepath, "fr")) || (fz = canzip && Path::check (filepath + ".gz", "fr"))))
630 {
631 const char *ext = strrchr (filepath.c_str(), '.');
632 const String mimetype = WebSocketServer::mime_type (ext ? ext + 1 : "", true);
633 cp->append_header ("Content-Type", mimetype);
634 // Use CACHE_FOREVER for text/css to reduce FOUC (flashing of unstyled content)
635 // with shadowRoot stylesheet links in Chrome-115. CACHE_AUTO worsens FOUC.
636 const CacheType caching = mimetype == "text/css" ? CACHE_FOREVER : CACHE_AUTO;
637 switch (caching) {
638 case CACHE_NEVER: // response must not be stored in cache
639 cp->append_header ("Cache-Control", "no-store");
640 break;
641 case CACHE_AUTO: // force response revalidation if content stored in cache
642 cp->append_header ("Cache-Control", "no-cache"); // max-age=0, must-revalidate
643 break;
644 case CACHE_BRIEFLY: // needs validation support: https://developer.mozilla.org/en-US/docs/Web/HTTP/Conditional_requests
645 cp->append_header ("Cache-Control", "max-age=7, stale-while-revalidate=31536000");
646 break;
647 case CACHE_FOREVER: // keep content forever in cache
648 cp->append_header ("Cache-Control", "max-age=31536000, public, immutable");
649 break;
650 }
651 if (fz) {
652 filepath = filepath + ".gz";
653 cp->append_header ("Content-Encoding", "gzip");
654 }
655 Blob blob = Blob::from_file (filepath);
656 cp->set_body (blob.string());
657 cp->set_status (websocketpp::http::status_code::ok); // 200 OK
658 }
659 else // 404
660 set_response (cp, websocketpp::http::status_code::not_found, // 404 Not Found
661 "Not Found",
662 "The requested URL was not found: <br /> <tt>" + cp->get_uri()->str() + "</tt>");
663 }
664 else // 405
665 set_response (cp, websocketpp::http::status_code::method_not_allowed, // 405 Method Not Allowed
666 "Method Not Allowed", "");
667
668 log_status (cp->get_response_code());
669}
670
671// == WebSocketServerImpl ==
673WebSocketServerImpl::make_connection (WppHdl hdl)
674{
675 WppConnectionP cp = wppconp (hdl);
676 assert_return (cp, nullptr);
679 wppserver_, hdl,
680 };
681 WebSocketConnectionP conp = make_con_ (internals, logflags_);
682 // capturing conp in handler contexts keeps it alive long enough for calls
683 cp->set_http_handler ([conp] (WppHdl hdl) {
684 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
685 assert_return (hdl == internals_.hdl);
686 conp->http_request();
687 });
688 // cp->set_validate_handler (...); - too late, validate_handler calls make_connection()
689 cp->set_fail_handler ([conp] (WppHdl hdl) {
690 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
691 assert_return (hdl == internals_.hdl);
692 conp->failed();
693 });
694 cp->set_open_handler ([conp] (WppHdl hdl) {
695 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
696 assert_return (hdl == internals_.hdl);
697 internals_.server->ws_opened (conp);
698 });
699 cp->set_message_handler ([conp] (WppHdl hdl, WppServer::message_ptr msg) {
700 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
701 assert_return (hdl == internals_.hdl);
702 conp->message (msg->get_payload());
703 });
704 cp->set_close_handler ([conp] (WppHdl hdl) {
705 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
706 assert_return (hdl == internals_.hdl);
707 internals_.server->ws_closed (conp);
708 });
709 return conp;
710}
711
712void
713WebSocketServerImpl::ws_opened (WebSocketConnectionP conp)
714{
715 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
716 internals_.opened = true;
717 opencons_.push_back (conp);
718 // Update client sources to include the new connection
719 update_client_sources();
720 conp->opened ();
721}
722
723void
724WebSocketServerImpl::ws_closed (WebSocketConnectionP conp)
725{
726 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
727 WppConnectionP wppconp = internals_.wppconp();
728 if (internals_.opened) {
729 internals_.opened = false;
730 auto it = std::find (opencons_.begin(), opencons_.end(), conp);
731 if (it != opencons_.end())
732 opencons_.erase (it);
733 // Find and remove the client from clients_
734 for (auto client_it = clients_.begin(); client_it != clients_.end(); ) {
735 if (client_it->second.con == wppconp) {
736 auto &[fd, client] = *client_it;
737 ::close (fd);
738 main_loop->cancel (&client.source_id);
739 IODEBUG ("%s:%u:%s: closed fd=%d ob=%d\n", __FILE__, __LINE__, __func__, fd, client.out_buffer.size());
740 client_it = clients_.erase (client_it);
741 } else
742 ++client_it;
743 }
744 conp->closed();
745 }
746}
747
748void
749WebSocketServerImpl::reset()
750{
751 // stop open connections
752 for (ssize_t i = ssize_t (opencons_.size()) - 1; i >= 0; i--)
753 {
754 WebSocketConnectionP conp = opencons_[i];
755 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
756 WppConnectionP cp = internals_.wppconp();
757 websocketpp::lib::error_code ec;
758 cp->close (websocketpp::close::status::going_away, "", ec); // omit_handshake
759 }
760
761 // cancel client sources and close raw sockets
762 for (auto &[fd, client] : clients_) {
763 ::close (fd);
764 main_loop->cancel (&client.source_id);
765 IODEBUG ("%s:%u:%s: closed fd=%d ob=%d\n", __FILE__, __LINE__, __func__, fd, client.out_buffer.size());
766 }
767 clients_.clear();
768}
769
770// == WebSocketServer ==
771WebSocketServer::~WebSocketServer()
772{}
773
774WebSocketServerP
775WebSocketServer::create (const MakeConnection &make, int logflags, const std::string &session_token)
776{
777 return std::make_shared<WebSocketServerImpl> (make, logflags, session_token);
778}
779
780String
781WebSocketServer::user_agent ()
782{
783 return String ("AnklangSynthEngine/") + ase_version();
784}
785
787bool
789{
790 return !!websocketpp::utf8_validator::validate (utf8string);
791}
792
793#include "mime-types.hh" // static const char mime_types[];
794
795String
796WebSocketServer::mime_type (const String &ext, bool utf8)
797{
799 static MimeMap mime_map = [] ()
800 {
801 MimeMap mime_map;
802 // mime_types, list of "mime/type ext ext2\n" lines
803 for (const String &line : string_split (mime_types, "\n"))
804 {
805 const StringS w = string_split (line, " ");
806 for (size_t i = 1; i < w.size(); i++)
807 if (!w[i].empty())
808 {
809 if (mime_map.end() != mime_map.find (w[i]))
810 warning ("mime-types.hh: duplicate extension: %s", w[i]);
811 mime_map[w[i]] = w[0];
812 }
813 }
814 return mime_map;
815 } ();
816 auto it = mime_map.find (ext);
817 String mimetype = it != mime_map.end() ? it->second : "application/octet-stream";
818 if (utf8)
819 {
820 if (mimetype == "text/html" || mimetype == "text/markdown" || mimetype == "text/plain")
821 mimetype += "; charset=utf-8";
822 }
823 return mimetype;
824}
825
826} // Ase
T c_str(T... args)
Binary large object storage container.
Definition blob.hh:12
static Blob from_file(const String &filename)
Create Blob by loading from filename.
Definition blob.cc:196
String string()
Copy Blob data into a zero terminated string.
Definition blob.cc:117
static String grep(const String &regex, const String &input, int group=0, Flags=DEFAULT)
Find regex in input and return matching string.
Definition regex.cc:225
static ssize_t search(const String &regex, const String &input, Flags=DEFAULT)
Find regex in input and return match position >= 0 or return < 0 otherwise.
Definition regex.cc:217
virtual void closed()
Pairs with opened().
Definition websocket.cc:537
virtual int validate()
Return true to allow opened().
Definition websocket.cc:533
virtual void http_request()
Independent of socket ops.
Definition websocket.cc:561
virtual void opened()
Pairs with closed().
Definition websocket.cc:535
bool send_binary(const String &blob)
Returns true if binary blob was sent.
Definition websocket.cc:433
virtual void message(const String &message)
Only if opened.
Definition websocket.cc:536
virtual void failed()
Never folloed by opened().
Definition websocket.cc:534
bool send_text(const String &message)
Returns true if text message was sent.
Definition websocket.cc:408
virtual bool authenticated(const std::string &token)
Return true to allow validate().
Definition websocket.cc:541
static bool utf8_validate(const std::string &utf8string)
Validate UTF-8 string with websocketpp::utf8_validator.
Definition websocket.cc:788
T clear(T... args)
#define ASE_UNLIKELY(expr)
Compiler hint to optimize for expr evaluating to false.
Definition cxxaux.hh:46
T data(T... args)
T empty(T... args)
T end(T... args)
T fail(T... args)
fcntl
T find(T... args)
T getline(T... args)
htonl
inet_addr
#define assert_return(expr,...)
Return from the current function if expr is unmet and issue an assertion warning.
Definition internal.hh:29
#define return_unless(cond,...)
Return silently if cond does not evaluate to true with return value ...
Definition internal.hh:73
log
T make_pair(T... args)
bool check(const String &file, const String &mode)
Definition path.cc:625
String simplify_abspath(const std::string &abspath_expression)
Remove extra slashes, './' and '../' from abspath_expression.
Definition path.cc:886
String abspath(const String &path, const String &incwd)
Definition path.cc:134
String normalize(const String &path)
Convert path to normal form.
Definition path.cc:86
The Anklang C++ API namespace.
Definition api.hh:9
std::string string_format(const char *format, const Args &...args) __attribute__((__format__(__printf__
Format a string similar to sprintf(3) with support for std::string and std::ostringstream convertible...
StringS string_split(const String &string, const String &splitter, size_t maxn)
Split a string, using splitter as delimiter.
Definition strings.cc:343
String string_from_int(int64 value)
Convert a 64bit signed integer into a string.
Definition strings.cc:604
const char * string_find_word(const char *haystack, const char *word)
Find occurance of word in haystack.
Definition strings.cc:520
@ NORMAL
Normal importantance, GUI event processing, RPC.
String string_url_decode(const String &urlstr, const bool form_url_encoded)
Decode URL %-sequences in a string, decode '+' if form_url_encoded.
Definition strings.cc:965
const char * ase_version()
Provide a string containing the package version.
Definition platform.cc:803
std::string String
Convenience alias for std::string.
Definition cxxaux.hh:35
bool string_startswith(const String &string, const String &fragment)
Returns whether string starts with fragment.
Definition strings.cc:846
size_t hash(size_t seed, const T &v)
T open(T... args)
T push_back(T... args)
T regex_search(T... args)
setsockopt
shutdown
T size(T... args)
socket
T stable_sort(T... args)
typedef uint64_t
strncmp
strrchr
Mirrors struct pollfd for poll(3posix)
Definition loop.hh:14
@ IN
RDNORM || RDBAND.
Definition loop.hh:20
typedef ssize_t