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