19#include <sys/socket.h>
20#include <netinet/in.h>
26#include <websocketpp/config/core.hpp>
27#include <websocketpp/server.hpp>
29#define IODEBUG(...) while (0) { printerr (__VA_ARGS__); break; }
33struct WebSocketServerImpl;
38 static const size_t connection_read_buffer_size = 16384;
41using WppServer = websocketpp::server<CustomServerConfig>;
42using WppConnectionP = WppServer::connection_ptr;
43using WppConnection = WppConnectionP::element_type;
44using WppHdl = websocketpp::connection_hdl;
47operator!= (
const WppHdl &a,
const WppHdl &b)
49 return a.owner_before (b) || b.owner_before (a);
51static inline bool operator== (
const WppHdl &a,
const WppHdl &b) {
return !(a != b); }
62 Ase::LoopID source_id = Ase::LoopID::INVALID;
70 String server_url_, dir_, token_, see_other_;
79 Ase::LoopID listen_source_id_ = Ase::LoopID::INVALID;
81 void update_client_sources();
82 bool io_handler (
PollFD &pfd);
84 void setup (
const String &host,
int port);
90 int listen_port ()
const override {
return listen_port_; }
91 String url ()
const override {
return server_url_; }
92 WppConnectionP wppconp (WppHdl hdl)
94 websocketpp::lib::error_code ec;
95 return wppserver_.get_con_from_hdl (hdl, ec);
98 http_dir (
const String &path)
override
108 aseignore.
open (Path::join (dir_,
".aseignore"));
110 if (!cxxline.
empty())
114 http_alias (
const String &webdir,
const String &path)
override
120 std::stable_sort (aliases_.begin(), aliases_.end(), [] (
const auto &a,
const auto &b) {
121 return a.first.size() > b.first.size();
125 map_url (
const String &urlpath)
override
137 for (
const auto &ignorepat : ignores_)
142 for (
const auto &pair : aliases_)
143 if (absurl == pair.first)
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());
151 return Path::join (dir_, absurl);
159 listen_source_id_ = main_loop->exec_io_handler ([
this] (
PollFD &pfd) {
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();
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);
182 con->set_write_handler ([this, client_fd] (WppHdl hdl, char const *data, size_t len)
184 auto it = clients_.find (client_fd);
185 if (it != clients_.end()) {
186 it->second.out_buffer.append (data, len);
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();
192 clients_[client_fd] = cs;
193 IODEBUG (
"%s:%u:%s: accept(%d)\n", __FILE__, __LINE__, __func__, client_fd);
196 update_client_sources();
203 see_other (
const String &uri)
override
213 main_loop->cancel (&listen_source_id_);
214 ::close (listen_fd_);
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)
226WebSocketServerImpl::setup (
const String &host,
int port)
228 wppserver_.set_user_agent (user_agent());
229 wppserver_.set_validate_handler ([
this] (WppHdl hdl)
231 WebSocketConnectionP conp = make_connection (hdl);
232 WppConnectionP cp = this->wppconp (hdl);
234 const bool is_authenticated = conp->authenticated (token_);
235 if (is_authenticated) {
236 const int index = conp->validate();
239 if (
size_t (index) < subprotocols.size())
241 cp->select_subprotocol (subprotocols[index]);
244 else if (subprotocols.size() == 0 && index == 0)
248 cp->set_status (websocketpp::http::status_code::forbidden);
249 conp->log_status (cp->get_response_code());
252 wppserver_.set_http_handler ([
this] (WppHdl hdl)
254 WebSocketConnectionP conp = make_connection (hdl);
256 conp->http_request();
258 wppserver_.clear_access_channels (websocketpp::log::alevel::all);
259 wppserver_.clear_error_channels (websocketpp::log::elevel::all);
262 listen_fd_ =
socket (AF_INET, SOCK_STREAM, 0);
264 fatal_error (
"failed to create socket for: %s:%d: %s", host, port);
266 setsockopt (listen_fd_, SOL_SOCKET, SO_REUSEADDR, &opt,
sizeof (opt));
269 addr.sin_family = AF_INET;
271 addr.sin_addr.s_addr =
htonl (INADDR_ANY);
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);
278 addr.sin_port =
htons (port);
280 if (bind (listen_fd_, (sockaddr*) &addr,
sizeof (addr)) < 0)
281 fatal_error (
"failed to bind to %s:%d", host, port);
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);
288 socklen_t len =
sizeof (addr);
289 if (getsockname (listen_fd_, (sockaddr*) &addr, &len) == 0)
290 listen_port_ = ntohs (addr.sin_port);
293 String fullurl =
string_format (
"http://%s:%d/", host.empty() ?
"127.0.0.1" : host.c_str(), listen_port_);
294 server_url_ = fullurl;
298WebSocketServerImpl::io_handler (PollFD &pfd)
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())
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);
312 client.out_buffer.erase (0, n);
313 else if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
314 client.con->fatal_error();
316 if ((pfd.revents & PollFD::IN) &&
317 client.con->get_state() != websocketpp::session::state::closed) {
319 ssize_t n = ::recv (fd, buf,
sizeof (buf), 0);
320 IODEBUG (
"%s:%u:%s: recv(%d)=%d\n", __FILE__, __LINE__, __func__, fd, n);
323 client.con->read_all (buf, n);
325 client.con->fatal_error();
330 else if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
331 client.con->fatal_error();
334 if (pfd.revents & (PollFD::ERR | PollFD::HUP | PollFD::NVAL))
335 client.con->fatal_error();
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;
341 pfd.events &= ~PollFD::OUT;
342 const bool closed = client.out_buffer.empty() && client.con->get_state() == websocketpp::session::state::closed;
348WebSocketServerImpl::update_client_sources()
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)
355 return this->io_handler (pfd);
356 }, fd,
"r", LoopPriority::NORMAL);
362 WppServer &wppserver;
366 WppConnectionP wppconp() {
return server->wppconp (hdl); }
371WebSocketConnection::log_status (
int intstatus,
const String &filepath)
373 const auto status = websocketpp::http::status_code::value (intstatus);
374 WppConnectionP cp = internals_.wppconp();
375 if (!cp || !(logflags_ & 16))
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",
385 cp->get_request().get_method(), resource, C0,
386 filepath.
empty() ?
"" :
" [" + filepath +
"]"));
389WebSocketConnection::WebSocketConnection (Internals &internals,
int logflags) :
390 internals_ (*new (internals_mem_) Internals (internals)), logflags_ (logflags)
392 static_assert (
sizeof (Internals) <=
sizeof (internals_mem_));
396WebSocketConnection::~WebSocketConnection()
398 internals_.~Internals();
402WebSocketConnection::is_open ()
const
404 return internals_.opened;
411 WppConnectionP cp = internals_.wppconp();
413 websocketpp::lib::error_code ec;
416 for (
const auto &[f, client] : internals_.server->clients_)
417 if (client.con == cp)
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);
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);
435 WppConnectionP cp = internals_.wppconp();
437 websocketpp::lib::error_code ec;
438 internals_.wppserver.send (internals_.hdl, blob, websocketpp::frame::opcode::binary, ec);
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);
450 for (
size_t i = 0; i < blob.
size(); i++)
452 if (i && 0 == i % 16)
464WebSocketConnection::get_info ()
466 WppConnectionP cp = internals_.wppconp();
467 const websocketpp::http::parser::request &rq = cp->get_request();
470 info.header = [headers] (
const String &header) {
471 const auto it = headers->find (header);
472 return it == headers->end() ?
"" : it->second;
475 for (
auto it : *headers)
476 IODEBUG (
"%s: %s\n", it.first, it.second);
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;
487 info.subs = cp->get_requested_subprotocols();
492WebSocketConnection::nickname ()
494 if (internals_.nickname_.
empty())
496 Info info = get_info();
497 const String ua = info.header (
"User-Agent");
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";
510 else if (
Re::search (R
"(\bElectron/)", ua) >= 0)
512 else if (
Re::search (R
"(\bChrome-Lighthouse\b)", ua) >= 0)
514 else if (
Re::search (R
"(\bChrome/)", ua) >= 0)
516 else if (
Re::search (R
"(\bSafari/)", ua) >= 0)
524 else if (
Re::search (R
"(\bPython-urllib/)", ua) >= 0)
528 internals_.nickname_ =
string_format (
"%s-%08x:%x", hh, uint32_t (hash ^ (hash >> 32)), info.rport);
530 return internals_.nickname_;
543 WppConnectionP cp = internals_.wppconp();
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;
563 if (internals_.server->dir_.empty())
565 WppConnectionP cp = internals_.wppconp();
566 const auto &parts =
string_split (cp->get_resource(),
"?");
567 const String query = cp->get_uri()->get_query();
571 auto set_response = [] (WppConnectionP cp, websocketpp::http::status_code::value status,
const String &title,
const String &msg)
575 "<html><head><title>%u %s</title></head>\n<body>\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());
583 cp->replace_header (
"Content-Type",
"text/html; charset=utf-8");
584 cp->set_status (status);
588 if (cp->get_request().get_method() ==
"GET" && parts[0] ==
"/~auth" &&
590 Re::search (
"\\btoken=" + internals_.server->token_ +
"\\b", query) >= 0)) {
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,
595 "Found",
"Redirecting to: <tt>" + target +
"</tt>");
596 log_status (cp->get_response_code());
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,
605 "Method Not Allowed",
"Authentication required.");
608 cp->replace_header (
"Location", see_other);
609 set_response (cp, websocketpp::http::status_code::see_other,
610 "See Other",
"<a href=\"" + see_other +
"\">" + see_other +
"</a>");
612 log_status (cp->get_response_code());
617 filepath = internals_.server->map_url (parts[0]);
621 filepath = Path::join (filepath,
"index.html");
624 if (cp->get_request().get_method() ==
"GET") {
626 const String accept_encoding = cp->get_request().get_header (
"Accept-Encoding");
628 bool fp =
false, fz =
false;
632 const String mimetype = WebSocketServer::mime_type (ext ? ext + 1 :
"",
true);
633 cp->append_header (
"Content-Type", mimetype);
636 const CacheType caching = mimetype ==
"text/css" ? CACHE_FOREVER : CACHE_AUTO;
639 cp->append_header (
"Cache-Control",
"no-store");
642 cp->append_header (
"Cache-Control",
"no-cache");
645 cp->append_header (
"Cache-Control",
"max-age=7, stale-while-revalidate=31536000");
648 cp->append_header (
"Cache-Control",
"max-age=31536000, public, immutable");
652 filepath = filepath +
".gz";
653 cp->append_header (
"Content-Encoding",
"gzip");
656 cp->set_body (blob.
string());
657 cp->set_status (websocketpp::http::status_code::ok);
660 set_response (cp, websocketpp::http::status_code::not_found,
662 "The requested URL was not found: <br /> <tt>" + cp->get_uri()->str() +
"</tt>");
665 set_response (cp, websocketpp::http::status_code::method_not_allowed,
666 "Method Not Allowed",
"");
668 log_status (cp->get_response_code());
673WebSocketServerImpl::make_connection (WppHdl hdl)
675 WppConnectionP cp = wppconp (hdl);
681 WebSocketConnectionP conp = make_con_ (internals, logflags_);
683 cp->set_http_handler ([conp] (WppHdl hdl) {
684 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
686 conp->http_request();
689 cp->set_fail_handler ([conp] (WppHdl hdl) {
690 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
694 cp->set_open_handler ([conp] (WppHdl hdl) {
695 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
697 internals_.server->ws_opened (conp);
699 cp->set_message_handler ([conp] (WppHdl hdl, WppServer::message_ptr msg) {
700 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
702 conp->message (msg->get_payload());
704 cp->set_close_handler ([conp] (WppHdl hdl) {
705 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
707 internals_.server->ws_closed (conp);
713WebSocketServerImpl::ws_opened (WebSocketConnectionP conp)
715 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
716 internals_.opened =
true;
717 opencons_.push_back (conp);
719 update_client_sources();
724WebSocketServerImpl::ws_closed (WebSocketConnectionP conp)
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);
734 for (
auto client_it = clients_.begin(); client_it != clients_.end(); ) {
735 if (client_it->second.con == wppconp) {
736 auto &[fd, client] = *client_it;
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);
749WebSocketServerImpl::reset()
752 for (ssize_t i = ssize_t (opencons_.size()) - 1; i >= 0; i--)
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);
762 for (
auto &[fd, client] : clients_) {
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());
771WebSocketServer::~WebSocketServer()
775WebSocketServer::create (
const MakeConnection &make,
int logflags,
const std::string &session_token)
781WebSocketServer::user_agent ()
790 return !!websocketpp::utf8_validator::validate (utf8string);
796WebSocketServer::mime_type (
const String &ext,
bool utf8)
799 static MimeMap mime_map = [] ()
806 for (
size_t i = 1; i < w.
size(); i++)
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];
816 auto it = mime_map.find (ext);
817 String mimetype = it != mime_map.
end() ? it->second :
"application/octet-stream";
820 if (mimetype ==
"text/html" || mimetype ==
"text/markdown" || mimetype ==
"text/plain")
821 mimetype +=
"; charset=utf-8";
Binary large object storage container.
static Blob from_file(const String &filename)
Create Blob by loading from filename.
String string()
Copy Blob data into a zero terminated string.
static String grep(const String ®ex, const String &input, int group=0, Flags=DEFAULT)
Find regex in input and return matching string.
static ssize_t search(const String ®ex, const String &input, Flags=DEFAULT)
Find regex in input and return match position >= 0 or return < 0 otherwise.
virtual void closed()
Pairs with opened().
virtual int validate()
Return true to allow opened().
virtual void http_request()
Independent of socket ops.
virtual void opened()
Pairs with closed().
bool send_binary(const String &blob)
Returns true if binary blob was sent.
virtual void message(const String &message)
Only if opened.
virtual void failed()
Never folloed by opened().
bool send_text(const String &message)
Returns true if text message was sent.
virtual bool authenticated(const std::string &token)
Return true to allow validate().
static bool utf8_validate(const std::string &utf8string)
Validate UTF-8 string with websocketpp::utf8_validator.
#define ASE_UNLIKELY(expr)
Compiler hint to optimize for expr evaluating to false.
#define assert_return(expr,...)
Return from the current function if expr is unmet and issue an assertion warning.
#define return_unless(cond,...)
Return silently if cond does not evaluate to true with return value ...
bool check(const String &file, const String &mode)
String simplify_abspath(const std::string &abspath_expression)
Remove extra slashes, './' and '../' from abspath_expression.
String abspath(const String &path, const String &incwd)
String normalize(const String &path)
Convert path to normal form.
The Anklang C++ API namespace.
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.
String string_from_int(int64 value)
Convert a 64bit signed integer into a string.
const char * string_find_word(const char *haystack, const char *word)
Find occurance of word in haystack.
@ 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.
const char * ase_version()
Provide a string containing the package version.
std::string String
Convenience alias for std::string.
bool string_startswith(const String &string, const String &fragment)
Returns whether string starts with fragment.
size_t hash(size_t seed, const T &v)
T regex_search(T... args)
T shared_from_this(T... args)
Mirrors struct pollfd for poll(3posix)